question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New Pattern for Coding Shaders

See original GitHub issue

@jeromeetienne has posted a gist with an interesting proposal. In it, he proposes a new pattern for coding shaders. I have provided some three.js-specific examples here.

Below, shader1 uses the current pattern; shader2 uses the new pattern. Notice that shader2 avoids the use of quotes and commas. It is also more readable in debug output.

THREE.Shader = {

'shader1' : {

    vertexShader : [

        "#ifdef USE_LIGHTMAP",

            "varying vec2 vUv2;",

        "#endif",

        "varying vec3 vNormal;",

        "void main() {",

            "#ifdef USE_LIGHTMAP",

                "vUv2 = uv2;",

            "#endif",

            "vNormal = normalize( normalMatrix * normal );",

            "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

        "}"

    ].join("\n")

},

'shader2' : {

    vertexShader : ( function() { /*

        #ifdef USE_LIGHTMAP

            varying vec2 vUv2;

        #endif

        varying vec3 vNormal;

        void main() {

            #ifdef USE_LIGHTMAP

                vUv2 = uv2;

            #endif

            vNormal = normalize( normalMatrix * normal );

            gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

        }

    */ } ).toString().split( '\n' ).slice( 1, -1 ).join( '\n' )

}

};

console.log( THREE.Shader[ 'shader1' ].vertexShader );

#ifdef USE_LIGHTMAP
varying vec2 vUv2;
#endif
varying vec3 vNormal;
void main() {
#ifdef USE_LIGHTMAP
vUv2 = uv2;
#endif
vNormal = normalize( normalMatrix * normal );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}

console.log( THREE.Shader[ 'shader2' ].vertexShader );

    #ifdef USE_LIGHTMAP

        varying vec2 vUv2;

    #endif

    varying vec3 vNormal;

    void main() {

        #ifdef USE_LIGHTMAP

            vUv2 = uv2;

        #endif

        vNormal = normalize( normalMatrix * normal );

        gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

    }

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:25 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
mrdoobcommented, Aug 19, 2013

Is there any particular reason why such an approach is not considered?

Saving on network requests?

0reactions
zz85commented, Oct 3, 2013

for those who still pursuing for a easier/different way to write shaders, ShaderDSL could be another interesting approach http://typedarray.org/shaderdsl-js-javascript-based-dsls-for-gpu-shaders/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Patterns - The Book of Shaders
Gentle step-by-step guide through the abstract and complex universe of Fragment Shaders.
Read more >
Shader Basics, Blending & Textures • Shaders for Game Devs ...
Welcome to my three part lecture on shader coding for game devs I hope you'll find this useful in your game dev journey!...
Read more >
Unity Shader Coding for Noobs! - YouTube
In this video we'll go over the basics of shader coding in Unity. If you have always wanted to know how shaders work...
Read more >
Writing Unity URP Code Shaders Tutorial [1/9] ✔️ 2021.3
The Graphics Pipeline and You | Writing Unity URP Code Shaders Tutorial [1/9] ✔️ 2021.3 · Comments • 107.
Read more >
Shader Coding: Making a starfield - Part 1 - YouTube
Shader Coding : Making a starfield - Part 2 · Smoothstep: The most useful function · ShaderToy FX Coding : Twisted Toroid ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found