Shader-less Shadows

Return to Panda Features in Development

Shader-less Shadows

Postby Sothh » Fri Mar 18, 2011 5:32 pm

Here is a shadow demo that uses no shaders (except the built in screen blur), renders very fast, and gives soft shadows.

Image

http://www.mediafire.com/?5rdwlfudc2gigsw

It works like this:

The scene is rendered without any lighting and textures and materials turned off by a camera placed at the lights position and pointed at the center of the screen onto a "Shadow" buffer. The buffer has a black background, and objects are rendered solid white. The ground is not rendered. The buffer is blurred using CommonFilters, then its texture is captured and projected back onto the scene from the cameras position and rotation.

This projected texture is applied using the blend mode, which makes anything white look dark on the ground, and black pixels are see through.
Last edited by Sothh on Sat Mar 19, 2011 3:14 pm, edited 4 times in total.
Sothh
 
Posts: 235
Joined: Sat Oct 24, 2009 10:33 am
Location: Virginia, USA

Postby treeform » Fri Mar 18, 2011 5:45 pm

But that will not create shadows. That will just illuminate how far the first pixel is from the light source. You cant blurry without shaders.

But you can do shadows without shaders using stencil shadows, but they are quite slow and require special closed hull models from your art team. And the math to get them to work is insane! Doom3 uses this shadow type.
User avatar
treeform
 
Posts: 2106
Joined: Sat May 05, 2007 5:15 pm
Location: SF, CA

Postby drwr » Fri Mar 18, 2011 5:48 pm

It's that blurring step that's going to kill you. If you're not using a shader, that means you have to blur the texture on the CPU, which means you have to copy the texture RAM from graphics memory to main memory and back again, and that double-copy is death to your frame rate. Plus the CPU isn't as good at that kind of processing as the GPU is, so you get a double hit.

It would probably be better to use an on-card approach to soft shadows, rendering multiple different (slightly offset) views from the light onto the same texture, blending them sensibly. But this means multiple rendering passes over the scene, which can again be a killer if your scene is complex.

David
drwr
 
Posts: 11425
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby drwr » Fri Mar 18, 2011 5:50 pm

But that will not create shadows. That will just illuminate how far the first pixel is from the light source.

You can certainly use this technique to create shadows without using a shader, if you're content with hard-edged (and slightly pixelly) shadows. The code in direct/src/showbase/ShadowDemo.py demonstrates precisely this.

David
drwr
 
Posts: 11425
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby Sothh » Fri Mar 18, 2011 6:01 pm

I am doing the blurring with CommonFilters, which though uses shaders, it does not need any custom code.

How can I set the the shadow camera to render all the models as white?
Sothh
 
Posts: 235
Joined: Sat Oct 24, 2009 10:33 am
Location: Virginia, USA

Postby Anon » Sat Mar 19, 2011 12:00 am

this wont work with the shader generator.
Good idea though, might use it in the future.
Anon
 
Posts: 1557
Joined: Thu Oct 29, 2009 3:07 am

Postby rdb » Sat Mar 19, 2011 1:44 am

Set an initial state on the shadow camera with a white colour attribute and a high priority value.
rdb
 
Posts: 9460
Joined: Mon Dec 04, 2006 5:58 am
Location: Netherlands

Postby Sothh » Sat Mar 19, 2011 6:53 am

rdb wrote:Set an initial state on the shadow camera with a white colour attribute and a high priority value.



How do I set priority?

Its looking really good so far.
Sothh
 
Posts: 235
Joined: Sat Oct 24, 2009 10:33 am
Location: Virginia, USA

Postby drwr » Sat Mar 19, 2011 9:32 am

See the aforementioned ShadowDemo.py, which as I said, does exactly this.

David
drwr
 
Posts: 11425
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby Sothh » Sat Mar 19, 2011 2:53 pm

Okay, I have finished a working demo. Its attached to the first post.

