2
$\begingroup$

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?

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

1 Answer 1

1
$\begingroup$

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).

$\endgroup$
2
  • $\begingroup$ Thanks for the answer! Unfortunately getFragmentProg() and getVertexProg() return blank strings for me.. $\endgroup$
    – gandalf3
    Commented Aug 7, 2016 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
    Commented Aug 7, 2016 at 22:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .