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:
- Created 10 years ago
- Comments:25 (21 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Saving on network requests?
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/