# Shader blending for 2 cameras # Help appreciated !

Hi guys,
I was wondering if a local Panda guru could provide some help on this one?
Many thanks!

So I have a scene observed by the main camera and displayed on the full window
Then I add two additional cameras: cam1 and cam2

From time to time I want to be able to switch from the main camera image to a special effect composite image built with cam1 and cam2

# Create an offscreen buffer to get Camera1's view
cam1Buffer=base.win.makeTextureBuffer('cam1Buf',W,H) 
cam1Buffer.setClearColor(VBase4(0, 0, 0, 0))
cam1texture = cam1Buffer.getTexture()
cam1=base.makeCamera(cam1Buffer,sort=-100,scene=render)

# ...same for cam2

# then set up blending filter
manager = FilterManager(base.win, base.cam)  #get filter manager
tex = Texture()                              # intermediate texture
quad = manager.renderSceneInto(colortex=tex) # 
quad.setShader(Shader.load("blendEffect.sha")) # load shader
quad.setShaderInput("tex",cam1texture)        # input camera1
quad.setShaderInput("tex_1",cam2texture)      # input camera2

The blending is performed by blendEffect.sha a Cg shader, here is the simplified code:

//Cg
void vshader(
    float4 vtx_position : POSITION,
    float2 vtx_texcoord0 : TEXCOORD0,
    out float4 l_position : POSITION,
    out float2 l_texcoord0 : TEXCOORD0,
    uniform float4 texpad_tex,
    uniform float4x4 mat_modelproj)
{
    l_position  = mul(mat_modelproj, vtx_position);
    l_texcoord0 = vtx_position.xz * texpad_tex.xy + texpad_tex.xy;
}
 
void fshader(float2 l_texcoord0 : TEXCOORD0,
             out float4 o_color : COLOR,
             uniform sampler2D tex_0 : TEXUNIT0,
             uniform sampler2D tex_1 : TEXUNIT1 ) 
{
    float4 A = tex2D(tex_0, l_texcoord0);
    float4 B = tex2D(tex_1, l_texcoord0);
 	o_color = float4( (A.x+B.x)/2 , B.y, (A.z + B.Z)*A.y, 0.5);
}

What happens is that the shader seems to ignore one of the two textures… so the blending is not performing correctly.

Any hints, thanks!

This is incorrect:

quad.setShaderInput("tex",cam1texture)        # input camera1 
quad.setShaderInput("tex_1",cam2texture)      # input camera2 

Because you refer to them as tex_0 and tex_1, you should pass them using setTexture with multiple TextureStages…
Or you should switch to using setShaderInput to pass them, and use different names.

Hi rdb,

Dank je wel for your guidance.

Actually I did manage yesterday to solve this by simply changing names in the shader. ie merely replacing tex by k_tex… and prefixing by “k_” the other texture reference…

It does work by I’m sure I didn’t understand at all the logic of name passing convention, even reading several times the doc ! (can’t find where there is a clear explanation)

quad.setShaderInput("tex",cam1texture)        
quad.setShaderInput("tex_1",cam2rtexture) 

then:

void fshader(float2 l_texcoord0 : TEXCOORD0,
             out float4 o_color : COLOR,
             uniform sampler2D k_tex : TEXUNIT0,
             uniform sampler2D k_tex_1 : TEXUNIT1
)