-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcerberusMLTandOBJLoad.js
More file actions
227 lines (182 loc) · 6.35 KB
/
Copy pathcerberusMLTandOBJLoad.js
File metadata and controls
227 lines (182 loc) · 6.35 KB
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
const canvas = document.getElementById('model3D');
const setAntialias = false;
const showWireframe = false;
const shapeShadows = false;
const sceneColor = 0xdddddd;
const cameraPositionX = 2;
const cameraPositionY = 2;
const cameraPositionZ = 5;
const cameraLookAtX = 0;
const cameraLookAtY = 0;
const cameraLookAtZ = 0;
let numberOfLights = 2
const sphereRadius = .1;
const sphereWidthSegments = 8;
const sphereHeightSegments = 8;
const sphereColor = 0xffffff;
const boxWidth = 0.3;
const boxHeight = 0.3;
const boxLength = 0.3;
const boxColor = 0x00fff0;
let object;
const manager = new THREE.LoadingManager(loadModel);
manager.onProgress = function (item, loaded, total) { console.log(item, loaded, total); };
var objLocationStr = 'assets/obj/cerberus/Cerberus.obj';
const mtlLoader = new THREE.MTLLoader(manager);
const loader = new THREE.OBJLoader(manager);
function init() {
var scene = new THREE.Scene();
scene.background = new THREE.Color(sceneColor);
var gui = new dat.GUI();
var camera = createCamera();
var renderer = createRenderer();
var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target = new THREE.Vector3(cameraLookAtX, cameraLookAtY, cameraLookAtZ);
createLightEnvironment(scene, gui);
var box = getBox(boxWidth, boxHeight, boxLength, boxColor);
scene.add(box);
getOBJRender(loader, scene);
document.getElementById('model3D').appendChild(renderer.domElement);
update(renderer, scene, camera, controls);
return scene;
}
function update(renderer, scene, camera, controls) {
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(function () {
update(renderer, scene, camera, controls);
});
}
function printShotgun(obj) {
console.log(obj);
}
function createCamera() {
// field of view || aspect ratio || near clipping plane || far clipping plane
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(cameraPositionX, cameraPositionY, cameraPositionZ);
camera.lookAt(cameraLookAtX, cameraLookAtY, cameraLookAtZ);
return camera;
}
function createRenderer() {
renderer = new THREE.WebGLRenderer({ antialias: setAntialias, alpha: true });
renderer.setSize(window.innerWidth / 1.05, window.innerHeight / 1.05);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
return renderer;
}
function getBox(boxWidth, boxHeight, boxLength, boxColor) {
var mesh = new THREE.Mesh(
new THREE.BoxGeometry(boxWidth, boxHeight, boxLength),
new THREE.MeshLambertMaterial({ color: boxColor, wireframe: showWireframe, }),
);
mesh.castShadow = true;
return mesh;
}
function getSphere(sphereRadius, sphereWidthSegments, sphereHeightSegments, sphereColor) {
var mesh = new THREE.Mesh(
new THREE.SphereGeometry(sphereRadius, sphereWidthSegments, sphereHeightSegments),
new THREE.MeshBasicMaterial({ color: sphereColor, wireframe: showWireframe, }),
);
mesh.castShadow = true;
return mesh;
}
function getAmbientLight(intensity) {
var light = new THREE.AmbientLight(0xffffff, intensity);
return light;
}
function getDirectionalLight(intensity) {
var light = new THREE.DirectionalLight(0xffffff, intensity);
light.castShadow = true;
return light;
}
function createLightEnvironment(scene, gui) {
var lightList = [];
for (var index = 0; index < numberOfLights; index++) {
lightList[index] = getDirectionalLight(1);
var sphere = getSphere(sphereRadius, sphereWidthSegments, sphereHeightSegments, sphereColor);
lightList[index].add(sphere);
(index % 2) === 0 ? lightList[index].position.set(-25, 15, 15) : lightList[index].position.set(25, 15, -15);
gui.add(lightList[index], 'intensity', 0, 10);
gui.add(lightList[index].position, 'x', -50, 50);
gui.add(lightList[index].position, 'y', -50, 50);
gui.add(lightList[index].position, 'z', -50, 50);
scene.add(lightList[index]);
}
return lightList;
}
function getMaterialComposition(type, color) {
var selectedMaterial;
var materialOptions = {
color: color === undefined ? 0xffffff : color,
};
switch (type) {
// Lower on the list equals more GPU demanding!
// Keep in mind if ever doing mobiles.
case 'basic':
selectedMaterial = new THREE.MeshBasicMaterial(materialOptions);
break;
case 'lambert':
// computes lighting only at vertices
selectedMaterial = new THREE.MeshLambertMaterial(materialOptions);
break;
case 'phong':
// computes lighting at every pixel
// texture focus on shininess
selectedMaterial = new THREE.MeshPhongMaterial(materialOptions);
break;
case 'standard':
// textures focus on metalness & roughness
selectedMaterial = new THREE.MeshStandardMaterial(materialOptions);
break;
case 'physical':
// textures focus on clearcoat & clearCoatRoughness
selectedMaterial = new THREE.MeshPhysicalMaterial(materialOptions);
break;
default:
selectedMaterial = new THREE.MeshBasicMaterial(materialOptions);
break;
}
return selectedMaterial;
}
function loadModel() {
// Textures shade can change based on the color level
var materialComposition = getMaterialComposition('physical', 0xcccccc);
object.traverse(function (child) {
child.material = materialComposition;
materialComposition.castShadow = true;
materialComposition.side = THREE.FrontSide; // Render outer layer only
materialComposition.wrapS = THREE.RepeatWrapping;
materialComposition.wrapT = THREE.RepeatWrapping;
materialComposition.magFilter = THREE.NearestFilter
materialComposition.bumpScale = 0;
materialComposition.metalness = 0.1; // ONLY on STANDARD! 0 to 1.0 == PURE METAL item
materialComposition.roughness = 0.4; // ONLY on STANDARD! Smooth Mirror reflection == 0 to 1
materialComposition.shininess = 100; // Only on Phong! This is the opposite of shininess.
materialComposition.wireframe = showWireframe;
materialComposition.map = new THREE.TextureLoader().load('assets/obj/cerberus/Cerberus_A.jpg');
// .depthTest
// .depthWrite when when drawing a 2D overlays
});
object.scale.x = 5;
object.scale.y = 5;
object.scale.z = 5;
return object;
}
function getOBJRender(loader, scene) {
loader.load(
objLocationStr,
function (obj) {
object = obj;
scene.add(object);
},
onProgress,
onError
);
}
function onProgress(xhr) {
console.log('Model downloaded: ' + Math.round((xhr.loaded / xhr.total * 100), 2) + '% loaded');
};
function onError(error) {
console.log('ERROR: Rendering Model');
};
var scene = init();