-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcss3d_panorama.html
250 lines (216 loc) · 7.89 KB
/
css3d_panorama.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
<!DOCTYPE html>
<html>
<head>
<title>three.js css3d - panorama</title>
<meta charset="utf-8">
<!--
如果没有设置viewport的width的话,网页很可能会超出手机屏幕宽度,具体多宽,要看浏览器定义的默认宽度是多少
user-scalable=no,规定了用户不能缩放网页,但有些浏览器对该项支持不是很好,故需要设置minimum-scale和maximum-scale相同来限制用户缩放
-->
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0;
cursor: move;
overflow: hidden;
}
a {
color: #ffffff;
}
#info {
position: absolute;
width: 100%;
color: #ffffff;
padding: 5px;
font-family: Monospace;
font-size: 13px;
font-weight: bold;
text-align: center;
z-index: 1;
}
</style>
</head>
<body>
<script src="../build/three.js"></script>
<!--
使用CSS3渲染3D的DOM元素
-->
<script src="js/renderers/CSS3DRenderer.js"></script>
<div id="info"><a href="http://threejs.org" target="_blank">three.js css3d</a> - panorama demo. cubemap by <a href="http://www.humus.name/index.php?page=Textures" target="_blank">Humus</a>.</div>
<script>
var camera, scene, renderer;
var geometry, material, mesh;
var target = new THREE.Vector3();
var lon = 90, lat = 0;
var phi = 0, theta = 0;
var touchX, touchY;
init();
animate();
function init() {
/*
PerspectiveCamera(fov, aspect, near, far)
fov(视场):从相机位置能够看到的部分场景。推荐默认值45
aspect(长宽比):渲染结果输出区域的横向长度和纵向长度的比值。推荐默认值window.innerWidth/window.innerHeight
near(近面):定义从距离相机多近的地方开始渲染场景。推荐默认值0.1
far(远面):定义相机可以从它所处的位置看多远。默认值1000
*/
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
scene = new THREE.Scene();
var sides = [
{
url: 'textures/cube/Bridge2/posx.jpg',
position: [ -512, 0, 0 ],
rotation: [ 0, Math.PI / 2, 0 ]
},
{
url: 'textures/cube/Bridge2/negx.jpg',
position: [ 512, 0, 0 ],
rotation: [ 0, -Math.PI / 2, 0 ]
},
{
url: 'textures/cube/Bridge2/posy.jpg',
position: [ 0, 512, 0 ],
rotation: [ Math.PI / 2, 0, Math.PI ]
},
{
url: 'textures/cube/Bridge2/negy.jpg',
position: [ 0, -512, 0 ],
rotation: [ - Math.PI / 2, 0, Math.PI ]
},
{
url: 'textures/cube/Bridge2/posz.jpg',
position: [ 0, 0, 512 ],
rotation: [ 0, Math.PI, 0 ]
},
{
url: 'textures/cube/Bridge2/negz.jpg',
position: [ 0, 0, -512 ],
rotation: [ 0, 0, 0 ]
}
];
/*
var sides = [
{
url: 'textures/cube/test/px.jpg',
position: [ -512, 0, 0 ],
rotation: [ 0, Math.PI / 2, 0 ]
},
{
url: 'textures/cube/test/nx.jpg',
position: [ 512, 0, 0 ],
rotation: [ 0, -Math.PI / 2, 0 ]
},
{
url: 'textures/cube/test/py.jpg',
position: [ 0, 512, 0 ],
rotation: [ Math.PI / 2, 0, Math.PI ]
},
{
url: 'textures/cube/test/ny.jpg',
position: [ 0, -512, 0 ],
rotation: [ - Math.PI / 2, 0, Math.PI ]
},
{
url: 'textures/cube/test/pz.jpg',
position: [ 0, 0, 512 ],
rotation: [ 0, Math.PI, 0 ]
},
{
url: 'textures/cube/test/nz.jpg',
position: [ 0, 0, -512 ],
rotation: [ 0, 0, 0 ]
}
];
*/
for ( var i = 0; i < sides.length; i ++ ) {
var side = sides[ i ];
var element = document.createElement( 'img' );
element.width = 1026; // 2 pixels extra to close the gap. 使用额外的两个像素来填补间隙,否则会有一条黑边
element.src = side.url;
var object = new THREE.CSS3DObject( element );
//fromArray(buffer, offset),从数组的offset位置开始转化为vectorN
object.position.fromArray( side.position );
object.rotation.fromArray( side.rotation );
scene.add( object );
}
renderer = new THREE.CSS3DRenderer();
//设置渲染场景的大小
renderer.setSize( window.innerWidth, window.innerHeight );
//将渲染器的DOM元素(即Canvas)添加到HTML中
document.body.appendChild( renderer.domElement );
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'wheel', onDocumentMouseWheel, false );
//注意:触摸事件比鼠标事件少了一个touchend,因为暂不存在不放开手指的情况
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
//重新设置相机的宽高比。如果宽高比不对,那么正方形可能就不是正方形了
camera.aspect = window.innerWidth / window.innerHeight;
//更新透视相机的投影矩阵
camera.updateProjectionMatrix();
//重新设置渲染场景的大小
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseDown( event ) {
//通知 Web 浏览器不要执行与事件关联的默认动作
event.preventDefault();
//在鼠标按下时监听鼠标移动和鼠标弹起事件,避免捕获不必要的事件浪费资源
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
}
function onDocumentMouseMove( event ) {
//currentEvent.movementX 是两个鼠标移动事件间隔时间中当中鼠标移动的相对坐标( currentEvent.screenX - previousEvent.screenX )
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
lon -= movementX * 0.1;
lat += movementY * 0.1;
}
function onDocumentMouseUp( event ) {
//鼠标弹起时移除不必要的事件
document.removeEventListener( 'mousemove', onDocumentMouseMove );
document.removeEventListener( 'mouseup', onDocumentMouseUp );
}
function onDocumentMouseWheel( event ) {
//delta可以获取鼠标滚轮的方向和速度。如果delta的值是负的,那么滚轮就是向下滚动,正的就是向上。deltaX, deltaY分别是滚轮滚动的坐标值
camera.fov += event.deltaY * 0.05;
//这里要设置一个视场范围,过大或过小的视场都会导致显示异常
camera.fov = Math.max(10, camera.fov);
camera.fov = Math.min(120, camera.fov);
//更新相机的投影矩阵
camera.updateProjectionMatrix();
}
function onDocumentTouchStart( event ) {
event.preventDefault();
var touch = event.touches[ 0 ];
touchX = touch.screenX;
touchY = touch.screenY;
}
function onDocumentTouchMove( event ) {
event.preventDefault();
var touch = event.touches[ 0 ];
lon -= ( touch.screenX - touchX ) * 0.1;
lat += ( touch.screenY - touchY ) * 0.1;
touchX = touch.screenX;
touchY = touch.screenY;
}
function animate() {
requestAnimationFrame( animate );
lon += 0.1;
//设置仰俯角范围[-85,85]
lat = Math.max( - 85, Math.min( 85, lat ) );
//THREE.Math.degToRad将角度转换成弧度
phi = THREE.Math.degToRad( 90 - lat );
theta = THREE.Math.degToRad( lon );
//相机注视的点是单位球上的一点,这是一个方向向量
target.x = Math.sin( phi ) * Math.cos( theta );
target.y = Math.cos( phi );
target.z = Math.sin( phi ) * Math.sin( theta );
camera.lookAt( target );
renderer.render( scene, camera );
}
</script>
</body>
</html>