NoiseNode.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { TempNode } from '../core/TempNode.js';
  2. import { FunctionNode } from '../core/FunctionNode.js';
  3. import { UVNode } from '../accessors/UVNode.js';
  4. function NoiseNode( uv ) {
  5. TempNode.call( this, 'f' );
  6. this.uv = uv || new UVNode();
  7. }
  8. NoiseNode.prototype = Object.create( TempNode.prototype );
  9. NoiseNode.prototype.constructor = NoiseNode;
  10. NoiseNode.prototype.nodeType = 'Noise';
  11. NoiseNode.Nodes = ( function () {
  12. var snoise = new FunctionNode( [
  13. 'float snoise(vec2 co) {',
  14. ' return fract( sin( dot( co.xy, vec2( 12.9898, 78.233 ) ) ) * 43758.5453 );',
  15. '}'
  16. ].join( '\n' ) );
  17. return {
  18. snoise: snoise
  19. };
  20. } )();
  21. NoiseNode.prototype.generate = function ( builder, output ) {
  22. var snoise = builder.include( NoiseNode.Nodes.snoise );
  23. return builder.format( snoise + '( ' + this.uv.build( builder, 'v2' ) + ' )', this.getType( builder ), output );
  24. };
  25. NoiseNode.prototype.copy = function ( source ) {
  26. TempNode.prototype.copy.call( this, source );
  27. this.uv = source.uv;
  28. return this;
  29. };
  30. NoiseNode.prototype.toJSON = function ( meta ) {
  31. var data = this.getJSONNode( meta );
  32. if ( ! data ) {
  33. data = this.createJSONNode( meta );
  34. data.uv = this.uv.toJSON( meta ).uuid;
  35. }
  36. return data;
  37. };
  38. export { NoiseNode };