CustomMaterial CastShadows Improvement

Materials, textures, lighting, postfx
2 posts Page 1 of 1
Steve_Yorkshire
Posts: 207
Joined: Tue Feb 03, 2015 10:30 pm
 
  by Steve_Yorkshire » Mon Jan 30, 2017 11:00 pm
Finally worked out how to get customMaterials to use castShadows the same way as normal materials. As it's spread over multiple pages of the refraction/distortion thread - it's posted here together.

First thing to do is stop customMaterials from ALWAYS casting a shadow. Someone at GG decided to force this regardless. Problem is, that it doesn't matter whether translucent or anything else has filtered shadows out, shadows will be forcibly set on here.

lighting/shadowMap/shadowMatHook.cpp
void ShadowMaterialHook::init( BaseMatInstance *inMat )
{
//...
// Do instancing in shadows if we can.
if ( inFeatures.hasFeature( MFT_UseInstancing ) )
features.addFeature( MFT_UseInstancing );

Material *shadowMat = (Material*)inMat->getMaterial();
/* yorks out start - let customMaterials decide for themselves
if (dynamic_cast< CustomMaterial* >(shadowMat))
{
// This is a custom material... who knows what it really does, but
// if it wasn't already filtered out of the shadow render then just
// give it some default depth out material.
shadowMat = MATMGR->getMaterialDefinitionByName("AL_DefaultShadowMaterial");
}
*/ //yorks end of out

// By default we want to disable some states
// that the material might enable for us.
GFXStateBlockDesc forced;
//...


source/materials/CustomMaterialDefinition.h
class CustomMaterial : public Material
{
typedef Material Parent;
public:
//...
bool mForwardLit;
bool mCastShadows;//yorks in

F32 mVersion; // 0 = legacy, 1 = DX 8.1, 2 = DX 9.0
bool mRefract;
//...
virtual bool onAdd();
virtual void onRemove();
virtual bool castsShadows() const { return mCastShadows; }//yorks in
//...


source/materials/CustomMaterialDefinition.cpp
CustomMaterial::CustomMaterial()
{
//...
mForwardLit = false;
mCastShadows = true;//yorks in
}

//--------------------------------------------------------------------------
// Init fields
//--------------------------------------------------------------------------
void CustomMaterial::initPersistFields()
{
//...
addField("forwardLit", TypeBool, Offset(mForwardLit, CustomMaterial),
"@brief Determines if the material should recieve lights in Basic Lighting. "
"Has no effect in Advanced Lighting.\n\n");
//yorks start in
addField("castShadows", TypeBool, Offset(mCastShadows, CustomMaterial),
"If set to false the lighting system will not cast shadows from this material.");
//yorks end in

Parent::initPersistFields();
}


After that you can control the shadows in your CustomMaterial.
Remember shadows are on by default so your TorqueScript definition wants to look like
singleton CustomMaterial(Mat_wobble_Soldier_Main : Mat_Soldier_Main)
{
mapTo = "wobble_Soldier_Main";

translucent = true;//translucent materials (custom or standard) never cast shadows

shader = wobbleTextureShader;
sampler["diffuseMap"] = "#PreRenderTranslucent";
};

singleton CustomMaterial(Mat_wobble2_Soldier_Main : Mat_Soldier_Main)
{
mapTo = "wobble2_Soldier_Main";

translucent = false;
//castShadows = true;//default is true so don't need this.

shader = wobbleTextureShader;
sampler["diffuseMap"] = "#PreRenderTranslucent";
};

singleton CustomMaterial(Mat_wobble3_Soldier_Main : Mat_Soldier_Main)
{
mapTo = "wobble3_Soldier_Main";

translucent = false;
castShadows = false;

shader = wobbleTextureShader;
sampler["diffuseMap"] = "#PreRenderTranslucent";
};


And these 3 then look like this in-game:
Image
Steve_Yorkshire
Posts: 207
Joined: Tue Feb 03, 2015 10:30 pm
 
by Steve_Yorkshire » Mon Jan 30, 2017 11:08 pm
And here's a standard customMaterial so you can actually see the model :P
Image
2 posts Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests