Skip to content

Commit 997c8b5

Browse files
authored
TSL: varyingProperty (#27198)
* TSL: varyingProperty * update example
1 parent b7645dd commit 997c8b5

File tree

6 files changed

+40
-31
lines changed

6 files changed

+40
-31
lines changed

examples/jsm/nodes/Nodes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export { default as NodeUniform } from './core/NodeUniform.js';
2727
export { default as NodeVar } from './core/NodeVar.js';
2828
export { default as NodeVarying } from './core/NodeVarying.js';
2929
export { default as ParameterNode, parameter } from './core/ParameterNode.js';
30-
export { default as PropertyNode, property, output, diffuseColor, roughness, metalness, clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, specularColor, shininess, dashSize, gapSize, pointWidth } from './core/PropertyNode.js';
30+
export { default as PropertyNode, property, varyingProperty, output, diffuseColor, roughness, metalness, clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, specularColor, shininess, dashSize, gapSize, pointWidth } from './core/PropertyNode.js';
3131
export { default as StackNode, stack } from './core/StackNode.js';
3232
export { default as TempNode } from './core/TempNode.js';
3333
export { default as UniformGroupNode, uniformGroup, objectGroup, renderGroup, frameGroup } from './core/UniformGroupNode.js';

examples/jsm/nodes/core/NodeBuilder.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ class NodeBuilder {
740740

741741
}
742742

743-
getVaryingFromNode( node, type ) {
743+
getVaryingFromNode( node, name = null, type = node.getNodeType( this ) ) {
744744

745745
const nodeData = this.getDataFromNode( node, 'any' );
746746

@@ -751,7 +751,9 @@ class NodeBuilder {
751751
const varyings = this.varyings;
752752
const index = varyings.length;
753753

754-
nodeVarying = new NodeVarying( 'nodeVarying' + index, type );
754+
if ( name === null ) name = 'nodeVarying' + index;
755+
756+
nodeVarying = new NodeVarying( name, type );
755757

756758
varyings.push( nodeVarying );
757759

examples/jsm/nodes/core/PropertyNode.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { nodeImmutable, nodeObject } from '../shadernode/ShaderNode.js';
33

44
class PropertyNode extends Node {
55

6-
constructor( nodeType, name = null ) {
6+
constructor( nodeType, name = null, varying = false ) {
77

88
super( nodeType );
99

1010
this.name = name;
11+
this.varying = varying;
1112

1213
this.isPropertyNode = true;
1314

@@ -27,7 +28,20 @@ class PropertyNode extends Node {
2728

2829
generate( builder ) {
2930

30-
return builder.getPropertyName( builder.getVarFromNode( this, this.name ) );
31+
let nodeVar;
32+
33+
if ( this.varying === true ) {
34+
35+
nodeVar = builder.getVaryingFromNode( this, this.name );
36+
nodeVar.needsInterpolation = true;
37+
38+
} else {
39+
40+
nodeVar = builder.getVarFromNode( this, this.name );
41+
42+
}
43+
44+
return builder.getPropertyName( nodeVar );
3145

3246
}
3347

@@ -36,6 +50,7 @@ class PropertyNode extends Node {
3650
export default PropertyNode;
3751

3852
export const property = ( type, name ) => nodeObject( new PropertyNode( type, name ) );
53+
export const varyingProperty = ( type, name ) => nodeObject( new PropertyNode( type, name, true ) );
3954

4055
export const diffuseColor = nodeImmutable( PropertyNode, 'vec4', 'DiffuseColor' );
4156
export const roughness = nodeImmutable( PropertyNode, 'float', 'Roughness' );

examples/jsm/nodes/core/VaryingNode.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,11 @@ class VaryingNode extends Node {
4040
const { name, node } = this;
4141
const type = this.getNodeType( builder );
4242

43-
const nodeVarying = builder.getVaryingFromNode( this, type );
43+
const nodeVarying = builder.getVaryingFromNode( this, name, type );
4444

4545
// this property can be used to check if the varying can be optimized for a var
4646
nodeVarying.needsInterpolation || ( nodeVarying.needsInterpolation = ( builder.shaderStage === 'fragment' ) );
4747

48-
if ( name !== null ) {
49-
50-
nodeVarying.name = name;
51-
52-
}
53-
5448
const propertyName = builder.getPropertyName( nodeVarying, NodeShaderStage.VERTEX );
5549

5650
// force node run in vertex stage

examples/jsm/nodes/materials/Line2NodeMaterial.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
22
import { temp } from '../core/VarNode.js';
33
import { varying } from '../core/VaryingNode.js';
4-
import { property } from '../core/PropertyNode.js';
4+
import { property, varyingProperty } from '../core/PropertyNode.js';
55
import { attribute } from '../core/AttributeNode.js';
66
import { cameraProjectionMatrix } from '../accessors/CameraNode.js';
77
import { materialColor, materialLineScale, materialLineDashSize, materialLineGapSize, materialLineDashOffset, materialLineWidth } from '../accessors/MaterialNode.js';
@@ -60,7 +60,7 @@ class Line2NodeMaterial extends NodeMaterial {
6060

6161
const a = cameraProjectionMatrix.element( 2 ).element( 2 ); // 3nd entry in 3th column
6262
const b = cameraProjectionMatrix.element( 3 ).element( 2 ); // 3nd entry in 4th column
63-
const nearEstimate = b.mul( -0.5 ).div( a );
63+
const nearEstimate = b.mul( - 0.5 ).div( a );
6464

6565
const alpha = nearEstimate.sub( start.z ).div( end.z.sub( start.z ) );
6666

@@ -70,7 +70,7 @@ class Line2NodeMaterial extends NodeMaterial {
7070

7171
this.vertexNode = tslFn( () => {
7272

73-
varying( vec2(), 'vUv' ).assign( uv() ); // @TODO: Analyze other way to do this
73+
varyingProperty( 'vec2', 'vUv' ).assign( uv() );
7474

7575
const instanceStart = attribute( 'instanceStart' );
7676
const instanceEnd = attribute( 'instanceEnd' );
@@ -85,8 +85,8 @@ class Line2NodeMaterial extends NodeMaterial {
8585

8686
if ( useWorldUnits ) {
8787

88-
varying( vec3(), 'worldStart' ).assign( start.xyz );
89-
varying( vec3(), 'worldEnd' ).assign( end.xyz );
88+
varyingProperty( 'vec3', 'worldStart' ).assign( start.xyz );
89+
varyingProperty( 'vec3', 'worldEnd' ).assign( end.xyz );
9090

9191
}
9292

@@ -97,7 +97,7 @@ class Line2NodeMaterial extends NodeMaterial {
9797
// but we need to perform ndc-space calculations in the shader, so we must address this issue directly
9898
// perhaps there is a more elegant solution -- WestLangley
9999

100-
const perspective = cameraProjectionMatrix.element( 2 ).element( 3 ).equal( -1.0 ); // 4th entry in the 3rd column
100+
const perspective = cameraProjectionMatrix.element( 2 ).element( 3 ).equal( - 1.0 ); // 4th entry in the 3rd column
101101

102102
If( perspective, () => {
103103

@@ -173,7 +173,7 @@ class Line2NodeMaterial extends NodeMaterial {
173173

174174
// set the world position
175175

176-
const worldPos = varying( vec4(), 'worldPos' );
176+
const worldPos = varyingProperty( 'vec4', 'worldPos' );
177177

178178
worldPos.assign( positionGeometry.y.lessThan( 0.5 ).cond( start, end ) );
179179
worldPos.assign( worldPos.add( vec4( offset, 0 ) ) );
@@ -257,7 +257,7 @@ class Line2NodeMaterial extends NodeMaterial {
257257

258258
this.colorNode = tslFn( () => {
259259

260-
const vUv = varying( vec2(), 'vUv' );
260+
const vUv = varyingProperty( 'vec2', 'vUv' );
261261

262262
if ( useDash ) {
263263

@@ -288,11 +288,11 @@ class Line2NodeMaterial extends NodeMaterial {
288288

289289
if ( useWorldUnits ) {
290290

291-
const worldStart = varying( vec3(), 'worldStart' );
292-
const worldEnd = varying( vec3(), 'worldEnd' );
291+
const worldStart = varyingProperty( 'vec3', 'worldStart' );
292+
const worldEnd = varyingProperty( 'vec3', 'worldEnd' );
293293

294294
// Find the closest points on the view ray and the line segment
295-
const rayEnd = varying( vec4(), 'worldPos' ).xyz.normalize().mul( 1e5 );
295+
const rayEnd = varyingProperty( 'vec4', 'worldPos' ).xyz.normalize().mul( 1e5 );
296296
const lineDir = worldEnd.sub( worldStart );
297297
const params = closestLineToLine( { p1: worldStart, p2: worldEnd, p3: vec3( 0.0, 0.0, 0.0 ), p4: rayEnd } );
298298

examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class WGSLNodeBuilder extends NodeBuilder {
259259

260260
if ( shaderStage === 'vertex' ) {
261261

262-
return `NodeVaryings.${ node.name }`;
262+
return `varyings.${ node.name }`;
263263

264264
}
265265

@@ -654,7 +654,7 @@ ${ flowData.code }
654654

655655
const code = snippets.join( ',\n\t' );
656656

657-
return shaderStage === 'vertex' ? this._getWGSLStruct( 'NodeVaryingsStruct', '\t' + code ) : code;
657+
return shaderStage === 'vertex' ? this._getWGSLStruct( 'VaryingsStruct', '\t' + code ) : code;
658658

659659
}
660660

@@ -808,7 +808,7 @@ ${ flowData.code }
808808

809809
if ( shaderStage === 'vertex' ) {
810810

811-
flow += 'NodeVaryings.Vertex = ';
811+
flow += 'varyings.Vertex = ';
812812

813813
} else if ( shaderStage === 'fragment' ) {
814814

@@ -888,23 +888,21 @@ ${shaderData.uniforms}
888888
889889
// varyings
890890
${shaderData.varyings}
891+
var<private> varyings : VaryingsStruct;
891892
892893
// codes
893894
${shaderData.codes}
894895
895896
@vertex
896-
fn main( ${shaderData.attributes} ) -> NodeVaryingsStruct {
897-
898-
// system
899-
var NodeVaryings: NodeVaryingsStruct;
897+
fn main( ${shaderData.attributes} ) -> VaryingsStruct {
900898
901899
// vars
902900
${shaderData.vars}
903901
904902
// flow
905903
${shaderData.flow}
906904
907-
return NodeVaryings;
905+
return varyings;
908906
909907
}
910908
`;

0 commit comments

Comments
 (0)