You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.0 KiB
50 lines
1.0 KiB
6 months ago
|
import { Return, VariableDeclaration, Accessor } from './AST.js';
|
||
|
import GLSLDecoder from './GLSLDecoder.js';
|
||
|
|
||
|
class ShaderToyDecoder extends GLSLDecoder {
|
||
|
|
||
|
constructor() {
|
||
|
|
||
|
super();
|
||
|
|
||
|
this.addPolyfill( 'iTime', 'float iTime = timerGlobal();' );
|
||
|
this.addPolyfill( 'iResolution', 'vec2 iResolution = viewportResolution;' );
|
||
|
this.addPolyfill( 'fragCoord', 'vec3 fragCoord = vec3( viewportCoordinate.x, viewportResolution.y - viewportCoordinate.y, viewportCoordinate.z );' );
|
||
|
|
||
|
}
|
||
|
|
||
|
parseFunction() {
|
||
|
|
||
|
const node = super.parseFunction();
|
||
|
|
||
|
if ( node.name === 'mainImage' ) {
|
||
|
|
||
|
node.params = []; // remove default parameters
|
||
|
node.type = 'vec4';
|
||
|
node.layout = false; // for now
|
||
|
|
||
|
const fragColor = new Accessor( 'fragColor' );
|
||
|
|
||
|
for ( const subNode of node.body ) {
|
||
|
|
||
|
if ( subNode.isReturn ) {
|
||
|
|
||
|
subNode.value = fragColor;
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
node.body.unshift( new VariableDeclaration( 'vec4', 'fragColor' ) );
|
||
|
node.body.push( new Return( fragColor ) );
|
||
|
|
||
|
}
|
||
|
|
||
|
return node;
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
export default ShaderToyDecoder;
|