Transpiler.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * A class that transpiles shader code from one language into another.
  3. *
  4. * `Transpiler` can only be used to convert GLSL into TSL right now. It is intended
  5. * to support developers when they want to migrate their custom materials from the
  6. * current to the new node-based material system.
  7. */
  8. class Transpiler {
  9. /**
  10. * Constructs a new transpiler.
  11. *
  12. * @param {GLSLDecoder} decoder - The GLSL decoder.
  13. * @param {TSLEncoder} encoder - The TSL encoder.
  14. */
  15. constructor( decoder, encoder ) {
  16. /**
  17. * The GLSL decoder. This component parse GLSL and produces
  18. * a language-independent AST for further processing.
  19. *
  20. * @type {GLSLDecoder}
  21. */
  22. this.decoder = decoder;
  23. /**
  24. * The TSL encoder. It takes the AST and emits TSL code.
  25. *
  26. * @type {TSLEncoder}
  27. */
  28. this.encoder = encoder;
  29. }
  30. /**
  31. * Parses the given GLSL source and returns TSL syntax.
  32. *
  33. * @param {string} source - The GLSL source.
  34. * @return {string} The TSL code.
  35. */
  36. parse( source ) {
  37. return this.encoder.emit( this.decoder.parse( source ) );
  38. }
  39. }
  40. export default Transpiler;
粤ICP备19079148号