-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchpt-4-5-texture.html
60 lines (47 loc) · 1.55 KB
/
chpt-4-5-texture.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
<!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, camera, scene, light;
renderer = new THREE.WebGLRenderer();
renderer.setSize(800, 600);
document.getElementsByTagName('body')[0].appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.OrthographicCamera(-2, 2, 1.5, -1.5, 1, 10);
//下面我们只改变照相机位置,观察两个角度的颜色变化:
//camera.position.set(3, 3, 6);
camera.position.set(3, 3, 6);
camera.lookAt(new THREE.Vector3(0, 0, 0));
scene.add(camera);
//创建平行光
light = new THREE.DirectionalLight();
light.position.set(2, 5, 3);
scene.add(light);
/**
*有时候我们希望以图像作为材质,这时候就需要导入图像作为纹理贴图,并添加到相应的材质中。
*选择图片,将其导入纹理中:
*/
var texture = THREE.ImageUtils.loadTexture('img/4s.jpg',{},function(){
renderer.render(scene, camera);
});
var greenCube = new THREE.Mesh(new THREE.CubeGeometry(0.4, 0.4, 0.4),new THREE.MeshLambertMaterial({
map: texture
}));
greenCube.position.x = 1.3;
scene.add(greenCube);
var cube3 = new THREE.Mesh(new THREE.SphereGeometry(0.4,20,20),new THREE.MeshLambertMaterial({
map: texture
}));
cube3.position.y = 1;
scene.add(cube3);
renderer.render(scene, camera);
}
</script>
</body>
</html>