DebugNode.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import TempNode from '../core/TempNode.js';
  2. import { addMethodChaining, nodeObject } from '../tsl/TSLCore.js';
  3. import { log } from '../../utils.js';
  4. class DebugNode extends TempNode {
  5. static get type() {
  6. return 'DebugNode';
  7. }
  8. constructor( node, callback = null ) {
  9. super();
  10. this.node = node;
  11. this.callback = callback;
  12. }
  13. getNodeType( builder ) {
  14. return this.node.getNodeType( builder );
  15. }
  16. setup( builder ) {
  17. return this.node.build( builder );
  18. }
  19. analyze( builder ) {
  20. return this.node.build( builder );
  21. }
  22. generate( builder ) {
  23. const callback = this.callback;
  24. const snippet = this.node.build( builder );
  25. if ( callback !== null ) {
  26. callback( builder, snippet );
  27. } else {
  28. const title = '--- TSL debug - ' + builder.shaderStage + ' shader ---';
  29. const border = '-'.repeat( title.length );
  30. let code = '';
  31. code += '// #' + title + '#\n';
  32. code += builder.flow.code.replace( /^\t/mg, '' ) + '\n';
  33. code += '/* ... */ ' + snippet + ' /* ... */\n';
  34. code += '// #' + border + '#\n';
  35. log( code );
  36. }
  37. return snippet;
  38. }
  39. }
  40. export default DebugNode;
  41. /**
  42. * TSL function for creating a debug node.
  43. *
  44. * @tsl
  45. * @function
  46. * @param {Node} node - The node to debug.
  47. * @param {?Function} [callback=null] - Optional callback function to handle the debug output.
  48. * @returns {DebugNode}
  49. */
  50. export const debug = ( node, callback = null ) => nodeObject( new DebugNode( nodeObject( node ), callback ) ).toStack();
  51. addMethodChaining( 'debug', debug );
粤ICP备19079148号