-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-ambient-and-spotlight.html
275 lines (221 loc) · 8.75 KB
/
01-ambient-and-spotlight.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
<!DOCTYPE html>
<html>
<head>
<title>Lab 02.01 - Ambient and SpotLight</title>
<script type="text/javascript" src="three.js"></script>
<script type="text/javascript" src="jquery-1.9.0.js"></script>
<script type="text/javascript" src="stats.js"></script>
<script type="text/javascript" src="dat.gui.js"></script>
<script type="text/javascript" src="OrbitControls.js"></script>
<style>
body {
/* set margin to 0 and overflow to hidden, to go fullscreen */
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>
<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">
var stats = initStats();
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 1000);
// create a render and set the size
var renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0xEEEEEE)); //background color and opacity
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true; // false is default
renderer.autoClear = true; // default
renderer.autoClearColor = true; // default
ctr = new THREE.OrbitControls( camera, renderer.domElement);
// create the ground plane
var planeGeometry = new THREE.PlaneGeometry(60, 60, 20, 20);
var planeMaterial = new THREE.MeshPhongMaterial({color: 0xffffff, side:THREE.DoubleSide});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
var plane2 = new THREE.Mesh(planeGeometry, planeMaterial);
var plane3 = new THREE.Mesh(planeGeometry, planeMaterial);
var plane4 = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true;
// rotate and position the plane
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 15
plane.position.y = 0
plane.position.z = 0
// plane2.rotation.x = - 2 *Math.PI;
// plane2.rotation.y = Math.PI;
// plane2.rotation.z = Math.PI;
plane2.receiveShadow = true;
plane2.position.x = 15
plane2.position.y = 30
plane2.position.z = 30
plane3.receiveShadow = true;
plane3.position.x = 15
plane3.position.y = 30
plane3.position.z = -30
plane4.rotation.y = -0.5 * Math.PI;
plane4.receiveShadow = true;
plane4.position.x = 45
plane4.position.y = 30
plane4.position.z = 0
// add the plane to the scene
scene.add(plane);
scene.add(plane2);
scene.add(plane3);
scene.add(plane4);
// create a cube
var cubeGeometry = new THREE.CubeGeometry(6, 6, 6);
var cubeMaterial = new THREE.MeshPhongMaterial({color: 0xff0000});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true;
cube.receiveShadow = true;
// position the cube
cube.position.x = -4;
cube.position.y = 12;
cube.position.z = 0;
var cube2 = cube.clone();
cube2.castShadow = true;
cube2.receiveShadow = true;
cube2.position.x = 16;
cube2.position.y = 3;
cube2.position.z = 0;
scene.add(cube2);
// add the cube to the scene
scene.add(cube);
var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
var sphereMaterial = new THREE.MeshPhongMaterial({color: 0x7777ff, specular:true, shininess:100});
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
// position the sphere
sphere.position.x = 20;
sphere.position.y = 0;
sphere.position.z = 2;
sphere.castShadow = true;
sphere.receiveShadow = true;
// add the sphere to the scene
scene.add(sphere);
// position and point the camera to the center of the scene
camera.position.x = -25;
camera.position.y = 30;
camera.position.z = 25;
camera.lookAt(new THREE.Vector3(10, 0, 0));
// add subtle ambient lighting
var ambiColor = "#0c0c0c";
var ambientLight = new THREE.AmbientLight(ambiColor);
scene.add(ambientLight);
// add spotlight for the shadows
var spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(-30, 10, 0);
spotLight.castShadow = true;
spotLight.angle = 0.25;
spotLight.penumbra = .7;
// spotLight.target = sphere;
// var helper = new THREE.CameraHelper( spotLight.shadow.camera );
// scene.add( helper );
spotLight.shadow.camera.near = 0.1;
spotLight.shadow.camera.far = 100;
spotLight.shadow.camera.fov = 30;
spotLight.shadow.mapSize.width = 2048;
spotLight.shadow.mapSize.height = 2048;
spotLight.target = cube;
scene.add(spotLight);
var anotherSpotLight = new THREE.SpotLight(0x00FFFF);
anotherSpotLight.position.set(0, 30, -30);
anotherSpotLight.castShadow = true;
anotherSpotLight.target = plane;
anotherSpotLight.shadow.camera.far = 1000;
anotherSpotLight.shadow.camera.fov = 15;
anotherSpotLight.shadow.camera.near = 0.5;
anotherSpotLight.angle = 0.35;
anotherSpotLight.penumbra = 1
scene.add(anotherSpotLight);
var anotherSpotLight2 = new THREE.SpotLight(0xffc0cb);
anotherSpotLight2.position.set(0, 30, 30);
anotherSpotLight2.castShadow = true;
anotherSpotLight2.target = plane;
anotherSpotLight2.shadow.camera.far = 1000;
anotherSpotLight2.shadow.camera.fov = 15;
anotherSpotLight2.shadow.camera.near = 0.5;
anotherSpotLight2.angle = 0.35;
anotherSpotLight2.penumbra = 1
scene.add(anotherSpotLight2);
anotherSpotLight.target = sphere;
anotherSpotLight2.target = sphere;
// var helper2 = new THREE.CameraHelper( anotherSpotLight2.shadow.camera );
// scene.add( helper2 );
// add the output of the renderer to the html element
$("#WebGL-output").append(renderer.domElement);
// call the render function
var step = 0;
var controls = {
rotationSpeed: 0.02,
bouncingSpeed: 0.03,
cubeSlidingSpeed: 0.3,
ambientColor: ambiColor,
targetSphere: true,
}
var gui = new dat.GUI();
gui.addColor(controls, 'ambientColor').onChange(function (e) {
ambientLight.color = new THREE.Color(e);
});
gui.add(controls, 'rotationSpeed').onChange( (v) => {
rotationSpeed = v
});
gui.add(controls, 'bouncingSpeed').onChange( (v) => {
rotationSpeed = v
});
gui.add(controls, 'cubeSlidingSpeed').onChange( (v) => {
cubeSlidingSpeed = v
});
gui.add(controls, 'targetSphere').onChange( (v) => {
if (v) {
anotherSpotLight.target = sphere;
anotherSpotLight2.target = sphere;
}
else {
anotherSpotLight.target = plane;
anotherSpotLight2.target = plane;
}
});
mod = 1;
render();
function render() {
stats.update();
// rotate the cube around its axes
cube.rotation.x += controls.rotationSpeed;
cube.rotation.y += controls.rotationSpeed;
cube.rotation.z += controls.rotationSpeed;
if (cube2.position.z >= 12){
mod= -1;
}
else if (cube2.position.z <= -12){
mod = 1;
}
cube2.position.z += mod*controls.cubeSlidingSpeed;
// bounce the sphere up and down
step += controls.bouncingSpeed;
sphere.position.x = 20 + ( 10 * (Math.cos(step)));
sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step)));
// render using requestAnimationFrame
requestAnimationFrame(render);
renderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
$("#Stats-output").append(stats.domElement);
return stats;
}
</script>
</body>
</html>