-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
/
Copy paththreejs-lots-of-objects.js
142 lines (99 loc) · 3.13 KB
/
threejs-lots-of-objects.js
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
import * as THREE from 'three';
import { threejsLessonUtils } from './threejs-lesson-utils.js';
import { GUI } from '../../examples/jsm/libs/lil-gui.module.min.js';
{
class DegRadHelper {
constructor( obj, prop ) {
this.obj = obj;
this.prop = prop;
}
get value() {
return THREE.MathUtils.radToDeg( this.obj[ this.prop ] );
}
set value( v ) {
this.obj[ this.prop ] = THREE.MathUtils.degToRad( v );
}
}
function scaleCube( zOffset ) {
const root = new THREE.Object3D();
const size = 3;
const geometry = new THREE.BoxGeometry( size, size, size );
geometry.applyMatrix4( new THREE.Matrix4().makeTranslation( 0, 0, zOffset * size ) );
const material = new THREE.MeshBasicMaterial( {
color: 'red',
} );
const cube = new THREE.Mesh( geometry, material );
root.add( cube );
cube.add( new THREE.LineSegments(
new THREE.EdgesGeometry( geometry ),
new THREE.LineBasicMaterial( { color: 'white' } ) ) );
[[ 0, 0 ], [ 1, 0 ], [ 0, 1 ]].forEach( ( rot ) => {
const size = 10;
const divisions = 10;
const gridHelper = new THREE.GridHelper( size, divisions );
root.add( gridHelper );
gridHelper.rotation.x = rot[ 0 ] * Math.PI * .5;
gridHelper.rotation.z = rot[ 1 ] * Math.PI * .5;
} );
return {
obj3D: root,
update: ( time ) => {
const s = THREE.MathUtils.lerp( 0.5, 2, Math.sin( time ) * .5 + .5 );
cube.scale.set( s, s, s );
},
};
}
threejsLessonUtils.addDiagrams( {
scaleCenter: {
create() {
return scaleCube( 0 );
},
},
scalePositiveZ: {
create() {
return scaleCube( .5 );
},
},
lonLatPos: {
create( info ) {
const { scene, camera, renderInfo } = info;
const size = 10;
const divisions = 10;
const gridHelper = new THREE.GridHelper( size, divisions );
scene.add( gridHelper );
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const lonHelper = new THREE.Object3D();
scene.add( lonHelper );
const latHelper = new THREE.Object3D();
lonHelper.add( latHelper );
const positionHelper = new THREE.Object3D();
latHelper.add( positionHelper );
{
const lonMesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 'green' } ) );
lonMesh.scale.set( 0.2, 1, 0.2 );
lonHelper.add( lonMesh );
}
{
const latMesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 'blue' } ) );
latMesh.scale.set( 1, 0.25, 0.25 );
latHelper.add( latMesh );
}
{
const geometry = new THREE.SphereGeometry( 0.1, 24, 12 );
const posMesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 'red' } ) );
posMesh.position.z = 1;
positionHelper.add( posMesh );
}
camera.position.set( 1, 1.5, 1.5 );
camera.lookAt( 0, 0, 0 );
const gui = new GUI( { autoPlace: false } );
renderInfo.elem.appendChild( gui.domElement );
gui.add( new DegRadHelper( lonHelper.rotation, 'y' ), 'value', - 180, 180 ).name( 'lonHelper x rotation' );
gui.add( new DegRadHelper( latHelper.rotation, 'x' ), 'value', - 90, 90 ).name( 'latHelper y rotation' );
return {
trackball: false,
};
},
},
} );
}