-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththree_app.js
194 lines (155 loc) · 4.75 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
//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 == "highlight"){
if(value){
var emit = 0x330000;
}
else{
var emit = 0x000000;
}
}
else{
var emit = mesh.material.emissive;
}
if(state_name == "switch"){
if(value){
var spec = 0x00aa00;
}
else{
var spec = 0x000000;
}
}
else{
var spec = mesh.material.specular;
}
var material = new THREE.MeshPhongMaterial( {
color: mesh.material.color,
emissive: emit,
specular: spec,
side: mesh.material.side,
flatShading: mesh.material.flatShading
});
mesh.material = material;
console.log(`${mesh.name} has emissive at ${emit.toString(16)}`);
}
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, 20 );
camera.position.z = -3;
camera.position.y = 2;
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 = 10;
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 load_scene(gltf_filename,on_load){
var loader = new THREE.GLTFLoader();
loader.load(gltf_filename,
// called when the resource is loaded
function ( gltf ) {
scene = gltf.scene;
console.log(`scene object names traversal`);
scene.traverse(obj =>{
console.log(` - ${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;
} );
//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);
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("./scene_light.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};