Fractal3DNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 { PositionNode } from '../accessors/PositionNode.js';
  7. import { Noise3DNode } from './Noise3DNode.js';
  8. const FRACTAL3D_SRC = `
  9. float fractal3d( vec3 p, float amplitude, int octaves, float lacunarity, float diminish ) {
  10. float result = 0.0;
  11. for (int i = 0; i < octaves; ++i) {
  12. result += noise3d(p, amplitude, 0.0);
  13. amplitude *= diminish;
  14. p *= lacunarity;
  15. }
  16. return result;
  17. }
  18. `.trim();
  19. /** Fractional Brownian motion. */
  20. class Fractal3DNode extends TempNode {
  21. constructor( position = new PositionNode(), amplitude = new FloatNode( 1.0 ), octaves = 3.0, lacunarity = 2.0, diminish = 0.5 ) {
  22. super( 'f' );
  23. this.position = position;
  24. this.amplitude = amplitude;
  25. this.octaves = new IntNode( octaves ).setReadonly( true );
  26. this.lacunarity = new FloatNode( lacunarity ).setReadonly( true );
  27. this.diminish = new FloatNode( diminish ).setReadonly( true );
  28. }
  29. generate( builder, output ) {
  30. const fractal3d = new FunctionCallNode( Fractal3DNode.Nodes.fractal3d, [
  31. this.position,
  32. this.amplitude,
  33. this.octaves,
  34. this.lacunarity,
  35. this.diminish,
  36. ] );
  37. return builder.format( fractal3d.generate( builder, output ), this.getType( builder ), output );
  38. }
  39. copy( source ) {
  40. super.copy( source );
  41. this.position = source.position;
  42. this.amplitude = source.amplitude;
  43. this.octaves = source.octaves;
  44. this.lacunarity = source.lacunarity;
  45. this.diminish = source.diminish;
  46. }
  47. toJSON( meta ) {
  48. let data = this.getJSONNode( meta );
  49. if ( ! data ) {
  50. data = this.createJSONNode( meta );
  51. data.position = this.position.toJSON( meta ).uuid;
  52. data.amplitude = this.amplitude.toJSON( meta ).uuid;
  53. data.octaves = this.octaves.toJSON( meta ).uuid;
  54. data.lacunarity = this.lacunarity.toJSON( meta ).uuid;
  55. data.diminish = this.diminish.toJSON( meta ).uuid;
  56. }
  57. return data;
  58. }
  59. }
  60. Fractal3DNode.prototype.nodeType = 'Fractal3D';
  61. Fractal3DNode.Nodes = ( function () {
  62. const fractal3d = new FunctionNode( FRACTAL3D_SRC );
  63. fractal3d.includes = [ Noise3DNode.Nodes.noise3d ];
  64. return { fractal3d };
  65. } )();
  66. export { Fractal3DNode };