materialDefinition.cpp 29 KB

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