Shadow Map Artifacts

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


Post Reply
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Shadow Map Artifacts

Post by Nickak2003 »

So I am new to shadows. I've implemented the static shadow maps workspace. I am lighting a terrain with a directional light.
Here's what it looks like:
https://pasteboard.co/J3m0391.png
Image

Either my shadow map code is off, or my mesh is off in the normals/tangent area. I feel like the tangent is wrong and I don't know much about it.
Here's my triangle generation code:

Code: Select all

void MyTriangleCollector3::processTriangle(btVector3* tris, int partId, int triangleIndex) {

	for (int k = 0; k < 3; ++k) {

		Vertex v;

		btVector3& tri = tris[k];
		v.pos = Ogre::Vector3(tri.getX(), tri.getY(), tri.getZ());

		v.tex.x = (v.pos.x + mWidth / 2) / mWidth;
		v.tex.y = (v.pos.z + mHeight / 2) / mHeight;

		btVector3 normal = (tris[0] - tris[1]).cross(tris[0] - tris[2]);
		normal.safeNormalize();
		v.normal = Ogre::Vector3(normal.getX(), normal.getY(), normal.getZ());

		v.tangent = Ogre::Vector3(0,0, 1).crossProduct(v.normal);

		mIndicesOut.push_back(mVerticesOut.size());
		mVerticesOut.push_back(v);
	}
}
So this function is passed one triangle at a time, 3 vertexes via the tris pointer. I loop over them and store vertexes which i later pass to ogre::manualobject. The terrain is x by z with y=height.

If that looks OK, then it must be the shadow code, I got it from the staticshadowmap demo, not sure what I may have missed.
Last edited by Nickak2003 on Sat Apr 11, 2020 8:18 pm, edited 1 time in total.
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: Shadow Map Artifacts

Post by xrgo »

Hello! seems to be a combination of peter panning and flat tris
to reduce peter panning adjust this value:

Code: Select all

mDatablock->mShadowConstantBias
but if you go too far then you'll introduce shadow acne
you'll have to find a balance

you can also play with this:

Code: Select all

Ogre::Root::getSingleton().getHlmsManager()->setShadowMappingUseBackFaces( false )
and for that flat tris, you need to merge vertex on the same pos and average the normals (but maybe its the look that you want?)

Saludos!
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5292
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Shadow Map Artifacts

Post by dark_sylinc »

I think the main issue he's worried about is flat shaded tris.

You need to average your normals so that smooth shading can be applied.

In pseudo code:

Code: Select all

for( each vertex )
{
    Vector3 normal = vertex->normal;
    for( adjVertex in adjacentVertices( vertex ) )
        normal += adjVertex->normal;
        
    normal.normalize();
    vertex->normal = normal;
}
For more information see Polycount vs Vertex count.
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

sorry for not describing better, but the issue i am worried about is the white outline at the start of the shadow at the base of the bump
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

also I am setting the shadow bias like so, but am unsure if it's working:

Code: Select all

	manualObj->setCastShadows(true);
	manualObj->getSection(0)->getDatablock()->mShadowConstantBias = 100;;
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5292
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Shadow Map Artifacts

Post by dark_sylinc »

Try negative values for the bias.
Though such a high value should've broken the shadows entirely. Not sure what's going on
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

I smoothed the normals, and merged the duplicated verts
https://pasteboard.co/J3nQ23j.png
Last edited by Nickak2003 on Sun Apr 12, 2020 6:48 pm, edited 2 times in total.
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

this is the material, if it matters:

Code: Select all

hlms hlms_terrain1 pbs
{

	diffuse_map	greystar.jpg

}
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

So, mShadowConstantBias, does have an effect of some sort, it just doesn't seem to be affecting the lit outlines at all.
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

So with backfaces false, it looks like this:
https://pasteboard.co/J3ta3My.png

the outlines are gone but now there is a lot of other oddity

also, I fixed the tangents when I added smoothing
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: Shadow Map Artifacts

Post by xrgo »

Nickak2003 wrote: Sun Apr 12, 2020 2:24 pm there is a lot of other oddity
that´s shadow acne

backfaces true or false, this should be able to be fixed with mShadowConstantBias, I would leave backfaces as default and keep trying bias values, it shouldn't be that hard to find a proper one, remember to try negative values (and usually values like 0.01 or so)

Saludos!
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

OK, so I randomly got it working and looking OK. However, I am concerned as it does not follow any of your suggestions. Here is what it is looking like:
https://pasteboard.co/J3v93wZ.png

no backfaces and bias -1000

What exactly is shadow bias and disabled backfaces?
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

So with -2000 it looks even better

The closer to zero the more acne, the further, the less
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

OK, so I'm trying to get it to work with backfaces(true) and a small negative constant bias, but I just can't seem to get the outline before the shadow to go away. It only goes away with backfaces(false), but that seems to create a lot of problems.
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

SOLVED!
manualObj->setCastShadows(true) causes the error
setCastShadows(false) resolves it'

Never mind, that resulted in no shadows whatsoever, working too late.
Last edited by Nickak2003 on Mon Apr 13, 2020 3:39 pm, edited 1 time in total.
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: Shadow Map Artifacts

Post by xrgo »

glad you solved it!
but seems strange, that's a huge bias, maybe its something about the scale/size of your terrain :S
and usually setCastShadows(false) omit completely the object from the caster pass, so you shouldn't have any shadow, but I have zero experience with manual objects, might behave differently on those
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

OK not solved, earlier shadows went away completely and i apparently didn't notice, lol. I am going to mess around some more and update
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Shadow Map Artifacts

Post by Nickak2003 »

OK, I think it's mainly an issue for when a slope encounters a flat plane, if there is more of a gradient, no issue or at least not as much. Could this be a normal or tangent issue I may have, though?
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: Shadow Map Artifacts

Post by xrgo »

Strange, I just noticed that shadowBias is not working on my engine

even if I force the values on shadowConstantBias (just for a quick test)

Code: Select all

outVs_Position.z = outVs_Position.z + shadowConstantBias * passBuf.depthRange.y * passBuf.depthRange.y;
like:

Code: Select all

outVs_Position.z = outVs_Position.z + 0.1* passBuf.depthRange.y * passBuf.depthRange.y;
but its working if I do:

Code: Select all

outVs_Position.z = outVs_Position.z + 0.1;
passBuf.depthRange.y might be 0
I'll dig more on this later

maybe something is broken... that might be the reason that you have to use such high values and see no difference
Post Reply