Noise3DNode.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 { PositionNode } from '../accessors/PositionNode.js';
  6. import { Noise2DNode } from './Noise2DNode.js';
  7. const NOISE3D_SRC = `
  8. float noise3d( vec3 P, float amplitude, float pivot ) {
  9. vec3 Pi0 = floor(P); // Integer part for indexing
  10. vec3 Pi1 = Pi0 + vec3(1.0); // Integer part + 1
  11. Pi0 = mod289(Pi0);
  12. Pi1 = mod289(Pi1);
  13. vec3 Pf0 = fract(P); // Fractional part for interpolation
  14. vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
  15. vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  16. vec4 iy = vec4(Pi0.yy, Pi1.yy);
  17. vec4 iz0 = Pi0.zzzz;
  18. vec4 iz1 = Pi1.zzzz;
  19. vec4 ixy = permute(permute(ix) + iy);
  20. vec4 ixy0 = permute(ixy + iz0);
  21. vec4 ixy1 = permute(ixy + iz1);
  22. vec4 gx0 = ixy0 * (1.0 / 7.0);
  23. vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;
  24. gx0 = fract(gx0);
  25. vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
  26. vec4 sz0 = step(gz0, vec4(0.0));
  27. gx0 -= sz0 * (step(0.0, gx0) - 0.5);
  28. gy0 -= sz0 * (step(0.0, gy0) - 0.5);
  29. vec4 gx1 = ixy1 * (1.0 / 7.0);
  30. vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;
  31. gx1 = fract(gx1);
  32. vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
  33. vec4 sz1 = step(gz1, vec4(0.0));
  34. gx1 -= sz1 * (step(0.0, gx1) - 0.5);
  35. gy1 -= sz1 * (step(0.0, gy1) - 0.5);
  36. vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);
  37. vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);
  38. vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);
  39. vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);
  40. vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);
  41. vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);
  42. vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);
  43. vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);
  44. vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  45. g000 *= norm0.x;
  46. g010 *= norm0.y;
  47. g100 *= norm0.z;
  48. g110 *= norm0.w;
  49. vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  50. g001 *= norm1.x;
  51. g011 *= norm1.y;
  52. g101 *= norm1.z;
  53. g111 *= norm1.w;
  54. float n000 = dot(g000, Pf0);
  55. float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
  56. float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
  57. float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
  58. float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
  59. float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
  60. float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
  61. float n111 = dot(g111, Pf1);
  62. vec3 fade_xyz = fade(Pf0);
  63. vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
  64. vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
  65. float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
  66. return 2.2 * n_xyz * amplitude + pivot;
  67. }
  68. `.trim();
  69. class Noise3DNode extends TempNode {
  70. constructor( position = new PositionNode(), amplitude = new FloatNode( 1.0 ), pivot = new FloatNode( 0.0 ) ) {
  71. super( 'f' );
  72. this.position = position;
  73. this.amplitude = amplitude;
  74. this.pivot = pivot;
  75. }
  76. generate( builder, output ) {
  77. const noise3d = new FunctionCallNode( Noise3DNode.Nodes.noise3d, [ this.position, this.amplitude, this.pivot ] );
  78. return builder.format( noise3d.generate( builder, output ), this.getType( builder ), output );
  79. }
  80. copy( source ) {
  81. super.copy( source );
  82. this.position = source.position;
  83. this.amplitude = source.amplitude;
  84. this.pivot = source.pivot;
  85. }
  86. toJSON( meta ) {
  87. let data = this.getJSONNode( meta );
  88. if ( ! data ) {
  89. data = this.createJSONNode( meta );
  90. data.position = this.position.toJSON( meta ).uuid;
  91. data.amplitude = this.amplitude.toJSON( meta ).uuid;
  92. data.pivot = this.pivot.toJSON( meta ).uuid;
  93. }
  94. return data;
  95. }
  96. }
  97. Noise3DNode.prototype.nodeType = 'Noise3D';
  98. Noise3DNode.Nodes = ( function () {
  99. const noise3d = new FunctionNode( NOISE3D_SRC );
  100. noise3d.includes = [ Noise2DNode.Nodes.noiseCommon ];
  101. return { noise3d };
  102. } )();
  103. export { Noise3DNode };