NoiseNode.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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( coord ) {
  8. TempNode.call( this, 'fv1' );
  9. this.coord = coord || 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.coord.build( builder, 'v2' ) + ' )', this.getType( builder ), output );
  27. };
  28. NoiseNode.prototype.copy = function ( source ) {
  29. TempNode.prototype.copy.call( this, source );
  30. this.coord = source.coord;
  31. };
  32. NoiseNode.prototype.toJSON = function ( meta ) {
  33. var data = this.getJSONNode( meta );
  34. if ( ! data ) {
  35. data = this.createJSONNode( meta );
  36. data.coord = this.coord.toJSON( meta ).uuid;
  37. }
  38. return data;
  39. };
  40. export { NoiseNode };