-
Notifications
You must be signed in to change notification settings - Fork 1
/
飞行控件示例.html
184 lines (137 loc) · 4.51 KB
/
飞行控件示例.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
html, body {
margin: 0;
height: 100%;
}
canvas {
display: block;
}
</style>
</head>
<body onload="draw();">
</body>
<script type="text/javascript" src="../js/threejs/three.js"></script>
<script type="text/javascript" src="../js/libs/stats.min.js"></script>
<script type="text/javascript" src="../js/libs/dat.gui.min.js"></script>
<script src="../js/controls/FlyControls.js"></script>
<script>
var renderer;
function initRender() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
//告诉渲染器需要阴影效果
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是,没有设置的这个清晰 THREE.PCFShadowMap
renderer.gammaInput = true;
renderer.gammaOutput = true;
document.body.appendChild(renderer.domElement);
}
var camera;
function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 40, 0);
}
var scene;
function initScene() {
scene = new THREE.Scene();
}
var ambientLight,pointLight;
function initLight() {
ambientLight = new THREE.AmbientLight("#111111");
scene.add(ambientLight);
pointLight = new THREE.PointLight("#ffffff");
pointLight.position.set(-40, 60, -10);
//告诉平行光需要开启阴影投射
pointLight.castShadow = true;
scene.add(pointLight);
}
var cube,plane;
function initModel() {
//辅助工具
var helper = new THREE.AxesHelper(10);
scene.add(helper);
//球体
var sphereGeometry = new THREE.SphereGeometry(10,30,30);
var sphereMaterial = new THREE.MeshStandardMaterial({color:0xff00ff});
var sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);
sphere.position.set(-20,20,0);
sphere.castShadow = true;
scene.add(sphere);
//立方体
var cubeGeometry = new THREE.CubeGeometry(10,10,10);
var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff});
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.x = 30;
cube.position.y = 5;
cube.position.z = -5;
//告诉立方体需要投射阴影
cube.castShadow = true;
scene.add(cube);
//底部平面
var planeGeometry = new THREE.PlaneGeometry(5000, 5000, 20, 20);
var planeMaterial = new THREE.MeshStandardMaterial({color: 0xaaaaaa});
plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -0.5 * Math.PI;
plane.position.y = -0;
//告诉底部平面需要接收阴影
plane.receiveShadow = true;
scene.add(plane);
}
//初始化性能插件
var stats;
function initStats() {
stats = new Stats();
document.body.appendChild(stats.dom);
}
//用户交互插件 鼠标左键按住旋转,右键按住平移,滚轮缩放
var controls,clock;
function initControls() {
clock = new THREE.Clock();
controls = new THREE.FlyControls( camera );
controls.movementSpeed = 100; //设置移动的速度
controls.rollSpeed = Math.PI / 6; //设置旋转速度
controls.autoForward = false;
controls.dragToLook = true;
}
//初始化dat.GUI简化试验流程
var param;
function initGui() {
}
function render() {
renderer.render(scene, camera);
}
//窗口变动触发的函数
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
var delta = clock.getDelta();
//更新控制器
render();
//更新性能插件
stats.update();
controls.update(delta);
requestAnimationFrame(animate);
}
function draw() {
initRender();
initScene();
initCamera();
initLight();
initModel();
initControls();
initStats();
initGui();
animate();
window.onresize = onWindowResize;
}
</script>
</html>