-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelrendering.js
319 lines (250 loc) · 9.74 KB
/
modelrendering.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//Import the THREE.js library
import * as THREE from "https://cdn.skypack.dev/three@0.129.0/build/three.module.js";
// To allow for the camera to move around the scene
import { OrbitControls } from "https://cdn.skypack.dev/three@0.129.0/examples/jsm/controls/OrbitControls.js";
// To allow for importing the .gltf file
import { GLTFLoader } from "https://cdn.skypack.dev/three@0.129.0/examples/jsm/loaders/GLTFLoader.js";
import { FBXLoader } from "https://cdn.skypack.dev/three@0.129.0/examples/jsm/loaders/FBXLoader.js";
//Create a Three.JS Scene
const scene = new THREE.Scene();
//create a new camera with positions and angles
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
//Keep track of the mouse position, so we can make the eye move
let mouseX = window.innerWidth / 2;
let mouseY = window.innerHeight / 2;
//Keep the 3D object on a global variable so we can access it later
let object;
//OrbitControls allow the camera to move around the scene
let controls;
//Set which object to render
let objToRender = 'dino';
let folder = 0;
let wireframe=false;
//Instantiate a loader for the .gltf file
const fbxLoader = new FBXLoader();
const repoOwner = 'AnotherOrganism657';
const repoName = 'ModelRepo';
const folderPath = 'modmodels/ShenheDurga';
//Add lights to the scene, so we can actually see the 3D model
const topLight = new THREE.DirectionalLight(0xffffff, 1); // (color, intensity)
topLight.position.set(250, 500, 1000) //top-left-ish
topLight.castShadow = true;
scene.add(topLight);
const ambientLight = new THREE.AmbientLight(0x333333, objToRender === "dino" ? 5 : 1);
scene.add(ambientLight);
fetchFolderContents(`modmodels/ExquisiteNingguang`, folder);
export function changeModel(change){
folder+=change;
while(scene.children.length > 0){
scene.remove(scene.children[0]);
}
const last = document.querySelector(".minus")
const next = document.querySelector(".plus")
if(folder==0){
last.style.display="none";
} else{
last.style.display="";
}
if(folder==3){
next.style.display="none";
} else{
next.style.display="";
}
//Add lights to the scene, so we can actually see the 3D model
const topLight = new THREE.DirectionalLight(0xffffff, 1); // (color, intensity)
topLight.position.set(250, 500, 1000) //top-left-ish
topLight.castShadow = true;
scene.add(topLight);
const ambientLight = new THREE.AmbientLight(0x333333, objToRender === "dino" ? 5 : 1);
scene.add(ambientLight);
fetchFolderContents(`modmodels`, folder, false);
}
export function changeWireframe(){
wireframe=!wireframe;
while(scene.children.length > 0){
scene.remove(scene.children[0]);
}
//Add lights to the scene, so we can actually see the 3D model
const topLight = new THREE.DirectionalLight(0xffffff, 1); // (color, intensity)
topLight.position.set(250, 500, 1000) //top-left-ish
topLight.castShadow = true;
scene.add(topLight);
const ambientLight = new THREE.AmbientLight(0x333333, objToRender === "dino" ? 5 : 1);
scene.add(ambientLight);
fetchFolderContents(`modmodels`, folder, false);
}
function fetchFolderContents(folderPath, searchFolder, run){
let folderNumber = 0;
fetch(`https://api.github.com/repos/${repoOwner}/${repoName}/contents/${folderPath}`)
.then(response => response.json())
.then(data => {
// Find the first FBX file in the folder
//const fbxFile = data.find(item => item.type === 'file' && item.name.endsWith('.fbx'));
data.forEach(item => {
if (item.type === 'file' && item.name.endsWith('.fbx')) {
// Use the three.js FBXLoader to load the FBX file
const loader = new FBXLoader();
loader.load(item.download_url, object => {
object.traverse(function (child) {
object.scale.set(0.01, 0.01, 0.01);
if (child.isMesh) {
if(!wireframe){
// basic monochromatic energy preservation
const textureLoader = new THREE.TextureLoader();
const diffuseColor = new THREE.Color(0.6, 0.6, 0.6);
//let colorTexture = textureLoader.load('models/jean/Avatar_Lady_Sword_Qin_Tex_Body_Diffuse.png') // Replace with the actual path to your texture
let newMat;
if(Array.isArray(child.material)){
let i =0;
child.material.forEach(function (material) {
newMat = new THREE.MeshToonMaterial( {
color: diffuseColor,
map: material.map, // Set the texture for color
transparent: true,
side: THREE.DoubleSide,
gradientMap: null
} );
material = newMat;
child.material[i] = newMat;
i++;
});
}else{
newMat = new THREE.MeshToonMaterial( {
color: diffuseColor,
map: child.material.map, // Set the texture for color
gradientMap: null
} );
child.material = newMat;
}
switch (child.name) {
case 'EffectMesh':
// Handle EffectMesh
child.visible = false;
break;
case 'EyeStar':
// Handle EyeStar
child.visible = false;
break;
default:
// Default case if the name doesn't match any of the cases
break;
}
} else{
var geo = new THREE.EdgesGeometry( child.geometry ); // or WireframeGeometry( geometry )
var mat = new THREE.LineBasicMaterial( { color: 0xdddddd, linewidth: 1 } );
var wireframe2 = new THREE.LineSegments( geo, mat );
wireframe2.position.y=-1;
scene.add( wireframe2 )
}
}
});
if(!wireframe){
object.position.y=-1;
scene.add(object);
}
});
} else if (item.type === 'dir') {
// For subfolders, recursively fetch their contents
if(folderNumber==searchFolder && run==false){
// console.log(item.url);
run=true;
fetchFolderContents("modmodels/"+item.name, searchFolder, run);
} else{
folderNumber++;
}
}
});
})
.catch(error => console.error('Error fetching folder contents:', error));
//Instantiate a new renderer and set its size
const renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true }); //Alpha: true allows for the transparent background
renderer.setSize(window.innerWidth, window.innerHeight);
//Add the renderer to the DOM
document.getElementById("container3D").appendChild(renderer.domElement);
//Set how far the camera will be from the 3D model
camera.position.z=1.25;
camera.position.y=0.5;
//console.log(camera.position);
// console.log(camera.rotation);
//This adds controls to the camera, so we can rotate / zoom it with the mouse
if (objToRender === "dino") {
controls = new OrbitControls(camera, renderer.domElement);
}
//Render the scene
function animate() {
requestAnimationFrame(animate);
//Here we could add some code to update the scene, adding some automatic movement
renderer.render(scene, camera);
}
//Add a listener to the window, so we can resize the window and the camera
window.addEventListener("resize", function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
//add mouse position listener, so we can make the eye move
document.onmousemove = (e) => {
mouseX = e.clientX;
mouseY = e.clientY;
}
//Start the 3D rendering
animate();
}
/*
fbxLoader.load(
'models/jean/Cs_Avatar_Lady_Sword_Qin.fbx',
(object) => {
object.traverse(function (child) {
if (child.isMesh) {
// basic monochromatic energy preservation
const textureLoader = new THREE.TextureLoader();
const diffuseColor = new THREE.Color(0.5, 0.5, 0.5);
let colorTexture = textureLoader.load('models/jean/Avatar_Lady_Sword_Qin_Tex_Body_Diffuse.png') // Replace with the actual path to your texture
let newMat;
if(Array.isArray(child.material)){
let i =0;
child.material.forEach(function (material) {
console.log(material.map.name);
newMat = new THREE.MeshToonMaterial( {
color: diffuseColor,
map: material.map, // Set the texture for color
gradientMap: null
} );
material = newMat;
child.material[i] = newMat;
i++;
});
}else{
console.log(child.material.name);
newMat = new THREE.MeshToonMaterial( {
color: diffuseColor,
map: child.material.map, // Set the texture for color
gradientMap: null
} );
child.material = newMat;
}
switch (child.name) {
case 'EffectMesh':
// Handle EffectMesh
child.visible = false;
break;
case 'EyeStar':
// Handle EyeStar
child.visible = false;
break;
default:
// Default case if the name doesn't match any of the cases
break;
}
}
});
scene.add(object)
},
(xhr) => {
console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
},
(error) => {
console.log(error)
}
)
*/