Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit 4e0c1d0

Browse files
author
Lucas Crane
authored
Three.js compatability (#85)
* add more compatability to three.js * else-if for scene decomposition
1 parent 0324fef commit 4e0c1d0

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

scenes/renderer-test/main.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ const tick = (time) => {
4848
requestAnimationFrame(tick);
4949
};
5050

51-
const geo = new THREE.SphereBufferGeometry(4, 24, 24);
51+
const geo = new THREE.SphereBufferGeometry(1, 24, 24);
5252

5353
function makeMesh() {
5454
const mat = new THREE.RayTracingMaterial();
5555
const mesh = new THREE.Mesh(geo, mat);
56+
57+
// test setting scale and position on mesh
5658
mesh.position.set(0, 4, 0);
59+
mesh.scale.set(4, 4, 4);
5760
return mesh;
5861
}
5962

@@ -205,9 +208,45 @@ function init() {
205208
model.add(mesh);
206209
}
207210

211+
// test box with .visible set to false
212+
// should not be visible in the scene
213+
{
214+
const geo = new THREE.BoxBufferGeometry(5, 5, 5);
215+
const mat = new THREE.MeshStandardMaterial();
216+
const mesh = new THREE.Mesh(geo, mat);
217+
mesh.position.set(0, 10, 0);
218+
mesh.visible = false;
219+
model.add(mesh);
220+
}
221+
222+
let unreadyMat;
223+
{
224+
// Create a test (non-buffer) Geometry
225+
const geo = new THREE.BoxGeometry(6, 6, 6);
226+
const mat = new THREE.MeshStandardMaterial();
227+
mat.roughness = 0.2;
228+
mat.metalness = 0.0;
229+
mat.color.set(0x993311);
230+
unreadyMat = mat;
231+
const mesh = new THREE.Mesh(geo, mat);
232+
mesh.position.set(0, 3, 30);
233+
model.add(mesh);
234+
}
235+
208236
scene.add(model);
209237

210-
THREE.DefaultLoadingManager.onLoad = tick;
238+
THREE.DefaultLoadingManager.onLoad = () => {
239+
240+
// give material an unloaded async texture. the renderer should handle this
241+
unreadyMat.map = new THREE.TextureLoader().load('diffuse.png');
242+
unreadyMat.normalMap = new THREE.TextureLoader().load('normal.png');
243+
const metalrough = new THREE.TextureLoader().load('metalrough.png');
244+
unreadyMat.roughnessMap = metalrough;
245+
unreadyMat.metalnessMap = metalrough;
246+
247+
THREE.DefaultLoadingManager.onLoad = undefined;
248+
tick();
249+
};
211250
}
212251

213252
init();

src/renderer/decomposeScene.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ export function decomposeScene(scene) {
88

99
scene.traverse(child => {
1010
if (child.isMesh) {
11-
if (!child.geometry || !child.geometry.getAttribute('position')) {
12-
console.warn(child, 'must have a geometry property with a position attribute');
11+
if (!child.geometry) {
12+
console.warn(child, 'must have a geometry property');
1313
}
1414
else if (!(child.material.isMeshStandardMaterial)) {
1515
console.warn(child, 'must use MeshStandardMaterial in order to be rendered.');
1616
} else {
1717
meshes.push(child);
1818
}
1919
}
20-
if (child.isDirectionalLight) {
20+
else if (child.isDirectionalLight) {
2121
directionalLights.push(child);
2222
}
23-
if (child.isAmbientLight) {
23+
else if (child.isAmbientLight) {
2424
ambientLights.push(child);
2525
}
26-
if (child.isEnvironmentLight) {
26+
else if (child.isEnvironmentLight) {
2727
if (environmentLights.length > 1) {
2828
console.warn(environmentLights, 'only one environment light can be used per scene');
2929
}

src/renderer/mergeMeshesToGeometry.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ export function mergeMeshesToGeometry(meshes) {
99
const materialIndexMap = new Map();
1010

1111
for (const mesh of meshes) {
12-
const geometry = cloneBufferGeometry(mesh.geometry, ['position', 'normal', 'uv']);
12+
if (!mesh.visible) {
13+
continue;
14+
}
15+
16+
const geometry = mesh.geometry.isBufferGeometry ?
17+
cloneBufferGeometry(mesh.geometry, ['position', 'normal', 'uv']) : // BufferGeometry object
18+
new BufferGeometry().fromGeometry(mesh.geometry); // Geometry object
1319

1420
const index = geometry.getIndex();
1521
if (!index) {

src/renderer/texturesFromMaterials.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ function texturesFromMaterials(materials, textureName, textures) {
3131
const indices = [];
3232

3333
for (const material of materials) {
34-
if (!material[textureName]) {
34+
const isTextureLoaded = material[textureName] && material[textureName].image;
35+
36+
if (!isTextureLoaded) {
3537
indices.push(-1);
3638
} else {
3739
let index = textures.length;

0 commit comments

Comments
 (0)