materialDefinition.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. AccuMap,
  85. DynamicShadowMap,
  86. };
  87. enum BlendOp
  88. {
  89. None = 0,
  90. Mul,
  91. Add,
  92. AddAlpha, // add modulated with alpha channel
  93. Sub,
  94. LerpAlpha, // linear interpolation modulated with alpha channel
  95. ToneMap,
  96. NumBlendTypes
  97. };
  98. enum AnimType
  99. {
  100. Scroll = 1,
  101. Rotate = 2,
  102. Wave = 4,
  103. Scale = 8,
  104. Sequence = 16,
  105. };
  106. enum WaveType
  107. {
  108. Sin = 0,
  109. Triangle,
  110. Square,
  111. };
  112. class StageData
  113. {
  114. protected:
  115. ///
  116. typedef HashTable<const FeatureType*,GFXTexHandle> TextureTable;
  117. /// The sparse table of textures by feature index.
  118. /// @see getTex
  119. /// @see setTex
  120. TextureTable mTextures;
  121. /// The cubemap for this stage.
  122. GFXCubemap *mCubemap;
  123. public:
  124. StageData()
  125. : mCubemap( NULL )
  126. {
  127. }
  128. /// Returns the texture object or NULL if there is no
  129. /// texture entry for that feature type in the table.
  130. inline GFXTextureObject* getTex( const FeatureType &type ) const
  131. {
  132. TextureTable::ConstIterator iter = mTextures.find( &type );
  133. if ( iter == mTextures.end() )
  134. return NULL;
  135. return iter->value.getPointer();
  136. }
  137. /// Assigns a texture object by feature type.
  138. inline void setTex( const FeatureType &type, GFXTextureObject *tex )
  139. {
  140. if ( !tex )
  141. {
  142. TextureTable::Iterator iter = mTextures.find( &type );
  143. if ( iter != mTextures.end() )
  144. mTextures.erase( iter );
  145. return;
  146. }
  147. TextureTable::Iterator iter = mTextures.findOrInsert( &type );
  148. iter->value = tex;
  149. }
  150. /// Returns true if we have a valid texture assigned to
  151. /// any feature in the texture table.
  152. inline bool hasValidTex() const
  153. {
  154. TextureTable::ConstIterator iter = mTextures.begin();
  155. for (; iter != mTextures.end(); ++iter)
  156. {
  157. if ( iter->value.isValid() )
  158. return true;
  159. }
  160. return false;
  161. }
  162. /// Returns the active texture features.
  163. void getFeatureSet( FeatureSet *outFeatures ) const;
  164. /// Returns the stage cubemap.
  165. GFXCubemap* getCubemap() const { return mCubemap; }
  166. /// Set the stage cubemap.
  167. void setCubemap( GFXCubemap *cubemap ) { mCubemap = cubemap; }
  168. };
  169. public:
  170. //-----------------------------------------------------------------------
  171. // Data
  172. //-----------------------------------------------------------------------
  173. FileName mDiffuseMapFilename[MAX_STAGES];
  174. bool mAccuEnabled[MAX_STAGES];
  175. F32 mAccuScale[MAX_STAGES];
  176. F32 mAccuDirection[MAX_STAGES];
  177. F32 mAccuStrength[MAX_STAGES];
  178. F32 mAccuCoverage[MAX_STAGES];
  179. F32 mAccuSpecular[MAX_STAGES];
  180. FileName mOverlayMapFilename[MAX_STAGES];
  181. FileName mLightMapFilename[MAX_STAGES];
  182. FileName mToneMapFilename[MAX_STAGES];
  183. FileName mDetailMapFilename[MAX_STAGES];
  184. FileName mNormalMapFilename[MAX_STAGES];
  185. FileName mSpecularMapFilename[MAX_STAGES];
  186. /// A second normal map which repeats at the detail map
  187. /// scale and blended with the base normal map.
  188. FileName mDetailNormalMapFilename[MAX_STAGES];
  189. /// The strength scalar for the detail normal map.
  190. F32 mDetailNormalMapStrength[MAX_STAGES];
  191. /// This color is the diffuse color of the material
  192. /// or if it has a texture it is multiplied against
  193. /// the diffuse texture color.
  194. ColorF mDiffuse[MAX_STAGES];
  195. ColorF mSpecular[MAX_STAGES];
  196. F32 mSpecularPower[MAX_STAGES];
  197. F32 mSpecularStrength[MAX_STAGES];
  198. bool mPixelSpecular[MAX_STAGES];
  199. bool mVertLit[MAX_STAGES];
  200. /// If true for a stage, vertex colors are multiplied
  201. /// against diffuse colors.
  202. bool mVertColor[ MAX_STAGES ];
  203. F32 mParallaxScale[MAX_STAGES];
  204. F32 mMinnaertConstant[MAX_STAGES];
  205. bool mSubSurface[MAX_STAGES];
  206. ColorF mSubSurfaceColor[MAX_STAGES];
  207. F32 mSubSurfaceRolloff[MAX_STAGES];
  208. /// The repetition scale of the detail texture
  209. /// over the base texture.
  210. Point2F mDetailScale[MAX_STAGES];
  211. U32 mAnimFlags[MAX_STAGES];
  212. Point2F mScrollDir[MAX_STAGES];
  213. F32 mScrollSpeed[MAX_STAGES];
  214. Point2F mScrollOffset[MAX_STAGES];
  215. F32 mRotSpeed[MAX_STAGES];
  216. Point2F mRotPivotOffset[MAX_STAGES];
  217. F32 mRotPos[MAX_STAGES];
  218. F32 mWavePos[MAX_STAGES];
  219. F32 mWaveFreq[MAX_STAGES];
  220. F32 mWaveAmp[MAX_STAGES];
  221. U32 mWaveType[MAX_STAGES];
  222. F32 mSeqFramePerSec[MAX_STAGES];
  223. F32 mSeqSegSize[MAX_STAGES];
  224. bool mGlow[MAX_STAGES]; // entire stage glows
  225. bool mEmissive[MAX_STAGES];
  226. Point2I mCellIndex[MAX_STAGES];
  227. Point2I mCellLayout[MAX_STAGES];
  228. U32 mCellSize[MAX_STAGES];
  229. bool mNormalMapAtlas[MAX_STAGES];
  230. /// Special array of UVs for imposter rendering.
  231. /// @see TSLastDetail
  232. Vector<RectF> mImposterUVs;
  233. /// Specual imposter rendering paramters.
  234. /// @see TSLastDetail
  235. Point4F mImposterLimits;
  236. /// If the stage should use anisotropic filtering.
  237. bool mUseAnisotropic[MAX_STAGES];
  238. // Deferred Shading: Metalness
  239. bool mUseMetalness[MAX_STAGES];
  240. bool mDoubleSided;
  241. String mCubemapName;
  242. CubemapData* mCubemapData;
  243. bool mDynamicCubemap;
  244. // Deferred Shading
  245. F32 mMatInfoFlags[MAX_STAGES];
  246. bool mTranslucent;
  247. BlendOp mTranslucentBlendOp;
  248. bool mTranslucentZWrite;
  249. /// A generic setting which tells the system to skip
  250. /// generation of shadows from this material.
  251. bool mCastShadows;
  252. bool mCastDynamicShadows;
  253. bool mAlphaTest;
  254. U32 mAlphaRef;
  255. bool mPlanarReflection;
  256. bool mAutoGenerated;
  257. static bool sAllowTextureTargetAssignment;
  258. ///@{
  259. /// Behavioral properties.
  260. bool mShowFootprints; ///< If true, show footprints when walking on surface with this material. Defaults to false.
  261. bool mShowDust; ///< If true, show dust emitters (footpuffs, hover trails, etc) when on surface with this material. Defaults to false.
  262. /// Color to use for particle effects and such when located on this material.
  263. ColorF mEffectColor[ NUM_EFFECT_COLOR_STAGES ];
  264. /// Footstep sound to play when walking on surface with this material.
  265. /// Numeric ID of footstep sound defined on player datablock (0 == soft,
  266. /// 1 == hard, 2 == metal, 3 == snow).
  267. /// Defaults to -1 which deactivates default sound.
  268. /// @see mFootstepSoundCustom
  269. S32 mFootstepSoundId;
  270. S32 mImpactSoundId;
  271. /// Sound effect to play when walking on surface with this material.
  272. /// If defined, overrides mFootstepSoundId.
  273. /// @see mFootstepSoundId
  274. SFXTrack* mFootstepSoundCustom;
  275. SFXTrack* mImpactSoundCustom;
  276. F32 mFriction; ///< Friction coefficient when moving along surface.
  277. F32 mDirectSoundOcclusion; ///< Amount of volume occlusion on direct sounds.
  278. F32 mReverbSoundOcclusion; ///< Amount of volume occlusion on reverb sounds.
  279. ///@}
  280. String mMapTo; // map Material to this texture name
  281. ///
  282. /// Material interface
  283. ///
  284. Material();
  285. /// Allocates and returns a BaseMatInstance for this material. Caller is responsible
  286. /// for freeing the instance
  287. virtual BaseMatInstance* createMatInstance();
  288. virtual bool isTranslucent() const { return mTranslucent && mTranslucentBlendOp != Material::None; }
  289. virtual bool isDoubleSided() const { return mDoubleSided; }
  290. virtual bool isAutoGenerated() const { return mAutoGenerated; }
  291. virtual void setAutoGenerated(bool isAutoGenerated) { mAutoGenerated = isAutoGenerated; }
  292. virtual bool isLightmapped() const;
  293. virtual bool castsShadows() const { return mCastShadows; }
  294. virtual bool castsDynamicShadows() const { return mCastDynamicShadows; }
  295. const String &getPath() const { return mPath; }
  296. void flush();
  297. /// Re-initializes all the material instances
  298. /// that use this material.
  299. void reload();
  300. /// Called to update time based parameters for a material. Ensures
  301. /// that it only happens once per tick.
  302. void updateTimeBasedParams();
  303. // SimObject
  304. virtual bool onAdd();
  305. virtual void onRemove();
  306. virtual void inspectPostApply();
  307. virtual bool writeField( StringTableEntry fieldname, const char *value );
  308. //
  309. // ConsoleObject interface
  310. //
  311. static void initPersistFields();
  312. // Accumulation
  313. static bool _setAccuEnabled( void *object, const char *index, const char *data );
  314. DECLARE_CONOBJECT(Material);
  315. protected:
  316. // Per material animation parameters
  317. U32 mLastUpdateTime;
  318. String mPath;
  319. static EnumTable mAnimFlagTable;
  320. static EnumTable mBlendOpTable;
  321. static EnumTable mWaveTypeTable;
  322. /// Map this material to the texture specified
  323. /// in the "mapTo" data variable.
  324. virtual void _mapMaterial();
  325. private:
  326. static GFXCubemapHandle smNormalizeCube;
  327. };
  328. typedef Material::AnimType MaterialAnimType;
  329. typedef Material::BlendOp MaterialBlendOp;
  330. typedef Material::WaveType MaterialWaveType;
  331. DefineBitfieldType( MaterialAnimType );
  332. DefineEnumType( MaterialBlendOp );
  333. DefineEnumType( MaterialWaveType );
  334. #endif // _MATERIALDEFINITION_H_