materialDefinition.h 14 KB

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