Skip to content

noname0310/typescript-glslminify-transformer

Repository files navigation

typescript-glslminify-transformer

minify glsl code in typescript string literal

Install

npm i -D typescript-glslminify-transformer ts-patch

Usage

add following to tsconfig.json

{
    "compilerOptions": {
        "plugins": [ { "transform": "typescript-glslminify-transformer" } ]
    }
}

then use tspc to compile

tspc

if you want use other prefix than /* glsl */, you can pass it as options

{
    "compilerOptions": {
        "plugins": [ { "transform": "typescript-glslminify-transformer", "customPrefixes": ["glsl", "wgsl"] } ]
    }
}

Example

every string literal with /* glsl */ comment will be minified

const glslString = /* glsl */ "gl_FragColor = vec4(vUv, 0.0, 1.0);";

const glslTemplateString = /* glsl */ `
    #define PI 3.141592653589793
    precision mediump float;
    varying vec2 vUv;
    void main () {
        #ifdef SOME_DEFINE
        someFunction();
        #endif
        gl_FragColor = vec4(vUv, 0.0, 1.0);
    }
`;

const glslTemplateStringWithPlaceholders = /* glsl */`
    precision mediump float;
    varying vec2 vUv;
    void main () {
        #ifdef ${"SOME" + "_DEFINE"}
        someFunction();
        #endif
        gl_FragColor = vec4(vUv, ${1}, ${2});
    }
`;

will be transformed to

const glslString = /* glsl */ "gl_FragColor=vec4(vUv,0.0,1.0);";
const glslTemplateString = /* glsl */ "#define PI 3.141592653589793\nprecision mediump float;varying vec2 vUv;void main () {\n#ifdef SOME_DEFINE\nsomeFunction();\n#endif\ngl_FragColor=vec4(vUv,0.0,1.0);}\n";
const glslTemplateStringWithPlaceholders = /* glsl */ `precision mediump float;varying vec2 vUv;void main () {\n#ifdef \n${"SOME" + "_DEFINE"}someFunction();\n#endif\ngl_FragColor=vec4(vUv,${1},${2});}\n`;

About

minify typescript string literal glsl code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published