-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
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
NodeMaterial: ShaderNode #22603
NodeMaterial: ShaderNode #22603
Conversation
Interesting! Could you add how would the WGSL output look like too? |
@mrdoob Yeah! The idea is use this syntax to generate both WGSL and GLSL. I need still finish some feature to generate WGSL but not needed change this code only the NodeBuilder and WebGPUNodeBuilder. I think that this syntax is more simple and hybrid in sense that it can generate different output and focus more in threejs features to make shaders if compared with WGSL. |
@mrdoob I edit the post and add WGSL for comparison purpose. |
Excellent! Reading #17105 also helped a lot. I've already forgotten about that conversation... My only feedback is that instead of Edit: Or |
I liked of |
Minor thing: Instead of |
@mrdoob About the WIP: It is NODE MATERIALconst material = new Nodes.MeshBasicNodeMaterial();
material.colorNode = new Nodes.PositionNode(); VERTEX//
// Three.JS r133 * NodeMaterial System
//
// uniforms
[[block]]
struct NodeUniformsStruct {
nodeUniform0 : mat4x4<f32>;
nodeUniform1 : mat4x4<f32>;
};
[[ binding( 0 ), group( 0 ) ]]
var<uniform> NodeUniforms : NodeUniformsStruct;
// varys
[[block]]
struct NodeVarysStruct {
[[ builtin( position ) ]] Vertex: vec4<f32>;
[[ location( 0 ) ]] nodeVary0 : vec3<f32>;
};
// codes
[[stage(vertex)]]
fn main( [[ location( 0 ) ]] position: vec3<f32> ) -> NodeVarysStruct {
// system
var NodeVarys: NodeVarysStruct;
// vars
var MaterialDiffuseColor : vec4<f32>; var nodeVar1 : vec4<f32>; var nodeVar2 : mat4x4<f32>;
// <flow>
// code
NodeVarys.nodeVary0 = position;
// SLOT: MVP
nodeVar2 = ( NodeUniforms.nodeUniform0 * NodeUniforms.nodeUniform1 ); nodeVar1 = ( nodeVar2 * vec4<f32>( position, 1.0 ) );
NodeVarys.Vertex = nodeVar1;
// </flow>
return NodeVarys;
} FRAGMENT//
// Three.JS r133 * NodeMaterial System
//
// uniforms
// codes
[[stage(fragment)]]
fn main( [[ location( 0 ) ]] nodeVary0 : vec3<f32> ) -> [[ location( 0 ) ]] vec4<f32> {
// vars
var MaterialDiffuseColor : vec4<f32>;
// <flow>
// code
// SLOT: COLOR
return vec4<f32>( nodeVary0, 1.0 );
// </flow>
} RESULT |
Using the example of this NODE MATERIALconst material = new Nodes.MeshBasicNodeMaterial();
material.colorNode = new Nodes.CheckerNode(); VERTEX//
// Three.JS r133 * NodeMaterial System
//
// uniforms
[[block]]
struct NodeUniformsStruct {
nodeUniform0 : mat4x4<f32>;
nodeUniform1 : mat4x4<f32>;
};
[[ binding( 0 ), group( 0 ) ]]
var<uniform> NodeUniforms : NodeUniformsStruct;
// varys
[[block]]
struct NodeVarysStruct {
[[ builtin( position ) ]] Vertex: vec4<f32>;
[[ location( 0 ) ]] nodeVary0 : vec2<f32>;
};
// codes
[[stage(vertex)]]
fn main( [[ location( 0 ) ]] position: vec3<f32>, [[ location( 1 ) ]] uv: vec2<f32> ) -> NodeVarysStruct {
// system
var NodeVarys: NodeVarysStruct;
// vars
var MaterialDiffuseColor : vec4<f32>; var nodeVar1 : vec4<f32>; var nodeVar2 : mat4x4<f32>;
// <flow>
// code
NodeVarys.nodeVary0 = uv;
// SLOT: MVP
nodeVar2 = ( NodeUniforms.nodeUniform0 * NodeUniforms.nodeUniform1 ); nodeVar1 = ( nodeVar2 * vec4<f32>( position, 1.0 ) );
NodeVarys.Vertex = nodeVar1;
// </flow>
return NodeVarys;
} FRAGMENT//
// Three.JS r133 * NodeMaterial System
//
// uniforms
// codes
// --
fn mod( a : f32, b : f32 ) -> f32 {
return f32( i32( a ) % i32( b ) );
}
[[stage(fragment)]]
fn main( [[ location( 0 ) ]] nodeVary0 : vec2<f32> ) -> [[ location( 0 ) ]] vec4<f32> {
// vars
var MaterialDiffuseColor : vec4<f32>; var nodeVar1 : f32; var nodeVar2 : vec2<f32>;
// <flow>
// code
// SLOT: COLOR
nodeVar2 = ( nodeVary0 * vec2<f32>( 2.0 ) ); nodeVar1 = ( floor( nodeVar2.x ) + floor( nodeVar2.y ) );
return vec4<f32>( vec3<f32>( sign( mod( nodeVar1, 2.0 ) ) ), 1.0 );
// </flow>
} RESULT |
@sunag This is looking great! 👏👏👏 |
@Oletus Yeah! Uniforms and any other node can be share, for example. If you create two |
WIP: A single Probably this weekend we can say hello to ShaderNodethree.js/examples/jsm/renderers/nodes/display/NormalMapNode.js Lines 16 to 40 in 7a51216
WGSLGLSL |
Introduction
If you need to create big shader code using
NodeMaterial
or only an extendedNode
that can translate to different outputs, this could be useful.I could say it's a
Three.JS Shading Language
, here you can create shaders using the newNodeMaterial
system with a language similar to the popularGLSL
, and it can be converted to bothGLSL
andWGSL
.Syntax
TJSL / Checker example
WGSL ( for comparison purposes )
GLSL ( for comparison purposes )
Inputs
Operators
Swizzle
Recommended hierarchy
Usage
Related issues
#21322 (comment), #17105, #17321
This contribution is funded by Google via Igalia.