-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchpt-7-1-light.html
41 lines (36 loc) · 1.67 KB
/
chpt-7-1-light.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
<!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, scene, 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);
//创建环境光环境光是指场景整体的光照效果,是由于场景内若干光源的多次反射形成的亮度一致的效果,通常用来为整个场景指定一个基础亮度。因此,环境光没有明确的光源位置,在各处形成的亮度也是一致的。在设置环境光时,只需要指定光的颜色:THREE.AmbientLight(hex)
light = new THREE.AmbientLight(0xffffff);
scene.add(light);
//但是,如果此时场景中没有物体,只添加了这个环境光,那么渲染的结果仍然是一片黑。所以,我们添加两个长方体看下效果:
var greenCube = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1),new THREE.MeshLambertMaterial({color: 0x00ff00}));
greenCube.position.x = 1.2;
scene.add(greenCube);
var whiteCube = new THREE.Mesh(new THREE.CubeGeometry(1.1, 1.1, 1.1),new THREE.MeshLambertMaterial({color: 0xffffff}));
whiteCube.position.x = -1.2;
scene.add(whiteCube);
renderer.render(scene, camera);
}
</script>
</body>
</html>