map33.js icon indicating copy to clipboard operation
map33.js copied to clipboard

Use onBeforeCompile callback instead of its own ThreeJS Material

Open mattrei opened this issue 4 years ago • 2 comments

Currently you have registered your own Material with its Vertex and Fragment shader code which is mostly copied from ThreeJs' source code. Because you make only small changes to the fragment shader, please consider using the onBeforeCompile callback, so that the the Mesh works on any ThreeJS shipped Material and is future proof...

See this article for an in-depth usage using onBeforeCompile.

If you want I can also try to provide a PR ... (if I find time next week)

mattrei avatar Jul 11 '20 18:07 mattrei

Ah yes, I wasn't really satisfied with my option to copy-paste the entire ShaderMaterial glsl code anyway but wasn't aware of any good alternative...

The interesting bit seems to be this part:

var myMaterial = new THREE.MeshStandardMaterial()myMaterial.onBeforeCompile = shader => {
  shader.fragmentShader = //this is the fragment program string in the template format
  shader.fragmentShader.replace( //we have to transform the string
    '#include <alphamap_fragment>', //we will swap out this chunk
    require('my_alphamap_fragment.glsl') //with our own
)}

Thank you for your contributions! If you find time for a PR, just update the issue to let me know, so we don't both work on it ;) that would be awesome!

blaze33 avatar Jul 11 '20 19:07 blaze33

great, so i think you can manage to integrate it. shouldn't be that hard for you considering how your coding skills. i am a bit busy at the moment. thx

mattrei avatar Jul 12 '20 17:07 mattrei