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

Node: onBefore, onAfter events #30212

Closed
Samsy opened this issue Dec 26, 2024 · 4 comments
Closed

Node: onBefore, onAfter events #30212

Samsy opened this issue Dec 26, 2024 · 4 comments
Labels

Comments

@Samsy
Copy link
Contributor

Samsy commented Dec 26, 2024

Description

Callbacks before a node is rendered, and after a node is rendered would be extremely useful to set renderer state such as autoClear etc.. but also helping for custom instructions

For example

a reflector, or water, if we need to setup a camera layer before those actually renders for selective render inside a reflector
this requires a callback before and after the node actually renders

An issue I mentionned here :
#30197

Solution

Provide a onBeforeNode, and onAfterNode render

@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 26, 2024

Are you aware of the existing possibilities to implement such use cases? The idea is to derive from the built-in node classes and then implement the already existing update methods.

/**
* The method can be implemented to update the node's internal state before it is used to render an object.
* The {@link Node#updateBeforeType} property defines how often the update is executed.
*
* @abstract
* @param {NodeFrame} frame - A reference to the current node frame.
* @return {Boolean?} An optional bool that indicates whether the implementation actually performed an update or not (e.g. due to caching).
*/
updateBefore( /*frame*/ ) {
console.warn( 'Abstract function.' );
}
/**
* The method can be implemented to update the node's internal state after it was used to render an object.
* The {@link Node#updateAfterType} property defines how often the update is executed.
*
* @abstract
* @param {NodeFrame} frame - A reference to the current node frame.
* @return {Boolean?} An optional bool that indicates whether the implementation actually performed an update or not (e.g. due to caching).
*/
updateAfter( /*frame*/ ) {
console.warn( 'Abstract function.' );
}

To let the methods executed per render, you must set node.updateBeforeType and node.updateAfterType to NodeUpdateType.RENDER.

@sunag sunag changed the title onBeforeNode render, onAfterNode render Node: onBefore, onAfter events Dec 27, 2024
@sunag sunag closed this as completed Dec 27, 2024
@Samsy
Copy link
Contributor Author

Samsy commented Dec 27, 2024

Hey @Mugen87

Thanks for the answer, I was not aware of this mechanism, as it isn't mentioned in the wiki, and not once in the examples !

I'll look at it, many thanks

@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 27, 2024

If you need more examples for this pattern, check out the post processing modules in examples/jsm/tsl/display. They use these methods all over the place.

@Samsy
Copy link
Contributor Author

Samsy commented Dec 27, 2024

Thanks @Mugen87

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

3 participants