(And the ShadowDemo.py is really old, and some things don't work in it.)[/img]
Sothh
 
Posts: 235
Joined: Sat Oct 24, 2009 10:33 am
Location: Virginia, USA

Postby wezu » Sun Mar 20, 2011 6:54 am

That works really good. I've got ~30fps on my really, really bad machine, and with a bit of tweaking I got it to run at ~60fps (so at top speed). I've set the buffer size 256x256 and setBlurSharpen(0.0). The shadows are more blurry (softer) and it runs faster.

Image
I may be totally wrong, cause I'm a dancin' fool.

NameError: global name 'elf' is not defined
User avatar
wezu
 
Posts: 715
Joined: Tue May 19, 2009 1:03 pm

Re: Shader-less Shadows

Postby Bradamante » Sun Mar 20, 2011 8:10 am

I have no idea why people in this thread were so sceptic. It works perfectly. Besides the usual error messages on the console it looks perfect, just with low FPS (9-10). Playing with the buffer size or blurSharpen doesnt improve things.

Mac OS X.6.6.

Sothh wrote:Here is a shadow demo that uses no shaders (except the built in screen blur), renders very fast, and gives soft shadows.

Image

http://www.mediafire.com/?5rdwlfudc2gigsw

It works like this:

The scene is rendered without any lighting and textures and materials turned off by a camera placed at the lights position and pointed at the center of the screen onto a "Shadow" buffer. The buffer has a black background, and objects are rendered solid white. The ground is not rendered. The buffer is blurred using CommonFilters, then its texture is captured and projected back onto the scene from the cameras position and rotation.

This projected texture is applied using the blend mode, which makes anything white look dark on the ground, and black pixels are see through.
CustoMac (2013), Mac OS X.9 - MacBookPro (2007), Mac OS X.8.5
@ YouTube
User avatar
Bradamante
 
Posts: 306
Joined: Tue Nov 25, 2008 10:58 am
Location: Leipzig, Germany

Postby drwr » Sun Mar 20, 2011 10:11 am

The original topic was edited after we had posted our suggestions. Our skepticism was directed at the original suggestion to blur the texture without using shaders. The final implementation does use shaders, which is a fine solution.

David
drwr
 
Posts: 11425
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Re: Shader-less Shadows

Postby Anon » Sun Mar 20, 2011 11:07 am

Bradamante wrote:I have no idea why people in this thread were so sceptic. It works perfectly. Besides the usual error messages on the console it looks perfect, just with low FPS (9-10)

Well yeah, very low fps is the issue. Other than that I have no problem with it.
Anon
 
Posts: 1557
Joined: Thu Oct 29, 2009 3:07 am

Postby Bradamante » Sun Mar 20, 2011 12:04 pm

I understand. I thought he didn't? I mean is the blur thing a shader? Because I don't see a .sha file in the demo.

drwr wrote:The original topic was edited after we had posted our suggestions. Our skepticism was directed at the original suggestion to blur the texture without using shaders. The final implementation does use shaders, which is a fine solution.

David
CustoMac (2013), Mac OS X.9 - MacBookPro (2007), Mac OS X.8.5
@ YouTube
User avatar
Bradamante
 
Posts: 306
Joined: Tue Nov 25, 2008 10:58 am
Location: Leipzig, Germany

Postby coppertop » Sun Mar 20, 2011 12:17 pm

It uses common filters, which run on shaders.

On a different note, I actually get amazingly good performance with this code. It tops 700 frames here, and the shadow looks very good. The only problem is that when I uncomment setShaderAuto(), the framerate drops to ~70-80 frames. Can't say I understand where such difference comes from.
User avatar
coppertop
 
Posts: 527
Joined: Sat Apr 18, 2009 5:48 am

Postby Sothh » Sun Mar 20, 2011 2:20 pm

coppertop wrote:It uses common filters, which run on shaders.

On a different note, I actually get amazingly good performance with this code. It tops 700 frames here, and the shadow looks very good. The only problem is that when I uncomment setShaderAuto(), the framerate drops to ~70-80 frames. Can't say I understand where such difference comes from.


I get really good performance as well.

I think there is a bug in Panda that is killing the frame rate when a projected texture is used along with the shader generator.

There is one bug in this code; if you have an model under the ground it will still show up as a shadow. I suggest using a clip plane on the shadow camera to make everything under the ground invisible. This should not be a common problem though, as I see no reason to place a model under the floor.
Sothh
 
Posts: 235
Joined: Sat Oct 24, 2009 10:33 am
Location: Virginia, USA

Postby coppertop » Sun Mar 20, 2011 3:53 pm

This should not be a common problem though, as I see no reason to place a model under the floor.

A building with multiple floors, a dungeon, a cave, an underground train station, sewers... ;)

