Browse Source

MathNode: Add `rand()`. (#28765)

Michael Herzog 1 year ago
parent
commit
77aefe8cd6
2 changed files with 12 additions and 2 deletions
  1. 1 1
      src/nodes/Nodes.js
  2. 11 1
      src/nodes/math/MathNode.js

+ 1 - 1
src/nodes/Nodes.js

@@ -38,7 +38,7 @@ import * as NodeUtils from './core/NodeUtils.js';
 export { NodeUtils };
 export { NodeUtils };
 
 
 // math
 // math
-export { default as MathNode, PI, PI2, EPSILON, INFINITY, radians, degrees, exp, exp2, log, log2, sqrt, inverseSqrt, floor, ceil, normalize, fract, sin, cos, tan, asin, acos, atan, abs, sign, length, lengthSq, negate, oneMinus, dFdx, dFdy, round, reciprocal, trunc, fwidth, bitcast, atan2, min, max, mod, step, reflect, distance, difference, dot, cross, pow, pow2, pow3, pow4, transformDirection, mix, clamp, saturate, refract, smoothstep, faceForward, cbrt, transpose, all, any, equals } from './math/MathNode.js';
+export { default as MathNode, PI, PI2, EPSILON, INFINITY, radians, degrees, exp, exp2, log, log2, sqrt, inverseSqrt, floor, ceil, normalize, fract, sin, cos, tan, asin, acos, atan, abs, sign, length, lengthSq, negate, oneMinus, dFdx, dFdy, round, reciprocal, trunc, fwidth, bitcast, atan2, min, max, mod, step, reflect, distance, difference, dot, cross, pow, pow2, pow3, pow4, transformDirection, mix, clamp, saturate, refract, smoothstep, faceForward, cbrt, transpose, all, any, equals, rand } from './math/MathNode.js';
 
 
 export { default as OperatorNode, add, sub, mul, div, remainder, equal, lessThan, greaterThan, lessThanEqual, greaterThanEqual, and, or, not, xor, bitAnd, bitNot, bitOr, bitXor, shiftLeft, shiftRight } from './math/OperatorNode.js';
 export { default as OperatorNode, add, sub, mul, div, remainder, equal, lessThan, greaterThan, lessThanEqual, greaterThanEqual, and, or, not, xor, bitAnd, bitNot, bitOr, bitXor, shiftLeft, shiftRight } from './math/OperatorNode.js';
 export { default as CondNode, cond } from './math/CondNode.js';
 export { default as CondNode, cond } from './math/CondNode.js';

+ 11 - 1
src/nodes/math/MathNode.js

@@ -1,7 +1,7 @@
 import TempNode from '../core/TempNode.js';
 import TempNode from '../core/TempNode.js';
 import { sub, mul, div } from './OperatorNode.js';
 import { sub, mul, div } from './OperatorNode.js';
 import { addNodeClass } from '../core/Node.js';
 import { addNodeClass } from '../core/Node.js';
-import { addNodeElement, nodeObject, nodeProxy, float, vec3, vec4 } from '../shadernode/ShaderNode.js';
+import { addNodeElement, nodeObject, nodeProxy, float, vec2, vec3, vec4, tslFn } from '../shadernode/ShaderNode.js';
 
 
 class MathNode extends TempNode {
 class MathNode extends TempNode {
 
 
@@ -330,6 +330,15 @@ export const refract = nodeProxy( MathNode, MathNode.REFRACT );
 export const smoothstep = nodeProxy( MathNode, MathNode.SMOOTHSTEP );
 export const smoothstep = nodeProxy( MathNode, MathNode.SMOOTHSTEP );
 export const faceForward = nodeProxy( MathNode, MathNode.FACEFORWARD );
 export const faceForward = nodeProxy( MathNode, MathNode.FACEFORWARD );
 
 
+export const rand = tslFn( ( [ uv ] ) => {
+
+	const a = 12.9898, b = 78.233, c = 43758.5453;
+	const dt = dot( uv.xy, vec2( a, b ) ), sn = mod( dt, PI );
+
+	return fract( sin( sn ).mul( c ) );
+
+} );
+
 export const mixElement = ( t, e1, e2 ) => mix( e1, e2, t );
 export const mixElement = ( t, e1, e2 ) => mix( e1, e2, t );
 export const smoothstepElement = ( x, low, high ) => smoothstep( low, high, x );
 export const smoothstepElement = ( x, low, high ) => smoothstep( low, high, x );
 
 
@@ -390,5 +399,6 @@ addNodeElement( 'difference', difference );
 addNodeElement( 'saturate', saturate );
 addNodeElement( 'saturate', saturate );
 addNodeElement( 'cbrt', cbrt );
 addNodeElement( 'cbrt', cbrt );
 addNodeElement( 'transpose', transpose );
 addNodeElement( 'transpose', transpose );
+addNodeElement( 'rand', rand );
 
 
 addNodeClass( 'MathNode', MathNode );
 addNodeClass( 'MathNode', MathNode );