materialDefinition.h 12 KB

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