2
$\begingroup$

I'm a noob and probably this is a completely silly question... Started using BGE two days ago...

I'm using GLSL to visualize a distance field while running in standalone player. I'm doing it using Vertex program and Shader program on the material of all mesh in the scene (all object with a mesh in fact). The shader program is applied on key pressed:

enter image description here

The transformation is performed through this module:

import bgl
import bge

vertex_shader = """
varying double DEPTH;

uniform double FARPLANE = 14.0; shader

void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    DEPTH = gl_Position.z / FARPLANE ; 
}
"""
fragment_shader = """
varying double DEPTH;

void main() {
    // far things appear white, near things black
    float val = (1-DEPTH) * 2.35;
    gl_FragColor.rgb = vec3(val, val, val);
}
"""
# Search all object with a mesh (only one mesh per object) and
# applies the code in the previous line
def enable_depth():
    scene = bge.logic.getCurrentScene()
    for object in scene.objects:
        if object.meshes:
            for mat in object.meshes[0].materials:
                shader = mat.getShader()
                if shader != None:
                    if not shader.isValid():
                        shader.setSource(vertex_shader, fragment_shader, 1)


vertex_reset = """
varying vec4 color;
void main() {
  color = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""
fragment_reset = """
varying vec4 color;
void main() { gl_FragColor = color; }
"""
# this should reset the shader... I've done different test, reported
# in the last lines of this function
def disable_depth():
    scene = bge.logic.getCurrentScene()
    for object in scene.objects:
        if object.meshes:
            for mat in object.meshes[0].materials:
                shader = mat.getShader()
                if shader != None:
                    if not shader.isValid():
                        #shader.delSource()
                        #shader.setSource(vertex_reset, fragment_reset, 1)
                        #shader.setSource(vertex_shader, fragment_shader, 0)

How can I go back with this transformation? (I mean, from right to left) I tried with some sort of vertex/fragment program (report in code) but simply does not work and I am sure this is not the right way. Maybe I should have two materials for each object? But if a mesh has two materials, how do I change the active material in python? And finally, am I asking the right questions? :)

P.S. Only Blender Game Editor, no Blender Render!

$\endgroup$
3
  • 1
    $\begingroup$ shader.delSource() works for me. (You might want to skip shader validation check before deletion). ------Side-Note: Be aware you need to evaluate the sensor states. Otherwise this code will run on key press and on key release (twice). $\endgroup$
    – Monster
    Commented Mar 17, 2016 at 5:41
  • $\begingroup$ It is absolutely correct Sir. Thank you. Could you write an answer so I can accept it? And thank you... The problem was that I have to skip isValid() line. With that, it does not work. $\endgroup$ Commented Mar 17, 2016 at 9:22
  • $\begingroup$ I wasn't sure if that helps you. ... adding the answer $\endgroup$
    – Monster
    Commented Mar 17, 2016 at 11:32

1 Answer 1

0
$\begingroup$

The shader(s) can be disabled/removed with shader.delSource().

$\endgroup$

You must log in to answer this question.

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