-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
286 lines (240 loc) · 10.1 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0, shrink-to-fit=no">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<title>SolarSystem</title>
<style type="text/css">
body {
width: 100%;
height: 100%;
background-color: #000;
color: #fff;
margin: 0px;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
</body>
<script>
/*
* Debug parameters.
*/
WebVRConfig = {
/**
* webvr-polyfill configuration
*/
//在VR显示模式下对WebVR推荐的屏幕比例进行缩放。在IOS下如果不为0.5会出现显示问题,查看
//https://github.com/borismus/webvr-polyfill/pull/106
BUFFER_SCALE: 0.5, // default: 1.0
};
</script>
<!--作者引入的一个promise polyfill-->
<script src="lib/es6-promise/dist/es6-promise.js"></script>
<!--three.js核心库-->
<script src="lib/three/three.js"></script>
<!--从连接的VR设备中获得位置信息并应用在camera对象上,将在下文展开-->
<script src="lib/three/examples/js/controls/VRControls.js"></script>
<!--处理立体视觉和绘制相关,将在下文展开-->
<script src="lib/three/examples/js/effects/VREffect.js"></script>
<!--WebVR polyfill,下文简述调用的API option-->
<script src="lib/webvr-polyfill/build/webvr-polyfill.js"></script>
<!--界面按钮以及进入/退出VR模式的控制等-->
<script src="lib/build/webvr-manager.js"></script>
<script>
var Sun,
Mercury, //水星
Venus, //金星
Earth,
Mars,
Jupiter, //木星
Saturn, //土星
Uranus, //天王
Neptune, //海王
stars = [];
// Setup three.js WebGL renderer. Note: Antialiasing is a big performance hit.
// Only enable it if you actually need to.
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
// Append the canvas element created by the renderer to document body element.
document.body.appendChild(renderer.domElement);
// Create a three.js scene.
var scene = new THREE.Scene();
// Create a three.js camera.
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.position.x = 30000;
camera.position.y = 30000;
camera.position.z = 30000;
//光源初始化
initLight();
function initLight() {
//环境光
var ambient = new THREE.AmbientLight(0x999999);
scene.add(ambient);
/*太阳光*/
var sunLight = new THREE.PointLight(0xddddaa, 1.5, 500);
scene.add(sunLight);
}
// Apply VR headset positional data to camera.
var controls = new THREE.VRControls(camera);
//站立姿态
controls.standing = false;
//controls.scale = 2;
controls.userHeight = 3.2;
// Apply VR stereo rendering to renderer.
var effect = new THREE.VREffect(renderer);
effect.setSize(window.innerWidth, window.innerHeight);
// Create a VR manager helper to enter and exit VR mode.
//按钮和全屏模式管理
var params = {
hideButton: false, // Default: false.
isUndistorted: false // Default: false.
};
var manager = new WebVRManager(renderer, effect, params);
//创建球形贴图星空背景
var geometry = new THREE.SphereGeometry(300, 100, 100);
var material = new THREE.MeshBasicMaterial();
material.map = THREE.ImageUtils.loadTexture('images/star3.png');
material.side = THREE.BackSide;
var mesh = new THREE.Mesh(geometry, material);
var skybox = new THREE.Mesh(geometry, material);
scene.add(skybox);
//正方形星空贴图实现
/* var geometry = new THREE.CubeGeometry(300, 100, 100);
var material = new THREE.MeshBasicMaterial();
material.map = THREE.ImageUtils.loadTexture('images/star3.png');
material.side = THREE.BackSide;
var mesh = new THREE.Mesh(geometry, material);
var skybox = new THREE.Mesh(geometry, material);
scene.add(skybox);*/
//粒子系统实现背景不行
/* var geom = new THREE.CircleGeometry();
var material = new THREE.ParticleBasicMaterial({
size: 0.1,
transparent: true,
opacity: 0.6,
vertexColors: 0xffffff,
sizeAttenuation: false,
color: false});
var range = 500;
for (var i = 0; i < 150; i++) {
var particle = new THREE.Vector3(Math.random() * range - range / 2, Math.random() * range - range / 2, Math.random() * range - range / 2);
geom.vertices.push(particle);
var color = new THREE.Color(0xFFFFFF);
//color.setHSL(color.getHSL().h, color.getHSL().s, Math.random() * color.getHSL().l);
//HSL是一种色彩模式
geom.colors.push(color);
}
system = new THREE.ParticleSystem(geom, material);
scene.add(system);*/
initObject();
// Kick off animation loop
requestAnimationFrame(animate);
window.addEventListener('resize', onResize, true);
window.addEventListener('vrdisplaypresentchange', onResize, true);
// Request animation frame loop function
var lastRender = 0;
function animate(timestamp) {
var delta = Math.min(timestamp - lastRender, 500);
lastRender = timestamp;
//立方体的旋转
move();
// Update VR headset position and apply to camera.
//更新获取HMD的信息
controls.update();
// Render the scene through the manager.
//进行camera更新和场景绘制
manager.render(scene, camera, timestamp);
requestAnimationFrame(animate);
}
function onResize(e) {
effect.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
//几何体初始化
function initObject() {
//构造太阳
var texture = THREE.ImageUtils.loadTexture("images/sunCore.jpg");
var mat = new THREE.MeshLambertMaterial();
mat.map = texture;
Sun = new THREE.Mesh( new THREE.SphereGeometry( 12 ,16 ,16 ),mat);
Sun.name='Sun';
scene.add(Sun);
Mercury = this.initPlanet('Mercury',20,2,'mercury.jpg', 0.02, 0);
stars.push(Mercury);
Venus = this.initPlanet('Venus',30,4,'venus.jpg', 0.012, 0);
stars.push(Venus);
Earth = this.initPlanet('Earth',40,5,'earth.jpg' ,0.010, 0);
stars.push(Earth);
Mars = this.initPlanet('Mars',50,4,'mars.jpg', 0.008, 0);
stars.push(Mars);
Jupiter = this.initPlanet('Jupiter',70,9,'Jupiter.jpg', 0.006, 0);
stars.push(Jupiter);
Saturn = this.initPlanet('Saturn',100,7,'Saturn.jpg', 0.005, 0);
stars.push(Saturn);
Uranus = this.initPlanet('Uranus',120,4,'Uranus.jpg', 0.003, 0);
stars.push(Uranus);
Neptune = this.initPlanet('Neptune',150,3,'Neptune.jpg', 0.002, 0);
stars.push(Neptune);
}
/**
* 初始化行星
* @param name 行星名字
* @param color 颜色
* @param distance 距离原点(太阳中心)的距离
* @param volume 体积
* @returns {{name: *, distance: *, volume: *, Mesh: THREE.Mesh}}
*/
function initPlanet(name,distance,volume,imageFile,speed,angle) {
var texture = THREE.ImageUtils.loadTexture("images/" + imageFile);
var mat = new THREE.MeshLambertMaterial();
mat.map = texture;
var mesh1 = new THREE.SphereGeometry(volume, 16, 16)
var mesh = new THREE.Mesh(mesh1,mat);
mesh.position.z = -distance;
mesh.receiveShadow = true;
mesh.castShadow = true;
mesh.name = name;
/*轨道*/
var track = new THREE.Mesh(new THREE.RingGeometry(distance - 0.2, distance + 0.2, 64, 1), new THREE.MeshBasicMaterial({ color: 0x888888, side: THREE.DoubleSide }));
track.rotation.x = -Math.PI / 2;
scene.add(track);
var star = {
name: name,
speed: speed,
angle: angle,
distance: distance,
volume: volume,
Mesh: mesh,
Mesh1: mesh1
}
mesh.position.set(0, controls.userHeight, -1);///我改过了
scene.add(mesh);
return star;
}
//所有行星转动
function moveEachStar(star) {
star.angle += star.speed;
if (star.angle > Math.PI * star.distance) {
star.angle -= Math.PI * star.distance;
}
star.Mesh.position.set(star.distance * Math.sin(star.angle), 0, star.distance * Math.cos(star.angle));
//star.Mesh1.rotation.y = star.Mesh1.rotation.y == 2 * Math.PI ? 0.0008 * Math.PI : star.Mesh1.rotation.y + 0.0008 * Math.PI;
}
//行星移动
function move() {
/*行星公转*/
for (var i = 0; i < stars.length; i++) {
moveEachStar(stars[i]);
}
/*太阳自转*/
Sun.rotation.y = Sun.rotation.y == 2 * Math.PI ? 0.0008 * Math.PI : Sun.rotation.y + 0.0008 * Math.PI;
}
</script>
</html>