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.

All in one shader code

See original GitHub issue

An additional shader property on ShaderMaterial that allows vertexs and fragment shaders in one code with a shared header would be useful. Declaring varyings or uniforms twice isn’t needed this way. Basically the property just parses the code and applies the properties to the ShaderMaterial. This way constants/defines, side, wireframe and other properties of ShaderMaterial could be declared as json too.

Additionally it could grab global uniforms stored in THREE.globals or similar.

The AIO shader code could look like this:


varying vec2 vUv;

@ properties {
	"side": "doubleSide"
}


@ vertex {

	uniform float uTime;

	void main() {

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

	}


}

@ fragment {

	uniform sampler2D map;

	void main() {

		gl_FragColor = texture2D(map, vUv);

	}

}

Shader property pseudo code: `

THREE.globals = {};

Object.assign(THREE.ShaderMaterial.prototype, {

    shaderCode: '',

    get shader() {

        return this.shaderCode

    },

    set shader( value ) {

        this.shaderCode = value;


        var name, type, uniform, match;


        // Parse uniforms

        var R_UNIFORMS = /uniform.([^ |\[]+).([A-z][^ |;|\[]+)/g;
        var R_SECTORS = /@*([a-z]+).*\{([^@]+)}/g;

        while ( match = R_UNIFORMS.exec(value) ) {

            if ( match.length == 3 ) {

                type = match[1];
                name = match[2];

                if ( THREE.globals[name] !== undefined ) {

                    this.uniforms[name] = THREE.globals[name];

                }



            }

        }




        // Header

        var header = '';

        match = /([^@]+)@/.exec(value);

        if ( match ) {

            header = match[1];

        }



        // Sectors

        match = /@([a-z]+).*.\{([^@]+)\}/g.exec(value);

        while ( match = R_SECTORS.exec(value) ) {

            switch ( match[1] ) {
                case 'vertex':

                    this.vertexShader = header + match[2];

                    break;
                case 'fragment':

                    this.fragmentShader = header + match[2];

                    break;
                case 'defines':

                    const defines = JSON.parse(match[2]);

                    for ( var name in defines ) {

                        this.defines[name] = defines[name];

                    }

                    break;
            }

        }


    }

});`

Most engines use a custom extended shader language with preprocessing for similar tasks, THREE also already does some preprocessing. It’s just easier to work with.

It might cause syntax errors in some fancy editors when using glsl though, so it would require an adpation for that purpose. In my case i use Notepad++ for shaders with C++ highlighter, it already doesn’t error markup anything and bracketfolding works. I also use a Ace based editor, which can be extended with the regex rules.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mrdoobcommented, Aug 1, 2017

I think you should be able to extend RawShaderMaterial and use onBeforeCompile()?

0reactions
Mugen87commented, Jan 5, 2020

Closing. The additional benefits do not justify the additional complexity in ShaderMaterial.

“Smart” approaches are not always better. They often complicate things and lead to unexpected behaviors/side effects. Better to keep things simple and explicitly define vertex and fragment shader on its own.

Read more comments on GitHub >

github_iconTop Results From Across the Web

All In 1 Sprite Shader - VFX - Unity Asset Store
Add depth to your next project with All In 1 Sprite Shader from Seaside Studios. Find this & more VFX Shaders on the...
Read more >
All In 1 Sprite Shader for Unity - Demo by GeriBP - Itch.io
All In 1 Sprite Shader is an all in one Unity solution to include cool popular sprite and UI effects to your project...
Read more >
Unity-Built-in-Shaders/Sprites-Diffuse.shader at master - GitHub
Unity Built in Shaders. Contribute to TwoTailsGames/Unity-Built-in-Shaders development by creating an account on GitHub.
Read more >
All In 1 Sprite Shader - Free Download - Unity Asset Collection
All In 1 Sprite Shader is an all in one solution to add cool popular sprite and UI effects to your project in...
Read more >
Writing Surface Shaders - Unity - Manual
See in Glossary (forward and deferred rendering), and the shader should somehow handle all that complexity. Surface Shaders is a code generation approach...
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