HashNode.js 804 B

1234567891011121314151617181920212223242526272829303132333435
  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. class HashNode extends Node {
  5. constructor( seedNode ) {
  6. super();
  7. this.seedNode = seedNode;
  8. }
  9. construct( /*builder*/ ) {
  10. const seed = this.seedNode;
  11. const state = add( mul( uint( seed ), 747796405 ), 2891336453 );
  12. const word = mul( bitXor( shiftRight( state, add( shiftRight( state, 28 ), 4 ) ), state ), 277803737 );
  13. const uintResult = bitXor( shiftRight( word, 22 ), word );
  14. return mul( 1 / 2 ** 32, uintResult ); // Convert to range [0, 1)
  15. }
  16. }
  17. export default HashNode;
  18. export const hash = nodeProxy( HashNode );
  19. addNodeElement( 'hash', hash );
  20. addNodeClass( HashNode );