Jump to content

CustomMaterial CastShadows Improvement


Steve_Yorkshire

Recommended Posts

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:

http://i.imgur.com/YJni7sA.jpg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...