Browse Source

Add HashNode (#26663)

sunag 1 year ago
parent
commit
acf97d01d2
2 changed files with 36 additions and 0 deletions
  1. 1 0
      examples/jsm/nodes/Nodes.js
  2. 35 0
      examples/jsm/nodes/math/HashNode.js

+ 1 - 0
examples/jsm/nodes/Nodes.js

@@ -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';

+ 35 - 0
examples/jsm/nodes/math/HashNode.js

@@ -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 );