Blurry PSSM Shadows

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
Max98
Gnoblar
Posts: 11
Joined: Sun Feb 22, 2015 1:10 pm
x 1

Blurry PSSM Shadows

Post by Max98 »

Hello there.
I've been trying to get PSSM shadows work correctly sice few weeks ago and I can't manage to get any good results.

Code: Select all

void ShadowManager::processPSSM()
{
	gEnv->sceneManager->setShadowFarDistance(2048);
	gEnv->sceneManager->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_ADDITIVE_INTEGRATED);

	gEnv->sceneManager->setShadowTextureCountPerLightType(Ogre::Light::LT_DIRECTIONAL, PSSM_Shadows.ShadowsTextureNum);

	if (PSSM_Shadows.mPSSMSetup.isNull())
	{
		// shadow camera setup
		Ogre::PSSMShadowCameraSetup* pssmSetup = new Ogre::PSSMShadowCameraSetup();
		pssmSetup->setSplitPadding(5);
		pssmSetup->setCameraLightDirectionThreshold(Ogre::Degree(5));
		pssmSetup->setUseAggressiveFocusRegion(true);
		pssmSetup->setUseSimpleOptimalAdjust(false);
		//pssmSetup->calculateSplitPoints(3, gEnv->mainCamera->getNearClipDistance(), gEnv->mainCamera->getFarClipDistance());

		pssmSetup->setOptimalAdjustFactor(0, 2);
		pssmSetup->setOptimalAdjustFactor(1, 1);
		pssmSetup->setOptimalAdjustFactor(2, 0.5);

		PSSM_Shadows.mPSSMSetup.bind(pssmSetup);

	}
	gEnv->sceneManager->setShadowCameraSetup(PSSM_Shadows.mPSSMSetup);


	gEnv->sceneManager->setShadowTextureCount(PSSM_Shadows.ShadowsTextureNum);

	gEnv->sceneManager->setShadowTextureConfig(0, 2048, 2048, PF_FLOAT16_RGB);
	gEnv->sceneManager->setShadowTextureConfig(1, 1024, 1024, PF_FLOAT16_RGB);
	gEnv->sceneManager->setShadowTextureConfig(2, 512,  512,  PF_FLOAT16_RGB);

	gEnv->sceneManager->setShadowTextureSelfShadow(true);
	gEnv->sceneManager->setShadowCasterRenderBackFaces(true);

	gEnv->sceneManager->setShadowTextureCasterMaterial(PSSM_Shadows.mDepthShadows ? "PSSM/shadow_caster" : Ogre::StringUtil::BLANK);

	updatePSSM();
}

void ShadowManager::updatePSSM(Ogre::Terrain* terrain)
{
	if (!PSSM_Shadows.mPSSMSetup.get())  return;

	Ogre::TerrainMaterialGeneratorA::SM2Profile *matProfile = 0;
	if (Ogre::TerrainGlobalOptions::getSingletonPtr())
	{
		matProfile = static_cast<Ogre::TerrainMaterialGeneratorA::SM2Profile*>(Ogre::TerrainGlobalOptions::getSingleton().getDefaultMaterialGenerator()->getActiveProfile());
		matProfile->setReceiveDynamicShadowsEnabled(true);
		matProfile->setReceiveDynamicShadowsLowLod(true);
		matProfile->setGlobalColourMapEnabled(true);
	}


	Ogre::PSSMShadowCameraSetup* pssmSetup = static_cast<Ogre::PSSMShadowCameraSetup*>(PSSM_Shadows.mPSSMSetup.get());
	Ogre::PSSMShadowCameraSetup::SplitPointList splitPointList = pssmSetup->getSplitPoints();

	
	splitPointList[0] = 1.0;
	splitPointList[1] = 94.0;
	splitPointList[2] = 250.0;
	splitPointList[3] = 512.0;
	pssmSetup->setSplitPoints(splitPointList);
	
	
	Ogre::Vector4 splitPoints;
	for (int i = 0; i < PSSM_Shadows.ShadowsTextureNum; ++i)
		splitPoints[i] = splitPointList[i];

	MaterialPtr mat = MaterialManager::getSingleton().getByName("RoR/Managed_Mats/Base");
	mat->getTechnique("ShadowTechnique")->getPass(0)->getFragmentProgramParameters()->setNamedConstant("pssmSplitPoints", splitPoints);

	// TODO: fix this
/*	setMaterialSplitPoints("road", splitPoints);
	setMaterialSplitPoints("road2", splitPoints);
	*/

	if (matProfile && terrain)
	{
		matProfile->generateForCompositeMap(terrain);
		matProfile->setReceiveDynamicShadowsDepth(PSSM_Shadows.mDepthShadows);
		matProfile->setReceiveDynamicShadowsPSSM(static_cast<Ogre::PSSMShadowCameraSetup*>(PSSM_Shadows.mPSSMSetup.get()));
	}
}
IMO, the code seems fine, but ingame, shadows get blurry depending on the angle of the camera. And If I manually decrease the splitpoints values, things get better, but I end up with a lot of unfixable cracks.
Image

