Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSL - CustomMaterial Features? #30654

Open
Bug-Reaper opened this issue Mar 5, 2025 · 5 comments
Open

TSL - CustomMaterial Features? #30654

Bug-Reaper opened this issue Mar 5, 2025 · 5 comments
Labels

Comments

@Bug-Reaper
Copy link
Contributor

Bug-Reaper commented Mar 5, 2025

Description

Yo the new TSL stuff is pretty slick. I've spent the past two days trying to feel it out, great bindings once you get the hang of it.

I've read through the God TSL document like 3 times, combed code examples, and I can't figure out a couple of thing?

  1. Easily mutate the final color of a Mesh<TYPE>NodeMaterial (chain on additional shader logic to the final product?)
  2. Similar vein, access material property values inside custom material nodes. Feels inconvenient that the colorNode which replaces map*color does not have access to the material's map unless re-added as a new uniform?

Perhaps I'm missing something obvious. It appears uv() and vertexColor() can be grabbed a-la-carte but is there no way to access other material properties inside custom TSL Fn()s?

Much appreciate any insight!

Solution

My dream is TSL "First Party" feeling materials with arbitrary top-level properties to drive uniforms. Modularity in these materials also pretty cool if possible.

Alternatives

Open minded, can contribute some help if it's a good fit.

Additional context

N/A

@Bug-Reaper
Copy link
Contributor Author

Bug-Reaper commented Mar 5, 2025

Classic solve it immediately after post, found a snippet somewhere but you can import { output } from 'three/tsl' and chain the outputNode` to itself to mutate a material to itself. Vis-a-Vis:

import { output } from 'three/tsl'

const mat = new THREE.MeshStandardNodeMaterial({});
const invert = Fn([frag]=>{
// Any TSL code ontop of final material color coming in as [frag]
return vec4(sub(1,frag.rgb),1)
}) 
mat.outputNode = invert(output);

This solves my first question about mutating shadernode outputs. May add some better examples to the doc along with any other things from my learning TSL adventures of the past few days.

@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 5, 2025

Similar vein, access material property values inside custom material nodes. Feels inconvenient that the colorNode which replaces map*color does not have access to the material's map unless re-added as a new uniform?

There are predefined TSL objects for this use case. Try it with materialColor:

/**
* TSL object that represents the diffuse color of the current material.
* The value is composed via `color` * `map`.
*
* @tsl
* @type {Node<vec3>}
*/
export const materialColor = /*@__PURE__*/ nodeImmutable( MaterialNode, MaterialNode.COLOR );

You can then do this:

material.colorNode = materialColor;

And of course modify the color with more TSL according to your use case.

@Bug-Reaper
Copy link
Contributor Author

Thanks @Mugen87 I had no idea we have all these node-accessors.

I guess my question is:

someMaterial.colorNode = Fn(()=>{
  // ?? => How to access material-props here??
})

I can't figure out how to reference a NodeMaterials's .map property from inside a custom node for example?

Mostly curious if all a node-material's properties can be accessed inside these custom-node functions?

@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 7, 2025

If you want individual access on map, you need to wrap it into a texture() TSL call.

someMaterial.colorNode = texture( map );

@Bug-Reaper
Copy link
Contributor Author

Bug-Reaper commented Mar 8, 2025

It's confusing that MeshStandardNodeMaterial cannot access it's own material properties from inside it's own nodes.

The material is able to reference these properties/uniforms internally right @Mugen87 could they be exposed easily? I think it's a good idea to expose access to all unique material properties including all .maps but also things like metalness/irridesence/etc. Particularly access in the context of custom-nodes attached to that material.


Let me know if that makes sense to you? Material-prop access like outlined above was possible and widely used in/via onbeforecompile creations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants