materialDefinition.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. #include "platform/platform.h"
  23. #include "materials/materialDefinition.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/engineAPI.h"
  26. #include "math/mathTypes.h"
  27. #include "materials/materialManager.h"
  28. #include "sceneData.h"
  29. #include "gfx/sim/cubemapData.h"
  30. #include "gfx/gfxCubemap.h"
  31. #include "math/mathIO.h"
  32. #include "materials/matInstance.h"
  33. #include "sfx/sfxTrack.h"
  34. #include "sfx/sfxTypes.h"
  35. #include "core/util/safeDelete.h"
  36. #include "T3D/accumulationVolume.h"
  37. IMPLEMENT_CONOBJECT( Material );
  38. ConsoleDocClass( Material,
  39. "@brief A material in Torque 3D is a data structure that describes a surface.\n\n"
  40. "It contains many different types of information for rendering properties. "
  41. "Torque 3D generates shaders from Material definitions. The shaders are compiled "
  42. "at runtime and output into the example/shaders directory. Any errors or warnings "
  43. "generated from compiling the procedurally generated shaders are output to the console "
  44. "as well as the output window in the Visual C IDE.\n\n"
  45. "@tsexample\n"
  46. "singleton Material(DECAL_scorch)\n"
  47. "{\n"
  48. " baseTex[0] = \"./scorch_decal.png\";\n"
  49. " vertColor[ 0 ] = true;\n\n"
  50. " translucent = true;\n"
  51. " translucentBlendOp = None;\n"
  52. " translucentZWrite = true;\n"
  53. " alphaTest = true;\n"
  54. " alphaRef = 84;\n"
  55. "};\n"
  56. "@endtsexample\n\n"
  57. "@see Rendering\n"
  58. "@see ShaderData\n"
  59. "@ingroup GFX\n");
  60. ImplementBitfieldType( MaterialAnimType,
  61. "The type of animation effect to apply to this material.\n"
  62. "@ingroup GFX\n\n")
  63. { Material::Scroll, "Scroll", "Scroll the material along the X/Y axis.\n" },
  64. { Material::Rotate, "Rotate" , "Rotate the material around a point.\n"},
  65. { Material::Wave, "Wave" , "Warps the material with an animation using Sin, Triangle or Square mathematics.\n"},
  66. { Material::Scale, "Scale", "Scales the material larger and smaller with a pulsing effect.\n" },
  67. { Material::Sequence, "Sequence", "Enables the material to have multiple frames of animation in its imagemap.\n" }
  68. EndImplementBitfieldType;
  69. ImplementEnumType( MaterialBlendOp,
  70. "The type of graphical blending operation to apply to this material\n"
  71. "@ingroup GFX\n\n")
  72. { Material::None, "None", "Disable blending for this material." },
  73. { Material::Mul, "Mul", "Multiplicative blending." },
  74. { Material::Add, "Add", "Adds the color of the material to the frame buffer with full alpha for each pixel." },
  75. { Material::AddAlpha, "AddAlpha", "The color is modulated by the alpha channel before being added to the frame buffer." },
  76. { Material::Sub, "Sub", "Subtractive Blending. Reverses the color model, causing dark colors to have a stronger visual effect." },
  77. { Material::LerpAlpha, "LerpAlpha", "Linearly interpolates between Material color and frame buffer color based on alpha." }
  78. EndImplementEnumType;
  79. ImplementEnumType( MaterialWaveType,
  80. "When using the Wave material animation, one of these Wave Types will be used to determine the type of wave to display.\n"
  81. "@ingroup GFX\n")
  82. { Material::Sin, "Sin", "Warps the material along a curved Sin Wave." },
  83. { Material::Triangle, "Triangle", "Warps the material along a sharp Triangle Wave." },
  84. { Material::Square, "Square", "Warps the material along a wave which transitions between two oppposite states. As a Square Wave, the transition is quick and sudden." },
  85. EndImplementEnumType;
  86. bool Material::sAllowTextureTargetAssignment = false;
  87. GFXCubemap * Material::GetNormalizeCube()
  88. {
  89. if(smNormalizeCube)
  90. return smNormalizeCube;
  91. smNormalizeCube = GFX->createCubemap();
  92. smNormalizeCube->initNormalize(64);
  93. return smNormalizeCube;
  94. }
  95. GFXCubemapHandle Material::smNormalizeCube;
  96. Material::Material()
  97. {
  98. for( U32 i=0; i<MAX_STAGES; i++ )
  99. {
  100. mDiffuse[i].set( 1.0f, 1.0f, 1.0f, 1.0f );
  101. mDiffuseMapSRGB[i] = true;
  102. mSpecular[i].set( 1.0f, 1.0f, 1.0f, 1.0f );
  103. mSpecularPower[i] = 8.0f;
  104. mSpecularStrength[i] = 1.0f;
  105. mPixelSpecular[i] = false;
  106. mAccuEnabled[i] = false;
  107. mAccuScale[i] = 1.0f;
  108. mAccuDirection[i] = 1.0f;
  109. mAccuStrength[i] = 0.6f;
  110. mAccuCoverage[i] = 0.9f;
  111. mAccuSpecular[i] = 16.0f;
  112. mParallaxScale[i] = 0.0f;
  113. mVertLit[i] = false;
  114. mVertColor[ i ] = false;
  115. mGlow[i] = false;
  116. mEmissive[i] = false;
  117. mDetailScale[i].set( 2.0f, 2.0f );
  118. mDetailNormalMapStrength[i] = 1.0f;
  119. mMinnaertConstant[i] = -1.0f;
  120. mSubSurface[i] = false;
  121. mSubSurfaceColor[i].set( 1.0f, 0.2f, 0.2f, 1.0f );
  122. mSubSurfaceRolloff[i] = 0.2f;
  123. mAnimFlags[i] = 0;
  124. mScrollDir[i].set( 0.0f, 0.0f );
  125. mScrollSpeed[i] = 0.0f;
  126. mScrollOffset[i].set( 0.0f, 0.0f );
  127. mRotSpeed[i] = 0.0f;
  128. mRotPivotOffset[i].set( 0.0f, 0.0f );
  129. mRotPos[i] = 0.0f;
  130. mWavePos[i] = 0.0f;
  131. mWaveFreq[i] = 0.0f;
  132. mWaveAmp[i] = 0.0f;
  133. mWaveType[i] = 0;
  134. mSeqFramePerSec[i] = 0.0f;
  135. mSeqSegSize[i] = 0.0f;
  136. // Deferred Shading
  137. mMatInfoFlags[i] = 0.0f;
  138. }
  139. dMemset(mCellIndex, 0, sizeof(mCellIndex));
  140. dMemset(mCellLayout, 0, sizeof(mCellLayout));
  141. dMemset(mCellSize, 0, sizeof(mCellSize));
  142. dMemset(mNormalMapAtlas, 0, sizeof(mNormalMapAtlas));
  143. dMemset(mUseAnisotropic, 0, sizeof(mUseAnisotropic));
  144. // Deferred Shading : Metalness
  145. dMemset(mUseMetalness, 0, sizeof(mUseMetalness));
  146. mImposterLimits = Point4F::Zero;
  147. mDoubleSided = false;
  148. mTranslucent = false;
  149. mTranslucentBlendOp = LerpAlpha;
  150. mTranslucentZWrite = false;
  151. mAlphaTest = false;
  152. mAlphaRef = 1;
  153. mCastShadows = true;
  154. mCastDynamicShadows = false;
  155. mPlanarReflection = false;
  156. mCubemapData = NULL;
  157. mDynamicCubemap = NULL;
  158. mLastUpdateTime = 0;
  159. mAutoGenerated = false;
  160. mShowDust = false;
  161. mShowFootprints = true;
  162. dMemset( mEffectColor, 0, sizeof( mEffectColor ) );
  163. mFootstepSoundId = -1; mImpactSoundId = -1;
  164. mFootstepSoundCustom = 0; mImpactSoundCustom = 0;
  165. mFriction = 0.0;
  166. mDirectSoundOcclusion = 1.f;
  167. mReverbSoundOcclusion = 1.0;
  168. }
  169. void Material::initPersistFields()
  170. {
  171. addField("mapTo", TypeRealString, Offset(mMapTo, Material),
  172. "Used to map this material to the material name used by TSShape." );
  173. addArray( "Stages", MAX_STAGES );
  174. addField("diffuseColor", TypeColorF, Offset(mDiffuse, Material), MAX_STAGES,
  175. "This color is multiplied against the diffuse texture color. If no diffuse texture "
  176. "is present this is the material color." );
  177. addField("diffuseMap", TypeImageFilename, Offset(mDiffuseMapFilename, Material), MAX_STAGES,
  178. "The diffuse color texture map." );
  179. addField("diffuseMapSRGB", TypeBool, Offset(mDiffuseMapSRGB, Material), MAX_STAGES,
  180. "Enable sRGB for the diffuse color texture map.");
  181. addField("overlayMap", TypeImageFilename, Offset(mOverlayMapFilename, Material), MAX_STAGES,
  182. "A secondary diffuse color texture map which will use the second texcoord of a mesh." );
  183. addField("lightMap", TypeImageFilename, Offset(mLightMapFilename, Material), MAX_STAGES,
  184. "The lightmap texture used with pureLight." );
  185. addField("toneMap", TypeImageFilename, Offset(mToneMapFilename, Material), MAX_STAGES,
  186. "The tonemap texture used with pureLight.");
  187. addField("detailMap", TypeImageFilename, Offset(mDetailMapFilename, Material), MAX_STAGES,
  188. "A typically greyscale detail texture additively blended into the material." );
  189. addField("detailScale", TypePoint2F, Offset(mDetailScale, Material), MAX_STAGES,
  190. "The scale factor for the detail map." );
  191. addField( "normalMap", TypeImageFilename, Offset(mNormalMapFilename, Material), MAX_STAGES,
  192. "The normal map texture. You can use the DXTnm format only when per-pixel "
  193. "specular highlights are disabled, or a specular map is in use." );
  194. addField( "detailNormalMap", TypeImageFilename, Offset(mDetailNormalMapFilename, Material), MAX_STAGES,
  195. "A second normal map texture applied at the detail scale. You can use the DXTnm "
  196. "format only when per-pixel specular highlights are disabled." );
  197. addField( "detailNormalMapStrength", TypeF32, Offset(mDetailNormalMapStrength, Material), MAX_STAGES,
  198. "Used to scale the strength of the detail normal map when blended with the base normal map." );
  199. addField("specular", TypeColorF, Offset(mSpecular, Material), MAX_STAGES,
  200. "The color of the specular highlight when not using a specularMap." );
  201. addField("specularPower", TypeF32, Offset(mSpecularPower, Material), MAX_STAGES,
  202. "The hardness of the specular highlight when not using a specularMap." );
  203. addField("specularStrength", TypeF32, Offset(mSpecularStrength, Material), MAX_STAGES,
  204. "The strength of the specular highlight when not using a specularMap." );
  205. addField("pixelSpecular", TypeBool, Offset(mPixelSpecular, Material), MAX_STAGES,
  206. "This enables per-pixel specular highlights controlled by the alpha channel of the "
  207. "normal map texture. Note that if pixel specular is enabled the DXTnm format will not "
  208. "work with your normal map, unless you are also using a specular map." );
  209. addProtectedField( "accuEnabled", TYPEID< bool >(), Offset( mAccuEnabled, Material ),
  210. &_setAccuEnabled, &defaultProtectedGetFn, MAX_STAGES, "Accumulation texture." );
  211. addField("accuScale", TypeF32, Offset(mAccuScale, Material), MAX_STAGES,
  212. "The scale that is applied to the accu map texture. You can use this to fit the texture to smaller or larger objects.");
  213. addField("accuDirection", TypeF32, Offset(mAccuDirection, Material), MAX_STAGES,
  214. "The direction of the accumulation. Chose whether you want the accu map to go from top to bottom (ie. snow) or upwards (ie. mold).");
  215. addField("accuStrength", TypeF32, Offset(mAccuStrength, Material), MAX_STAGES,
  216. "The strength of the accu map. This changes the transparency of the accu map texture. Make it subtle or add more contrast.");
  217. addField("accuCoverage", TypeF32, Offset(mAccuCoverage, Material), MAX_STAGES,
  218. "The coverage ratio of the accu map texture. Use this to make the entire shape pick up some of the accu map texture or none at all.");
  219. addField("accuSpecular", TypeF32, Offset(mAccuSpecular, Material), MAX_STAGES,
  220. "Changes specularity to this value where the accumulated material is present.");
  221. addField( "specularMap", TypeImageFilename, Offset(mSpecularMapFilename, Material), MAX_STAGES,
  222. "The specular map texture. The RGB channels of this texture provide a per-pixel replacement for the 'specular' parameter on the material. "
  223. "If this texture contains alpha information, the alpha channel of the texture will be used as the gloss map. "
  224. "This provides a per-pixel replacement for the 'specularPower' on the material" );
  225. addField( "parallaxScale", TypeF32, Offset(mParallaxScale, Material), MAX_STAGES,
  226. "Enables parallax mapping and defines the scale factor for the parallax effect. Typically "
  227. "this value is less than 0.4 else the effect breaks down." );
  228. addField( "useAnisotropic", TypeBool, Offset(mUseAnisotropic, Material), MAX_STAGES,
  229. "Use anisotropic filtering for the textures of this stage." );
  230. addField("vertLit", TypeBool, Offset(mVertLit, Material), MAX_STAGES,
  231. "If true the vertex color is used for lighting." );
  232. addField( "vertColor", TypeBool, Offset( mVertColor, Material ), MAX_STAGES,
  233. "If enabled, vertex colors are premultiplied with diffuse colors." );
  234. addField("minnaertConstant", TypeF32, Offset(mMinnaertConstant, Material), MAX_STAGES,
  235. "The Minnaert shading constant value. Must be greater than 0 to enable the effect." );
  236. addField("subSurface", TypeBool, Offset(mSubSurface, Material), MAX_STAGES,
  237. "Enables the subsurface scattering approximation." );
  238. addField("subSurfaceColor", TypeColorF, Offset(mSubSurfaceColor, Material), MAX_STAGES,
  239. "The color used for the subsurface scattering approximation." );
  240. addField("subSurfaceRolloff", TypeF32, Offset(mSubSurfaceRolloff, Material), MAX_STAGES,
  241. "The 0 to 1 rolloff factor used in the subsurface scattering approximation." );
  242. addField("glow", TypeBool, Offset(mGlow, Material), MAX_STAGES,
  243. "Enables rendering this material to the glow buffer." );
  244. addField("emissive", TypeBool, Offset(mEmissive, Material), MAX_STAGES,
  245. "Enables emissive lighting for the material." );
  246. addField("doubleSided", TypeBool, Offset(mDoubleSided, Material),
  247. "Disables backface culling casing surfaces to be double sided. "
  248. "Note that the lighting on the backside will be a mirror of the front "
  249. "side of the surface." );
  250. addField("animFlags", TYPEID< AnimType >(), Offset(mAnimFlags, Material), MAX_STAGES,
  251. "The types of animation to play on this material." );
  252. addField("scrollDir", TypePoint2F, Offset(mScrollDir, Material), MAX_STAGES,
  253. "The scroll direction in UV space when scroll animation is enabled." );
  254. addField("scrollSpeed", TypeF32, Offset(mScrollSpeed, Material), MAX_STAGES,
  255. "The speed to scroll the texture in UVs per second when scroll animation is enabled." );
  256. addField("rotSpeed", TypeF32, Offset(mRotSpeed, Material), MAX_STAGES,
  257. "The speed to rotate the texture in degrees per second when rotation animation is enabled." );
  258. addField("rotPivotOffset", TypePoint2F, Offset(mRotPivotOffset, Material), MAX_STAGES,
  259. "The piviot position in UV coordinates to center the rotation animation." );
  260. addField("waveType", TYPEID< WaveType >(), Offset(mWaveType, Material), MAX_STAGES,
  261. "The type of wave animation to perform when wave animation is enabled." );
  262. addField("waveFreq", TypeF32, Offset(mWaveFreq, Material), MAX_STAGES,
  263. "The wave frequency when wave animation is enabled." );
  264. addField("waveAmp", TypeF32, Offset(mWaveAmp, Material), MAX_STAGES,
  265. "The wave amplitude when wave animation is enabled." );
  266. addField("sequenceFramePerSec", TypeF32, Offset(mSeqFramePerSec, Material), MAX_STAGES,
  267. "The number of frames per second for frame based sequence animations if greater than zero." );
  268. addField("sequenceSegmentSize", TypeF32, Offset(mSeqSegSize, Material), MAX_STAGES,
  269. "The size of each frame in UV units for sequence animations." );
  270. // Texture atlasing
  271. addField("cellIndex", TypePoint2I, Offset(mCellIndex, Material), MAX_STAGES,
  272. "@internal" );
  273. addField("cellLayout", TypePoint2I, Offset(mCellLayout, Material), MAX_STAGES,
  274. "@internal");
  275. addField("cellSize", TypeS32, Offset(mCellSize, Material), MAX_STAGES,
  276. "@internal");
  277. addField("bumpAtlas", TypeBool, Offset(mNormalMapAtlas, Material), MAX_STAGES,
  278. "@internal");
  279. // For backwards compatibility.
  280. //
  281. // They point at the new 'map' fields, but reads always return
  282. // an empty string and writes only apply if the value is not empty.
  283. //
  284. addProtectedField("baseTex", TypeImageFilename, Offset(mDiffuseMapFilename, Material),
  285. defaultProtectedSetNotEmptyFn, emptyStringProtectedGetFn, MAX_STAGES,
  286. "For backwards compatibility.\n@see diffuseMap\n" );
  287. addProtectedField("detailTex", TypeImageFilename, Offset(mDetailMapFilename, Material),
  288. defaultProtectedSetNotEmptyFn, emptyStringProtectedGetFn, MAX_STAGES,
  289. "For backwards compatibility.\n@see detailMap\n");
  290. addProtectedField("overlayTex", TypeImageFilename, Offset(mOverlayMapFilename, Material),
  291. defaultProtectedSetNotEmptyFn, emptyStringProtectedGetFn, MAX_STAGES,
  292. "For backwards compatibility.\n@see overlayMap\n");
  293. addProtectedField("bumpTex", TypeImageFilename, Offset(mNormalMapFilename, Material),
  294. defaultProtectedSetNotEmptyFn, emptyStringProtectedGetFn, MAX_STAGES,
  295. "For backwards compatibility.\n@see normalMap\n");
  296. addProtectedField("colorMultiply", TypeColorF, Offset(mDiffuse, Material),
  297. defaultProtectedSetNotEmptyFn, emptyStringProtectedGetFn, MAX_STAGES,
  298. "For backwards compatibility.\n@see diffuseColor\n");
  299. endArray( "Stages" );
  300. addField( "castShadows", TypeBool, Offset(mCastShadows, Material),
  301. "If set to false the lighting system will not cast shadows from this material." );
  302. addField( "castDynamicShadows", TypeBool, Offset(mCastDynamicShadows, Material),
  303. "If set to false the lighting system will not cast dynamic shadows from this material." );
  304. addField("planarReflection", TypeBool, Offset(mPlanarReflection, Material), "@internal" );
  305. addField("translucent", TypeBool, Offset(mTranslucent, Material),
  306. "If true this material is translucent blended." );
  307. addField("translucentBlendOp", TYPEID< BlendOp >(), Offset(mTranslucentBlendOp, Material),
  308. "The type of blend operation to use when the material is translucent." );
  309. addField("translucentZWrite", TypeBool, Offset(mTranslucentZWrite, Material),
  310. "If enabled and the material is translucent it will write into the depth buffer." );
  311. addField("alphaTest", TypeBool, Offset(mAlphaTest, Material),
  312. "Enables alpha test when rendering the material.\n@see alphaRef\n" );
  313. addField("alphaRef", TypeS32, Offset(mAlphaRef, Material),
  314. "The alpha reference value for alpha testing. Must be between 0 to 255.\n@see alphaTest\n" );
  315. addField("cubemap", TypeRealString, Offset(mCubemapName, Material),
  316. "The name of a CubemapData for environment mapping." );
  317. addField("dynamicCubemap", TypeBool, Offset(mDynamicCubemap, Material),
  318. "Enables the material to use the dynamic cubemap from the ShapeBase object its applied to." );
  319. addGroup( "Behavioral" );
  320. addField( "showFootprints", TypeBool, Offset( mShowFootprints, Material ),
  321. "Whether to show player footprint decals on this material.\n\n"
  322. "@see PlayerData::decalData" );
  323. addField( "showDust", TypeBool, Offset( mShowDust, Material ),
  324. "Whether to emit dust particles from a shape moving over the material. This is, for example, used by "
  325. "vehicles or players to decide whether to show dust trails." );
  326. addField( "effectColor", TypeColorF, Offset( mEffectColor, Material ), NUM_EFFECT_COLOR_STAGES,
  327. "If #showDust is true, this is the set of colors to use for the ParticleData of the dust "
  328. "emitter.\n\n"
  329. "@see ParticleData::colors" );
  330. addField( "footstepSoundId", TypeS32, Offset( mFootstepSoundId, Material ),
  331. "What sound to play from the PlayerData sound list when the player walks over the material. -1 (default) to not play any sound.\n"
  332. "\n"
  333. "The IDs are:\n\n"
  334. "- 0: PlayerData::FootSoftSound\n"
  335. "- 1: PlayerData::FootHardSound\n"
  336. "- 2: PlayerData::FootMetalSound\n"
  337. "- 3: PlayerData::FootSnowSound\n"
  338. "- 4: PlayerData::FootShallowSound\n"
  339. "- 5: PlayerData::FootWadingSound\n"
  340. "- 6: PlayerData::FootUnderwaterSound\n"
  341. "- 7: PlayerData::FootBubblesSound\n"
  342. "- 8: PlayerData::movingBubblesSound\n"
  343. "- 9: PlayerData::waterBreathSound\n"
  344. "- 10: PlayerData::impactSoftSound\n"
  345. "- 11: PlayerData::impactHardSound\n"
  346. "- 12: PlayerData::impactMetalSound\n"
  347. "- 13: PlayerData::impactSnowSound\n"
  348. "- 14: PlayerData::impactWaterEasy\n"
  349. "- 15: PlayerData::impactWaterMedium\n"
  350. "- 16: PlayerData::impactWaterHard\n"
  351. "- 17: PlayerData::exitingWater\n" );
  352. addField( "customFootstepSound", TypeSFXTrackName, Offset( mFootstepSoundCustom, Material ),
  353. "The sound to play when the player walks over the material. If this is set, it overrides #footstepSoundId. This field is "
  354. "useful for directly assigning custom footstep sounds to materials without having to rely on the PlayerData sound assignment.\n\n"
  355. "@warn Be aware that materials are client-side objects. This means that the SFXTracks assigned to materials must be client-side, too." );
  356. addField( "impactSoundId", TypeS32, Offset( mImpactSoundId, Material ),
  357. "What sound to play from the PlayerData sound list when the player impacts on the surface with a velocity equal or greater "
  358. "than PlayerData::groundImpactMinSpeed.\n\n"
  359. "For a list of IDs, see #footstepSoundId" );
  360. addField( "customImpactSound", TypeSFXTrackName, Offset( mImpactSoundCustom, Material ),
  361. "The sound to play when the player impacts on the surface with a velocity equal or greater than PlayerData::groundImpactMinSpeed. "
  362. "If this is set, it overrides #impactSoundId. This field is useful for directly assigning custom impact sounds to materials "
  363. "without having to rely on the PlayerData sound assignment.\n\n"
  364. "@warn Be aware that materials are client-side objects. This means that the SFXTracks assigned to materials must be client-side, too." );
  365. //Deactivate these for the moment as they are not used.
  366. #if 0
  367. addField( "friction", TypeF32, Offset( mFriction, Material ) );
  368. addField( "directSoundOcclusion", TypeF32, Offset( mDirectSoundOcclusion, Material ) );
  369. addField( "reverbSoundOcclusion", TypeF32, Offset( mReverbSoundOcclusion, Material ) );
  370. #endif
  371. endGroup( "Behavioral" );
  372. Parent::initPersistFields();
  373. }
  374. bool Material::writeField( StringTableEntry fieldname, const char *value )
  375. {
  376. // Never allow the old field names to be written.
  377. if ( fieldname == StringTable->insert("baseTex") ||
  378. fieldname == StringTable->insert("detailTex") ||
  379. fieldname == StringTable->insert("overlayTex") ||
  380. fieldname == StringTable->insert("bumpTex") ||
  381. fieldname == StringTable->insert("envTex") ||
  382. fieldname == StringTable->insert("colorMultiply") )
  383. return false;
  384. return Parent::writeField( fieldname, value );
  385. }
  386. bool Material::onAdd()
  387. {
  388. if (Parent::onAdd() == false)
  389. return false;
  390. mCubemapData = dynamic_cast<CubemapData*>(Sim::findObject( mCubemapName ) );
  391. if( mTranslucentBlendOp >= NumBlendTypes || mTranslucentBlendOp < 0 )
  392. {
  393. Con::errorf( "Invalid blend op in material: %s", getName() );
  394. mTranslucentBlendOp = LerpAlpha;
  395. }
  396. SimSet *matSet = MATMGR->getMaterialSet();
  397. if( matSet )
  398. matSet->addObject( (SimObject*)this );
  399. // save the current script path for texture lookup later
  400. const String scriptFile = Con::getVariable("$Con::File"); // current script file - local materials.cs
  401. String::SizeType slash = scriptFile.find( '/', scriptFile.length(), String::Right );
  402. if ( slash != String::NPos )
  403. mPath = scriptFile.substr( 0, slash + 1 );
  404. _mapMaterial();
  405. return true;
  406. }
  407. void Material::onRemove()
  408. {
  409. smNormalizeCube = NULL;
  410. Parent::onRemove();
  411. }
  412. void Material::inspectPostApply()
  413. {
  414. Parent::inspectPostApply();
  415. // Reload the material instances which
  416. // use this material.
  417. if ( isProperlyAdded() )
  418. reload();
  419. }
  420. bool Material::isLightmapped() const
  421. {
  422. bool ret = false;
  423. for( U32 i=0; i<MAX_STAGES; i++ )
  424. ret |= mLightMapFilename[i].isNotEmpty() || mToneMapFilename[i].isNotEmpty() || mVertLit[i];
  425. return ret;
  426. }
  427. void Material::updateTimeBasedParams()
  428. {
  429. U32 lastTime = MATMGR->getLastUpdateTime();
  430. F32 dt = MATMGR->getDeltaTime();
  431. if (mLastUpdateTime != lastTime)
  432. {
  433. for (U32 i = 0; i < MAX_STAGES; i++)
  434. {
  435. mScrollOffset[i] += mScrollDir[i] * mScrollSpeed[i] * dt;
  436. mRotPos[i] += mRotSpeed[i] * dt;
  437. mWavePos[i] += mWaveFreq[i] * dt;
  438. }
  439. mLastUpdateTime = lastTime;
  440. }
  441. }
  442. void Material::_mapMaterial()
  443. {
  444. if( String(getName()).isEmpty() )
  445. {
  446. Con::warnf( "[Material::mapMaterial] - Cannot map unnamed Material" );
  447. return;
  448. }
  449. // If mapTo not defined in script, try to use the base texture name instead
  450. if( mMapTo.isEmpty() )
  451. {
  452. if ( mDiffuseMapFilename[0].isEmpty() )
  453. return;
  454. else
  455. {
  456. // extract filename from base texture
  457. if ( mDiffuseMapFilename[0].isNotEmpty() )
  458. {
  459. U32 slashPos = mDiffuseMapFilename[0].find('/',0,String::Right);
  460. if (slashPos == String::NPos)
  461. // no '/' character, must be no path, just the filename
  462. mMapTo = mDiffuseMapFilename[0];
  463. else
  464. // use everything after the last slash
  465. mMapTo = mDiffuseMapFilename[0].substr(slashPos+1, mDiffuseMapFilename[0].length() - slashPos - 1);
  466. }
  467. }
  468. }
  469. // add mapping
  470. MATMGR->mapMaterial(mMapTo,getName());
  471. }
  472. BaseMatInstance* Material::createMatInstance()
  473. {
  474. return new MatInstance(*this);
  475. }
  476. void Material::flush()
  477. {
  478. MATMGR->flushInstance( this );
  479. }
  480. void Material::reload()
  481. {
  482. MATMGR->reInitInstance( this );
  483. }
  484. void Material::StageData::getFeatureSet( FeatureSet *outFeatures ) const
  485. {
  486. TextureTable::ConstIterator iter = mTextures.begin();
  487. for ( ; iter != mTextures.end(); iter++ )
  488. {
  489. if ( iter->value.isValid() )
  490. outFeatures->addFeature( *iter->key );
  491. }
  492. }
  493. DefineConsoleMethod( Material, flush, void, (),,
  494. "Flushes all material instances that use this material." )
  495. {
  496. object->flush();
  497. }
  498. DefineConsoleMethod( Material, reload, void, (),,
  499. "Reloads all material instances that use this material." )
  500. {
  501. object->reload();
  502. }
  503. DefineConsoleMethod( Material, dumpInstances, void, (),,
  504. "Dumps a formatted list of the currently allocated material instances for this material to the console." )
  505. {
  506. MATMGR->dumpMaterialInstances( object );
  507. }
  508. DefineConsoleMethod( Material, getAnimFlags, const char*, (U32 id), , "" )
  509. {
  510. char * animFlags = Con::getReturnBuffer(512);
  511. if(object->mAnimFlags[ id ] & Material::Scroll)
  512. {
  513. if(dStrcmp( animFlags, "" ) == 0)
  514. dStrcpy( animFlags, "$Scroll", 512 );
  515. }
  516. if(object->mAnimFlags[ id ] & Material::Rotate)
  517. {
  518. if(dStrcmp( animFlags, "" ) == 0)
  519. dStrcpy( animFlags, "$Rotate", 512 );
  520. else
  521. dStrcat( animFlags, " | $Rotate", 512);
  522. }
  523. if(object->mAnimFlags[ id ] & Material::Wave)
  524. {
  525. if(dStrcmp( animFlags, "" ) == 0)
  526. dStrcpy( animFlags, "$Wave", 512 );
  527. else
  528. dStrcat( animFlags, " | $Wave", 512);
  529. }
  530. if(object->mAnimFlags[ id ] & Material::Scale)
  531. {
  532. if(dStrcmp( animFlags, "" ) == 0)
  533. dStrcpy( animFlags, "$Scale", 512 );
  534. else
  535. dStrcat( animFlags, " | $Scale", 512);
  536. }
  537. if(object->mAnimFlags[ id ] & Material::Sequence)
  538. {
  539. if(dStrcmp( animFlags, "" ) == 0)
  540. dStrcpy( animFlags, "$Sequence", 512 );
  541. else
  542. dStrcat( animFlags, " | $Sequence", 512);
  543. }
  544. return animFlags;
  545. }
  546. DefineConsoleMethod(Material, getFilename, const char*, (),, "Get filename of material")
  547. {
  548. SimObject *material = static_cast<SimObject *>(object);
  549. return material->getFilename();
  550. }
  551. DefineConsoleMethod( Material, isAutoGenerated, bool, (),,
  552. "Returns true if this Material was automatically generated by MaterialList::mapMaterials()" )
  553. {
  554. return object->isAutoGenerated();
  555. }
  556. DefineConsoleMethod( Material, setAutoGenerated, void, (bool isAutoGenerated), ,
  557. "setAutoGenerated(bool isAutoGenerated): Set whether or not the Material is autogenerated." )
  558. {
  559. object->setAutoGenerated(isAutoGenerated);
  560. }
  561. // Accumulation
  562. bool Material::_setAccuEnabled( void *object, const char *index, const char *data )
  563. {
  564. Material* mat = reinterpret_cast< Material* >( object );
  565. if ( index )
  566. {
  567. U32 i = dAtoui(index);
  568. mat->mAccuEnabled[i] = dAtob(data);
  569. AccumulationVolume::refreshVolumes();
  570. }
  571. return true;
  572. }