Also, it seems like there is a problem with the.. uhm bias? Shadows are rendered over the side where the light is hitting?
Image

Thanks for your help.
MattStevens
Goblin
Posts: 239
Joined: Mon Apr 07, 2008 10:27 pm
x 4

Re: Blurry PSSM Shadows

Post by MattStevens »

You do need a bias, because a surface that is lit will have the same depth compared (The depth of the fragment rendered compared to the depth saved in the shadow texture). It can cause Z-fighting in the same way as when you render a object in multiple passes. I had the same problem and I managed to get something working but far from perfect. Let me know if you find the way to calculate and set the bias.

Also is there any reason that you set the split points yourself ? I personally let the CameraSetup choose them and it works pretty well. It looks something like this:

Code: Select all

pssm->calculateSplitPoints(3, nearClip, mSceneManager->getShadowFarDistance());
pssm->setSplitPadding(10);
pssm->setOptimalAdjustFactor(0, -1);
pssm->setOptimalAdjustFactor(1, -1);
pssm->setOptimalAdjustFactor(2, -1);

Ogre::Vector4 splitPoints;
const Ogre::PSSMShadowCameraSetup::SplitPointList& splitPointList = pssm->getSplitPoints();
for (int i = 0; i < 3; ++i)
{
	splitPoints[i] = splitPointList[i];
}
Max98
Gnoblar
Posts: 11
Joined: Sun Feb 22, 2015 1:10 pm
x 1

Re: Blurry PSSM Shadows

Post by Max98 »

I've chosen to set the split points myself because, each time I use calculateSplitPoints, I get flickering shadows.
Also, I don't think that I'm going to work any more on this, I've moved to Ogre 2.0 (Currently updating my project)

But thanks anyway. :D
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: Blurry PSSM Shadows

Post by areay »

MattStevens wrote: pssm->setOptimalAdjustFactor(0, -1);
pssm->setOptimalAdjustFactor(1, -1);
pssm->setOptimalAdjustFactor(2, -1);
What? Negative adjust factors!!??? I've never tried that, is that what looks best for you by trial and error?
Max98
Gnoblar
Posts: 11
Joined: Sun Feb 22, 2015 1:10 pm
x 1

Re: Blurry PSSM Shadows

Post by Max98 »

So I've switched to Ogre 2.0 and it seems to work pretty well. :)
I've got much better results than in Ogre 1.8

You can notice the splits in this image:
Image

Self shadows here:
Image
MattStevens
Goblin
Posts: 239
Joined: Mon Apr 07, 2008 10:27 pm
x 4

Re: Blurry PSSM Shadows

Post by MattStevens »

areay wrote:
MattStevens wrote: pssm->setOptimalAdjustFactor(0, -1);
pssm->setOptimalAdjustFactor(1, -1);
pssm->setOptimalAdjustFactor(2, -1);
What? Negative adjust factors!!??? I've never tried that, is that what looks best for you by trial and error?
Yeah it is what worked best for me, but I can't remember the logic behind it. It changes a bit the way the camera setups their frustum. I would have to look around to find the explanation.
Post Reply