Allow ShaderChunk overrides in materials (feature request/idea)

I know that the challenge of customizable materials has been discussed quite a bit, but here is a feature I would love to see to that end. I’ve included proposed (incomplete) code changes, and I could try doing the PR, but I am not sure if the idea is well conceived.

Problem:

I often run into situations where I want to use an existing material, e.g. THREE.MeshPhongMaterial and modify the shaders. Rebuilding such materials from scratch as a THREE.ShaderMaterial is cumbersome, but my current solution.

This tool I found allows you to insert code into existing shaders, which is nice, though a bit hacky.
https://www.npmjs.com/package/three-material-modifier
But in some cases I would like to modify a line or more.

Proposed Solution:

Materials allow a shaderChunks argument to be passed, which is an object containing glsl strings that override ShaderChunks used by ShaderLib. For example:

var shaderChunks = {
//monochrome just from R channel of texture
map_fragment:  "diffuseColor *= vec4(vec3(texture2D( map, vUv ).x),1.0);"
};
var material = new THREE.MeshBasicMaterial({shaderChunks:shaderChunks});

Implementation

I started making some changes to see how hard it would be:
nhalloran@7795a9a
nhalloran@44d35a5

But a tricky part would be the handling the #includes in the ShaderLib directory
https://github.com/mrdoob/three.js/tree/dev/src/renderers/shaders/ShaderLib

I’m not exactly sure how those are built….

Would love feedback before going any further towards a PR.

Author: Fantashit

1 thought on “Allow ShaderChunk overrides in materials (feature request/idea)

Comments are closed.