Still, a clip plane or camera masks should solve that, yeah.
User avatar
coppertop
 
Posts: 527
Joined: Sat Apr 18, 2009 5:48 am

Postby Anon » Mon Mar 21, 2011 1:29 am

Sothh wrote:I think there is a bug in Panda that is killing the frame rate when a projected texture is used along with the shader generator.

Yep.
Last time I encountered this when I wanted to project water caustics on the ocean floor.
And when I wanted to project flashlight.

Too bad because thats some pretty sweet looking shadows.
Anon
 
Posts: 1557
Joined: Thu Oct 29, 2009 3:07 am

Postby drwr » Mon Mar 21, 2011 1:41 am

One way to work around this bug is to save out the generated shader into a file, and then load it explicitly and apply it to your model instead of enabling the auto-shader. You do have to do this separately for each part of your model that has different states, though.

David
drwr
 
Posts: 11425
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby Sothh » Mon Mar 21, 2011 9:23 am

Any idea when a patch will be released?

For multi levels I never actualy use a model with different floors, I simply load a lower or higher floor when the player gets on the elevator, ect.
Sothh
 
Posts: 235
Joined: Sat Oct 24, 2009 10:33 am
Location: Virginia, USA

Postby juce » Mon Mar 21, 2011 11:20 am

looks very nice, but is indeed very slow (7 fps) on my machine.

Mac OSX 10.6, Panda 1.7.1
juce
 
Posts: 23
Joined: Thu Oct 09, 2008 7:21 pm

Postby drwr » Mon Mar 21, 2011 11:36 am

Any idea when a patch will be released?

Probably not until Panda3D 2.0, unfortunately (which is in the works). This is actually a difficult bug to fix with the current design.

David
drwr
 
Posts: 11425
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby Anon » Tue Mar 22, 2011 12:49 am

Well, from the development speed of panda3d, it could be a long time, so you should try to assign the shader yourself.

I was thinking of some method like disableShaderUpdate() which would do a very hacky thing: dump the shader itself and assign it to the node.

We could do it ourselves, but we would need to set all the shader imputs, which would still require knowledge of shaders to know what the shader accepts. And I don't even know if shadergenerator stuff defined in the egg file (such as normalMaps) would work at all.
Anon
 
Posts: 1557
Joined: Thu Oct 29, 2009 3:07 am

Postby Anon » Sat Mar 26, 2011 1:48 am

bump

Dumped 2 shaders:
Code: Select all
//Cg
void vshader(float4 vtx_position : POSITION,
 out float4 l_position : POSITION,
 uniform float4 texpad_txcolor,
 uniform float4 texpix_txcolor,
 out float4 l_texcoordC : TEXCOORD0,
 uniform float4 texpad_txblur1,
 out float4 l_texcoordBS : TEXCOORD3,
 uniform float4x4 mat_modelproj)
{
 l_position=mul(mat_modelproj, vtx_position);
 l_texcoordC=(vtx_position.xzxz * texpad_txcolor) + texpad_txcolor;
 l_texcoordBS=(vtx_position.xzxz * texpad_txblur1) + texpad_txblur1;
}
void fshader(
float4 l_texcoordC : TEXCOORD0,
uniform float4 texpix_txcolor,
float4 l_texcoordBS : TEXCOORD3,
uniform float4 k_blurval,
uniform sampler2D k_txcolor,
uniform sampler2D k_txblur0,
uniform sampler2D k_txblur1,
out float4 o_color : COLOR)
{
 o_color = tex2D(k_txcolor, l_texcoordC.xy);
 o_color = lerp(tex2D(k_txblur1, l_texcoordBS.xy), o_color, k_blurval.x);
}

for those of you who know Cg, is this a correct shader file (or maybe corrupt)? So I'll know if its worth setting all the inputs manually.
Hm, looks like there are some blurring functions...

