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

TSL: Add output and material.outputNode #26419

Merged
merged 2 commits into from
Jul 13, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export { default as NodeKeywords } from './core/NodeKeywords.js';
export { default as NodeUniform } from './core/NodeUniform.js';
export { default as NodeVar } from './core/NodeVar.js';
export { default as NodeVarying } from './core/NodeVarying.js';
export { default as PropertyNode, property, diffuseColor, roughness, metalness, specularColor, shininess } from './core/PropertyNode.js';
export { default as PropertyNode, property, output, diffuseColor, roughness, metalness, specularColor, shininess } from './core/PropertyNode.js';
export { default as StackNode, stack } from './core/StackNode.js';
export { default as TempNode } from './core/TempNode.js';
export { default as UniformNode, uniform } from './core/UniformNode.js';
Expand Down
1 change: 1 addition & 0 deletions examples/jsm/nodes/core/PropertyNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ export const sheen = nodeImmutable( PropertyNode, 'vec3', 'Sheen' );
export const sheenRoughness = nodeImmutable( PropertyNode, 'float', 'SheenRoughness' );
export const specularColor = nodeImmutable( PropertyNode, 'color', 'SpecularColor' );
export const shininess = nodeImmutable( PropertyNode, 'float', 'Shininess' );
export const output = nodeImmutable( PropertyNode, 'vec4', 'Output' );

addNodeClass( PropertyNode );
16 changes: 13 additions & 3 deletions examples/jsm/nodes/materials/NodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Material, ShaderMaterial, NoColorSpace } from 'three';
import { getNodeChildren, getCacheKey } from '../core/NodeUtils.js';
import { attribute } from '../core/AttributeNode.js';
import { diffuseColor } from '../core/PropertyNode.js';
import { output, diffuseColor } from '../core/PropertyNode.js';
import { materialNormal } from '../accessors/ExtendedMaterialNode.js';
import { materialAlphaTest, materialColor, materialOpacity, materialEmissive } from '../accessors/MaterialNode.js';
import { modelViewProjection } from '../accessors/ModelViewProjectionNode.js';
Expand Down Expand Up @@ -76,6 +76,8 @@ class NodeMaterial extends ShaderMaterial {

builder.addStack();

let outputNode;

if ( this.isUnlit === false ) {

if ( this.normals === true ) this.constructNormal( builder );
Expand All @@ -85,14 +87,18 @@ class NodeMaterial extends ShaderMaterial {

const outgoingLightNode = this.constructLighting( builder );

builder.stack.outputNode = this.constructOutput( builder, vec4( outgoingLightNode, diffuseColor.a ) );
outputNode = this.constructOutput( builder, vec4( outgoingLightNode, diffuseColor.a ) );

if ( this.outputNode !== null ) outputNode = this.outputNode;

} else {

builder.stack.outputNode = this.constructOutput( builder, this.outputNode || vec4( 0, 0, 0, 1 ) );
outputNode = this.constructOutput( builder, this.outputNode || vec4( 0, 0, 0, 1 ) );

}

builder.stack.outputNode = outputNode;

builder.addFlow( 'fragment', builder.removeStack() );

}
Expand Down Expand Up @@ -330,6 +336,10 @@ class NodeMaterial extends ShaderMaterial {

if ( fogNode ) outputNode = vec4( fogNode.mixAssign( outputNode.rgb ), outputNode.a );

// OUTPUT NODE

builder.stack.assign( output, outputNode );

return outputNode;

}
Expand Down
9 changes: 8 additions & 1 deletion examples/webgpu_backdrop.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<script type="module">

import * as THREE from 'three';
import { float, vec3, color, toneMapping, viewportSharedTexture, viewportTopLeft, checker, uv, oscSine, MeshStandardNodeMaterial } from 'three/nodes';
import { float, vec3, color, toneMapping, viewportSharedTexture, viewportTopLeft, checker, uv, timerLocal, oscSine, output, MeshStandardNodeMaterial } from 'three/nodes';

import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

Expand Down Expand Up @@ -74,6 +74,13 @@
const object = gltf.scene;
mixer = new THREE.AnimationMixer( object );

const material = object.children[ 0 ].children[ 0 ].material;

// output material effect ( better using hsv )
// ignore output.sRGBToLinear().linearTosRGB() for now

material.outputNode = oscSine( timerLocal( .1 ) ).mix( output, output.add( .1 ).posterize( 4 ).mul( 2 ) );

const action = mixer.clipAction( gltf.animations[ 0 ] );
action.play();

Expand Down