Skip to content

Commit

Permalink
Add HashNode (mrdoob#26663)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunag authored Aug 28, 2023
1 parent 0738cec commit acf97d0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export { NodeUtils };
export { default as MathNode, EPSILON, INFINITY, radians, degrees, exp, exp2, log, log2, sqrt, inverseSqrt, floor, ceil, normalize, fract, sin, cos, tan, asin, acos, atan, abs, sign, length, negate, oneMinus, dFdx, dFdy, round, reciprocal, trunc, fwidth, atan2, min, max, mod, step, reflect, distance, difference, dot, cross, pow, pow2, pow3, pow4, transformDirection, mix, clamp, saturate, refract, smoothstep, faceForward } from './math/MathNode.js';
export { default as OperatorNode, add, sub, mul, div, remainder, equal, assign, lessThan, greaterThan, lessThanEqual, greaterThanEqual, and, or, xor, bitAnd, bitOr, bitXor, shiftLeft, shiftRight } from './math/OperatorNode.js';
export { default as CondNode, cond } from './math/CondNode.js';
export { default as HashNode, hash } from './math/HashNode.js';

// utils
export { default as ArrayElementNode } from './utils/ArrayElementNode.js';
Expand Down
35 changes: 35 additions & 0 deletions examples/jsm/nodes/math/HashNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Node, { addNodeClass } from '../core/Node.js';
import { add, mul, bitXor, shiftRight } from './OperatorNode.js';
import { addNodeElement, nodeProxy, uint } from '../shadernode/ShaderNode.js';

class HashNode extends Node {

constructor( seedNode ) {

super();

this.seedNode = seedNode;

}

construct( /*builder*/ ) {

const seed = this.seedNode;

const state = add( mul( uint( seed ), 747796405 ), 2891336453 );
const word = mul( bitXor( shiftRight( state, add( shiftRight( state, 28 ), 4 ) ), state ), 277803737 );
const uintResult = bitXor( shiftRight( word, 22 ), word );

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

}

}

export default HashNode;

export const hash = nodeProxy( HashNode );

addNodeElement( 'hash', hash );

addNodeClass( HashNode );

0 comments on commit acf97d0

Please sign in to comment.