and
Code: Select all
//Cg
void vshader(
    in float4 vtx_texcoord0 : TEXCOORD0,
    out float4 l_texcoord0 : TEXCOORD0,
    uniform float4x4 trans_model_to_world,
    out float4 l_world_position : TEXCOORD1,
    uniform float4x4 trans_model_to_view,
    out float4 l_eye_position : TEXCOORD2,
    uniform float4x4 tpose_view_to_model,
    out float4 l_eye_normal : TEXCOORD3,
    in float4 vtx_normal : TEXCOORD1,
    float4 vtx_position : POSITION,
    out float4 l_position : POSITION,
    uniform float4x4 mat_modelproj
) {
    l_position = mul(mat_modelproj, vtx_position);
    l_world_position = mul(trans_model_to_world, vtx_position);
    l_eye_position = mul(trans_model_to_view, vtx_position);
    l_eye_normal.xyz = mul((float3x3)tpose_view_to_model, vtx_normal.xyz);
    l_eye_normal.w = 0;
}

void fshader(
    in float4 l_world_position : TEXCOORD1,
    in float4 l_eye_position : TEXCOORD2,
    in float4 l_eye_normal : TEXCOORD3,
    uniform sampler2D tex_0,
    uniform float4x4 texmat_0,
    uniform float4 alight_alight0,
    uniform float4x4 dlight_dlight0_rel_view,
    out float4 o_color : COLOR0,
    uniform float4 attr_color,
    uniform float4 attr_colorscale
) {
    float4 result;
    float4 l_texcoord0 = l_world_position;
    l_texcoord0 = mul(texmat_0, l_texcoord0);
    l_texcoord0.xyz /= l_texcoord0.w;
    // Fetch all textures.
    float4 tex0 = tex2D(tex_0, l_texcoord0.xy);
    // Correct the surface normal for interpolation effects
    l_eye_normal.xyz = normalize(l_eye_normal.xyz);
    // Begin view-space light calculations
    float ldist,lattenv,langle;
    float4 lcolor,lspec,lvec,lpoint,latten,ldir,leye,lhalf;    float4 tot_ambient = float4(0,0,0,0);
    float4 tot_diffuse = float4(0,0,0,0);
    // Ambient Light 0
    lcolor = alight_alight0;
    tot_ambient += lcolor;
    // Directional Light 0
    lcolor = dlight_dlight0_rel_view[0];
    lspec  = dlight_dlight0_rel_view[1];
    lvec   = dlight_dlight0_rel_view[2];
    lcolor *= saturate(dot(l_eye_normal.xyz, lvec.xyz));
    tot_diffuse += lcolor;
    // Begin view-space light summation
    result = float4(0,0,0,0);
    result += tot_ambient;
    result += tot_diffuse;
    result = saturate(result);
    // End view-space light calculations
    result.rgb = lerp(result, tex0 * float4(0, 0, 0, 1), tex0.r).rgb;
    result *= attr_colorscale;
    o_color = result * 1.000001;
}
Anon
 
Posts: 1557
Joined: Thu Oct 29, 2009 3:07 am

Postby DangerOnTheRanger » Thu Mar 31, 2011 11:05 pm

Awww, this doesn't work for me. Just like with the Shadow samples...

It loads, but at a very low frame rate (3.5 FPS).
Also, there are no shadows or lighting.

In case it helps, I'm using Panda 1.7.1 on Ubuntu 10.04, with an Intel GM45 GPU.

Anyone else had this problem?
User avatar
DangerOnTheRanger
 
Posts: 240
Joined: Fri Aug 27, 2010 4:59 pm

Postby Sothh » Fri Apr 01, 2011 7:47 am

DangerOnTheRanger wrote:Awww, this doesn't work for me. Just like with the Shadow samples...

It loads, but at a very low frame rate (3.5 FPS).
Also, there are no shadows or lighting.

In case it helps, I'm using Panda 1.7.1 on Ubuntu 10.04, with an Intel GM45 GPU.

Anyone else had this problem?


The problem is your graphics card. I cant believe it will run at all on a GM45.
Sothh
 
Posts: 235
Joined: Sat Oct 24, 2009 10:33 am
Location: Virginia, USA

Postby DangerOnTheRanger » Fri Apr 01, 2011 8:13 am

... :( ... :cry:

Ah well...
Time to get a new PC! :D

On a more serious note, why not?
User avatar
DangerOnTheRanger
 
Posts: 240
Joined: Fri Aug 27, 2010 4:59 pm


Return to Panda Features in Development

Who is online

Users browsing this forum: No registered users and 0 guests