ShadowMap Problem

Started by
9 comments, last by B. / 6 years ago

Hello guys,

I tried to draw shadows in my scene with a direction light but in the end result i see the shadow + the view space of my direction light too, (see image).

4 yeahrs ago, a known, that i haven't contact anymore, wrote me a shadowmap shader and the geometry shader for doing this, but never fix this. So i haven't really much knowledge of shadow math and never fix this by my self :(

I also tried to filter the light view space background with, if(shadow > 0.15) shadow = 1.0f (shadow = lightIntensity). But with this filter looks shadows of complexe geometries awful :(

My Bias Value is 0.0001f

Can anyone help me and explain me what is wrong?

Greets

Benjamin

ps. (I uploaded the two HLSL Shaders in the attachment)

01.jpg

ShadowMap.fx

SimpleShader.fx

Advertisement

Hi B. /, I  you can to try Shadows without PCF Filter:


#if 1
 float shadowFactor = ShadowMap.SampleCmpLevelZero(CmpSampler, input.LightPos.xy, lightDepth);
#else
 float shadowFactor = 0;

 // Perform PCF filtering on a 4 x 4 texel neighborhood
 for (float y = -1.5; y <= 1.5; y += 1.0)
 {
  for (float x = -1.5; x <= 1.5; x += 1.0)
  {
   float2 uvOffset = float2(x, y) * (1.0 / ShadowMapSize);
   shadowFactor += ShadowMap.SampleCmpLevelZero(CmpSampler, input.LightPos.xy + uvOffset, lightDepth);
  }
 }
 
 shadowFactor /= 16.0;
#endif

Which result will you receive ?

 

3DGraphics,Direct3D12,Vulkan,OpenCL,Algorithms

Hello Andrey,

you mean instead of PCF Filtering only?:


 float shadowFactor = ShadowMap.SampleCmpLevelZero(CmpSampler, input.LightPos.xy, lightDepth); 

if(shadowFactor != 1.0f)
 shadowFactor = 0.0f;

 or


 float shadowFactor = ShadowMap.SampleCmpLevelZero(CmpSampler, input.LightPos.xy, lightDepth); 

 

Both already tried, nothing change :(

Greets

Benjamin

ps. If i only remove the line:


shadowFactor /= 16.0;

The light view space will be white, instead of black

11 hours ago, B. / said:

you mean instead of PCF Filtering only?

Yes, you should try to replace PCF filter. Ok, I think your function GetShadows should return only shadow Factor.

Why did you callculated light intensity twice ?( in fuction GetShadows and in pixelShader ?

Also your light direction will be received from VertexShader, Why did you callculate light direction in GetShadows?

Final fuction GetShadows:


float GetShadows(Pixel input)
{
  ...
//float3 lightDir = normalize(LightPosition - input.WorldPos); // your light direction will be received from VertexShader.
 //float lightIntensity = dot(input.Normal, lightDir); // light intensity will be calculated in pixelShader

 // Multiply the light intensity with the shadowFactor
 //lightIntensity *= shadowFactor;


// Return the final lightIntensity
// return lightIntensity;
 return shadowFactor; // return shadowFactor only.
}

Try to use this code, If you have incorrect result you can attach project with executable file and shaders,

3DGraphics,Direct3D12,Vulkan,OpenCL,Algorithms

OMG, you right, it works!!!!!!!!!!!!!!!!

I love you Andrey, just kidding xD

Now I see only the shadow of my cube :)

But i think two things are still strange.

The first one is: I normalize input.Normal in the VertexShader and if i don't normalize the normalized value again in the Pixel Shader by float3 Nn = normalize(input.Normal); my lighting result of all geometries will be darker. Why i need to normalize a normalized value again to get right lighting results?

And the second one is: If i rotate my direction light in my scene, my light look direction vector will be rotate right, that i see on two things. my visual light arrow in the scene and the black light view space that i saw befor in the bug, both rotate in the same direction as my direction light. But not the shadow it self. There it's seems to be, only change the position/stretch angle, if i tranlate the light postion and he always looks in the center 0 0 0 of the scene? But i want that the shadow draw in the same direction as my light direction?

Greets

Benjamin

ps. Updated Shader in the attachment

SimpleShader.fx

2 hours ago, B. / said:

The first one is: I normalize input.Normal in the VertexShader and if i don't normalize the normalized value again in the Pixel Shader by float3 Nn = normalize(input.Normal); my lighting result of all geometries will be darker. Why i need to normalize a normalized value again to get right lighting results?

The vertex normals just get linearly interpolated, like anything else. So the midpoint of (-0.3, 0.4, 0) and (0.3, 0.4, 0) becomes just (0, 0.4, 0).

Because this result has length smaller than 1, you get darker colors and renormalization is necessary.

 

The second question is probably related to a setup where you can set light direction and projection independent of each other. You probably adjust the just projection alone and need to find out how to change light direction as well.

Hi Joe,

my current direction light has more the behavior of a point light in Shadow drawing, as a direction light. In the SimpleShader i calculate the shadows with the view and projection matrix of a light, that i create a the CPU on a Update Fuction in my Light Class

 


        // Space is a Class where I store Transformation Informations of the World Space Matrix and Side/Up/Look Vectors etc. of the Light
		// Translate is the Postion of the Light

        public void Update(float renderTargetWidth, float renderTargetHeight)
        {
            this.direction = this.space.LocalLook;

            this.view = Matrix.LookAtLH(this.space.Translate, this.space.Translate + this.space.LocalLook, this.space.LocalUp);

            this.projection = Matrix.PerspectiveFovLH(Math.DegreesToRadians(45.0f),
                renderTargetWidth / renderTargetHeight, 0.1f, 1000.0f);
        }

 

But i am a totally noob in Light and Shadow Stuff. So i need to start from the beginning.

In Graphics and Game Engine like Maya, Unity etc. if i create a direction light, all geometries will lighting from the direction of the light. I do this with:

float3 lightFront = LightColor * saturate(dot(Nn, -Ln)); (Ln = Light Direction of my DirectionLight).

But how works it with the shadows, there are must be a fix positon, where the light source looks of all geometries from far, far away, always in the right angle, i imagine?

Can someone explain me it, how a direction light calculate all the shadows in the whole scene with a view and projection matrix?

Greets

Benjamin

5 hours ago, B. / said:

But how works it with the shadows, there are must be a fix positon, where the light source looks of all geometries from far, far away, always in the right angle, i imagine?

Can someone explain me it, how a direction light calculate all the shadows in the whole scene with a view and projection matrix?

The difference between point/spot light and directional light is the fromer use a perspective projection, and the latter use an orthogonal projection, so you can look how to setup those types of camera projection and use the same code for the lights.

Hi Joe,

I recode my update function to orthogonal projection:


        public void Update(float renderTargetWidth, float renderTargetHeight, float near, float far)
        {
            this.direction = this.space.LocalLook;

            this.view = Matrix.LookAtLH(this.space.Translate, this.space.Translate + this.space.LocalLook, this.space.LocalUp);

            this.projection = Matrix.OrthoLH(renderTargetWidth, renderTargetHeight, 0.1f, 1500.0f);
        }

But now I see no shadows anymore, just this on the image (Bias Value is 0.001f) :(

Do I had something forget? Tried also in the ShadowMap Shader only to multiply worldpos with only LightProjection. Result was even worser, nothing see :(

Greets

Benjamin

01.jpg

ps. Ok, if i change the width and height (befor was 1920x1080) to smaller like 100x100 i see now Shadows, but is that the correct way?

This topic is closed to new replies.

Advertisement