Different shaders for individual Viewports

Hi, I am new on this page and I am so happy to work with BGE, but I have got a question about it. Can I have got two different shaders on two different viewports (Cameras).I want to make something like screen space reflections with 6 viewports and I want to cull some faces, which is displayed in other viewports. Is it possible? I worked with UPBGE too, but I do not know how to do that(I tried to do that with planar reflections, because it runs faster than my render to texture ,but I do not know how to set shader for viewport of planar reflection). On my GTX 1070 7 reflection planes with 1024 x 1024 and 900000 verts runs more than 300 fps without V-sync.
Thanks for your ansvers.

P.S.:Sorry, my English is on low level.

well yes, you can…but it would take some crazy witchcraft to produce reflections from an overlay scene since it would only generate reflections from that specific scene…but I’m sure you can also use multiple cameras with either different clipping ranges or some sort of blender mode…I’m no GURU here…hopefully someone else better versed in this can drop by and correct me…and even better, give ‘good’ advice :slight_smile:

the only advice I have for you is to look at using ‘scene’ logic brick to a camera

Thanks for answer. I looked on the logic api, but camera has no attribute getShader() which is used with materials. I hope, that is another way to set the shader. Here is the example of minimal shader which I tried to set to camera.

import bge

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner

VertexShader = “”"
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
“”"

FragmentShader = “”"
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
“”"

for camera in scene.cameras:
shader = camera.getShader()
if shader != None:
if not shader.isValid():
shader.setSource(VertexShader, FragmentShader, 1)

but I have got this error:

Blender Game Engine Started
Python script error - object ‘Camera’, controller ‘Python’:
Traceback (most recent call last):
File “Text”, line 32, in <module>
AttributeError: ‘KX_Camera’ object has no attribute ‘getShader’
Blender Game Engine Finished

here are the links of logic:

https://docs.blender.org/api/blender_python_api_2_76_1/bge.types.KX_Camera.html#bge.types.KX_Camera
https://docs.blender.org/api/blender_python_api_2_76_9/bge.types.KX_BlenderMaterial.html
https://docs.blender.org/api/blender_python_api_2_76_9/bge.types.BL_Shader.html#bge.types.BL_Shader

and here is some similar problem, but with unity:

OK, Finally I have got it. Yes, It is possible, but porobably slow. You can differentiate viewports by their position. Here is my example.(Tried on upbge 0.1.8. I do not know if it works on BFBGE)



from bge import logic, types 
cont = logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()
cam = scene.objects['Camera.001'].worldPosition.copy() # camera name


VertexShader = """


void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""
FragmentShader = """
uniform vec3 Pos;
uniform vec3 cam;


void main()
{
    vec3 nol = (normalize(Pos));
    vec3 fol = (normalize(cam));
    
    if(nol.x -fol.x &lt; 0.1 && nol.x -fol.x &gt; -0.1 && nol.y -fol.y &lt; 0.1 && nol.y -fol.y &gt; -0.1 && nol.z -fol.z &lt; 0.1 && nol.z -fol.z &gt; -0.1){
    gl_FragColor.rgb = vec3(1);
    gl_FragColor.a = 1.0;
    }
    
    else{
    gl_FragColor.rgb = vec3(0.5,0.3,0.1);
    gl_FragColor.a = 1.0;
    }
    
}
"""
mesh = cont.owner.meshes[0]
for material in mesh.materials:
    shader = material.getShader()
    if shader != None:
        if not shader.isValid():
            shader.setSource(VertexShader, FragmentShader, True)
        else:
            shader.setUniformDef('Pos', logic.CAM_POS)
            shader.setUniformfv('cam', cam)
    else:
        if shader.isValid():
            shader.delSource()

You can try it, rename the camera name to yours camera name and it will work, but pay attention, if you are writing own script, camera position must be dynamic update uniform. And remember it is only an example. It has got some bugs.

P.S.:If you heve got better solution for vector comparing, please reply.