Forgot to mention, I am not using the shader generator, since I have custom shaders.
Maybe this will help to illustrate, the panda should cast onto all of the planes, but the planes should not cast onto each other. I can’t remove/mask out the planes from the depth texture since they need to be there to receive shadow, is that right?
test.py
from pandac.PandaModules import *
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.actor import Actor
class World(DirectObject):
def __init__(self):
self.accept('escape', __import__('sys').exit)
# shadowmap
depthmap = Texture()
depthmap.setFormat(Texture.FDepthComponent)
depthmap.setMinfilter(Texture.FTShadow)
depthmap.setMagfilter(Texture.FTShadow)
buffer = base.win.makeTextureBuffer('depthmap', 1024, 1024, depthmap)
self.light = base.makeCamera(buffer)
self.light.reparentTo(render)
self.light.node().getLens().setFov(20)
self.light.node().getLens().setNearFar(0.1, 100)
self.light.setPos(-150, -150, 150)
self.light.lookAt(render)
self.light.setScale(3.0) # scale according to scene size
# shaders
self.shadow_shader = Shader.load('assets/shaders/shadow.cg')
#self.basic_shader = Shader.load('assets/shaders/basic.cg')
render.setShaderInput('light', self.light)
render.setShaderInput('depthmap', depthmap)
#render.setShaderInput('cutout', cutout)
# scene
render.setTransparency(TransparencyAttrib.MNone, 1)
floorTex=loader.loadTexture('maps/envir-ground.jpg')
for i in range(0, 3):
cm = CardMaker('')
cm.setFrame(-1, 1, -1, 1)
floor = render.attachNewNode(cm.generate())
floor.setScale(i * 10 + 5)
floor.setPos(-i)
floor.setP(-90)
floor.setTexture(floorTex)
floor.setShader(self.shadow_shader)
model = Actor.Actor('panda-model', {'walk': 'panda-walk4'})
model.setScale(0.01)
model.actorInterval('walk').loop()
model.reparentTo(render)
#render.setShader(self.basic_shader)
World()
run()
shadow.cg
//Cg
void vshader(float4 vtx_position : POSITION,
float2 vtx_texcoord0: TEXCOORD0,
float2 vtx_texcoord1: TEXCOORD1,
uniform float4x4 trans_model_to_clip_of_light,
uniform float4x4 mat_modelproj,
out float4 l_position : POSITION,
out float2 l_texcoord0 : TEXCOORD0,
out float2 l_texcoord1 : TEXCOORD1,
out float4 l_texcoord2 : TEXCOORD2)
{
float bias_value = -0.000001;
float4x4 bias_matrix = {
0.5f, 0.0f, 0.0f, 0.5,
0.0f, 0.5f, 0.0f, 0.5,
0.0f, 0.0f, 0.5f, 0.5 + bias_value,
0.0f, 0.0f, 0.0f, 1.0f};
l_position = mul(mat_modelproj, vtx_position);
float4x4 tex_matrix = mul(bias_matrix, trans_model_to_clip_of_light); // transformation to the light's clip space
l_texcoord0 = vtx_texcoord0;
l_texcoord1 = vtx_texcoord1;
l_texcoord2 = mul(tex_matrix, vtx_position);
}
void fshader(float2 l_texcoord0: TEXCOORD0,
float2 l_texcoord1: TEXCOORD1,
float4 l_texcoord2: TEXCOORD2,
uniform sampler2D tex_0: TEXUNIT0,
uniform sampler2D tex_1: TEXUNIT1,
uniform sampler2D k_depthmap,
out float4 o_color: COLOR)
{
float4 base0 = tex2D(tex_0, l_texcoord0);
float4 base1 = tex2D(tex_1, l_texcoord1);
float3 shadow_uv = l_texcoord2.xyz / l_texcoord2.w;
float delta = 0.06;
if(shadow_uv.x > delta && shadow_uv.x < 1.0 - delta && shadow_uv.y > delta && shadow_uv.y < 1.0 - delta) {
float shade1 = tex2Dproj(k_depthmap, float4(l_texcoord2.x + delta, l_texcoord2.y, l_texcoord2.z, l_texcoord2.w));
float shade2 = tex2Dproj(k_depthmap, float4(l_texcoord2.x + delta, l_texcoord2.y + delta, l_texcoord2.z, l_texcoord2.w));
float shade3 = tex2Dproj(k_depthmap, float4(l_texcoord2.x + delta, l_texcoord2.y - delta, l_texcoord2.z, l_texcoord2.w));
float shade4 = tex2Dproj(k_depthmap, float4(l_texcoord2.x, l_texcoord2.y + delta, l_texcoord2.z, l_texcoord2.w));
float shade5 = tex2Dproj(k_depthmap, l_texcoord2);
float shade6 = tex2Dproj(k_depthmap, float4(l_texcoord2.x, l_texcoord2.y - delta, l_texcoord2.z, l_texcoord2.w));
float shade7 = tex2Dproj(k_depthmap, float4(l_texcoord2.x - delta, l_texcoord2.y, l_texcoord2.z, l_texcoord2.w));
float shade8 = tex2Dproj(k_depthmap, float4(l_texcoord2.x - delta, l_texcoord2.y + delta, l_texcoord2.z, l_texcoord2.w));
float shade9 = tex2Dproj(k_depthmap, float4(l_texcoord2.x - delta, l_texcoord2.y - delta, l_texcoord2.z, l_texcoord2.w));
float shade = (shade1 + shade2 + shade3 + shade4 + shade5 + shade6 + shade7 + shade8 + shade9) / 9.0;
o_color = base0 * shade;
} else {
o_color = base0;
}
o_color.a = base1.a;
}