materialDefinition.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MATERIALDEFINITION_H_
  23. #define _MATERIALDEFINITION_H_
  24. #ifndef _BASEMATERIALDEFINITION_H_
  25. #include "materials/baseMaterialDefinition.h"
  26. #endif
  27. #ifndef _TDICTIONARY_H_
  28. #include "core/util/tDictionary.h"
  29. #endif
  30. #ifndef _GFXTEXTUREHANDLE_H_
  31. #include "gfx/gfxTextureHandle.h"
  32. #endif
  33. #ifndef _GFXSTRUCTS_H_
  34. #include "gfx/gfxStructs.h"
  35. #endif
  36. #ifndef _GFXCUBEMAP_H_
  37. #include "gfx/gfxCubemap.h"
  38. #endif
  39. #ifndef _DYNAMIC_CONSOLETYPES_H_
  40. #include "console/dynamicTypes.h"
  41. #endif
  42. class CubemapData;
  43. class SFXTrack;
  44. struct SceneData;
  45. class FeatureSet;
  46. class FeatureType;
  47. class MaterialSoundProfile;
  48. class MaterialPhysicsProfile;
  49. /// The basic material definition.
  50. class Material : public BaseMaterialDefinition
  51. {
  52. typedef BaseMaterialDefinition Parent;
  53. public:
  54. static GFXCubemap *GetNormalizeCube();
  55. //-----------------------------------------------------------------------
  56. // Enums
  57. //-----------------------------------------------------------------------
  58. enum Constants
  59. {
  60. MAX_TEX_PER_PASS = 8, ///< Number of textures per pass
  61. MAX_STAGES = 4,
  62. NUM_EFFECT_COLOR_STAGES = 2, ///< Number of effect color definitions for transitioning effects.
  63. };
  64. enum TexType
  65. {
  66. NoTexture = 0,
  67. Standard = 1,
  68. Detail,
  69. Bump,
  70. DetailBump,
  71. Env,
  72. Cube,
  73. SGCube, // scene graph cube - probably dynamic
  74. Lightmap,
  75. ToneMapTex,
  76. Mask,
  77. BackBuff,
  78. ReflectBuff,
  79. Misc,
  80. DynamicLight,
  81. DynamicLightMask,
  82. NormalizeCube,
  83. TexTarget,
  84. };
  85. enum BlendOp
  86. {
  87. None = 0,
  88. Mul,
  89. Add,
  90. AddAlpha, // add modulated with alpha channel
  91. Sub,
  92. LerpAlpha, // linear interpolation modulated with alpha channel
  93. ToneMap,
  94. NumBlendTypes
  95. };
  96. enum AnimType
  97. {
  98. Scroll = 1,
  99. Rotate = 2,
  100. Wave = 4,
  101. Scale = 8,
  102. Sequence = 16,
  103. };
  104. enum WaveType
  105. {
  106. Sin = 0,
  107. Triangle,
  108. Square,
  109. };
  110. class StageData
  111. {
  112. protected:
  113. ///
  114. typedef HashTable<const FeatureType*,GFXTexHandle> TextureTable;
  115. /// The sparse table of textures by feature index.
  116. /// @see getTex
  117. /// @see setTex
  118. TextureTable mTextures;
  119. /// The cubemap for this stage.
  120. GFXCubemap *mCubemap;
  121. public:
  122. StageData()
  123. : mCubemap( NULL )
  124. {
  125. }
  126. /// Returns the texture object or NULL if there is no
  127. /// texture entry for that feature type in the table.
  128. inline GFXTextureObject* getTex( const FeatureType &type ) const
  129. {
  130. TextureTable::ConstIterator iter = mTextures.find( &type );
  131. if ( iter == mTextures.end() )
  132. return NULL;
  133. return iter->value.getPointer();
  134. }
  135. /// Assigns a texture object by feature type.
  136. inline void setTex( const FeatureType &type, GFXTextureObject *tex )
  137. {
  138. if ( !tex )
  139. {
  140. TextureTable::Iterator iter = mTextures.find( &type );
  141. if ( iter != mTextures.end() )
  142. mTextures.erase( iter );
  143. return;
  144. }
  145. TextureTable::Iterator iter = mTextures.findOrInsert( &type );
  146. iter->value = tex;
  147. }
  148. /// Returns true if we have a valid texture assigned to
  149. /// any feature in the texture table.
  150. inline bool hasValidTex() const
  151. {
  152. TextureTable::ConstIterator iter = mTextures.begin();
  153. for ( ; iter != mTextures.end(); iter++ )
  154. {
  155. if ( iter->value.isValid() )
  156. return true;
  157. }
  158. return false;
  159. }
  160. /// Returns the active texture features.
  161. void getFeatureSet( FeatureSet *outFeatures ) const;
  162. /// Returns the stage cubemap.
  163. GFXCubemap* getCubemap() const { return mCubemap; }
  164. /// Set the stage cubemap.
  165. void setCubemap( GFXCubemap *cubemap ) { mCubemap = cubemap; }
  166. };
  167. public:
  168. //-----------------------------------------------------------------------
  169. // Data
  170. //-----------------------------------------------------------------------
  171. FileName mDiffuseMapFilename[MAX_STAGES];
  172. FileName mOverlayMapFilename[MAX_STAGES];
  173. FileName mLightMapFilename[MAX_STAGES];
  174. FileName mToneMapFilename[MAX_STAGES];
  175. FileName mDetailMapFilename[MAX_STAGES];
  176. FileName mNormalMapFilename[MAX_STAGES];
  177. FileName mSpecularMapFilename[MAX_STAGES];
  178. /// A second normal map which repeats at the detail map
  179. /// scale and blended with the base normal map.
  180. FileName mDetailNormalMapFilename[MAX_STAGES];
  181. /// The strength scalar for the detail normal map.
  182. F32 mDetailNormalMapStrength[MAX_STAGES];
  183. FileName mEnvMapFilename[MAX_STAGES];
  184. /// This color is the diffuse color of the material
  185. /// or if it has a texture it is multiplied against
  186. /// the diffuse texture color.
  187. ColorF mDiffuse[MAX_STAGES];
  188. ColorF mSpecular[MAX_STAGES];
  189. F32 mSpecularPower[MAX_STAGES];
  190. F32 mSpecularStrength[MAX_STAGES];
  191. bool mPixelSpecular[MAX_STAGES];
  192. bool mVertLit[MAX_STAGES];
  193. /// If true for a stage, vertex colors are multiplied
  194. /// against diffuse colors.
  195. bool mVertColor[ MAX_STAGES ];
  196. F32 mParallaxScale[MAX_STAGES];
  197. F32 mMinnaertConstant[MAX_STAGES];
  198. bool mSubSurface[MAX_STAGES];
  199. ColorF mSubSurfaceColor[MAX_STAGES];
  200. F32 mSubSurfaceRolloff[MAX_STAGES];
  201. /// The repetition scale of the detail texture
  202. /// over the base texture.
  203. Point2F mDetailScale[MAX_STAGES];
  204. U32 mAnimFlags[MAX_STAGES];
  205. Point2F mScrollDir[MAX_STAGES];
  206. F32 mScrollSpeed[MAX_STAGES];
  207. Point2F mScrollOffset[MAX_STAGES];
  208. F32 mRotSpeed[MAX_STAGES];
  209. Point2F mRotPivotOffset[MAX_STAGES];
  210. F32 mRotPos[MAX_STAGES];
  211. F32 mWavePos[MAX_STAGES];
  212. F32 mWaveFreq[MAX_STAGES];
  213. F32 mWaveAmp[MAX_STAGES];
  214. U32 mWaveType[MAX_STAGES];
  215. F32 mSeqFramePerSec[MAX_STAGES];
  216. F32 mSeqSegSize[MAX_STAGES];
  217. bool mGlow[MAX_STAGES]; // entire stage glows
  218. bool mEmissive[MAX_STAGES];
  219. Point2I mCellIndex[MAX_STAGES];
  220. Point2I mCellLayout[MAX_STAGES];
  221. U32 mCellSize[MAX_STAGES];
  222. bool mNormalMapAtlas[MAX_STAGES];
  223. /// Special array of UVs for imposter rendering.
  224. /// @see TSLastDetail
  225. Vector<RectF> mImposterUVs;
  226. /// Specual imposter rendering paramters.
  227. /// @see TSLastDetail
  228. Point4F mImposterLimits;
  229. /// If the stage should use anisotropic filtering.
  230. bool mUseAnisotropic[MAX_STAGES];
  231. bool mDoubleSided;
  232. String mCubemapName;
  233. CubemapData* mCubemapData;
  234. bool mDynamicCubemap;
  235. bool mTranslucent;
  236. BlendOp mTranslucentBlendOp;
  237. bool mTranslucentZWrite;
  238. /// A generic setting which tells the system to skip
  239. /// generation of shadows from this material.
  240. bool mCastShadows;
  241. bool mAlphaTest;
  242. U32 mAlphaRef;
  243. bool mPlanarReflection;
  244. bool mAutoGenerated;
  245. static bool sAllowTextureTargetAssignment;
  246. ///@{
  247. /// Behavioral properties.
  248. bool mShowFootprints; ///< If true, show footprints when walking on surface with this material. Defaults to false.
  249. bool mShowDust; ///< If true, show dust emitters (footpuffs, hover trails, etc) when on surface with this material. Defaults to false.
  250. /// Color to use for particle effects and such when located on this material.
  251. ColorF mEffectColor[ NUM_EFFECT_COLOR_STAGES ];
  252. /// Footstep sound to play when walking on surface with this material.
  253. /// Numeric ID of footstep sound defined on player datablock (0 == soft,
  254. /// 1 == hard, 2 == metal, 3 == snow).
  255. /// Defaults to -1 which deactivates default sound.
  256. /// @see mFootstepSoundCustom
  257. S32 mFootstepSoundId;
  258. S32 mImpactSoundId;
  259. /// Sound effect to play when walking on surface with this material.
  260. /// If defined, overrides mFootstepSoundId.
  261. /// @see mFootstepSoundId
  262. SFXTrack* mFootstepSoundCustom;
  263. SFXTrack* mImpactSoundCustom;
  264. F32 mFriction; ///< Friction coefficient when moving along surface.
  265. F32 mDirectSoundOcclusion; ///< Amount of volume occlusion on direct sounds.
  266. F32 mReverbSoundOcclusion; ///< Amount of volume occlusion on reverb sounds.
  267. ///@}
  268. String mMapTo; // map Material to this texture name
  269. ///
  270. /// Material interface
  271. ///
  272. Material();
  273. /// Allocates and returns a BaseMatInstance for this material. Caller is responsible
  274. /// for freeing the instance
  275. virtual BaseMatInstance* createMatInstance();
  276. virtual bool isTranslucent() const { return mTranslucent && mTranslucentBlendOp != Material::None; }
  277. virtual bool isDoubleSided() const { return mDoubleSided; }
  278. virtual bool isAutoGenerated() const { return mAutoGenerated; }
  279. virtual void setAutoGenerated(bool isAutoGenerated) { mAutoGenerated = isAutoGenerated; }
  280. virtual bool isLightmapped() const;
  281. virtual bool castsShadows() const { return mCastShadows; }
  282. const String &getPath() const { return mPath; }
  283. void flush();
  284. /// Re-initializes all the material instances
  285. /// that use this material.
  286. void reload();
  287. /// Called to update time based parameters for a material. Ensures
  288. /// that it only happens once per tick.
  289. void updateTimeBasedParams();
  290. // SimObject
  291. virtual bool onAdd();
  292. virtual void onRemove();
  293. virtual void inspectPostApply();
  294. virtual bool writeField( StringTableEntry fieldname, const char *value );
  295. //
  296. // ConsoleObject interface
  297. //
  298. static void initPersistFields();
  299. DECLARE_CONOBJECT(Material);
  300. protected:
  301. // Per material animation parameters
  302. U32 mLastUpdateTime;
  303. String mPath;
  304. static EnumTable mAnimFlagTable;
  305. static EnumTable mBlendOpTable;
  306. static EnumTable mWaveTypeTable;
  307. /// Map this material to the texture specified
  308. /// in the "mapTo" data variable.
  309. virtual void _mapMaterial();
  310. private:
  311. static GFXCubemapHandle smNormalizeCube;
  312. };
  313. typedef Material::AnimType MaterialAnimType;
  314. typedef Material::BlendOp MaterialBlendOp;
  315. typedef Material::WaveType MaterialWaveType;
  316. DefineBitfieldType( MaterialAnimType );
  317. DefineEnumType( MaterialBlendOp );
  318. DefineEnumType( MaterialWaveType );
  319. #endif // _MATERIALDEFINITION_H_