terrMaterial.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "terrain/terrMaterial.h"
  24. #include "console/consoleTypes.h"
  25. #include "gfx/gfxTextureManager.h"
  26. #include "gfx/bitmap/gBitmap.h"
  27. #ifdef TORQUE_TOOLS
  28. #include "console/persistenceManager.h"
  29. #endif
  30. #include "T3D/assets/TerrainMaterialAsset.h"
  31. #include <string>
  32. IMPLEMENT_CONOBJECT( TerrainMaterial );
  33. ConsoleDocClass( TerrainMaterial,
  34. "@brief The TerrainMaterial class orginizes the material settings "
  35. "for a single terrain material layer.\n\n"
  36. "@note You should not be creating TerrainMaterials by hand in code. "
  37. "All TerrainMaterials should be created in the editors, as intended "
  38. "by the system.\n\n"
  39. "@tsexample\n"
  40. "// Created by the Terrain Painter tool in the World Editor\n"
  41. "new TerrainMaterial()\n"
  42. "{\n"
  43. " internalName = \"grass1\";\n"
  44. " diffuseMap = \"art/terrains/Test/grass1\";\n"
  45. " detailMap = \"art/terrains/Test/grass1_d\";\n"
  46. " detailSize = \"10\";\n"
  47. " isManaged = \"1\";\n"
  48. " detailBrightness = \"1\";\n"
  49. " Enabled = \"1\";\n"
  50. " diffuseSize = \"200\";\n"
  51. "};\n"
  52. "@endtsexample\n\n"
  53. "@see Materials\n"
  54. "@ingroup enviroMisc\n");
  55. TerrainMaterial::TerrainMaterial()
  56. : mDiffuseSize( 500.0f ),
  57. mDetailSize( 5.0f ),
  58. mDetailStrength( 1.0f ),
  59. mDetailDistance( 50.0f ),
  60. mSideProjection( false ),
  61. mMacroSize( 200.0f ),
  62. mMacroStrength( 0.7f ),
  63. mMacroDistance( 500.0f ),
  64. mParallaxScale( 0.0f ),
  65. mBlendDepth( 0.0f ),
  66. mBlendContrast( 1.0f ),
  67. mIsSRGB(false),
  68. mInvertRoughness(false)
  69. {
  70. INIT_ASSET(DiffuseMap);
  71. INIT_ASSET(NormalMap);
  72. INIT_ASSET(DetailMap);
  73. INIT_ASSET(ORMConfigMap);
  74. INIT_ASSET(MacroMap);
  75. }
  76. TerrainMaterial::~TerrainMaterial()
  77. {
  78. }
  79. void TerrainMaterial::initPersistFields()
  80. {
  81. INITPERSISTFIELD_IMAGEASSET(DiffuseMap, TerrainMaterial,"Base Albedo stretched over the whole map");
  82. addField( "diffuseSize", TypeF32, Offset( mDiffuseSize, TerrainMaterial ), "Used to scale the diffuse map to the material square" );
  83. INITPERSISTFIELD_IMAGEASSET(NormalMap, TerrainMaterial,"NormalMap");
  84. addField( "parallaxScale", TypeF32, Offset( mParallaxScale, TerrainMaterial ), "Used to scale the height from the normal map to give some self "
  85. "occlusion effect (aka parallax) to the terrain material" );
  86. addField("blendHeightBase", TypeF32, Offset(mBlendDepth, TerrainMaterial), "A fixed value to add while blending using heightmap-based blending."
  87. "Higher numbers = larger blend radius.");
  88. addField("blendHeightContrast", TypeF32, Offset(mBlendContrast, TerrainMaterial), "A fixed value to add while blending using heightmap-based blending."
  89. "Higher numbers = larger blend radius.");
  90. INITPERSISTFIELD_IMAGEASSET(DetailMap, TerrainMaterial, "Raises and lowers the RGB result of the Base Albedo up close.");
  91. addField( "detailSize", TypeF32, Offset( mDetailSize, TerrainMaterial ), "Used to scale the detail map to the material square" );
  92. addField( "detailStrength", TypeF32, Offset( mDetailStrength, TerrainMaterial ), "Exponentially sharpens or lightens the detail map rendering on the material" );
  93. addField( "detailDistance", TypeF32, Offset( mDetailDistance, TerrainMaterial ), "Changes how far camera can see the detail map rendering on the material" );
  94. addField( "useSideProjection", TypeBool, Offset( mSideProjection, TerrainMaterial ),"Makes that terrain material project along the sides of steep "
  95. "slopes instead of projected downwards");
  96. INITPERSISTFIELD_IMAGEASSET(ORMConfigMap, TerrainMaterial, "AO|Roughness|metalness map (uses DetailMap UV Coords)");
  97. addField("isSRGB", TypeBool, Offset(mIsSRGB, TerrainMaterial), "Is the PBR Config map's image in sRGB format?");
  98. addField("invertRoughness", TypeBool, Offset(mInvertRoughness, TerrainMaterial), "Should the roughness channel of the PBR Config map be inverted?");
  99. //Macro maps additions
  100. INITPERSISTFIELD_IMAGEASSET(MacroMap, TerrainMaterial, "Raises and lowers the RGB result of the Base Albedo at a distance.");
  101. addField( "macroSize", TypeF32, Offset( mMacroSize, TerrainMaterial ), "Used to scale the Macro map to the material square" );
  102. addField( "macroStrength", TypeF32, Offset( mMacroStrength, TerrainMaterial ), "Exponentially sharpens or lightens the Macro map rendering on the material" );
  103. addField( "macroDistance", TypeF32, Offset( mMacroDistance, TerrainMaterial ), "Changes how far camera can see the Macro map rendering on the material" );
  104. Parent::initPersistFields();
  105. // Gotta call this at least once or it won't get created!
  106. Sim::getTerrainMaterialSet();
  107. }
  108. bool TerrainMaterial::onAdd()
  109. {
  110. if ( !Parent::onAdd() )
  111. return false;
  112. SimSet *set = Sim::getTerrainMaterialSet();
  113. // Make sure we have an internal name set.
  114. if (!mInternalName || !mInternalName[0])
  115. {
  116. Con::warnf("TerrainMaterial::onAdd() - No internal name set!");
  117. return false;
  118. }
  119. else
  120. {
  121. SimObject *object = set->findObjectByInternalName( mInternalName );
  122. if (object)
  123. {
  124. Con::warnf("TerrainMaterial::onAdd() - Internal name collision; '%s' already exists!", mInternalName);
  125. return false;
  126. }
  127. }
  128. set->addObject( this );
  129. return true;
  130. }
  131. TerrainMaterial* TerrainMaterial::getWarningMaterial()
  132. {
  133. return findOrCreate( NULL );
  134. }
  135. TerrainMaterial* TerrainMaterial::findOrCreate( const char *nameOrPath )
  136. {
  137. SimSet *set = Sim::getTerrainMaterialSet();
  138. if ( !nameOrPath || !nameOrPath[0] )
  139. nameOrPath = "warning_material";
  140. // See if we can just find it.
  141. TerrainMaterial *mat = dynamic_cast<TerrainMaterial*>( set->findObjectByInternalName( StringTable->insert( nameOrPath ) ) );
  142. if ( mat )
  143. return mat;
  144. StringTableEntry assetId = TerrainMaterialAsset::getAssetIdByMaterialName(nameOrPath);
  145. if (assetId != StringTable->EmptyString())
  146. {
  147. TerrainMaterialAsset* terrMatAsset = AssetDatabase.acquireAsset<TerrainMaterialAsset>(assetId);
  148. if (terrMatAsset)
  149. {
  150. mat = terrMatAsset->getMaterialDefinition();
  151. if (mat)
  152. return mat;
  153. }
  154. }
  155. // We didn't find it... so see if its a path to a
  156. // file. If it is lets assume its the texture.
  157. if ( GBitmap::sFindFiles( nameOrPath, NULL ) )
  158. {
  159. mat = new TerrainMaterial();
  160. mat->setInternalName( nameOrPath );
  161. mat->_setDiffuseMap(nameOrPath);
  162. mat->registerObject();
  163. Sim::getRootGroup()->addObject( mat );
  164. return mat;
  165. }
  166. // Ok... return a placeholder material then.
  167. mat = new TerrainMaterial();
  168. mat->setInternalName(nameOrPath);
  169. mat->_setDiffuseMap(GFXTextureManager::getWarningTexturePath());
  170. mat->mDiffuseSize = 500;
  171. mat->_setDetailMap(StringTable->EmptyString());
  172. mat->mDetailSize = 5;
  173. mat->_setMacroMap(StringTable->EmptyString());
  174. mat->mMacroSize = 200;
  175. mat->registerObject();
  176. Sim::getRootGroup()->addObject(mat);
  177. return mat;
  178. }
  179. //declare general get<entry>, get<entry>Asset and set<entry> methods
  180. //signatures are:
  181. //using DiffuseMap as an example
  182. //material.getDiffuseMap(); //returns the raw file referenced
  183. //material.getDiffuseMapAsset(); //returns the asset id
  184. //material.setDiffuseMap(%texture); //tries to set the asset and failing that attempts a flat file reference
  185. DEF_ASSET_BINDS(TerrainMaterial, DiffuseMap);
  186. DEF_ASSET_BINDS(TerrainMaterial, NormalMap);
  187. DEF_ASSET_BINDS(TerrainMaterial, DetailMap);
  188. DEF_ASSET_BINDS(TerrainMaterial, ORMConfigMap);
  189. DEF_ASSET_BINDS(TerrainMaterial, MacroMap);