-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfect_spheres.html
311 lines (247 loc) · 8.18 KB
/
perfect_spheres.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<html>
<head>
<title>Perfect spheres</title>
</head>
<body>
<script src="three.js-master/build/three.js"></script>
<script src="three.js-master/examples/js/controls/OrbitControls.js"></script>
<nav style="position: absolute; top:0; left:0, height:2vh; z-index:999">
<a href="." style="color: #66ffff">Back to index.</a>
</nav>
<script>
// See https://discourse.threejs.org/t/coding-jam-pixel-perfect-spheres-without-high-res-geometry/10154
"use strict";
//Globals
var container, renderer, scene, camera, controls, quads;
init();
function init() {
//Renderer setup
document.body.style = "overflow: hidden;";
container = document.createElement("div");
Object.assign(container.style, {
position: "absolute",
top: 0,
left: 0,
width: "100vw",
height: "100vh"
});
document.body.appendChild(container);
renderer = new THREE.WebGLRenderer({antialias: true});
container.appendChild(renderer.domElement);
//Scene setup:
scene = new THREE.Scene();
//scene.add(new THREE.AxesHelper(50));
//scene.add(new THREE.GridHelper(50, 10));
//scene.add(new THREE.AmbientLight(0xffffff, 0.2));
/*var sun = new THREE.DirectionalLight(0xffffff, 0.5);
sun.position.set(243, 66,363);
scene.add(sun);*/
//Spheres
const N = 1000;
const S = 50;
const radius_max = 2;
let geom = new THREE.BufferGeometry();
let pa = new Float32Array(18*N);
let ca = new Float32Array(18*N); //centers
let ra = new Float32Array(6*N); //radiuses
let cla = new Float32Array(18*N); //colors
for (let i = 0; i < N; i++) {
//console.log("Setting positions!");
//x coordiiates of quad
pa[18*i] = -1;
pa[18*i+3] = 1;
pa[18*i+6] = 1;
pa[18*i+9] = -1;
pa[18*i+12] = 1;
pa[18*i+15] = -1;
//y coordiiates of quad
pa[18*i+1] = -1;
pa[18*i+4] = -1;
pa[18*i+7] = 1;
pa[18*i+10] = -1;
pa[18*i+13] = 1;
pa[18*i+16] = 1;
let cx = S*(Math.random()-0.5);
let cy = S*(Math.random()-0.5);
let cz = S*(Math.random()-0.5);
let r = Math.random();
let g = Math.random();
let b = Math.random();
let radius = radius_max*(0.25 + 0.75*Math.random());
//console.log(cx,cy,cz);
for (let j = 0; j < 6; j++) {
//console.log("Setting centers!");
ca[18*i + 3*j] = cx;
ca[18*i + 3*j+1] = cy;
ca[18*i + 3*j+2] = cz;
cla[18*i + 3*j] = r;
cla[18*i + 3*j+1] = g;
cla[18*i + 3*j+2] = b;
ra[6*i+j] = radius;
//z coordiiates of quad
pa[18*i + 3*j+2] = 1;
}
}
geom.addAttribute("position", new THREE.BufferAttribute(pa,3));
geom.addAttribute("center", new THREE.BufferAttribute(ca,3));
geom.addAttribute("radius", new THREE.BufferAttribute(ra,1));
geom.addAttribute("color", new THREE.BufferAttribute(cla,3));
let mat = new THREE.ShaderMaterial(
{
vertexShader: `
//uniform mat4 modelViewMatrix;
//uniform mat4 projectionMatrix;
//attribute vec3 position;
attribute vec3 center;
attribute float radius;
attribute vec3 color;
varying vec3 vCenterViewPos;
varying vec3 vViewPos;
varying float vRadius;
varying vec3 vColor;
bool isPerspectiveMatrix(mat4 m) {
return m[2][3] == -1.0;
}
void main() {
vec4 centerViewPos = modelViewMatrix*vec4(center, 1.0);
//Artificially padding the radius solves the clipping
//problem in perspective raycast, but I don't know yet
//why it is necessary, and don't know how much padding
//is sufficient in the general case. So this is ugly!
vec4 viewPos = centerViewPos + vec4((radius+2.*float(isPerspectiveMatrix(projectionMatrix)))*position.xy,radius*position.z, 0.0)*centerViewPos.w;
//Attempt at alternative solution:
//vec4 viewPos = centerViewPos + radius*vec4(position, 0.0)*centerViewPos.w;
gl_Position = projectionMatrix*viewPos;
vCenterViewPos = centerViewPos.xyz/centerViewPos.w;
vViewPos = viewPos.xyz/viewPos.w;
vRadius = radius;
vColor = color;
}
`,
fragmentShader: `
//precision highp float;
uniform mat4 projectionMatrix;
uniform vec3 uLightViewPos;
varying vec3 vCenterViewPos;
varying vec3 vViewPos;
varying float vRadius;
varying vec3 vColor;
bool isPerspectiveMatrix(mat4 m) {
return m[2][3] == -1.0;
}
void main() {
float opacity = 1.0;
vec3 normal;
vec3 localViewPos;
vec3 localPos;
if (isPerspectiveMatrix(projectionMatrix)) {
//Perspective raycast, based on Ray.intersectSphere:
vec3 dir = normalize(vViewPos);
float tca = dot(vCenterViewPos,dir);
float d2 = dot(vCenterViewPos,vCenterViewPos) - tca * tca;
float radius2 = vRadius*vRadius;
if (d2 > radius2) {
discard;
//opacity = 0.0;
}
float thc = sqrt(radius2-d2);
float t0 = tca-thc;
localViewPos = t0*dir;
localPos = localViewPos-vCenterViewPos;
normal = normalize(localPos);
} else {
//Orthographic raycast (works):
localPos = vViewPos-vCenterViewPos;
float theta = atan(localPos.y, localPos.x);
float s_phi = length(localPos.xy)/vRadius;
if (s_phi > 1.0) {
discard;
}
float c_phi = sqrt(1.-s_phi*s_phi);
float c_theta = cos(theta);
float s_theta = sin(theta);
//Using Vector3.setFromSphericalCoords as reference, but
//adapting to z up (towards the camera):
float nx = s_phi*c_theta;
float ny = s_phi*s_theta;
float nz = c_phi;
normal = vec3(nx,ny,nz);
localPos = vRadius*normal;
localViewPos = vCenterViewPos+localPos;
}
//Diffuse (Lambertian) shading:
vec3 lightDir = uLightViewPos-localViewPos;
float lightDistance = length(lightDir);
lightDir /= lightDistance;
float lightIntensity = 1000.0/(lightDistance*lightDistance);
float diffuseLight = saturate(dot(normal,lightDir))*lightIntensity;
vec3 diffuseColor = vColor;//vec3(0.2, 0.5, 0.7);
//Ambient light
float ambient = 0.1;
//Specular (Blinn-Phong) highlights:
vec3 specularColor = vec3(1.);
vec3 eyeDir = normalize(-localViewPos);
vec3 halfway = normalize(eyeDir+lightDir);
float highlight = pow(saturate(dot(normal,halfway)), 20.)*lightIntensity;
gl_FragColor = vec4(diffuseColor*(diffuseLight+ambient) + specularColor*highlight, opacity);
//gl_FragColor = vec4(diffuseColor*(diffuseLight+ambient) + specularColor*highlight, 1.)*opacity;
//Debugging
//gl_FragColor = vec4(normal, 1.0);
//gl_FragColor = vec4(halfway, 1.0);
//Reference: https://stackoverflow.com/questions/10264949/glsl-gl-fragcoord-z-calculation-and-setting-gl-fragdepth
#ifdef gl_FragDepthEXT
vec4 temp = projectionMatrix*vec4(localViewPos, 1.);
gl_FragDepthEXT = 0.5*(gl_DepthRange.diff*(temp.z/temp.w)+gl_DepthRange.near+gl_DepthRange.far);
#endif
//DEBUG:
//gl_FragColor = vec4(vec3(gl_FragDepthEXT),opacity);
}
`,
//side: THREE.DoubleSide,
//lights: true
//write depth, write alpha?
uniforms: {
uLightViewPos: new THREE.Uniform(new THREE.Vector3())
},
extensions: {
fragDepth: true,
//derivatives: true,
},
//transparent: true,
}
);
quads = new THREE.Mesh(geom, mat);
quads.frustumCulled = false;
var light = new THREE.Mesh(new THREE.SphereBufferGeometry(1,24,12), new THREE.MeshBasicMaterial());
light.position.set(15, 16, 40);
scene.add(light);
quads.onBeforeRender = function() {
quads.material.uniforms.uLightViewPos.value.copy(light.position).applyMatrix4(camera.matrixWorldInverse);
}
scene.add(quads);
//Camera setup
camera = new THREE.PerspectiveCamera(26, window.innerWidth/window.innerHeight, 0.25, 500);
window.addEventListener('resize', onResize, false);
camera.position.set(138, 50, 53);
camera.lookAt(scene.position);
scene.add(camera);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.screenSpacePanning = true;
controls.addEventListener("change", ()=>requestAnimationFrame(render));
onResize();
requestAnimationFrame(render)
}
function onResize() {
let w = container.clientWidth;
let h = container.clientHeight;
camera.aspect = w/h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
requestAnimationFrame(render);
}
function render() {
renderer.render(scene, camera);
}
</script>
</body>
</html>