CheckerNode.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 { FloatNode } from '../inputs/FloatNode.js';
  7. import { UVNode } from '../accessors/UVNode.js';
  8. function CheckerNode( uv ) {
  9. TempNode.call( this, 'f' );
  10. this.uv = uv || new UVNode();
  11. };
  12. CheckerNode.prototype = Object.create( TempNode.prototype );
  13. CheckerNode.prototype.constructor = CheckerNode;
  14. CheckerNode.prototype.nodeType = "Noise";
  15. CheckerNode.Nodes = (function() {
  16. // https://github.com/mattdesl/glsl-checker/blob/master/index.glsl
  17. var checker = new FunctionNode( [
  18. "float checker( vec2 uv ) {",
  19. " float cx = floor( uv.x );",
  20. " float cy = floor( uv.y ); ",
  21. " float result = mod( cx + cy, 2.0 );",
  22. " return sign( result );",
  23. "}"
  24. ].join( "\n" ) );
  25. return {
  26. checker: checker
  27. };
  28. })();
  29. CheckerNode.prototype.generate = function ( builder, output ) {
  30. var snoise = builder.include( CheckerNode.Nodes.checker );
  31. return builder.format( snoise + '( ' + this.uv.build( builder, 'v2' ) + ' )', this.getType( builder ), output );
  32. };
  33. CheckerNode.prototype.copy = function ( source ) {
  34. TempNode.prototype.copy.call( this, source );
  35. this.uv = source.uv;
  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 };