Dynamnic Shadow for custom shader possible?

In Blender there are three ways to generate a shadow texture.

  1. Using the video texture module.
  2. Render a own into a framebuffer.
  3. Use the existing one.

The problem with your code is that you are generating a new texture and assign it to exiting shadow texture.
So you will overwrite the exiting texture and the other objects will be rendered without a correct shadow texture.

Here is the modified example using method 3. But there is still a problem. I think one of the matrices is wrong or the shader is wrong.
Because I need to invert the X scaling of the lamp to get the shader working.

Woow thats perfect! Thank you :slight_smile:

I think the matrices/ the shader should be fixable problem in contrast to the hard work for the shadowmap.

I fixed and simplified the shader. The frustum should work now, but still need a negative x scaling on the light.
The download link should be updated. Just download the file again.

@HG1: Thanks for your simplification

I just tested your file and it doesn’t work for me. I’ll take a look tomorrow to see if I can do something. If we could write something which would work for any types of lamps, it would be very cool. But we need a matrix (or a glsl) master to do find the correct formulas :slight_smile: Happy christmas!

@HG1: Hi, to make it work on my computer (GTX970), I have to add this line:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);

to your code.

https://drive.google.com/file/d/0B3GouQIyoCmrSHZUQmhib01INDA/view?usp=sharing

And I have no problem with X/Y axis…

EDIT: https://developer.blender.org/D1690

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);

This will overwrite the original texture. The reason why it works is because you have two lamps (lamp, lamp.001) on the same position.
So you overwrite the comparison mode for currently bound depth textures of the lamp, and because of your break in the loop the second shadow map will be untouched. So blender render the objects with the second light.
Uncomment “//gl_FragColor = texture2D(shadowMap, shadowCoord.st);” to see if the texture is bound correctly.
You should see very tiny scaled shadow on the floor. There are only some pixels.

And I have no problem with X/Y axis…

To get it working I have inverted the x axis scaling (0.5 to -0.5) of the spotlight.
I don’t have to inverted the y or z axis.

I have a GTX 460. Driver version 340.52.

Meanwhile I added a orthographic frustum matrix for direction light’s like a sun.
I updated the download link.

@HG1: what I meant is that with your code and without the line: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); , I get this result: http://www.pasteall.org/pic/96865

If I add the line glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); , I get this result:

Uncomment “//gl_FragColor = texture2D(shadowMap, shadowCoord.st);” to see if the texture is bound correctly.

If I do that just after, I’ve got nothing but white on the plane’s texture.

BUT, if in the same session, I remove glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
the shadows work… Weird.

If I added a second lamp, this is because in any case, with only one lamp, suzannes wer’e rendered black.

The break in my code seems to not change something.

Meanwhile I added a orthographic frustum matrix for direction light’s like a sun.

Very cool! I’ll test it :slight_smile: And Thanks for my patch review!

To access directly the depth texture without looping throught all 31 textures, I’ll see if I can do a second patch to access the bindocode from python… I’ll search GL_DEPTH_COMPONENT keyword in the sources to see.

I’m fairly new to glsl and haven’t touch on any opengl, but I’m curious, will this method to access shadows allow us to modify the shadow texture and make cascading shadow map that Kupoman was working before possible?

@guramarx: I don’t know what are cascading shadows, but for me, the main goal was to have the possibility to access the scene (lamp) shadows when you write a material GLSL shader. Before, each time you wrote a glsl material (example: moving grass material), you could have light on it, diffuse, specular… but you couldn’t have the shadows generated by lights. With this, you access create fun GLSL materials with lights AND shadows. So you could create a nice moving grass with shadows :stuck_out_tongue:

@HG1: what I meant is that with your code and without the line: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); , I get this result: http://www.pasteall.org/pic/96865

The white field looks good to me. Because that means that some kind of texture is bound to the shader.
So maybe a wring texture is bound to the shader or the texture coordinates are wrong.
Try to play with the shader and also with the x scale of the lamp and try to use shadowCoord.z instead of viewDepthFragment.

Uncomment “//gl_FragColor = texture2D(shadowMap, shadowCoord.st);” to see if the texture is bound correctly. If I do that just after, I’ve got nothing but white on the plane’s texture.

This is how it look like for me. For a sun it renders normal.


