terrMaterial.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/bitmap/gBitmap.h"
  26. IMPLEMENT_CONOBJECT( TerrainMaterial );
  27. ConsoleDocClass( TerrainMaterial,
  28. "@brief The TerrainMaterial class orginizes the material settings "
  29. "for a single terrain material layer.\n\n"
  30. "@note You should not be creating TerrainMaterials by hand in code. "
  31. "All TerrainMaterials should be created in the editors, as intended "
  32. "by the system.\n\n"
  33. "@tsexample\n"
  34. "// Created by the Terrain Painter tool in the World Editor\n"
  35. "new TerrainMaterial()\n"
  36. "{\n"
  37. " internalName = \"grass1\";\n"
  38. " diffuseMap = \"art/terrains/Test/grass1\";\n"
  39. " detailMap = \"art/terrains/Test/grass1_d\";\n"
  40. " detailSize = \"10\";\n"
  41. " isManaged = \"1\";\n"
  42. " detailBrightness = \"1\";\n"
  43. " Enabled = \"1\";\n"
  44. " diffuseSize = \"200\";\n"
  45. "};\n"
  46. "@endtsexample\n\n"
  47. "@see Materials\n"
  48. "@ingroup enviroMisc\n");
  49. TerrainMaterial::TerrainMaterial()
  50. : mSideProjection( false ),
  51. mDiffuseSize( 500.0f ),
  52. mDetailSize( 5.0f ),
  53. mDetailStrength( 1.0f ),
  54. mDetailDistance( 50.0f ),
  55. mParallaxScale( 0.0f )
  56. {
  57. }
  58. TerrainMaterial::~TerrainMaterial()
  59. {
  60. }
  61. void TerrainMaterial::initPersistFields()
  62. {
  63. addField( "diffuseMap", TypeStringFilename, Offset( mDiffuseMap, TerrainMaterial ), "Base texture for the material" );
  64. addField( "diffuseSize", TypeF32, Offset( mDiffuseSize, TerrainMaterial ), "Used to scale the diffuse map to the material square" );
  65. addField( "normalMap", TypeStringFilename, Offset( mNormalMap, TerrainMaterial ), "Bump map for the material" );
  66. addField( "detailMap", TypeStringFilename, Offset( mDetailMap, TerrainMaterial ), "Detail map for the material" );
  67. addField( "detailSize", TypeF32, Offset( mDetailSize, TerrainMaterial ), "Used to scale the detail map to the material square" );
  68. addField( "detailStrength", TypeF32, Offset( mDetailStrength, TerrainMaterial ), "Exponentially sharpens or lightens the detail map rendering on the material" );
  69. addField( "detailDistance", TypeF32, Offset( mDetailDistance, TerrainMaterial ), "Changes how far camera can see the detail map rendering on the material" );
  70. addField( "useSideProjection", TypeBool, Offset( mSideProjection, TerrainMaterial ),"Makes that terrain material project along the sides of steep "
  71. "slopes instead of projected downwards");
  72. addField( "parallaxScale", TypeF32, Offset( mParallaxScale, TerrainMaterial ), "Used to scale the height from the normal map to give some self "
  73. "occlusion effect (aka parallax) to the terrain material" );
  74. Parent::initPersistFields();
  75. // Gotta call this at least once or it won't get created!
  76. Sim::getTerrainMaterialSet();
  77. }
  78. bool TerrainMaterial::onAdd()
  79. {
  80. if ( !Parent::onAdd() )
  81. return false;
  82. SimSet *set = Sim::getTerrainMaterialSet();
  83. // Make sure we have an internal name set.
  84. if ( !mInternalName || !mInternalName[0] )
  85. Con::warnf( "TerrainMaterial::onAdd() - No internal name set!" );
  86. else
  87. {
  88. SimObject *object = set->findObjectByInternalName( mInternalName );
  89. if ( object )
  90. Con::warnf( "TerrainMaterial::onAdd() - Internal name collision; '%s' already exists!", mInternalName );
  91. }
  92. set->addObject( this );
  93. return true;
  94. }
  95. TerrainMaterial* TerrainMaterial::getWarningMaterial()
  96. {
  97. return findOrCreate( NULL );
  98. }
  99. TerrainMaterial* TerrainMaterial::findOrCreate( const char *nameOrPath )
  100. {
  101. SimSet *set = Sim::getTerrainMaterialSet();
  102. if ( !nameOrPath || !nameOrPath[0] )
  103. nameOrPath = "warning_material";
  104. // See if we can just find it.
  105. TerrainMaterial *mat = dynamic_cast<TerrainMaterial*>( set->findObjectByInternalName( StringTable->insert( nameOrPath ) ) );
  106. if ( mat )
  107. return mat;
  108. // We didn't find it... so see if its a path to a
  109. // file. If it is lets assume its the texture.
  110. if ( GBitmap::sFindFiles( nameOrPath, NULL ) )
  111. {
  112. mat = new TerrainMaterial();
  113. mat->setInternalName( nameOrPath );
  114. mat->mDiffuseMap = nameOrPath;
  115. mat->registerObject();
  116. Sim::getRootGroup()->addObject( mat );
  117. return mat;
  118. }
  119. // Ok... return a debug material then.
  120. mat = dynamic_cast<TerrainMaterial*>( set->findObjectByInternalName( StringTable->insert( "warning_material" ) ) );
  121. if ( !mat )
  122. {
  123. // This shouldn't happen.... the warning_texture should
  124. // have already been defined in script, but we put this
  125. // fallback here just in case it gets "lost".
  126. mat = new TerrainMaterial();
  127. mat->setInternalName( "warning_material" );
  128. mat->mDiffuseMap = "core/art/warnMat.png";
  129. mat->mDiffuseSize = 500;
  130. mat->mDetailMap = "core/art/warnMat.png";
  131. mat->mDetailSize = 5;
  132. mat->registerObject();
  133. Sim::getRootGroup()->addObject( mat );
  134. }
  135. return mat;
  136. }