CheckerNode.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 CheckerNode( uv ) {
  8. TempNode.call( this, 'f' );
  9. this.uv = uv || new UVNode();
  10. }
  11. CheckerNode.prototype = Object.create( TempNode.prototype );
  12. CheckerNode.prototype.constructor = CheckerNode;
  13. CheckerNode.prototype.nodeType = "Noise";
  14. CheckerNode.Nodes = ( function () {
  15. // https://github.com/mattdesl/glsl-checker/blob/master/index.glsl
  16. var checker = new FunctionNode( [
  17. "float checker( vec2 uv ) {",
  18. " float cx = floor( uv.x );",
  19. " float cy = floor( uv.y ); ",
  20. " float result = mod( cx + cy, 2.0 );",
  21. " return sign( result );",
  22. "}"
  23. ].join( "\n" ) );
  24. return {
  25. checker: checker
  26. };
  27. } )();
  28. CheckerNode.prototype.generate = function ( builder, output ) {
  29. var snoise = builder.include( CheckerNode.Nodes.checker );
  30. return builder.format( snoise + '( ' + this.uv.build( builder, 'v2' ) + ' )', this.getType( builder ), output );
  31. };
  32. CheckerNode.prototype.copy = function ( source ) {
  33. TempNode.prototype.copy.call( this, source );
  34. this.uv = source.uv;
  35. return this;
  36. };
  37. CheckerNode.prototype.toJSON = function ( meta ) {
  38. var data = this.getJSONNode( meta );
  39. if ( ! data ) {
  40. data = this.createJSONNode( meta );
  41. data.uv = this.uv.toJSON( meta ).uuid;
  42. }
  43. return data;
  44. };
  45. export { CheckerNode };