Monster's manic gadgets - the ShaderConfigurator 1.0

Hi there

you want to apply a Shader to an object within your scene?


You need:

  • a GLSL compatible GPU
  • a Vertex or Fragment Shader
  • The ShaderConfigurator

Wait, you already have a working “Shaderscript”? DO you understand what it does? Do you understand it contains three different parts:

  • VertexShader code,
  • Fragment Shader code and
  • most-likely confusing bge code.

The ShaderConfigurator places the confusing part outside of your custom code.

Basic steps
A)
An instance of the ShaderConfigurator which you assign your Vertex and Fragment shader code to:


from shaderConfigurator import ShaderConfigurator 
...
myShaderConfigurator = ShaderConfigurator( myVertexShader, myFragmentShader )

B)
Now you can apply the shaders to any object you like:


myShaderConfigurator.applyShadersTo( objectToApplyTheShadersTo )

you can use any object you like as long as it has a materiel. You can even use a KX_MeshProxy.

Advanced Step
If you defined some parameters within your shaders, simply add them to your ShaderConfigurator (before applying the shaders)


shaderConfigurator.parameters["myParameter"] = [1.0, 0.5, 0.0, 1.0]
myShaderConfigurator.applyShadersTo( objectToApplyTheShadersTo )

You want to update the provided shader parameters?
Update the parameters in the ShaderConfigurator and apply it again:


shaderConfigurator.parameters["myParameter"] = [5.0, 0.5, 1.0, 1.0]
myShaderConfigurator.applyShadersTo( objectToApplyTheShadersTo )

The ShaderConfigurator will not set the shader again, but it will update the parameters.

Have a look at the demo file.

Attachments

ShaderConfigurator.blend (501 KB)

DefaultVertexShader:


void main() {
	gl_Position = ftransform();
}

this does nothing as far as I can see.

ColorizeOrangeFragmentShader:


void main() {
        gl_FragColor = vec4(1.0, 0.5, 0.0, 1.0);
}

(stolen from this page)

this is hard-coded orange. To parametrize this code, we declare a variable and get the ColorizeFragmentShader:


uniform vec4 color;

void main() {
        gl_FragColor = color;
}

The one and only parameter is “color” which requires a list of 4 floats.

Now we can colorize our objects:


colorizeShaderConfigurator = ShaderConfigurator(DefaultVertexShader, ColorizeFragmentShader)
colorizeShaderConfigurator.parameters["color"] = [1.0, 0.5, 0.0, 1]
shaderConfigurator.applyShadersTo( objectToBeColorized )

From here it is up to you how you calculate the color and when to apply the shader. From here it is “simple” bge python code.

Simple example code for Module mode:


from shaderConfigurator import ShaderConfigurator

colorizerShaderConfigurator =  ShaderConfigurator(
"""
void main() {
	gl_Position = ftransform();
}
"""
,
"""
uniform vec4 color;

void main() {
        gl_FragColor = color;
}
"""
)

ORANGE = [1.0, 0.5, 0.0, 1]

def applyOrange(controller):
    colorizerShaderConfigurator.parameters["color"] = ORANGE
    colorizerShaderConfigurator.applyShadersTo( controller.owner )

to be used with:
Always (no Pulses!) -> Python Module: <yourModuleName>.applyOrange

Simple Example for Script mode:


from shaderConfigurator import ShaderConfigurator
import bge

colorizerShaderConfigurator =  ShaderConfigurator(
"""
void main() {
	gl_Position = ftransform();
}
"""
,
"""
uniform vec4 color;

void main() {
        gl_FragColor = color;
}
"""
)

ORANGE = [1.0, 0.5, 0.0, 1]

colorizerShaderConfigurator.parameters["color"] = ORANGE
colorizerShaderConfigurator.applyShadersTo( bge.logic.getCurrentController().owner )

Always (no Pulses!) -> Python Script: <yourScriptName>

Do not forget you need shaderConfigurator.py next to your blend or as text block inside your blend.

BTW: Are there any Vertex shaders and Fragment shaders out there?

Here are some: https://bitbucket.org/Urfoex/bge-shader/src

Cool! I will try them.