-
Notifications
You must be signed in to change notification settings - Fork 60
/
step1.html
117 lines (107 loc) · 4.1 KB
/
step1.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>第一步:搭架子</title>
<script type="text/javascript" src="./threejs/three.js"></script>
<link rel="stylesheet" href="./reset.import.css">
<style type="text/css">
div#canvas-frame {
cursor: pointer;
width:100%;
height:100%;
background-color: #EEEEEE;
}
</style>
</head>
<body onload="threeStart();">
<div id="canvas-frame"></div>
<script>
var renderer;//渲染器
var width;//页面宽度
var height;//页面高度
var origPoint = new THREE.Vector3(0, 0, 0);//原点
window.requestAnimFrame = (function() {//如果有变化则可能还需要requestAnimationFrame刷新
return window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.webkitRequestAnimationFrame;
})();
//开始
function threeStart() {
initThree();
initCamera();
initScene();
initLight();
initObject();
render();
}
//根据页面宽度和高度创建渲染器,并添加容器中
function initThree() {
width = window.innerWidth;
height = window.innerHeight;
renderer = new THREE.WebGLRenderer({
antialias : true
});
renderer.setSize(width, height);
renderer.setClearColor(0xFFFFFF, 1.0);
document.getElementById('canvas-frame').appendChild(renderer.domElement);
}
//创建相机,并设置正方向和中心点
var camera;
function initCamera() {
camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000);
camera.position.set(200, 400, 600);
camera.up.set(0, 1, 0);//正方向
camera.lookAt(origPoint);
}
//创建场景,后续元素需要加入到场景中才会显示出来
var scene;
function initScene() {
scene = new THREE.Scene();
}
//创建光线
var light;
function initLight() {
light = new THREE.AmbientLight(0xfefefe);
scene.add(light);
}
//创建展示场景所需的各种元素
var cubes
function initObject() {
//坐标轴
var xmat = new THREE.LineBasicMaterial({ color: 0xff0000 });
var xgeo = new THREE.Geometry();
xgeo.vertices.push(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(300, 0, 0)
);
var xline = new THREE.Line(xgeo, xmat);
scene.add(xline);
var ymat = new THREE.LineBasicMaterial({ color: 0x00ff00 });
var ygeo = new THREE.Geometry();
ygeo.vertices.push(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 300, 0)
);
var yline = new THREE.Line(ygeo, ymat);
scene.add(yline);
var zmat = new THREE.LineBasicMaterial({ color: 0x0000ff });
var zgeo = new THREE.Geometry();
zgeo.vertices.push(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 300)
);
var zline = new THREE.Line(zgeo, zmat);
scene.add(zline);
}
//渲染
function render(){
renderer.clear();
renderer.render(scene, camera);
window.requestAnimFrame(render);
}
</script>
</body>
</html>