Noise2DNode.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { TempNode } from '../core/TempNode.js';
  2. import { FunctionNode } from '../core/FunctionNode.js';
  3. import { FunctionCallNode } from '../core/FunctionCallNode.js';
  4. import { IntNode } from '../inputs/IntNode.js';
  5. import { FloatNode } from '../inputs/FloatNode.js';
  6. import { UVNode } from '../accessors/UVNode.js';
  7. const NOISE_COMMON_SRC = `
  8. vec3 mod289( vec3 x ) {
  9. return x - floor( x * ( 1.0 / 289.0 ) ) * 289.0;
  10. }
  11. vec4 mod289( vec4 x ) {
  12. return x - floor( x * ( 1.0 / 289.0 ) ) * 289.0;
  13. }
  14. vec4 permute( vec4 x ) {
  15. return mod289( ( ( x * 34.0 ) + 1.0 ) * x );
  16. }
  17. vec4 taylorInvSqrt( vec4 r ) {
  18. return 1.79284291400159 - 0.85373472095314 * r;
  19. }
  20. vec2 fade( vec2 t ) {
  21. return t * t * t * ( t * ( t * 6.0 - 15.0 ) + 10.0 );
  22. }
  23. vec3 fade( vec3 t ) {
  24. return t * t * t * ( t * ( t * 6.0 - 15.0 ) + 10.0 );
  25. }
  26. `.trim();
  27. const NOISE2D_SRC = `
  28. float noise2d( vec2 P, float amplitude, float pivot ) {
  29. vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
  30. vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
  31. Pi = mod289(Pi); // To avoid truncation effects in permutation
  32. vec4 ix = Pi.xzxz;
  33. vec4 iy = Pi.yyww;
  34. vec4 fx = Pf.xzxz;
  35. vec4 fy = Pf.yyww;
  36. vec4 i = permute(permute(ix) + iy);
  37. vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
  38. vec4 gy = abs(gx) - 0.5 ;
  39. vec4 tx = floor(gx + 0.5);
  40. gx = gx - tx;
  41. vec2 g00 = vec2(gx.x,gy.x);
  42. vec2 g10 = vec2(gx.y,gy.y);
  43. vec2 g01 = vec2(gx.z,gy.z);
  44. vec2 g11 = vec2(gx.w,gy.w);
  45. vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
  46. g00 *= norm.x;
  47. g01 *= norm.y;
  48. g10 *= norm.z;
  49. g11 *= norm.w;
  50. float n00 = dot(g00, vec2(fx.x, fy.x));
  51. float n10 = dot(g10, vec2(fx.y, fy.y));
  52. float n01 = dot(g01, vec2(fx.z, fy.z));
  53. float n11 = dot(g11, vec2(fx.w, fy.w));
  54. vec2 fade_xy = fade(Pf.xy);
  55. vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
  56. float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
  57. return 2.3 * n_xy * amplitude + pivot;
  58. }
  59. `.trim();
  60. class Noise2DNode extends TempNode {
  61. constructor( uv = new UVNode(), amplitude = new FloatNode( 1.0 ), pivot = new FloatNode( 0.0 ) ) {
  62. super( 'f' );
  63. this.uv = uv;
  64. this.amplitude = amplitude;
  65. this.pivot = pivot;
  66. }
  67. generate(builder, output) {
  68. const noise2d = new FunctionCallNode( Noise2DNode.Nodes.noise2d, [ this.uv, this.amplitude, this.pivot ] );
  69. return builder.format( noise2d.generate( builder, output ), this.getType( builder ), output );
  70. }
  71. copy( source ) {
  72. super.copy( source );
  73. this.uv = source.uv;
  74. this.amplitude = source.amplitude;
  75. this.pivot = source.pivot;
  76. }
  77. toJSON( meta ) {
  78. const data = this.getJSONNode( meta );
  79. if ( ! data ) {
  80. data = this.createJSONNode( meta );
  81. data.uv = this.uv.toJSON( meta ).uuid;
  82. data.amplitude = this.amplitude.toJSON( meta ).uuid;
  83. data.pivot = this.pivot.toJSON( meta ).uuid;
  84. }
  85. return data;
  86. }
  87. }
  88. Noise2DNode.prototype.nodeType = 'Noise2D';
  89. Noise2DNode.Nodes = (function () {
  90. const noiseCommon = new FunctionNode( NOISE_COMMON_SRC );
  91. const noise2d = new FunctionNode( NOISE2D_SRC );
  92. noise2d.includes = [ noiseCommon ];
  93. return { noiseCommon, noise2d };
  94. })();
  95. export { Noise2DNode };