-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Expand file tree
/
Copy pathHash.js
More file actions
22 lines (17 loc) · 731 Bytes
/
Copy pathHash.js
File metadata and controls
22 lines (17 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { Fn } from '../tsl/TSLBase.js';
import { uint } from '../tsl/TSLCore.js';
/**
* Generates a hash value in the range `[0, 1]` from the given seed.
*
* @tsl
* @function
* @param {Node<float>} seed - The seed.
* @return {Node<float>} The hash value.
*/
export const hash = /*@__PURE__*/ Fn( ( [ seed ] ) => {
// Taken from https://www.shadertoy.com/view/XlGcRh, originally from pcg-random.org
const state = seed.toUint().mul( uint( 747796405 ) ).add( uint( 2891336453 ) );
const word = state.shiftRight( state.shiftRight( 28 ).add( 4 ) ).bitXor( state ).mul( uint( 277803737 ) );
const result = word.shiftRight( 22 ).bitXor( word );
return result.toFloat().mul( 1 / 2 ** 32 ); // Convert to range [0, 1)
} );