Skip to content

Commit 9a0da1a

Browse files
authored
TLS: Introduce builtinShadowContext (#32384)
1 parent 8ea91af commit 9a0da1a

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

examples/webgpu_postprocessing_sss.html

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<script type="module">
3737

3838
import * as THREE from 'three/webgpu';
39-
import { pass, vec3, vec4, mrt, screenUV, velocity, context } from 'three/tsl';
39+
import { pass, vec3, vec4, mrt, screenUV, velocity, builtinShadowContext } from 'three/tsl';
4040
import { sss } from 'three/addons/tsl/display/SSSNode.js';
4141
import { traa } from 'three/addons/tsl/display/TRAANode.js';
4242

@@ -158,18 +158,7 @@
158158
// scene context
159159

160160
const sssSample = sssPass.getTextureNode().sample( screenUV ).r;
161-
162-
const sssContext = context( {
163-
164-
getShadow: ( light ) => {
165-
166-
if ( light === dirLight ) return sssSample;
167-
168-
return null;
169-
170-
}
171-
172-
} );
161+
const sssContext = builtinShadowContext( sssSample, dirLight );
173162

174163
scenePass.contextNode = sssContext;
175164

src/Three.TSL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export const bufferAttribute = TSL.bufferAttribute;
100100
export const bumpMap = TSL.bumpMap;
101101
export const burn = TSL.burn;
102102
export const builtin = TSL.builtin;
103+
export const builtinShadowContext = TSL.builtinShadowContext;
103104
export const bvec2 = TSL.bvec2;
104105
export const bvec3 = TSL.bvec3;
105106
export const bvec4 = TSL.bvec4;

src/nodes/core/ContextNode.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,36 @@ export const uniformFlow = ( node ) => context( node, { uniformFlow: true } );
202202
*/
203203
export const setName = ( node, name ) => context( node, { nodeName: name } );
204204

205+
/**
206+
* TSL function for defining a built-in shadow context for a given node.
207+
*
208+
* @tsl
209+
* @function
210+
* @param {ShadowNode} shadowNode - The shadow node representing the light's shadow.
211+
* @param {Light} light - The light associated with the shadow.
212+
* @param {Node} [node=null] - The node whose context should be modified.
213+
* @returns {ContextNode}
214+
*/
215+
export function builtinShadowContext( shadowNode, light, node = null ) {
216+
217+
return context( node, {
218+
219+
getShadow: ( { light: shadowLight, shadowColorNode } ) => {
220+
221+
if ( light === shadowLight ) {
222+
223+
return shadowColorNode.mul( shadowNode );
224+
225+
}
226+
227+
return shadowColorNode;
228+
229+
}
230+
231+
} );
232+
233+
}
234+
205235
/**
206236
* TSL function for defining a label context value for a given node.
207237
*
@@ -224,3 +254,4 @@ addMethodChaining( 'context', context );
224254
addMethodChaining( 'label', label );
225255
addMethodChaining( 'uniformFlow', uniformFlow );
226256
addMethodChaining( 'setName', setName );
257+
addMethodChaining( 'builtinShadowContext', ( node, shadowNode, light ) => builtinShadowContext( shadowNode, light, node ) );

src/nodes/lighting/AnalyticLightNode.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,7 @@ class AnalyticLightNode extends LightingNode {
190190

191191
if ( builder.context.getShadow ) {
192192

193-
const shadow = builder.context.getShadow( this.light, builder );
194-
195-
if ( shadow ) {
196-
197-
shadowColorNode = shadowColorNode.mul( shadow );
198-
199-
}
193+
shadowColorNode = builder.context.getShadow( this, builder );
200194

201195
}
202196

0 commit comments

Comments
 (0)