-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchpt-3-1-Geometry.html
77 lines (61 loc) · 2.68 KB
/
chpt-3-1-Geometry.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>几何形状Geometry</title>
<script type="text/javascript" src="js/three.js"></script>
</head>
<body onload="init()">
<script>
function init(){
//而如果想要Three.js生成Canvas元素,在HTML中就不需要定义Canvas元素,在JavaScript代码中可以这样写:
var renderer = new THREE.WebGLRenderer();
renderer.setSize(800, 600);
document.getElementsByTagName('body')[0].appendChild(renderer.domElement);
//设置渲染器的背景色
renderer.setClearColor(0x000000);
//创建场景
var scene = new THREE.Scene();
//正交投影照相机(Orthographic Camera)设置起来较为直观,它的构造函数是:THREE.OrthographicCamera(left, right, top, bottom, near, far)
//var camera = new THREE.OrthographicCamera(-1, 3, 1.5, -1.5, 1, 10);
var 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);
//1.立方体CubeGeometry),但它其实是长方体,也就是长宽高可以设置为不同的值。其构造函数是:THREE.CubeGeometry(width, height, depth, widthSegments, heightSegments, depthSegments)
var cube = new THREE.Mesh(new THREE.CubeGeometry(0.5, 0.5, 4),
new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: true
})
);
scene.add(cube);
//2.平面(PlaneGeometry)其实是一个长方形,而不是数学意义上无限大小的平面。其构造函数为:THREE.PlaneGeometry(width, height, widthSegments, heightSegments)
var cube2 = new THREE.Mesh(new THREE.PlaneGeometry(1,3,0,2),
new THREE.MeshBasicMaterial({
color: 0xfff000,
wireframe: true
})
);
scene.add(cube2);
//3.球体(SphereGeometry)的构造函数是:THREE.SphereGeometry(radius, segmentsWidth, segmentsHeight, phiStart, phiLength, thetaStart, thetaLength)
var cube3 = new THREE.Mesh(new THREE.SphereGeometry(1,8,6,Math.PI / 7, Math.PI / 4),
new THREE.MeshBasicMaterial({
color:0x178fe6,
wireframe: true
})
);
scene.add(cube3);
var cube4 = new THREE.Mesh(new THREE.SphereGeometry(1,8,6,Math.PI/2, Math.PI, Math.PI/6, Math.PI/2),
new THREE.MeshBasicMaterial({
color:0x00ffff,
wireframe: true
})
);
scene.add(cube4);
//在定义了场景中的物体,设置好的照相机之后,渲染器就知道如何渲染出二维的结果了。这时候,我们只需要调用渲染器的渲染函数,就能使其渲染一次了。
renderer.render(scene, camera);
}
</script>
</body>
</html>