Noise2DNode.js 3.0 KB

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