Skip to content

Allow ShaderChunk overrides in materials (feature request/idea) #13364

@nhalloran

Description

@nhalloran

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions