materialDefinition.h 13 KB

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