CheckerNode.js 1.2 KB

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