How to initialize shader program?

I have tried this simple CustomShader:

class Main extends Sprite {
    public function new () {
	super ();
	var sprite = new Sprite ();
	sprite.graphics.beginFill (0xFF0000);
	sprite.graphics.drawRect (0, 0, 100, 100);
	sprite.filters = [ new ShaderFilter (new CustomShader ()) ];
	addChild (sprite);
       }
    }


class CustomShader extends Shader {

    public function new () {
	glFragmentSource = 
		"
		uniform sampler2D tex0;
                    varying vec3 eyeTS;
                    varying vec2 texCoords;

                    void main( void )
                    {
                            const float plxScale = 0.03;
                            const float plxBias = -0.015;

                            // Iterative parallax mapping
                            vec3 newCoords = vec3( texCoords, 0 );
                            vec3 eye = normalize( eyeTS );
                            for( int i = 0; i < 4; ++i )
                            {
                                    vec4 nmap = texture2D( tex0, texCoords.st );
                                    float height = nmap.a * plxScale + plxBias;
                                    newCoords += (height - newCoords.p) * nmap.z * eye;
                            }

                            vec4 glow = texture2D( tex0, newCoords.st ).rgba;
                            glow *= glow.a;

                            gl_FragColor = glow;
                    }
		
		";
	
	super ();
	
}

But, I got error:

Uncaught Error: Unable to initialize the shader program.

Can some one tell me what’s wrong in my code?

1 Like

For now, you’ll need to have the same variable names as the default shader, here:

https://github.com/openfl/openfl/blob/develop/openfl/display/Shader.hx#L57-L75

I am not sure that we can pass additional information to a custom shader at the moment, but I hope that the filter system will continue to improve and become more robust for features like that

Thanks Singmajesty, as I my big goal is to build my app into HTML5 target, can you please check this link (which needs flash player) to see the glow effects I need to do.

According to you answer, you don’t think that I will be able to generate those glow effects using WebGL in OpenFL currently? right?

1 Like

Has there been any update in how custom shaders are implemented?

Yes, you can do something like this (in the current development builds)

var shader = new CustomShader ();
shader.data.uMyUniform = [ 100 ];

sprite.filters = [ new ShaderFilter (shader) ];
1 Like