Repository: playcanvas/engine
File: src/extras/gizmo/shaders.js (JavaScript)

Code before:
    return {
        uniqueName: `gizmo-unlit-${depthTest ? 'depth' : 'nodepth'}`,
        attributes: {
            vertex_position: SEMANTIC_POSITION
        },
        vertexGLSL: /* glsl */`
            attribute vec3 vertex_position;
        
            uniform mat4 matrix_model;
            uniform mat4 matrix_viewProjection;
        
            void main(void) {
                gl_Position = matrix_viewProjection * matrix_model * vec4(vertex_position, 1.0);
                gl_Position.z = clamp(gl_Position.z, -abs(gl_Position.w), abs(gl_Position.w));
            }
        `,
        fragmentGLSL: /* glsl */`
            #include "gammaPS"
        
            precision highp float;
        
            uniform vec4 uColor;

            void main(void) {
                gl_FragColor = vec4(gammaCorrectOutput(decodeGamma(uColor)), uColor.w);
                ${depthTest ? '' : 'gl_FragDepth = 0.0;'}
            }
        `,
        vertexWGSL: /* wgsl */`
            attribute vertex_position: vec3f;

            uniform matrix_model: mat4x4f;
            uniform matrix_viewProjection: mat4x4f;

            @vertex
            fn vertexMain(input: VertexInput) -> VertexOutput {
                var output: VertexOutput;
                let pos = vec4f(input.vertex_position, 1.0);
                output.position = uniform.matrix_viewProjection * uniform.matrix_model * pos;
                output.position.z = clamp(output.position.z, -abs(output.position.w), abs(output.position.w));
                return output;
            }
        `,
        fragmentWGSL: /* wgsl */`
            #include "gammaPS"

            uniform uColor: vec4f;

            @fragment
            fn fragmentMain(input: FragmentInput) -> FragmentOutput {
                var output: FragmentOutput;

Review comment (performance):
This works, but I'd prefer to keep the string (shader source code) constant, to avoid generating these larger strings each time unlitShader is called.
I should use fragmentDefines, and define 'DEPTH_TEST' or so.
So in the case you already created this shader, there is minimal string operation taking place - and you'd get shader from internal cache.


Code after:
    attributes: {
        vertex_position: SEMANTIC_POSITION
    },
    vertexGLSL: /* glsl */`
        attribute vec3 vertex_position;
    
        uniform mat4 matrix_model;
        uniform mat4 matrix_viewProjection;
    
        void main(void) {
            gl_Position = matrix_viewProjection * matrix_model * vec4(vertex_position, 1.0);
            gl_Position.z = clamp(gl_Position.z, -abs(gl_Position.w), abs(gl_Position.w));
        }
    `,
    fragmentGLSL: /* glsl */`
        #include "gammaPS"
    
        precision highp float;
    
        uniform vec4 uColor;

        void main(void) {
            gl_FragColor = vec4(gammaCorrectOutput(decodeGamma(uColor)), uColor.w);
            #if DEPTH_WRITE == 0
                gl_FragDepth = 0.0;
            #endif
        }
    `,
    vertexWGSL: /* wgsl */`
        attribute vertex_position: vec3f;

        uniform matrix_model: mat4x4f;
        uniform matrix_viewProjection: mat4x4f;

        @vertex
        fn vertexMain(input: VertexInput) -> VertexOutput {
            var output: VertexOutput;
            let pos = vec4f(input.vertex_position, 1.0);
            output.position = uniform.matrix_viewProjection * uniform.matrix_model * pos;
            output.position.z = clamp(output.position.z, -abs(output.position.w), abs(output.position.w));
            return output;
        }
    `,
    fragmentWGSL: /* wgsl */`
        #include "gammaPS"

        uniform uColor: vec4f;

        @fragment
        fn fragmentMain(input: FragmentInput) -> FragmentOutput {
            var output: FragmentOutput;