ShaderToyDecoder.js 1010 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Return, VariableDeclaration, Accessor } from './AST.js';
  2. import GLSLDecoder from './GLSLDecoder.js';
  3. class ShaderToyDecoder extends GLSLDecoder {
  4. constructor() {
  5. super();
  6. this.addPolyfill( 'iTime', 'float iTime = time;' );
  7. this.addPolyfill( 'iResolution', 'vec2 iResolution = screenSize;' );
  8. this.addPolyfill( 'fragCoord', 'vec3 fragCoord = vec3( screenCoordinate.x, screenSize.y - screenCoordinate.y, screenCoordinate.z );' );
  9. }
  10. parseFunction() {
  11. const node = super.parseFunction();
  12. if ( node.name === 'mainImage' ) {
  13. node.params = []; // remove default parameters
  14. node.type = 'vec4';
  15. node.layout = false; // for now
  16. const fragColor = new Accessor( 'fragColor' );
  17. for ( const subNode of node.body ) {
  18. if ( subNode.isReturn ) {
  19. subNode.value = fragColor;
  20. }
  21. }
  22. node.body.unshift( new VariableDeclaration( 'vec4', 'fragColor' ) );
  23. node.body.push( new Return( fragColor ) );
  24. }
  25. return node;
  26. }
  27. }
  28. export default ShaderToyDecoder;