-
Notifications
You must be signed in to change notification settings - Fork 0
/
christmas_ball.html
308 lines (239 loc) · 8.07 KB
/
christmas_ball.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
<html>
<head>
<title>Christmas ball</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>
"use strict";
//Globals
var clock, renderer, scene, camera, controls;
var christmasBall;
(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 not indexed,
//merely assigning rgb rgb rgb ... will suffice!
var christmasBallGeometry = new THREE.IcosahedronBufferGeometry(1, 2);//3);
var L = christmasBallGeometry.attributes.position.array.length;
var F = L/9;
var ca = new Float32Array(L).fill(0);
for (let f = 0; f < F; f++) {
ca[9*f] = 1.0;
ca[9*f+3+1] = 1.0;
ca[9*f+6+2] = 1.0;
}
christmasBallGeometry.addAttribute("color", new THREE.BufferAttribute(ca, 3));
//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 christmasBallMaterial = 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>
varying vec3 vPosition;
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>
vPosition = position;
}
`,
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));
}
varying vec3 vPosition;
void main() {
//vColor now actually holds BARYCENTRIC coordinates
//Star-shaped dents:
float maC = max(vColor.x, max(vColor.y, vColor.z));
float miC = min(vColor.x, min(vColor.y, vColor.z));
float mid = 1.-maC-miC;
float radius = mid/(maC+miC+mid);
float bump = sigmoid(100.*(radius-1./6.));
float bumpScale = 1.0;
//#include <clipping_planes_fragment>
vec4 color1 = vec4(1.0, 1.0, 0.5, 0.8);
vec4 color2 = vec4(0.6, 0., 0.1, 1.0);
float select = saturate(bump);
vec4 diffuseColor = vec4(diffuse, opacity)*(select*color1 + (1.-select)*color2);
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>
//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,
side: THREE.DoubleSide,
extensions: {
derivatives: true
}
});
christmasBallMaterial.uniforms.shininess.value = 150;
christmasBall = new THREE.Mesh(christmasBallGeometry, christmasBallMaterial);
scene.add(christmasBall);
//Camera setup
camera = new THREE.PerspectiveCamera(26, window.innerWidth/window.innerHeight, 0.01, 100);
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>
</body>
</html>