Skip to content

Commit d6d0d48

Browse files
committed
8.4 Spotlight
1 parent b0636b4 commit d6d0d48

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

Chapter8/8.4.1.html

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
<html>
33
<head>
44
<script type="text/javascript" src="../lib/three.js"></script>
5-
<script type="text/javascript" src="../lib/MTLLoader.js"></script>
6-
<script type="text/javascript" src="../lib/OBJMTLLoader.js"></script>
75

86
<script type="text/javascript">
97
var scene = null;
108
var camera = null;
119
var renderer = null;
1210

11+
var cube = null;
12+
var alpha = 0;
13+
1314
function init() {
1415
renderer = new THREE.WebGLRenderer({
1516
canvas: document.getElementById('mainCanvas')
1617
});
1718
renderer.setClearColor(0x000000);
1819
renderer.shadowMapEnabled = true;
19-
renderer.shadowMapSoft = true;
2020

2121
scene = new THREE.Scene();
2222

@@ -25,28 +25,43 @@
2525
camera.lookAt(new THREE.Vector3(0, 0, 0));
2626
scene.add(camera);
2727

28-
var light = new THREE.DirectionalLight(0xffffff);
29-
light.castShadow = true;
30-
light.shadowDarkness = 0.5;
31-
light.shadowCameraNear = 0.1;
32-
light.shadowCameraFar = 20;
33-
light.shadowCameraVisible = true;
34-
light.position.set(-2.0, 5.0, 3.0);
35-
scene.add(light);
36-
37-
var greenCube = new THREE.Mesh(new THREE.CubeGeometry(2, 2, 2),
38-
new THREE.MeshLambertMaterial({color: 0x00ff00}));
39-
greenCube.castShadow = true;
40-
scene.add(greenCube);
41-
42-
var plane = new THREE.Mesh(new THREE.PlaneGeometry(8, 8),
28+
var plane = new THREE.Mesh(new THREE.PlaneGeometry(8, 8, 16, 16),
4329
new THREE.MeshLambertMaterial({color: 0xcccccc}));
4430
plane.rotation.x = -Math.PI / 2;
4531
plane.position.y = -1;
4632
plane.receiveShadow = true;
4733
scene.add(plane);
4834

35+
cube = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1),
36+
new THREE.MeshLambertMaterial({color: 0x00ff00}));
37+
cube.position.x = 2;
38+
cube.castShadow = true;
39+
scene.add(cube);
40+
41+
var light = new THREE.SpotLight(0xffff00, 1, 100, Math.PI / 6, 25);
42+
light.position.set(2, 5, 3);
43+
light.target = cube;
44+
light.castShadow = true;
45+
scene.add(light);
46+
47+
// ambient light
48+
var ambient = new THREE.AmbientLight(0x666666);
49+
scene.add(ambient);
50+
51+
requestAnimationFrame(draw);
52+
}
53+
54+
function draw() {
55+
alpha += 0.01;
56+
if (alpha > Math.PI * 2) {
57+
alpha -= Math.PI * 2;
58+
}
59+
60+
cube.position.set(2 * Math.cos(alpha), 0, 2 * Math.sin(alpha));
61+
4962
renderer.render(scene, camera);
63+
64+
requestAnimationFrame(draw);
5065
}
5166
</script>
5267
</head>

0 commit comments

Comments
 (0)