-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththree_app.js
234 lines (193 loc) · 6.22 KB
/
three_app.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
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
//import "../libs/three/three.module.js";
//import "../libs/three/OrbitControls.js";
var camera, scene, renderer;
var controls;
function setBulbState(mesh,state_name,value){
if(state_name == "switch"){
if(value){
var emit = new THREE.Color( 0, 0.5, 0 );
}
else{
var emit = new THREE.Color( 0, 0, 0 );
}
}
else{
if(state_name == "highlight"){
if(value){
var emit = new THREE.Color( 1, 0, 0 );
}
else{
var emit = new THREE.Color( 0, 0, 0 );
}
}
else{
var emit = mesh.material.emissive;
}
}
var material = new THREE.MeshPhongMaterial( {
color: mesh.material.color,
emissive: emit,
side: mesh.material.side,
flatShading: mesh.material.flatShading
});
mesh.material = material;
console.log(`${mesh.name} has emissive at ${emit.getHexString()}`);
}
function create_camera(){
var container = document.getElementById('viewer');
var w = container.clientWidth;
var h = container.clientHeight;
camera = new THREE.PerspectiveCamera( 45, w / h, 0.01, 50 );
camera.position.y = 10;
camera.position.x = 0;
camera.position.z = 15;
return camera;
}
function create_renderer(){
var container = document.getElementById('viewer');
var w = container.clientWidth;
var h = container.clientHeight;
renderer = new THREE.WebGLRenderer( { antialias: true,alpha:true } );
renderer.setSize( w, h );
renderer.setClearColor( 0x000000, 0.0 );
renderer.physicallyCorrectLights = true;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
container.appendChild(renderer.domElement);
return renderer;
}
function add_view_orbit(camera,renderer){
controls = new THREE.OrbitControls( camera, renderer.domElement );
//controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop)
controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
controls.dampingFactor = 1.5;//0.1:too rolly, 1: smooth, 2 unstable
controls.screenSpacePanning = false;
controls.minDistance = 0.10;
controls.maxDistance = 30;
controls.minPolarAngle = 10 * Math.PI / 180;
controls.maxPolarAngle = 80 * Math.PI / 180;
controls.rotateSpeed = 0.7;
return controls;
}
function onWindowResize() {
var container = document.getElementById('viewer');
var w = container.clientWidth;
var h = container.clientHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize( w, h );
}
function add_ambient_light(){
var dirLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
dirLight.position.set( 1, 3, 0 );
dirLight.castShadow = true;
//dirLight.receiveShadow = true;
dirLight.shadow.mapSize.width = 2048;
dirLight.shadow.mapSize.height = 2048;
var d = 6;
dirLight.shadow.camera.left = - d;
dirLight.shadow.camera.right = d;
dirLight.shadow.camera.top = d;
dirLight.shadow.camera.bottom = - d;
dirLight.shadow.camera.far = 7;
dirLight.shadow.bias = - 0.01;
scene.add( dirLight );
}
function log_scene_info(scene){
console.log(`scene object names traversal`);
scene.traverse(obj =>{
console.log(` - ${obj.type} ; ${obj.name}`);
//cannot distinguish Light from Camera so apply shadow options to all children without if(obj.type == "Mesh")
obj.castShadow = true;
obj.receiveShadow = true;
} );
}
function get_scene_box(scene){
let first = true;
var box;
scene.traverse(obj =>{
if(obj.type == "Mesh"){
if(first){
box = new THREE.Box3().setFromObject(obj);
first = false;
}else{
box.expandByObject(obj)
}
}
} );
return box;
}
function center_scene(scene){
console.log(`centering the scene`);
var box = get_scene_box(scene);
var s = box.getSize();
console.log(`scene boxed from (${box.min.x},${box.min.y},${box.min.z}) to (${box.max.x},${box.max.y},${box.max.z}) with size (${s.x},${s.y},${s.z})`);
const center_x = (box.max.x - box.min.x)/2;
const center_y = (box.max.y - box.min.y)/2;
console.log(`shifting the scene by x = ${-center_x} , y = ${-center_y}`);
//scene.position.set(scene.position.x - center_x, scene.position.y - center_y, scene.position.z);
scene.traverse(obj =>{
//though only meshes are taken as input, here everything is shifted as lights shall shift too
//hierarchical structure does move end leaves multiple times, so selection of meshes only moved as workaround
if(obj.type == "Mesh"){
obj.position.set(obj.position.x + center_x, obj.position.y - center_y,obj.position.z);
}
} );
box = get_scene_box(scene);
s = box.getSize();
console.log(`now scene boxed from (${box.min.x},${box.min.y},${box.min.z}) to (${box.max.x},${box.max.y},${box.max.z}) with size (${s.x},${s.y},${s.z})`);
}
function load_scene(gltf_filename,user_on_load){
var loader = new THREE.GLTFLoader();
loader.load(gltf_filename,
// called when the resource is loaded
function ( gltf ) {
scene = gltf.scene;
//log_scene_info(scene);
center_scene(scene);
//The glTF camera is not having the correct window spect ratio and does produce very minimal orbit control movements
//camera = gltf.cameras[0];
//The glTF light might not have all fine tuning options such as shadow.mapsize
//add_ambient_light();
camera = create_camera();
renderer = create_renderer();
controls = add_view_orbit(camera,renderer);
user_on_load();
},
// called while loading is progressing
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log( 'An error happened' );
}
);
}
function init(on_load){
console.log("three_app> init()");
load_scene("./rooms.gltf",on_load);
window.addEventListener( 'resize', onWindowResize, false );
}
function animate() {
requestAnimationFrame( animate );
controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true
renderer.render( scene, camera );
}
function getObjects(objects_names){
var mesh_list = [];
objects_names.forEach(name => {
let mesh = scene.getObjectByName(name);
if(typeof mesh !== 'undefined'){
mesh_list.push(mesh);
}
else{
console.log(`No object named ${name} in 'scene'`);
}
})
return mesh_list;
}
function getCamera(){
return camera;
}
export{init,animate,getObjects,setBulbState,getCamera};