CheckerNode.js 1.4 KB

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