materialDefinition.h 12 KB

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