Skip to content

Commit acf97d0

Browse files
authored
Add HashNode (mrdoob#26663)
1 parent 0738cec commit acf97d0

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

examples/jsm/nodes/Nodes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export { NodeUtils };
3838
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';
3939
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';
4040
export { default as CondNode, cond } from './math/CondNode.js';
41+
export { default as HashNode, hash } from './math/HashNode.js';
4142

4243
// utils
4344
export { default as ArrayElementNode } from './utils/ArrayElementNode.js';

examples/jsm/nodes/math/HashNode.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Node, { addNodeClass } from '../core/Node.js';
2+
import { add, mul, bitXor, shiftRight } from './OperatorNode.js';
3+
import { addNodeElement, nodeProxy, uint } from '../shadernode/ShaderNode.js';
4+
5+
class HashNode extends Node {
6+
7+
constructor( seedNode ) {
8+
9+
super();
10+
11+
this.seedNode = seedNode;
12+
13+
}
14+
15+
construct( /*builder*/ ) {
16+
17+
const seed = this.seedNode;
18+
19+
const state = add( mul( uint( seed ), 747796405 ), 2891336453 );
20+
const word = mul( bitXor( shiftRight( state, add( shiftRight( state, 28 ), 4 ) ), state ), 277803737 );
21+
const uintResult = bitXor( shiftRight( word, 22 ), word );
22+
23+
return mul( 1 / 2 ** 32, uintResult ); // Convert to range [0, 1)
24+
25+
}
26+
27+
}
28+
29+
export default HashNode;
30+
31+
export const hash = nodeProxy( HashNode );
32+
33+
addNodeElement( 'hash', hash );
34+
35+
addNodeClass( HashNode );

0 commit comments

Comments
 (0)