Skip to content

Commit 9d8aa59

Browse files
authored
TSL: Introduce events (#31514)
* introduce events * add js-docs * Update EventNode.js * Update EventNode.js * Update EventNode.js
1 parent fe5f4b2 commit 9d8aa59

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/Three.TSL.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ export const objectRadius = TSL.objectRadius;
391391
export const objectScale = TSL.objectScale;
392392
export const objectViewPosition = TSL.objectViewPosition;
393393
export const objectWorldMatrix = TSL.objectWorldMatrix;
394+
export const OnObjectUpdate = TSL.OnObjectUpdate;
395+
export const OnMaterialUpdate = TSL.OnMaterialUpdate;
394396
export const oneMinus = TSL.oneMinus;
395397
export const or = TSL.or;
396398
export const orthographicDepthToViewZ = TSL.orthographicDepthToViewZ;

src/nodes/Nodes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export { default as ReflectorNode } from './utils/ReflectorNode.js';
5555
export { default as RTTNode } from './utils/RTTNode.js';
5656
export { default as MemberNode } from './utils/MemberNode.js';
5757
export { default as DebugNode } from './utils/DebugNode.js';
58+
export { default as EventNode } from './utils/EventNode.js';
5859

5960
// accessors
6061
export { default as UniformArrayNode } from './accessors/UniformArrayNode.js';

src/nodes/TSL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export * from './utils/ReflectorNode.js';
4343
export * from './utils/RTTNode.js';
4444
export * from './utils/PostProcessingUtils.js';
4545
export * from './utils/SampleNode.js';
46+
export * from './utils/EventNode.js';
4647

4748
// three.js shading language
4849
export * from './tsl/TSLBase.js';

src/nodes/utils/EventNode.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import Node from '../core/Node.js';
2+
import { NodeUpdateType } from '../core/constants.js';
3+
import { nodeObject } from '../tsl/TSLCore.js';
4+
5+
/**
6+
* EventNode is a node that executes a callback during specific update phases.
7+
*
8+
* @augments Node
9+
*/
10+
class EventNode extends Node {
11+
12+
static get type() {
13+
14+
return 'EventNode';
15+
16+
}
17+
18+
/**
19+
* Creates an EventNode.
20+
*
21+
* @param {string} eventType - The type of event
22+
* @param {Function} callback - The callback to execute on update.
23+
*/
24+
constructor( eventType, callback ) {
25+
26+
super( 'void' );
27+
28+
this.eventType = eventType;
29+
this.callback = callback;
30+
31+
if ( eventType === EventNode.OBJECT ) {
32+
33+
this.updateType = NodeUpdateType.OBJECT;
34+
35+
} else if ( eventType === EventNode.MATERIAL ) {
36+
37+
this.updateType = NodeUpdateType.RENDER;
38+
39+
}
40+
41+
}
42+
43+
update( frame ) {
44+
45+
this.callback( frame );
46+
47+
}
48+
49+
}
50+
51+
EventNode.OBJECT = 'object';
52+
EventNode.MATERIAL = 'material';
53+
54+
export default EventNode;
55+
56+
/**
57+
* Helper to create an EventNode and add it to the stack.
58+
*
59+
* @param {string} type - The event type.
60+
* @param {Function} callback - The callback function.
61+
* @returns {EventNode}
62+
*/
63+
const createEvent = ( type, callback ) => nodeObject( new EventNode( type, callback ) ).toStack();
64+
65+
/**
66+
* Creates an event that triggers a function every time an object (Mesh|Sprite) is rendered.
67+
*
68+
* The event will be bound to the declared TSL function `Fn()`; it must be declared within a `Fn()` or the JS function call must be inherited from one.
69+
*
70+
* @param {Function} callback - The callback function.
71+
* @returns {EventNode}
72+
*/
73+
export const OnObjectUpdate = ( callback ) => createEvent( EventNode.OBJECT, callback );
74+
75+
/**
76+
* Creates an event that triggers a function when the first object that uses the material is rendered.
77+
*
78+
* The event will be bound to the declared TSL function `Fn()`; it must be declared within a `Fn()` or the JS function call must be inherited from one.
79+
*
80+
* @param {Function} callback - The callback function.
81+
* @returns {EventNode}
82+
*/
83+
export const OnMaterialUpdate = ( callback ) => createEvent( EventNode.MATERIAL, callback );

0 commit comments

Comments
 (0)