-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdyna_texture.html
437 lines (400 loc) · 17.2 KB
/
dyna_texture.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>动态全景图</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/dat-gui-light-theme.css" type="text/css" />
<meta name="viewport" content="user-scalable=no, initial-scale=1">
<style>
body {
margin: 0px;
overflow: hidden;
z-index: -1000;
}
#progress {
position: absolute;
bottom: 0;
right: 10%;
width: 80%;
}
</style>
</head>
<body>
<div id="container"></div>
<input id="progress" type="range" name="progress" min="0" max="1" step="0.00001" value="0" />
<script src="js/jquery.min.js"></script>
<script src="js/Detector.js"></script>
<script src="js/three.js"></script>
<script src="js/d3.v4.min.js"></script>
<script src="js/CanvasRenderer.js"></script>
<script src="js/Stats.js"></script>
<script src="js/DeviceOrientationControls.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/dat.gui.min.js"></script>
<script src="js/tween.min.js"></script>
<script src="js/utils.js"></script>
<script src="js/DualTriMesh.js"></script>
<script>
"use strict";
function newTriangle(a,b,c) { // a,b,c
var geometry = new THREE.Geometry();
geometry.vertices.push(a.clone());
geometry.vertices.push(b.clone());
geometry.vertices.push(c.clone());
geometry.faces.push( new THREE.Face3(0,1,2) );
geometry.updatePosition = function (a,b,c) {
geometry.vertices[0].copy(a);
geometry.vertices[1].copy(b);
geometry.vertices[2].copy(c);
geometry.verticesNeedUpdate = true;
};
return geometry;
}
// 这个渲染器用来得到【纹理帧】
var _renderer = new THREE.WebGLRenderer({ antialias: true });
_renderer.setSize(4096, 2048);
// 正射相机用于采样得到【纹理帧】
var orthCamera = new THREE.OrthographicCamera(-2048,2048,1024,-1024,1,5000); // left, right, top, bottom, near, far
orthCamera.position.set(0,0,3000);
orthCamera.lookAt(new THREE.Vector3(0,0,0));
var group = new THREE.Group();
var materialA = new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader().load( 'texture1.jpg' ), side: THREE.DoubleSide, transparent: true } );
var materialB = new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader().load( 'texture2.jpg' ), side: THREE.DoubleSide, transparent: false } );
// 一组三角形,通过三个同名点 renference r1、r2、r3 来控制三角形的位移和变形
function newPairTriangle(r1,r2,r3) {
var group = new THREE.Group();
// 把 renference 保存起来
group.pairs = [ r1,r2,r3 ];
// p{1~3} 是初始位置,p{4~6} 是目标位置
var p1 = new THREE.Vector3(r1.x1,r1.y1,0),
p2 = new THREE.Vector3(r2.x1,r2.y1,0),
p3 = new THREE.Vector3(r3.x1,r3.y1,0),
p4 = new THREE.Vector3(r1.x2,r1.y2,0),
p5 = new THREE.Vector3(r2.x2,r2.y2,0),
p6 = new THREE.Vector3(r3.x2,r3.y2,0);
// 用初始位置初始三个三角形 tri1、tri2、tri3
var tri1 = newTriangle( p1.clone().setZ(200), p2.clone().setZ(200), p3.clone().setZ(200) ),
tri2 = newTriangle( p1.clone().setZ(100), p2.clone().setZ(100), p3.clone().setZ(100) ),
tri3 = newTriangle( p1.clone().setZ(300), p2.clone().setZ(300), p3.clone().setZ(300) );
// tri1 应用当前全景纹理,tri2 应用下一站全景纹理,tri3 应用白色线框
var mesh1 = new THREE.Mesh(tri1, materialA),
mesh2 = new THREE.Mesh(tri2, materialB),
mesh3 = new THREE.Mesh(tri3, config.whiteFrameMaterial);
group.add(mesh1).add(mesh2).add(mesh3);
if (!config.debug) {
mesh3.visible = false;
}
mesh1.geometry.faceVertexUvs[0] = []; // set up an array
mesh1.geometry.faceVertexUvs[0].push([ // and push some elements
Util.xy2uv(p1.x,p1.y),
Util.xy2uv(p2.x,p2.y),
Util.xy2uv(p3.x,p3.y)
]);
mesh1.uvsNeedUpdate = true;
mesh2.geometry.faceVertexUvs[0] = [];
mesh2.geometry.faceVertexUvs[0].push([
Util.xy2uv(p4.x,p4.y),
Util.xy2uv(p5.x,p5.y),
Util.xy2uv(p6.x,p6.y)
]);
mesh2.uvsNeedUpdate = true;
group.tri1 = tri1; group.mesh1 = mesh1;
group.tri2 = tri2; group.mesh2 = mesh2;
group.tri3 = tri3; group.mesh3 = mesh3;
// 更新位置
group.updatePosition = function(alpha) {
var r1 = group.pairs[0],
r2 = group.pairs[1],
r3 = group.pairs[2];
var p1 = new THREE.Vector3(r1.x1,r1.y1,0), p4 = new THREE.Vector3(r1.x2,r1.y2,0),
p2 = new THREE.Vector3(r2.x1,r2.y1,0), p5 = new THREE.Vector3(r2.x2,r2.y2,0),
p3 = new THREE.Vector3(r3.x1,r3.y1,0), p6 = new THREE.Vector3(r3.x2,r3.y2,0);
// 线性内插
p1.lerp(p4, alpha);
p2.lerp(p5, alpha);
p3.lerp(p6, alpha);
// 分别更新三角形
group.tri1.updatePosition(p1.clone().setZ(200),p2.clone().setZ(200),p3.clone().setZ(200));
group.tri2.updatePosition(p1.clone().setZ(100),p2.clone().setZ(100),p3.clone().setZ(100));
group.tri3.updatePosition(p1.clone().setZ(300),p2.clone().setZ(300),p3.clone().setZ(300));
group.mesh3.visible = config.debug;
};
group.isPairTriangle = true;
return group;
}
var container, camera, scene, renderer,
texture,
material,
geometry,
mesh,
controlsDeviceOri, controlsOrbit,
stats;
var Config = function() {
var _this = this;
this.debug = false;
this.autorot = 0;
this.whiteFrameMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true, side: THREE.DoubleSide, transparent: true });
// 压测
this.benchmark = false;
this.lastProgress = -1.0;
this.progress = 0.0;
this.inProgress = false;
this.applyProgress = function() {
$(".close-button").click();
$("#progress")[0].value = _this.progress = 0.0;
_this.inProgress = true;
var t1 = new TWEEN.Tween(_this)
.delay(100)
.to({ progress: 1.0 }, 5000)
.onComplete(function() {
_this.inProgress = false;
});
t1.start();
};
// 变形
this.morph = function(t) {
var t = t || _this.progress;
if (t === _this.lastProgress) { return; }
_this.lastProgress = t;
// 更新三角形组(有很多三角形组,每个三角形组由三个三角形,分别应用了当前纹理、下一站纹理,以及线框纹理)
group.children.forEach(function(tri){
if (tri.isPairTriangle === true) {
tri.updatePosition(t);
}
});
// render a canvas,渲染【纹理帧】
materialA.opacity = 1-t;
_renderer.render(scene, orthCamera);
// 渲染后,_renderer.domElement 就更新了,现在提醒一下 texture
texture.needsUpdate = true;
};
this.fov = 60;
this.lon = 180;
this.lat = 0;
this.freeze = false;
this.toggleCamera = function() {
controlsOrbit.enabled = !controlsOrbit.enabled;
if (controlsOrbit.enabled) {
camera.position.copy(camera.position0);
controlsOrbit.target.set(0,0,0);
} else {
camera.position0.copy(camera.position);
camera.position.set(0,0,0);
}
};
this.loadDefaultPairs = function() {
$.get('pairs.json', function(data) {
config.loadPairsTxt(data);
config.applyTriangulation();
}, 'text');
};
this.pairs = [];
// 这个说过了
this.loadPairsTxt = function(text) {
_this.pairs = [];
var pairs = JSON.parse(text);
pairs.forEach(function(pair){
var p = new Pair({
loading: true,
x1: pair.x1,
y1: pair.y1,
x2: pair.x2,
y2: pair.y2
});
if (p.valid === true) {
_this.pairs.push(p);
}
});
};
// 这个说过了
this.applyTriangulation = function(){
var sites = [];
var refs = {};
var xmin = 100000000, xmax = 0, ymin = 10000000000, ymax = 0;
_this.pairs.forEach(function(pair){
if (pair.valid) {
var p = [pair.x1, pair.y1];
refs[Util.pos2name(p[0],p[1])] = pair;
sites.push(p);
if (xmin > p[0]) { xmin = p[0]; }
if (xmax < p[0]) { xmax = p[0]; }
if (ymin > p[1]) { ymin = p[1]; }
if (ymax < p[1]) { ymax = p[1]; }
}
});
var voronoi = d3.voronoi().extent([[xmin-10, ymin-10], [xmax+10, ymax+10]]);
var diagram = voronoi(sites);
var triangles = diagram.triangles();
var index = 0;
group.childeren = [];
triangles.forEach(function(tri){
var p1 = tri[0];
var p2 = tri[1];
var p3 = tri[2];
var p1n = Util.pos2name(p1[0],p1[1]);
var p2n = Util.pos2name(p2[0],p2[1]);
var p3n = Util.pos2name(p3[0],p3[1]);
var ref1 = refs[p1n];
var ref2 = refs[p2n];
var ref3 = refs[p3n];
if ( ref1 !== undefined && ref2 !== undefined && ref3 !== undefined ) {
// editor
var mesh = newPairTriangle(ref1,ref2,ref3);
group.add(mesh);
}
});
};
};
var config = new Config();
var isDesktop = !navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|BB10|mobi|tablet|opera mini|nexus 7)/i);
var gui = new dat.GUI(); gui.closed = true;
window.addEventListener('load', function() {
var animate = function(time){
window.requestAnimationFrame( animate );
TWEEN.update();
if (config.inProgress) {
$("#progress")[0].value = config.progress;
}
if (config.benchmark) {
config.progress = time%100/100;
}
config.morph();
if (controlsOrbit.enabled) {
controlsOrbit.update();
} else {
if (controlsDeviceOri.enabled) {
if (!config.freeze) {
controlsDeviceOri.update();
}
} else {
camera.lon += config.autorot/5;
if (camera.lon > 360) { camera.lon -= 360; }
if (camera.lon < 0) { camera.lon += 360; }
camera.lat = Math.max( - 85, Math.min( 85, camera.lat ) );
var phi = THREE.Math.degToRad( 90 - camera.lat );
var theta = THREE.Math.degToRad( camera.lon );
camera.target.x = 500 * Math.sin( phi ) * Math.cos( theta );
camera.target.y = 500 * Math.cos( phi );
camera.target.z = 500 * Math.sin( phi ) * Math.sin( theta );
camera.lookAt( camera.target.add(camera.position) );
}
}
renderer.render(scene, camera);
stats.update();
};
stats = new Stats();
document.body.appendChild(stats.domElement);
camera = new THREE.PerspectiveCamera(config.fov, window.innerWidth / window.innerHeight, 1, 10000000);
camera.isUserInteracting = false;
camera.lon = 169.89999999999998;
camera.lat = -3.3000000000000003;
camera.onPointerDownPointerX = 0;
camera.onPointerDownPointerY = 0;
camera.onPointerDownLon = 0;
camera.onPointerDownLat = 0;
camera.target = new THREE.Vector3( 0, 0, 0 );
camera.position0 = new THREE.Vector3( 1000, 0, 0 );
scene = new THREE.Scene();
scene.add(new THREE.AmbientLight(0xffffff));
group.position.set(0,0,2500);
scene.add(group);
scene.add(new THREE.AxisHelper(1000));
var geometry = new THREE.PlaneGeometry( 4096, 2048, 32, 16 );
var plane1 = new THREE.Mesh( geometry.clone(), materialA ); plane1.position.set(0,0,2000);
var plane2 = new THREE.Mesh( geometry.clone(), materialB ); plane2.position.set(0,0,1000);
scene.add(plane1).add(plane2);
config.plane1 = plane1;
config.plane2 = plane2;
// 注意这个纹理,是从 _renderer 的 domElement 而来
texture = new THREE.Texture( _renderer.domElement, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
texture.needsUpdate = true;
geometry = new THREE.SphereGeometry( 500, 64, 32 ); geometry.scale( - 1, 1, 1 );
// 全景球上用的就是这个纹理
material = new THREE.MeshBasicMaterial( { map: texture, side: THREE.DoubleSide } ),
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
container = document.getElementById( 'container' );
if( Detector.webgl ){
renderer = new THREE.WebGLRenderer({
antialias: true
// , alpha: true
});
} else {
renderer = new THREE.CanvasRenderer();
}
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.domElement.style.position = 'absolute';
renderer.domElement.style.top = 0;
container.appendChild(renderer.domElement);
controlsDeviceOri = new THREE.DeviceOrientationControls( camera );
controlsOrbit = new THREE.OrbitControls( camera, renderer.domElement );
controlsOrbit.enabled = false;
if (isDesktop) {
controlsDeviceOri.enabled = false;
}
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}, false);
container.addEventListener( 'mousedown', function(event) {
if (controlsOrbit.enabled) { return; }
camera.isUserInteracting = true;
camera.onPointerDownPointerX = event.clientX;
camera.onPointerDownPointerY = event.clientY;
camera.onPointerDownLon = camera.lon;
camera.onPointerDownLat = camera.lat;
}, false );
container.addEventListener( 'mousemove', function(event) {
if (controlsOrbit.enabled) { return; }
if ( camera.isUserInteracting === true ) {
camera.lon = ( camera.onPointerDownPointerX - event.clientX ) * 0.1 + camera.onPointerDownLon;
camera.lat = ( event.clientY - camera.onPointerDownPointerY ) * 0.1 + camera.onPointerDownLat;
}
}, false );
container.addEventListener( 'mouseup', function(event) {
if (controlsOrbit.enabled) { return; }
camera.isUserInteracting = false;
}, false );
container.addEventListener( 'wheel', function(event) {
var fov = camera.fov + event.deltaY * 0.05;
camera.fov = THREE.Math.clamp(Math.round(fov), 10, 100);
config.fov = camera.fov; gui.updateDisplay();
camera.updateProjectionMatrix();
}, false );
gui.add(config, 'applyProgress').name("过渡");
gui.add(config, 'benchmark').name("压力测试");
gui.add(config, 'toggleCamera').name("切换相机");
gui.add(config, 'autorot').min(-5).max(+5).step(1).listen().name("自动浏览");
gui.add(config, 'fov').min(10).max(150).step(1).listen().name("视场角").onChange(function(value){
if (config.fov !== camera.fov) {
camera.fov = value;
camera.updateProjectionMatrix();
}
});
gui.add(config, 'debug').name("显示网格").onChange(function(){
config.lastProgress = -1.0;
});
if (isDesktop) {
gui.add(camera, 'lon').min( 0).max(360).step(1).listen().name("lon");
gui.add(camera, 'lat').min(-85).max( 85).step(1).listen().name("lat");
} else {
gui.add(config, 'freeze').name("锁定重力感应");
}
var links = gui.addFolder('链接');
var link = {
"README": function() { window.location = "README.html"; },
"dyna_sphere": function() { window.location = "dyna_sphere.html"; }
};
links.add(link, 'dyna_sphere').name("动态全景球方案");
links.add(link, 'README').name("文档");
animate();
config.loadDefaultPairs();
}, false);
$("#progress").on("input", function(){
config.progress = this.value;
});
</script>
</body>
</html>