-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modified_polyhedron.html
458 lines (368 loc) · 11.6 KB
/
Modified_polyhedron.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<html>
<head>
<title>Golf ball</title>
</head>
<body>
<script src="three.js-master/build/three.js"></script>
<script src="three.js-master/examples/js/controls/OrbitControls.js"></script>
<script src="js/Polyhedron_modification.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>
"use strict";
//Globals
var clock, renderer, scene, camera, controls;
var golfBall;
(function main() {
//Renderer setup
clock = new THREE.Clock(/*false*/); //false vil skru av autostart
document.body.style = "overflow: hidden;";
var container = document.createElement("div");
container.style = "position: absolute; top: 0; left: 0;"
document.body.appendChild(container);
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
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);*/
var lamp0 = new THREE.PointLight(0xffffff, 0.5);
lamp0.position.set(3, 45,-52);
scene.add(lamp0);
var lamp1 = new THREE.PointLight(0xffffff, 1.0);
lamp1.position.set(-51, -2, 54);
scene.add(lamp1);
var lamp2 = new THREE.PointLight(0xffffff, 0.5);
lamp2.position.set(56, -52, 4);
scene.add(lamp2);
//Golf ball:
//Since the geometry is now indexed (irregularly),
//I need to to the coloring search anyway.
//NOTE: OctahedronBufferGeometry fails with indexing
//(even without vertex colors) at detail >= 2 !
//This must be an error caused (or triggered) by my indexing.
//Remember that 0 detail is the default true geometries.
//Both Octahedra and Dodecahedra can theoretically be three-colored on vertices.
var USE_INDEXED_GEOMETRY = true;
var USE_VERTEX_COLORS = true;
var golfBallGeometry = new THREE.OctahedronBufferGeometry(1, 0, USE_INDEXED_GEOMETRY);
//var golfBallGeometry = new THREE.DodecahedronBufferGeometry(1, 0, USE_INDEXED_GEOMETRY);
if (USE_VERTEX_COLORS && golfBallGeometry.index) {
var L = golfBallGeometry.attributes.position.array.length;
var V = L/3;
var F = V/3;
var index = golfBallGeometry.index.array;
//console.log(index);
//return;
var vertexToFaces = new Proxy({}, {
get(target, key) {
key = +key;
if (target[key] === undefined) {
console.log("Init vertexToFaces[%d]", key);
target[key] = [];
}
return target[key];
},
});
for (let f = 0; f < F; f++) {
for (let a = 3*f; a < 3*f+3; a++) {
let iv = index[a];
vertexToFaces[iv].push(f);
}
}
var F_taken = new Proxy({}, {
get(target, key) {
key = +key;
if (target[key] === undefined) {
console.log("Init F_taken[%d]", key);
target[key] = {count:0};
}
return target[key];
},
});
var colors = [];
var colorsAssigned = 0;
function assignColor(v,c) {
if (colors[v] !== undefined) {
console.log("Error: Color already assigned.");
return;
}
colors[v] = c;
colorsAssigned++;
for (let f of vertexToFaces[v]) {
switch (F_taken[f][c]) {
case undefined:
F_taken[f][c] = v;
F_taken[f].count++;
break;
case v:
console.log("revisited f,c,v=",f,c,v);
break;
default:
console.log("Error: Color already taken.");
break;
}
}
}
//Minimal set of assumptions (determining only order of color set):
assignColor(index[0],0);
assignColor(index[1],1);
var frontier = [{/*f: 0, */v: index[2], c: 2}];
while (frontier.length > 0) {
for (let {v,c} of frontier) {
//Apply change:
assignColor(v,c);
}
let nFrontier = [];
while (frontier.length > 0) {
let {v,c} = frontier.pop();
for (let nf of vertexToFaces[v]) {
let ftaken = F_taken[nf];
//console.log("F_taken[%d].count === %d", nf, ftaken.count);
if (ftaken.count !== 2) {
continue;
}
let nc = 0;
while (ftaken[nc] !== undefined) {
nc++;
}
if (nc === 3) {
console.log("nc === 3 (ERROR?)");
continue;
}
let nvCands = [index[3*nf], index[3*nf+1], index[3*nf+2]];
for (let nv of nvCands) {
if (colors[nv] === undefined) {
let element = {/*f: nf,*/ v: nv, c: nc};
console.log(element);
nFrontier.push(element);
break;
}
}
}
}
frontier = nFrontier;
}
if (colorsAssigned !== V) {
console.warn("colorsAssigned !== V", colorsAssigned, V);
console.log(F_taken);
}
var ca = new Float32Array(L).fill(0);
for (let f = 0; f < F; f++) {
for (let c = 0; c < 3; c++) {
ca[3*index[3*f+c]+colors[index[3*f+c]]] = 1.0;
}
}
golfBallGeometry.addAttribute("color", new THREE.BufferAttribute(ca, 3));
}
var golfBallMaterial;
if (USE_VERTEX_COLORS) {
golfBallMaterial = new THREE.MeshPhongMaterial({vertexColors: THREE.VertexColors});
} else {
golfBallMaterial = new THREE.MeshPhongMaterial({color: "red"});
}
golfBall = new THREE.Mesh(golfBallGeometry, golfBallMaterial);
scene.add(golfBall);
//Camera setup
camera = new THREE.PerspectiveCamera(26, window.innerWidth/window.innerHeight, 1, 100000);
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
requestAnimationFrame(render);
}, false);
camera.up.set(0,0,1);
camera.position.set(3, 7, 5);
camera.lookAt(scene.position);
scene.add(camera);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener("change", ()=>requestAnimationFrame(render));
requestAnimationFrame(render);
})();
function render() {
renderer.render(scene, camera);
}
</script>
<!--
<script>
//The shaders are based on Phong, but with unimportant parts omitted.
//It would be interesting to apply an environment map, though.
var phong = THREE.ShaderLib.phong;
var golfBallMaterial = new THREE.ShaderMaterial({
uniforms: THREE.UniformsUtils.merge([phong.uniforms]),
vertexShader:
/* glsl */`
//#define PHONG
varying vec3 vViewPosition;
#ifndef FLAT_SHADED
varying vec3 vNormal;
#endif
#include <common>
//#include <uv_pars_vertex>
varying vec2 vUv;
uniform mat3 uvTransform;
//#include <uv2_pars_vertex>
//#include <displacementmap_pars_vertex>
#include <envmap_pars_vertex>
#include <color_pars_vertex>
//#include <fog_pars_vertex>
//#include <morphtarget_pars_vertex>
//#include <skinning_pars_vertex>
#include <shadowmap_pars_vertex>
//#include <logdepthbuf_pars_vertex>
//#include <clipping_planes_pars_vertex>
void main() {
//#include <uv_vertex>
vUv = ( uvTransform * vec3( uv, 1 ) ).xy;
//#include <uv2_vertex>
#include <color_vertex>
#include <beginnormal_vertex>
//#include <morphnormal_vertex>
//#include <skinbase_vertex>
//#include <skinnormal_vertex>
#include <defaultnormal_vertex>
#ifndef FLAT_SHADED // Normal computed with derivatives when FLAT_SHADED
vNormal = normalize( transformedNormal );
#endif
#include <begin_vertex>
//#include <morphtarget_vertex>
//#include <skinning_vertex>
//#include <displacementmap_vertex>
#include <project_vertex>
//#include <logdepthbuf_vertex>
//#include <clipping_planes_vertex>
vViewPosition = - mvPosition.xyz;
#include <worldpos_vertex>
#include <envmap_vertex>
#include <shadowmap_vertex>
//#include <fog_vertex>
}
`,
fragmentShader:
/* glsl */`
#extension GL_OES_standard_derivatives : enable
//#define PHONG
uniform vec3 diffuse;
uniform vec3 emissive;
uniform vec3 specular;
uniform float shininess;
uniform float opacity;
#include <common>
#include <packing>
//#include <dithering_pars_fragment>
#include <color_pars_fragment>
//#include <uv_pars_fragment>
varying vec2 vUv;
//#include <uv2_pars_fragment>
//#include <map_pars_fragment>
//#include <alphamap_pars_fragment>
//#include <aomap_pars_fragment>
//#include <lightmap_pars_fragment>
//#include <emissivemap_pars_fragment>
#include <envmap_pars_fragment>
//#include <gradientmap_pars_fragment>
//#include <fog_pars_fragment>
#include <bsdfs>
#include <lights_pars_begin>
#include <lights_phong_pars_fragment>
#include <shadowmap_pars_fragment>
//#include <bumpmap_pars_fragment>
//From bumpmap_pars_fragment:
vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {
// Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
vec3 vSigmaX = vec3( dFdx( surf_pos.x ), dFdx( surf_pos.y ), dFdx( surf_pos.z ) );
vec3 vSigmaY = vec3( dFdy( surf_pos.x ), dFdy( surf_pos.y ), dFdy( surf_pos.z ) );
vec3 vN = surf_norm; // normalized
vec3 R1 = cross( vSigmaY, vN );
vec3 R2 = cross( vN, vSigmaX );
float fDet = dot( vSigmaX, R1 );
fDet *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );
vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );
return normalize( abs( fDet ) * surf_norm - vGrad );
}
//#include <normalmap_pars_fragment>
#include <specularmap_pars_fragment>
//#include <logdepthbuf_pars_fragment>
//#include <clipping_planes_pars_fragment>
float sigmoid(float x) {
return 1./(1.+exp(x));
}
void main() {
//#include <clipping_planes_fragment>
vec4 diffuseColor = vec4( diffuse, opacity );
ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
vec3 totalEmissiveRadiance = emissive;
//#include <logdepthbuf_fragment>
//#include <map_fragment>
//#include <color_fragment>
//Insert custom handling of COLORS in the normals section instead
//#include <alphamap_fragment>
//#include <alphatest_fragment>
#include <specularmap_fragment>
#include <normal_fragment_begin>
float maC = max(max(vColor.r, vColor.g), vColor.b);
//Triangular dents:
//float bump = 1.-sigmoid(dot(vColor,vColor)-4.5);
//Small, circular dents:
float bump = 1.-sigmoid(28.*dot(vColor,vColor)-4.0);
//Try adjusting the bumpScale too:
float bumpScale = 3.5;
//Calculate derivative of bump height in screen space:
vec2 df_bump = bumpScale*vec2(dFdx(bump), dFdy(bump));
normal = perturbNormalArb( -vViewPosition, normal, df_bump);
//#include <normal_fragment_maps>
//#include <emissivemap_fragment>
// accumulation
#include <lights_phong_fragment>
#include <lights_fragment_begin>
#include <lights_fragment_maps>
#include <lights_fragment_end>
// modulation
//#include <aomap_fragment>
vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;
#include <envmap_fragment>
gl_FragColor = vec4( outgoingLight, diffuseColor.a );
//#include <tonemapping_fragment>
//#include <encodings_fragment>
//#include <fog_fragment>
//#include <premultiplied_alpha_fragment>
//#include <dithering_fragment>
}
`,
lights: true,
transparent: true,
vertexColors: THREE.VertexColors,
extensions: {
derivatives: true
}
});
//console.log(golfBallMaterial.extensions);
golfBall = new THREE.Mesh(golfBallGeometry, golfBallMaterial);
scene.add(golfBall);
//Camera setup
camera = new THREE.PerspectiveCamera(26, window.innerWidth/window.innerHeight, 1, 100000);
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}, false);
camera.up.set(0,0,1);
camera.position.set(3, 7, 5);
camera.lookAt(scene.position);
scene.add(camera);
controls = new THREE.OrbitControls(camera, renderer.domElement);
animate();
})();
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
</script>-->
</body>
</html>