Improve the drop-in-replacement engine aspect #84
Description
Context
This engine is intended to be used as a drop-in replacement to Three.js’s WebGLRenderer
.
Problem
It tends not to work as a drop-in replacement out-of-the-box, because the renderer crashes when:
- Meshes contain a
THREE.Geometry
instead of aTHREE.BufferGeometry
. - It uses materials for which the texture images are not loaded yet.
This might cause people who try to replace the WebGLRenderer
with RayTracingRenderer
to drop out early, because 'it does not work as promised' out-of-the box.
This would be a shame, because if you get it up and running, it works great.
Solution
In my engine I have provided a few simple workarounds for these issues by pre-processing the THREE.Object3D
before I attach it to the scene; this is an excerpt from the code:
public placeholderMaterial = new THREE.MeshStandardMaterial({color: 0xccffcc, emissive: 0xccffcc, emissiveIntensity: 0.5});
materialHasUnloadedMaps(m:THREE.MeshStandardMaterial) {
return (m.map && !m.map.image) ||
(m.normalMap && !m.normalMap.image) ||
(m.roughnessMap && !m.roughnessMap.image) ||
(m.metalnessMap && ! m.metalnessMap.image);
}
adaptModelForRenderer(model: THREE.Object3D) {
model.traverse(o => {
const mesh = o as THREE.Mesh;
const mm = mesh.material as THREE.MeshStandardMaterial;
const mg = mesh.geometry as THREE.BufferGeometry;
const mgg = mesh.geometry as THREE.Geometry;
// Convert THREE.Geometry to THREE.BufferGeometry
if (mgg && mgg.vertices) {
mesh.geometry = new THREE.BufferGeometry().fromGeometry(mgg)
}
// Convert non-MeshStandardMaterial and Materials that are still loading
// images with the placeholderMaterial
if (mm) {
if (mm.type !== "MeshStandardMaterial" || this.materialHasUnloadedMaps(mm)) {
mesh.material = this.placeholderMaterial;
}
}
// rtRenderer does not support Texture.repeat and -.offset, so update uvs instead
// ... and lots of other adjustments, removed for brevity
});
}
Proposal
As I can imagine that I am not the only one experiencing these issues, and intercepting these cases is probably not rocket science, I wonder if you would be open to intercept any THREE.Geometry
instances and convert these to THREE.BufferGeometry
.
As for the invalid materials, I would suggest to replace invalid instances with a placeholderMaterial
, which could be specified in the params
upon startup.