Skip to content

Commit 22e9ff0

Browse files
committed
8.5 Shadow
1 parent d6d0d48 commit 22e9ff0

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

Chapter8/8.5.1.html

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html>
3+
<head>
4+
<script type="text/javascript" src="../lib/three.js"></script>
5+
6+
<script type="text/javascript">
7+
var scene = null;
8+
var camera = null;
9+
var renderer = null;
10+
11+
var cube = null;
12+
var alpha = 0;
13+
14+
function init() {
15+
renderer = new THREE.WebGLRenderer();
16+
renderer.setSize(400, 300);
17+
18+
var container = document.getElementById('canvas');
19+
container.appendChild(renderer.domElement);
20+
renderer.shadowMapEnabled = true;
21+
renderer.shadowMapSoft = true;
22+
23+
scene = new THREE.Scene();
24+
25+
camera = new THREE.OrthographicCamera(-5, 5, 3.75, -3.75, 0.1, 100);
26+
camera.position.set(5, 15, 25);
27+
camera.lookAt(new THREE.Vector3(0, 0, 0));
28+
scene.add(camera);
29+
30+
var plane = new THREE.Mesh(new THREE.PlaneGeometry(8, 8, 16, 16),
31+
new THREE.MeshLambertMaterial({color: 0xcccccc}));
32+
plane.rotation.x = -Math.PI / 2;
33+
plane.position.y = -1;
34+
plane.receiveShadow = true;
35+
scene.add(plane);
36+
37+
cube = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1),
38+
new THREE.MeshLambertMaterial({color: 0x00ff00}));
39+
cube.position.x = 2;
40+
cube.castShadow = true;
41+
scene.add(cube);
42+
43+
var light = new THREE.SpotLight(0xffff00, 1, 100, Math.PI / 6, 25);
44+
light.position.set(2, 5, 3);
45+
light.target = cube;
46+
light.castShadow = true;
47+
48+
light.shadowCameraNear = 2;
49+
light.shadowCameraFar = 10;
50+
light.shadowCameraFov = 30;
51+
light.shadowCameraVisible = true;
52+
53+
light.shadowMapWidth = 1024;
54+
light.shadowMapHeight = 1024;
55+
light.shadowDarkness = 0.3;
56+
57+
scene.add(light);
58+
59+
// ambient light
60+
var ambient = new THREE.AmbientLight(0x666666);
61+
scene.add(ambient);
62+
63+
requestAnimationFrame(draw);
64+
}
65+
66+
function draw() {
67+
alpha += 0.01;
68+
if (alpha > Math.PI * 2) {
69+
alpha -= Math.PI * 2;
70+
}
71+
72+
cube.position.set(2 * Math.cos(alpha), 0, 2 * Math.sin(alpha));
73+
74+
renderer.render(scene, camera);
75+
76+
requestAnimationFrame(draw);
77+
}
78+
</script>
79+
</head>
80+
81+
<body onload="init()">
82+
<!--canvas id="mainCanvas" width="400px" height="300px" ></canvas-->
83+
<div id="canvas" width="400px" height="300px"></div>
84+
</body>
85+
</html>

0 commit comments

Comments
 (0)