-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathshow_3d_2d.html
501 lines (324 loc) · 11 KB
/
show_3d_2d.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - interactive - voxel painter</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body id="body">
<script src="libs/three.js"></script>
<script src="libs/Detector.js"></script>
<script src="libs/OrbitControls.js"></script>
<script src="libs/hilbert2D.js"></script>
<script src="libs/hilbert3D.js"></script>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var camera, scene, renderer;
var plane, cube;
var mouse, raycaster, isShiftDown = false;
var rollOverMesh, rollOverMaterial;
var cubeGeo, cubeMaterial;
var objects = [];
var voxels = []
var earth_raduis = 180
var cubeMap={}
var objects=[]
var hilbert3DIt =3
//var points2d = hilbert2D( new THREE.Vector3( 0, 0, 0 ), width, it )
var points = hilbert3D( new THREE.Vector3( 0, 0, 0 ), earth_raduis, hilbert3DIt, 0, 1, 2, 3, 4, 5, 6, 7 )
var cubeLen = (earth_raduis*2)/Math.pow(points.length,1/3)
var it = Math.log(points.length)/Math.log(4)-1
var width = cubeLen*Math.sqrt(Math.pow(4,it+1))/2
var points2d = hilbert2D( new THREE.Vector3( 0, -earth_raduis, 0 ), width, it )
var indexIn2d = 0
var cubeGeo = new THREE.BoxBufferGeometry( cubeLen, cubeLen, cubeLen );
init();
render();
show3dHilbertCurve(points)
//show2dHilbertCurve(points2d)
//animate();
function init() {
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 100000 );
camera.position.set( 0, 1000, 1000 );
camera.lookAt( new THREE.Vector3() );
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x000000 );
//objects.push( plane );
// lights
var ambientLight = new THREE.AmbientLight( 0x606060 );
scene.add( ambientLight );
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.set( 1, 0.75, 0.5 ).normalize();
scene.add( directionalLight );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
window.addEventListener( 'resize', onWindowResize, false );
var controls = new THREE.OrbitControls( camera, renderer.domElement);
controls.minDistance = 2;
controls.maxDistance = 100000;
controls.maxPolarAngle = Math.PI;
render();
}
function show3dHilbertCurve(points){
// 3d lines
// 3d points length:8^(n+1)
var geometry3 = new THREE.Geometry()
var colors3 = [];
for ( var i = 0; i < points.length; i ++ ) {
geometry3.vertices.push( points[ i ] );
colors3[ i ] = new THREE.Color( 0xffffff );
colors3[ i ].setHSL( i / points.length, 1, 0.5 );
}
geometry3.colors = colors3;
// lines
var material = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 3, vertexColors: THREE.VertexColors } );
var line3d = new THREE.Line( geometry3, material );
//line3d.scale.set( 0.4, 0.4, 0.4 );
line3d.position.set( 0, 0, 0 );
scene.add( line3d );
}
function show2dHilbertCurve(points){
var geometry2 = new THREE.Geometry()
var colors2 = []
for ( var i = 0; i < points.length; i ++ ) {
geometry2.vertices.push( points[ i ] );
colors2[ i ] = new THREE.Color( 0xffffff );
colors2[ i ].setHSL( i / points.length, 1, 0.5 );
}
geometry2.colors = colors2;
// lines
var material = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 3, vertexColors: THREE.VertexColors } );
var line2d = new THREE.Line( geometry2, material );
line2d.position.set( 0, -2, 0 );
scene.add( line2d );
}
function createInnerEarth(){
//createEarth
var whiteMaterial= new THREE.MeshPhongMaterial({
color:0xffffff,//三角面颜色
//side:THREE.DoubleSide//两面可见
});
var yellowMaterial= new THREE.MeshPhongMaterial({
color:0xffff00,//三角面颜色
//side:THREE.DoubleSide//两面可见
});
var orangeMaterial= new THREE.MeshPhongMaterial({
color:0xFFA500,//三角面颜色
//side:THREE.DoubleSide//两面可见
});
var redMaterial= new THREE.MeshPhongMaterial({
color:0xF75000,//三角面颜色
//side:THREE.DoubleSide//两面可见
});
var darkRedMaterial= new THREE.MeshPhongMaterial({
color:0xCE0000,//三角面颜色
//side:THREE.DoubleSide//两面可见
});
var crustMaterial= new THREE.MeshPhongMaterial({
color:0x333333,//三角面颜色
//side:THREE.DoubleSide//两面可见
});
var shellMaterial= new THREE.MeshPhongMaterial({
color:0x0033ff,//三角面颜色
//side:THREE.DoubleSide//两面可见
});
function getInnerKenalMaterial(){
var r = Math.floor(Math.random()*10)
if(r<3){
return whiteMaterial
}else{
return yellowMaterial
}
}
function getOutKenalMaterial(){
var r = Math.floor(Math.random()*10)
if(r<1){
return whiteMaterial
}else if(r<4){
return yellowMaterial
}else{
return orangeMaterial
}
}
function getLowMantleMaterial(){
var r = Math.floor(Math.random()*10)
if(r<2){
return orangeMaterial
}else{
return redMaterial
}
}
function getUpperMantleMaterial(sqaure_dis){
return crustMaterial
}
function getCrustMaterial(){
return crustMaterial
}
for(var i=0;i<points.length;i++){
var point = points[i]
var x = point.x
var y = point.y
var z = point.z
var sqaure_dis = Math.pow(x,2)+Math.pow(y,2)+Math.pow(z,2)
var voxel = null
var add2Scene = true
if(sqaure_dis < Math.pow(earth_raduis*0.215,2)){
voxel = new THREE.Mesh( cubeGeo, getInnerKenalMaterial() );
}else if(sqaure_dis < Math.pow(earth_raduis*(0.215+0.33),2)){
voxel = new THREE.Mesh( cubeGeo, getOutKenalMaterial() );
}else if(sqaure_dis < Math.pow(earth_raduis*(0.215+0.33+0.32),2)){
voxel = new THREE.Mesh( cubeGeo, getLowMantleMaterial() );
}else if(sqaure_dis < Math.pow(earth_raduis*(0.215+0.33+0.32+0.1),2)){
voxel = new THREE.Mesh( cubeGeo, getUpperMantleMaterial(sqaure_dis) );
}else if(sqaure_dis < Math.pow(earth_raduis,2)){
voxel = new THREE.Mesh( cubeGeo, getCrustMaterial() );
}else{
voxel = new THREE.Mesh( cubeGeo, shellMaterial );
add2Scene = false
}
if(cubeMap[x+":"+y+":"+z]){//地壳已经包含
continue
}
if(voxel){
voxel.position.set(x,y,z)
objects.push( voxel );
voxels.push(voxel)
// if(add2Scene&&z<0){//z<0 for half
// scene.add( voxel );
// }
if(add2Scene){
scene.add( voxel );
}
}
cubeMap[x+":"+y+":"+z] = voxel
}
}
function createEarthShell(points){
for(var i=0;i<points.length;i++){
var point = points[i]
// if(point['z']>0){
// continue
// }
var color = point['color']
var shellMaterial= new THREE.MeshPhongMaterial({
color:color,//三角面颜色
//side:THREE.DoubleSide//两面可见
});
var voxel = new THREE.Mesh( cubeGeo, shellMaterial );
voxel.position.set(point['x'],point['y'],point['z'])
objects.push( voxel );
voxels.push(voxel)
scene.add( voxel );
cubeMap[point['x']+":"+point['y']+":"+point['z']] = voxel
}
}
function loadModels(){
$.get("earth_shell_3it.json", function(result,status){
if(result == "file not found"){
alert("加载地壳模型异常!")
}else{
var points = result//JSON.parse(result)
createEarthShell(points)
createInnerEarth()
animate()
//transDim()
}
})
}
loadModels()
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function render() {
renderer.render( scene, camera );
}
var actionStep = 0
var theta = 0
var alpha = 0.5
function animate() {
render();
requestAnimationFrame( animate );
moveCamera()
if(actionStep == 0){
add2dCurve()
}else{
moveCube()
}
//moveCube()
}
var pushIndex = 0
function add2dCurve(){
if(pushIndex>=points2d.length-1){
actionStep = 1
return
}
var geometry2 = new THREE.Geometry()
var colors2 = []
//for ( var i = 0; i < points.length; i ++ ) {
geometry2.vertices.push( points2d[ pushIndex ] );
geometry2.vertices.push( points2d[ pushIndex+1 ] );
colors2[ 0 ] = new THREE.Color( 0xffffff );
colors2[ 1 ] = new THREE.Color( 0xffffff );
colors2[ 0 ].setHSL( pushIndex / points.length, 1, 0.5 );
colors2[ 1 ].setHSL( (pushIndex+1) / points.length, 1, 0.5 );
//}
geometry2.colors = colors2;
// lines
var material = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 3, vertexColors: THREE.VertexColors } );
var line2d = new THREE.Line( geometry2, material );
line2d.position.set( 0, 0, 0 );
scene.add( line2d );
pushIndex++
}
function moveCamera(){
var r = 2200
var cz = r*Math.sin(alpha)*Math.sin(theta)
var cx = r*Math.sin(alpha)*Math.cos(theta)
var cy = r*Math.cos(alpha)
theta-=0.001
//alpha+=0.001
camera.position.set(cx,cy,cz)
camera.lookAt(0,-650,0)
camera.rotateOnWorldAxis(new THREE.Vector3(0,1,0),0.001)
}
function moveCube(){
for (var i=0;i<points.length-1;i++){
var point = points[i]
var voxel = cubeMap[point.x+":"+point.y+":"+point.z]
var nextPoint = points[i+1]
var nextVoxel = cubeMap[nextPoint.x+":"+nextPoint.y+":"+nextPoint.z]
voxel.position.copy(nextVoxel.position)
}
var lastIndex = points.length-1
var lastPoint = points[lastIndex]
var lastVoxel = cubeMap[lastPoint.x+":"+lastPoint.y+":"+lastPoint.z]
var copyVec = new THREE.Vector3()
copyVec.copy(points2d[indexIn2d])
var distance = copyVec.sub(lastVoxel.position)
if(distance.length()<cubeLen){
lastVoxel.position.copy(points2d[indexIn2d])
indexIn2d++
}else{
var step = distance.normalize().multiplyScalar(cubeLen)
lastVoxel.position.add(step)
}
}
function transDim(){
for (var i=0;i<points.length-1;i++){
var point = points[i]
var voxel = cubeMap[point.x+":"+point.y+":"+point.z]
var point2d = points2d[i]
voxel.position.x = point2d.x
voxel.position.y = point2d.y
voxel.position.z = point2d.z
}
}
</script>
</body>
</html>