-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.html
139 lines (124 loc) · 4.26 KB
/
template.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>模板</title>
<style type="text/css">
html,
body {
margin: 0;
padding: 0;
overflow: hidden;
}
#box {
position: absolute;
width: 100%;
height: 100%;
}
</style>
</head>
<body onload="threeExcute();">
<div id="box"></div>
<script src="./js/threejs/three.js"></script>
<script src="./js/stats.js"></script>
<script>
var renderer, scene, camera, light;
var width, height;
var cube;
var stats; //绘画帧率监测
//初始化渲染器
function initRenderer() {
width = document.getElementById("box").clientWidth;
height = document.getElementById("box").clientHeight;
renderer = new THREE.WebGLRenderer({
antialias: true //抗锯齿
}); /*生成渲染器对象(属性:抗锯齿效果为设置有效)*/
renderer.setSize(width, height);
document.getElementById("box").appendChild(renderer.domElement);
/*设置canvas背景色(clearColor)和背景色透明度(clearAlpha) */
renderer.setClearColor(0xFFFFFF, 1.0);
}
//初始化场景
function initScene() {
scene = new THREE.Scene();
}
//初始化照相机
function initCamera() {
camera = new THREE.OrthographicCamera(-2, 2, 1.5, -1.5, 1, 10);
//定义camera的位置
camera.position.set(4, -3, 5);
camera.lookAt(new THREE.Vector3(0, 0, 0));
//这里的lookAt函数是将视角指定为看原点
//将camera添加到scene中
scene.add(camera);
}
//初始化光源
function initLight() {
light = new THREE.DirectionalLight(0xFF0000, 1.0, 0); //平行光
light.position.set(100, 100, 200); //设置光源位置
scene.add(light); //将光源添加到场景
}
//创建对象
function initObjects() {
//这里是创建一个长方形
cube = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1),
new THREE.MeshBasicMaterial({
color: "green",
wireframe: true //这里指不使用实心材料,所以为true
})
);
//这里要将这个长方形加入这个scene中
scene.add(cube);
}
//开启动画实时刷新
function animation() {
if (null != stats) stats.update();
renderer.render(scene, camera);
cube.rotation.x += 0.02;
cube.rotation.y += 0.02;
cube.rotation.z += 0.02;
requestAnimationFrame(animation);
}
//添加新的div到body
//divId - 要创建的DIV id
function addElementDiv(divId) {
var parent = document.getElementById("box");
console.log(parent)
//添加 div
var div = document.createElement("div");
//设置 div 属性,如 id
div.setAttribute("id", divId);
parent.appendChild(div);
}
//实时监测绘画帧率状态 - 开启动画时
function initStats() {
// <div id="Stats-output"></div>
addElementDiv("Stats-output");
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
document.getElementById("Stats-output").appendChild(stats.domElement);
return stats;
}
function threeExcute() {
//开启绘画帧率监测
stats = initStats();
//初始化渲染器
initRenderer();
//初始化场景
initScene();
//初始化照相机
initCamera();
//初始化光源
initLight();
//创建对象
initObjects();
//开启动画
animation();
}
</script>
</body>
</html>