terrMaterial.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include <string>
  28. IMPLEMENT_CONOBJECT( TerrainMaterial );
  29. ConsoleDocClass( TerrainMaterial,
  30. "@brief The TerrainMaterial class orginizes the material settings "
  31. "for a single terrain material layer.\n\n"
  32. "@note You should not be creating TerrainMaterials by hand in code. "
  33. "All TerrainMaterials should be created in the editors, as intended "
  34. "by the system.\n\n"
  35. "@tsexample\n"
  36. "// Created by the Terrain Painter tool in the World Editor\n"
  37. "new TerrainMaterial()\n"
  38. "{\n"
  39. " internalName = \"grass1\";\n"
  40. " diffuseMap = \"art/terrains/Test/grass1\";\n"
  41. " detailMap = \"art/terrains/Test/grass1_d\";\n"
  42. " detailSize = \"10\";\n"
  43. " isManaged = \"1\";\n"
  44. " detailBrightness = \"1\";\n"
  45. " Enabled = \"1\";\n"
  46. " diffuseSize = \"200\";\n"
  47. "};\n"
  48. "@endtsexample\n\n"
  49. "@see Materials\n"
  50. "@ingroup enviroMisc\n");
  51. TerrainMaterial::TerrainMaterial()
  52. : mDiffuseSize( 500.0f ),
  53. mDetailSize( 5.0f ),
  54. mDetailStrength( 1.0f ),
  55. mDetailDistance( 50.0f ),
  56. mSideProjection( false ),
  57. mMacroSize( 200.0f ),
  58. mMacroStrength( 0.7f ),
  59. mMacroDistance( 500.0f ),
  60. mParallaxScale( 0.0f ),
  61. mIsSRGB(false),
  62. mInvertRoughness(false)
  63. {
  64. initMapSlot(DiffuseMap);
  65. initMapSlot(NormalMap);
  66. initMapSlot(DetailMap);
  67. initMapSlot(ORMConfigMap);
  68. initMapSlot(MacroMap);
  69. }
  70. TerrainMaterial::~TerrainMaterial()
  71. {
  72. }
  73. void TerrainMaterial::initPersistFields()
  74. {
  75. scriptBindMapSlot(DiffuseMap, TerrainMaterial,"Base Albedo stretched over the whole map");
  76. addField( "diffuseSize", TypeF32, Offset( mDiffuseSize, TerrainMaterial ), "Used to scale the diffuse map to the material square" );
  77. scriptBindMapSlot(NormalMap, TerrainMaterial,"NormalMap");
  78. addField( "parallaxScale", TypeF32, Offset( mParallaxScale, TerrainMaterial ), "Used to scale the height from the normal map to give some self "
  79. "occlusion effect (aka parallax) to the terrain material" );
  80. scriptBindMapSlot(DetailMap, TerrainMaterial, "Raises and lowers the RGB result of the Base Albedo up close.");
  81. addField( "detailSize", TypeF32, Offset( mDetailSize, TerrainMaterial ), "Used to scale the detail map to the material square" );
  82. addField( "detailStrength", TypeF32, Offset( mDetailStrength, TerrainMaterial ), "Exponentially sharpens or lightens the detail map rendering on the material" );
  83. addField( "detailDistance", TypeF32, Offset( mDetailDistance, TerrainMaterial ), "Changes how far camera can see the detail map rendering on the material" );
  84. addField( "useSideProjection", TypeBool, Offset( mSideProjection, TerrainMaterial ),"Makes that terrain material project along the sides of steep "
  85. "slopes instead of projected downwards");
  86. scriptBindMapSlot(ORMConfigMap, TerrainMaterial, "AO|Roughness|metalness map (uses DetailMap UV Coords)");
  87. addField("isSRGB", TypeBool, Offset(mIsSRGB, TerrainMaterial), "Is the PBR Config map's image in sRGB format?");
  88. addField("invertRoughness", TypeBool, Offset(mInvertRoughness, TerrainMaterial), "Should the roughness channel of the PBR Config map be inverted?");
  89. //Macro maps additions
  90. scriptBindMapSlot(MacroMap, TerrainMaterial, "Raises and lowers the RGB result of the Base Albedo at a distance.");
  91. addField( "macroSize", TypeF32, Offset( mMacroSize, TerrainMaterial ), "Used to scale the Macro map to the material square" );
  92. addField( "macroStrength", TypeF32, Offset( mMacroStrength, TerrainMaterial ), "Exponentially sharpens or lightens the Macro map rendering on the material" );
  93. addField( "macroDistance", TypeF32, Offset( mMacroDistance, TerrainMaterial ), "Changes how far camera can see the Macro map rendering on the material" );
  94. Parent::initPersistFields();
  95. // Gotta call this at least once or it won't get created!
  96. Sim::getTerrainMaterialSet();
  97. }
  98. bool TerrainMaterial::onAdd()
  99. {
  100. if ( !Parent::onAdd() )
  101. return false;
  102. SimSet *set = Sim::getTerrainMaterialSet();
  103. // Make sure we have an internal name set.
  104. if ( !mInternalName || !mInternalName[0] )
  105. Con::warnf( "TerrainMaterial::onAdd() - No internal name set!" );
  106. else
  107. {
  108. SimObject *object = set->findObjectByInternalName( mInternalName );
  109. if ( object )
  110. Con::warnf( "TerrainMaterial::onAdd() - Internal name collision; '%s' already exists!", mInternalName );
  111. }
  112. //bind any assets we have
  113. bindMapSlot(DiffuseMap);
  114. bindMapSlot(NormalMap);
  115. bindMapSlot(DetailMap);
  116. bindMapSlot(ORMConfigMap);
  117. bindMapSlot(MacroMap);
  118. set->addObject( this );
  119. return true;
  120. }
  121. TerrainMaterial* TerrainMaterial::getWarningMaterial()
  122. {
  123. return findOrCreate( NULL );
  124. }
  125. TerrainMaterial* TerrainMaterial::findOrCreate( const char *nameOrPath )
  126. {
  127. SimSet *set = Sim::getTerrainMaterialSet();
  128. if ( !nameOrPath || !nameOrPath[0] )
  129. nameOrPath = "warning_material";
  130. // See if we can just find it.
  131. TerrainMaterial *mat = dynamic_cast<TerrainMaterial*>( set->findObjectByInternalName( StringTable->insert( nameOrPath ) ) );
  132. if ( mat )
  133. return mat;
  134. // We didn't find it... so see if its a path to a
  135. // file. If it is lets assume its the texture.
  136. if ( GBitmap::sFindFiles( nameOrPath, NULL ) )
  137. {
  138. mat = new TerrainMaterial();
  139. mat->setInternalName( nameOrPath );
  140. mat->mDiffuseMapFilename = nameOrPath;
  141. mat->registerObject();
  142. Sim::getRootGroup()->addObject( mat );
  143. return mat;
  144. }
  145. // Ok... return a debug material then.
  146. mat = dynamic_cast<TerrainMaterial*>( set->findObjectByInternalName( StringTable->insert( "warning_material" ) ) );
  147. if ( !mat )
  148. {
  149. // This shouldn't happen.... the warning_texture should
  150. // have already been defined in script, but we put this
  151. // fallback here just in case it gets "lost".
  152. mat = new TerrainMaterial();
  153. mat->setInternalName( "warning_material" );
  154. mat->mDiffuseMapFilename = GFXTextureManager::getWarningTexturePath();
  155. mat->mDiffuseSize = 500;
  156. mat->mDetailMapFilename = GFXTextureManager::getWarningTexturePath();
  157. mat->mDetailSize = 5;
  158. mat->mMacroMapFilename = GFXTextureManager::getWarningTexturePath();
  159. mat->mMacroSize = 200;
  160. mat->registerObject();
  161. Sim::getRootGroup()->addObject( mat );
  162. }
  163. return mat;
  164. }