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: Introduce onUpdate #27964

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/jsm/nodes/core/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ class Node extends EventDispatcher {

}

onUpdate( callback, updateType ) {

this.updateType = updateType;
this.update = callback.bind( this.getSelf() );

return this;

}

onFrameUpdate( callback ) {

return this.onUpdate( callback, NodeUpdateType.FRAME );

}

onRenderUpdate( callback ) {

return this.onUpdate( callback, NodeUpdateType.RENDER );

}

onObjectUpdate( callback ) {

return this.onUpdate( callback, NodeUpdateType.OBJECT );

}

getSelf() {

// Returns non-node object.
Expand Down
20 changes: 20 additions & 0 deletions examples/jsm/nodes/core/UniformNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ class UniformNode extends InputNode {

}

onUpdate( callback, updateType ) {

const self = this.getSelf();

callback = callback.bind( self );

return super.onUpdate( ( frame ) => {

const value = callback( frame, self );

if ( value !== undefined ) {

this.value = value;

}

}, updateType );

}

generate( builder, output ) {

const type = this.getNodeType( builder );
Expand Down
Binary file modified examples/screenshots/webgpu_custom_fog.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions examples/webgpu_custom_fog.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<script type="module">

import * as THREE from 'three';
import { color, fog, float, positionWorld, triNoise3D, positionView, normalWorld, timerLocal, MeshPhongNodeMaterial } from 'three/nodes';
import { color, fog, float, positionWorld, triNoise3D, positionView, normalWorld, uniform, MeshPhongNodeMaterial } from 'three/nodes';

import WebGPU from 'three/addons/capabilities/WebGPU.js';
import WebGL from 'three/addons/capabilities/WebGL.js';
Expand Down Expand Up @@ -64,7 +64,8 @@
const alpha = .98;
const groundFogArea = float( distance ).sub( positionWorld.y ).div( distance ).pow( 3 ).saturate().mul( alpha );

const timer = timerLocal( 1 );
// a alternative way to create a TimerNode
const timer = uniform( 0 ).onFrameUpdate( ( frame ) => frame.time );

const fogNoiseA = triNoise3D( positionWorld.mul( .005 ), 0.2, timer );
const fogNoiseB = triNoise3D( positionWorld.mul( .01 ), 0.2, timer.mul( 1.2 ) );
Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_materials.html
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
materials.push( material );

const loopCount = 10;
material.colorNode = loop( loopCount, ( { i }, stack ) => {
material.colorNode = loop( loopCount, ( { i } ) => {

const output = vec4().temp();
const scale = oscSine().mul( .09 ); // just a value to test
Expand Down