NoiseNode.js 1.3 KB

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