I'd like to make vertices of an object randomly move around a little. To do this, I created a vertex shader.

I assigned the vertex shader to the object's material with shader.setSource() like so:

import bge

cont = bge.logic.getCurrentController()

VertexShader = """

uniform float time;

void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex + sin(time + gl_Vertex.z);


# dummy fragment shader
FragmentShader = """

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


mesh = cont.owner.meshes[0]
for mat in mesh.materials:
    if mesh.getMaterialName(mat.getMaterialIndex()) == "MAleaves":
        shader = mat.getShader()
        if shader != None:
            if not shader.isValid():
                shader.setSource(VertexShader, FragmentShader, 1)
            shader.setUniform1f('time', bge.logic.getRealTime())

However I'd prefer to use the blender material shading instead of the fragment shader. How can I do this?

  • $\begingroup$ Does Blender support geometry shaders? $\endgroup$
    – iKlsR
    Oct 7 '16 at 1:17
  • $\begingroup$ @iKlsR unfortunately no, at least as far as I'm aware. $\endgroup$
    – gandalf3
    Oct 7 '16 at 3:37

While I was digging through the docs one day, I stumbled upon the gpu.export_shader(scene, material) function, which seems to allow exporting the shader code of blenders internal materials. Note that this call eats non BGE types. Here is an example which extracts the code for a simple material on a lovely blue cube (*.blend below)

enter image description here

It should be possible to combine this extracted fragment shader with your own vertex shader (e.g. copy from console and paste in your BGE script).

  • $\begingroup$ Thanks for the answer! Unfortunately getFragmentProg() and getVertexProg() return blank strings for me.. $\endgroup$
    – gandalf3
    Aug 7 '16 at 17:38
  • $\begingroup$ You are right. I was looking for a way to extract Blenders GLSL code one day and found a function which may help you probably. It's not the getFragmentProg() and getVertexProg() as in my initial answer - I edited it accordingly. $\endgroup$
    – AeroLynx
    Aug 7 '16 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.