-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmale02MTLandOBJLoad.js
More file actions
142 lines (109 loc) · 3.77 KB
/
Copy pathmale02MTLandOBJLoad.js
File metadata and controls
142 lines (109 loc) · 3.77 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
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 = 1;
const boxHeight = 1;
const boxLength = 1;
const boxColor = 0x00fff0;
const mtlFileStr = 'assets/obj/male02/male02.mtl';
const objFileStr = 'assets/obj/male02/male02.obj';
const loader = new THREE.TextureLoader();
const mtlLoader = new THREE.MTLLoader();
const objLoader = new THREE.OBJLoader();
const gui = new dat.GUI();
const scene = new THREE.Scene();
scene.background = new THREE.Color(sceneColor);
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(cameraPositionX, cameraPositionY, cameraPositionZ);
camera.lookAt(cameraLookAtX, cameraLookAtY, cameraLookAtZ);
const 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;
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target = new THREE.Vector3(cameraLookAtX, cameraLookAtY, cameraLookAtZ);
controls.update();
var box = getBox(boxWidth, boxHeight, boxLength, boxColor);
scene.add(box);
createLightEnvironment(scene, gui);
getMTLandOBJRender(scene)
canvas.appendChild(renderer.domElement);
const animate = function () {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
};
function printShotgun(obj) {
console.log(obj);
};
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() {
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 getMTLandOBJRender() {
mtlLoader.load(mtlFileStr, (mtl) => {
mtl.preload();
objLoader.setMaterials(mtl);
objLoader.load(objFileStr, (root) => {
scene.add(root);
},
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');
};
animate();