Transpiler.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import Linker from './Linker.js';
  2. /**
  3. * A class that transpiles shader code from one language into another.
  4. *
  5. * `Transpiler` can only be used to convert GLSL into TSL right now. It is intended
  6. * to support developers when they want to migrate their custom materials from the
  7. * current to the new node-based material system.
  8. *
  9. * @three_import import Transpiler from 'three/addons/transpiler/Transpiler.js';
  10. */
  11. class Transpiler {
  12. /**
  13. * Constructs a new transpiler.
  14. *
  15. * @param {GLSLDecoder} decoder - The GLSL decoder.
  16. * @param {TSLEncoder} encoder - The TSL encoder.
  17. */
  18. constructor( decoder, encoder ) {
  19. /**
  20. * The GLSL decoder. This component parse GLSL and produces
  21. * a language-independent AST for further processing.
  22. *
  23. * @type {GLSLDecoder}
  24. */
  25. this.decoder = decoder;
  26. /**
  27. * The TSL encoder. It takes the AST and emits TSL code.
  28. *
  29. * @type {TSLEncoder}
  30. */
  31. this.encoder = encoder;
  32. /**
  33. * The linker. It processes the AST and resolves
  34. * variable and function references, ensuring that all
  35. * dependencies are properly linked.
  36. *
  37. * @type {Linker}
  38. */
  39. this.linker = new Linker();
  40. }
  41. /**
  42. * Parses the given GLSL source and returns TSL syntax.
  43. *
  44. * @param {string} source - The GLSL source.
  45. * @return {string} The TSL code.
  46. */
  47. parse( source ) {
  48. const ast = this.decoder.parse( source );
  49. // Process the AST to resolve variable and function references and optimizations.
  50. this.linker.process( ast );
  51. return this.encoder.emit( ast );
  52. }
  53. }
  54. export default Transpiler;
粤ICP备19079148号