Skip to content

Commit

Permalink
WebGPURenderer: Update Packing, Hash, Discard (#29188)
Browse files Browse the repository at this point in the history
* PackingNode: Move to TSL approach

* update webgpu_mrt

* DiscardNode: Move to TSL approach

* HashNode: Move to TSL approach

* no references to discard() without chaining

* Update DiscardNode.js

* Update DiscardNode.js

* renames
  • Loading branch information
sunag authored Aug 22, 2024
1 parent 00f9175 commit 7bc2d4a
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 123 deletions.
Binary file modified examples/screenshots/webgpu_mrt.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/webgpu_mrt.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<script type="module">

import * as THREE from 'three';
import { output, transformedNormalWorld, pass, step, diffuseColor, emissive, viewportUV, mix, mrt, Fn } from 'three/tsl';
import { output, transformedNormalView, pass, step, diffuseColor, emissive, directionToColor, viewportUV, mix, mrt, Fn } from 'three/tsl';

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

Expand Down Expand Up @@ -84,7 +84,7 @@
const scenePass = pass( scene, camera, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter } );
scenePass.setMRT( mrt( {
output: output,
normal: transformedNormalWorld.directionToColor(),
normal: directionToColor( transformedNormalView ),
diffuse: diffuseColor,
emissive: emissive
} ) );
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export { default as MathNode, PI, PI2, EPSILON, INFINITY, radians, degrees, exp,

export { default as OperatorNode, add, sub, mul, div, modInt, equal, lessThan, greaterThan, lessThanEqual, greaterThanEqual, and, or, not, xor, bitAnd, bitNot, bitOr, bitXor, shiftLeft, shiftRight, remainder } from './math/OperatorNode.js';
export { default as CondNode, select, cond } from './math/CondNode.js';
export { default as HashNode, hash } from './math/HashNode.js';
export * from './math/Hash.js';

// math utils
export { parabola, gain, pcurve, sinc } from './math/MathUtils.js';
Expand All @@ -51,15 +51,15 @@ export { triNoise3D } from './math/TriNoise3D.js';
// utils
export { default as ArrayElementNode } from './utils/ArrayElementNode.js';
export { default as ConvertNode } from './utils/ConvertNode.js';
export { default as DiscardNode, discard, Return } from './utils/DiscardNode.js';
export * from './utils/Discard.js';
export { default as EquirectUVNode, equirectUV } from './utils/EquirectUVNode.js';
export { default as FunctionOverloadingNode, overloadingFn } from './utils/FunctionOverloadingNode.js';
export { default as JoinNode } from './utils/JoinNode.js';
export { default as LoopNode, Loop, Continue, Break } from './utils/LoopNode.js';
export { default as MatcapUVNode, matcapUV } from './utils/MatcapUVNode.js';
export { default as MaxMipLevelNode, maxMipLevel } from './utils/MaxMipLevelNode.js';
export { default as OscNode, oscSine, oscSquare, oscTriangle, oscSawtooth } from './utils/OscNode.js';
export { default as PackingNode, directionToColor, colorToDirection } from './utils/PackingNode.js';
export * from './utils/Packing.js';
export { default as RemapNode, remap, remapClamp } from './utils/RemapNode.js';
export * from './utils/UVUtils.js';
export * from './utils/SpriteUtils.js';
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/materials/MeshNormalNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { diffuseColor } from '../core/PropertyNode.js';
import { directionToColor } from '../utils/PackingNode.js';
import { directionToColor } from '../utils/Packing.js';
import { materialOpacity } from '../accessors/MaterialNode.js';
import { transformedNormalView } from '../accessors/NormalNode.js';
import { float, vec4 } from '../shadernode/ShaderNode.js';
Expand Down
15 changes: 15 additions & 0 deletions src/nodes/math/Hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Fn, addNodeElement } from '../shadernode/ShaderNode.js';

export const hash = Fn( ( [ seed ] ) => {

// Taken from https://www.shadertoy.com/view/XlGcRh, originally from pcg-random.org

const state = seed.toUint().mul( 747796405 ).add( 2891336453 );
const word = state.shiftRight( state.shiftRight( 28 ).add( 4 ) ).bitXor( state ).mul( 277803737 );
const result = word.shiftRight( 22 ).bitXor( word );

return result.toFloat().mul( 1 / 2 ** 32 ); // Convert to range [0, 1)

} );

addNodeElement( 'hash', hash );
34 changes: 0 additions & 34 deletions src/nodes/math/HashNode.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/nodes/utils/Discard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { select } from '../math/CondNode.js';
import { expression } from '../code/ExpressionNode.js';
import { addNodeElement } from '../shadernode/ShaderNode.js';

export const Discard = ( conditional ) => ( conditional ? select( conditional, expression( 'discard' ) ) : expression( 'discard' ) ).append();
export const Return = () => expression( 'return' ).append();

addNodeElement( 'discard', Discard );
28 changes: 0 additions & 28 deletions src/nodes/utils/DiscardNode.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/nodes/utils/Packing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { nodeObject } from '../shadernode/ShaderNode.js';

export const directionToColor = ( node ) => nodeObject( node ).mul( 0.5 ).add( 0.5 );
export const colorToDirection = ( node ) => nodeObject( node ).mul( 2.0 ).sub( 1 );
55 changes: 0 additions & 55 deletions src/nodes/utils/PackingNode.js

This file was deleted.

0 comments on commit 7bc2d4a

Please sign in to comment.