Transpiler.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. * @three_import import Transpiler from 'three/addons/transpiler/Transpiler.js';
  9. */
  10. class Transpiler {
  11. /**
  12. * Constructs a new transpiler.
  13. *
  14. * @param {GLSLDecoder} decoder - The GLSL decoder.
  15. * @param {TSLEncoder} encoder - The TSL encoder.
  16. */
  17. constructor( decoder, encoder ) {
  18. /**
  19. * The GLSL decoder. This component parse GLSL and produces
  20. * a language-independent AST for further processing.
  21. *
  22. * @type {GLSLDecoder}
  23. */
  24. this.decoder = decoder;
  25. /**
  26. * The TSL encoder. It takes the AST and emits TSL code.
  27. *
  28. * @type {TSLEncoder}
  29. */
  30. this.encoder = encoder;
  31. }
  32. /**
  33. * Parses the given GLSL source and returns TSL syntax.
  34. *
  35. * @param {string} source - The GLSL source.
  36. * @return {string} The TSL code.
  37. */
  38. parse( source ) {
  39. return this.encoder.emit( this.decoder.parse( source ) );
  40. }
  41. }
  42. export default Transpiler;