If I added a second lamp, this is because in any case, with only one lamp, suzannes wer’e rendered black.

Yes that is exactly he scenario that I has described in my last post. With glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE) you will change the texture comparison mode for currently bound depth textures. So everything turns black. If you add a second lamp this lamp will also render a second shadow map. The first will turn all into black, and the second will render everything correct because you only changed the glTexParameteri() from the first one.

I have updated the file. I merged the two scripts into one. I also added some deactivated code to the shader were you can play with it.

Everything is allright for me with your .blend when I replace sampler2D with sampler2Dshadow and float depth = shadow2DProj(shadowMap, shadowCoord).r with float depth = texture2D(shadowMap, shadowCoord.xy / shadowCoord.w).r in the shader:
http://www.pasteall.org/blend/39807
http://www.pasteall.org/pic/96894
http://www.pasteall.org/pic/96895

Great! Thanks HG1 :slight_smile: That’s a great ressource… Weird that it doesn’t work the same for all people.
On my computer (gtx 970, last nvidia drivers, windows 10 64), to make the things work correctly, I have two options:

  1. Use glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE) and 2 lights and sampler2D in the shader.
  2. Don’t use glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE), use only one light and sampler2Dshadow in the shader.

Using sampler2Dshadow doesn’t work on all computers? If it doesn’t, we would have to understand in what case we have to use sampler2Dshadow and in what case we have to use sampler2D, and find a way, according to each computer specs, define the method to use in the shader? (you already know the answer because in your shader you put the 2 options?)

EDIT: What do you think about this design simplification: http://www.pasteall.org/blend/39808 ?
(I thought it would be easier for the user to have 2 scripts… The GLSL shader alone where he can directly write in and bind its own uniforms. He has also only to import the light. Some more simplifications could be done…)

Using sampler2Dshadow doesn’t work on all computers?

Where do you get this?
Sampler2Dshadow and vec4 shadow2DProj(sampler2DShadowsampler, vec4 coord [, float bias]) was introduces in GLSL 1.10 so it should work with most of the graphic cards out there.

http://web.eecs.umich.edu/~sugih/courses/eecs487/common/notes/APITables.xml
So if shadow2DProj is not working then the graphic card is very old or there is a driver bug (some intel or old ATI / Nvidia cards) or the peoples are using the driver (mostly only GLSL 1.00 support) which comes with the OS.

You can also try texture2DProj instead of shadow2DProj because Blender internal use it for shadowing.

EDIT: What do you think about this design simplification: http://www.pasteall.org/blend/39808 ?

I personally like it.
But I think for the most users (non programmer) it is more complicated to handle to scripts. The don’t know that they have to copy both to get it working. Also the most users are not able to program her own shades.
But for developing I will use your version. Because It is easier to work wit it.

I updated your version. Because you don’t updated the camera to lamp matrix every frame. This is necessary if the camera is moving or rotating. Also now the scale-transform and the projections matrix will be calculated now only one time.

ut I think for the most users (non programmer) it is more complicated to handle to scripts. The don’t know that they have to copy both to get it working. Also the most users are not able to program her own shades.

@HG1: But I thought this ressource will be usefull only for people who write their own GLSL shaders, no?
And the two scripts would be automatically appended when importing the light so the user is not supposed to know that there are two scripts. He just has to use shader.py… The other script is only an external module.

I updated your version. Because you don’t updated the camera to lamp matrix every frame. This is necessary if the camera is moving or rotating. Also now the scale-transform and the projections matrix will be calculated now only one time.

Thanks!

I implemented the orginal blender simple shadow shader. Now the rect cippling is correct.
I alos got variance shadows working.

Cool! So what we need now from blender (in addition of clipstart, clipend, and frustum matrix) is bleed bias, bias, buffer type and bindcodes for GL_DEPTH_COMPONENT (simple buffer) and RG32F (variance buffer) :slight_smile: bleed bias, bias and buffer type are not a problem but I don’t know how to get bincodes…

(To make an unique shadow.py script and an unique shader.py script that takes into account all lamp types and all shadow parameters)

I’m late to this, and unfortunately won’t be able to test the blends here any soon. So, is this like custom buffer shadows using camera render to textures? Does it have the same performance impact as built-in shadows unfiltered? Does does this mean that custom filtering for shadows within GLSL is possible now?