I am currently exploring the integration of an id material override function using onBeforeRender in my THREEjs application. While this is a common feature in rendering applications, it seems like I will need to create my own solution based on my research.
Here is my approach:
When I create objects:
...
m.onBeforeRender = function (){
if(Renderer._renderID){
this.material = new THREE.MeshBasicMaterial(this.userData.idColor.getHex());
this.material.needsUpdate = true;
// Attempting to use a shader material
//this.material = Renderer._idMat;
//this.material.uniforms.idColor.value = this.userData.idColor;
//this.material.uniforms.needsUpdate = true;
}
}
m.onAfterRender = function (){
if(Renderer._renderID){
this.material = this.userData.material;
}
}
...
In the render loop:
...
var idTarget = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight);
Renderer._renderID = true;
Renderer._renderer.render(Renderer._scene, Renderer._camera, idTarget);
Renderer._renderID = false;
...
Currently, both the id buffer and diffuse buffer are being rendered with the diffuse material.
If anyone has any advice or suggestions, I would greatly appreciate it. Feel free to ask for more information, and I will update this post accordingly.