-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchpt-7-4-SpotLight.html
58 lines (49 loc) · 2.2 KB
/
chpt-7-4-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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>光与影-聚光灯</title>
<script type="text/javascript" src="js/three.js"></script>
</head>
<body onload="init()">
<script>
function init(){
var renderer, sence, camera, light;
renderer = new THREE.WebGLRenderer();
renderer.setSize(800, 600);
document.getElementsByTagName('body')[0].appendChild(renderer.domElement);
//设置渲染器的背景色
renderer.setClearColor(0x000000);
scene = new THREE.Scene();
camera = new THREE.OrthographicCamera(-2, 2, 1.5, -1.5, 1, 10);
camera.position.set(3, 3, 6);
camera.lookAt(new THREE.Vector3(0, 0, 0));
scene.add(camera);
var plane = new THREE.Mesh(new THREE.PlaneGeometry(8, 20, 16, 16),
new THREE.MeshLambertMaterial({color: 0xcccccc}));
// plane.rotation.x = -Math.PI / 2;
// plane.position.y = -1;
plane.position.z = -1;
scene.add(plane);
//但是,如果此时场景中没有物体,只添加了这个环境光,那么渲染的结果仍然是一片黑。所以,我们添加两个长方体看下效果:
var greenCube = new THREE.Mesh(new THREE.CubeGeometry(0.5, 0.5, 0.5),new THREE.MeshLambertMaterial({color: 0x00ff00}));
greenCube.position.x = 1.2;
scene.add(greenCube);
var whiteCube = new THREE.Mesh(new THREE.CubeGeometry(0.5, 0.5, 0.5),new THREE.MeshLambertMaterial({color: 0xffffff}));
whiteCube.position.x = -1.2;
scene.add(whiteCube);
//聚光灯是一种特殊的点光源,它能够朝着一个方向投射光线。聚光灯投射出的是类似圆锥形的光线,这与我们现实中看到的聚光灯是一致的。
/**
*其构造函数为:THREE.SpotLight(hex, intensity, distance, angle, exponent)
*相比点光源,多了angle和exponent两个参数。angle是聚光灯的张角,缺省值是Math.PI / 3,最大值是Math.PI / 2;
*exponent是光强在偏离target的衰减指数(target需要在之后定义,缺省值为(0, 0, 0)),缺省值是10。
*/
light = new THREE.SpotLight(0xffff00, 1, 100, Math.PI / 6, 25);
light.position.set(0, 2, 4.8);
light.target = greenCube;
scene.add(light);
renderer.render(scene, camera);
}
</script>
</body>
</html>