2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
# include "platform/platform.h"
# include "terrain/terrMaterial.h"
# include "console/consoleTypes.h"
2013-01-30 19:09:22 +00:00
# include "gfx/gfxTextureManager.h"
2012-09-19 15:15:01 +00:00
# include "gfx/bitmap/gBitmap.h"
2021-12-13 21:56:14 +00:00
# ifdef TORQUE_TOOLS
2021-12-15 00:44:44 +00:00
# include "console/persistenceManager.h"
2021-12-13 21:56:14 +00:00
# endif
2022-03-20 21:17:06 +00:00
# include "T3D/assets/TerrainMaterialAsset.h"
2020-06-26 04:33:01 +00:00
# include <string>
2012-09-19 15:15:01 +00:00
IMPLEMENT_CONOBJECT ( TerrainMaterial ) ;
ConsoleDocClass ( TerrainMaterial ,
" @brief The TerrainMaterial class orginizes the material settings "
" for a single terrain material layer. \n \n "
" @note You should not be creating TerrainMaterials by hand in code. "
" All TerrainMaterials should be created in the editors, as intended "
" by the system. \n \n "
" @tsexample \n "
" // Created by the Terrain Painter tool in the World Editor \n "
" new TerrainMaterial() \n "
" { \n "
" internalName = \" grass1 \" ; \n "
" diffuseMap = \" art/terrains/Test/grass1 \" ; \n "
" detailMap = \" art/terrains/Test/grass1_d \" ; \n "
" detailSize = \" 10 \" ; \n "
" isManaged = \" 1 \" ; \n "
" detailBrightness = \" 1 \" ; \n "
" Enabled = \" 1 \" ; \n "
" diffuseSize = \" 200 \" ; \n "
" }; \n "
" @endtsexample \n \n "
" @see Materials \n "
" @ingroup enviroMisc \n " ) ;
TerrainMaterial : : TerrainMaterial ( )
2016-10-14 23:16:55 +00:00
: mDiffuseSize ( 500.0f ) ,
2012-09-19 15:15:01 +00:00
mDetailSize ( 5.0f ) ,
mDetailStrength ( 1.0f ) ,
mDetailDistance ( 50.0f ) ,
2016-10-14 23:16:55 +00:00
mSideProjection ( false ) ,
2013-03-28 00:58:37 +00:00
mMacroSize ( 200.0f ) ,
mMacroStrength ( 0.7f ) ,
mMacroDistance ( 500.0f ) ,
2019-11-11 07:40:55 +00:00
mParallaxScale ( 0.0f ) ,
2021-01-03 00:26:19 +00:00
mBlendDepth ( 0.0f ) ,
2021-01-02 02:20:18 +00:00
mBlendContrast ( 1.0f ) ,
2019-11-11 07:40:55 +00:00
mIsSRGB ( false ) ,
2020-09-30 18:51:12 +00:00
mInvertRoughness ( false )
2012-09-19 15:15:01 +00:00
{
2021-10-03 07:56:26 +00:00
INIT_ASSET ( DiffuseMap ) ;
INIT_ASSET ( NormalMap ) ;
INIT_ASSET ( DetailMap ) ;
INIT_ASSET ( ORMConfigMap ) ;
INIT_ASSET ( MacroMap ) ;
2012-09-19 15:15:01 +00:00
}
TerrainMaterial : : ~ TerrainMaterial ( )
{
}
void TerrainMaterial : : initPersistFields ( )
{
2021-07-19 06:07:08 +00:00
INITPERSISTFIELD_IMAGEASSET ( DiffuseMap , TerrainMaterial , " Base Albedo stretched over the whole map " ) ;
2012-09-19 15:15:01 +00:00
addField ( " diffuseSize " , TypeF32 , Offset ( mDiffuseSize , TerrainMaterial ) , " Used to scale the diffuse map to the material square " ) ;
2021-07-19 06:07:08 +00:00
INITPERSISTFIELD_IMAGEASSET ( NormalMap , TerrainMaterial , " NormalMap " ) ;
2020-09-30 18:50:23 +00:00
addField ( " parallaxScale " , TypeF32 , Offset ( mParallaxScale , TerrainMaterial ) , " Used to scale the height from the normal map to give some self "
" occlusion effect (aka parallax) to the terrain material " ) ;
2012-09-19 15:15:01 +00:00
2021-01-02 02:20:18 +00:00
addField ( " blendHeightBase " , TypeF32 , Offset ( mBlendDepth , TerrainMaterial ) , " A fixed value to add while blending using heightmap-based blending. "
" Higher numbers = larger blend radius. " ) ;
addField ( " blendHeightContrast " , TypeF32 , Offset ( mBlendContrast , TerrainMaterial ) , " A fixed value to add while blending using heightmap-based blending. "
2020-12-29 01:00:14 +00:00
" Higher numbers = larger blend radius. " ) ;
2021-07-19 06:07:08 +00:00
INITPERSISTFIELD_IMAGEASSET ( DetailMap , TerrainMaterial , " Raises and lowers the RGB result of the Base Albedo up close. " ) ;
2020-09-30 18:50:23 +00:00
addField ( " detailSize " , TypeF32 , Offset ( mDetailSize , TerrainMaterial ) , " Used to scale the detail map to the material square " ) ;
2012-09-19 15:15:01 +00:00
addField ( " detailStrength " , TypeF32 , Offset ( mDetailStrength , TerrainMaterial ) , " Exponentially sharpens or lightens the detail map rendering on the material " ) ;
addField ( " detailDistance " , TypeF32 , Offset ( mDetailDistance , TerrainMaterial ) , " Changes how far camera can see the detail map rendering on the material " ) ;
2020-09-30 18:50:23 +00:00
2012-09-19 15:15:01 +00:00
addField ( " useSideProjection " , TypeBool , Offset ( mSideProjection , TerrainMaterial ) , " Makes that terrain material project along the sides of steep "
" slopes instead of projected downwards " ) ;
2013-03-28 00:58:37 +00:00
2021-07-19 06:07:08 +00:00
INITPERSISTFIELD_IMAGEASSET ( ORMConfigMap , TerrainMaterial , " AO|Roughness|metalness map (uses DetailMap UV Coords) " ) ;
2020-09-30 18:50:23 +00:00
addField ( " isSRGB " , TypeBool , Offset ( mIsSRGB , TerrainMaterial ) , " Is the PBR Config map's image in sRGB format? " ) ;
addField ( " invertRoughness " , TypeBool , Offset ( mInvertRoughness , TerrainMaterial ) , " Should the roughness channel of the PBR Config map be inverted? " ) ;
2013-03-28 00:58:37 +00:00
//Macro maps additions
2021-07-19 06:07:08 +00:00
INITPERSISTFIELD_IMAGEASSET ( MacroMap , TerrainMaterial , " Raises and lowers the RGB result of the Base Albedo at a distance. " ) ;
2013-03-28 00:58:37 +00:00
addField ( " macroSize " , TypeF32 , Offset ( mMacroSize , TerrainMaterial ) , " Used to scale the Macro map to the material square " ) ;
addField ( " macroStrength " , TypeF32 , Offset ( mMacroStrength , TerrainMaterial ) , " Exponentially sharpens or lightens the Macro map rendering on the material " ) ;
addField ( " macroDistance " , TypeF32 , Offset ( mMacroDistance , TerrainMaterial ) , " Changes how far camera can see the Macro map rendering on the material " ) ;
2012-09-19 15:15:01 +00:00
Parent : : initPersistFields ( ) ;
// Gotta call this at least once or it won't get created!
Sim : : getTerrainMaterialSet ( ) ;
}
bool TerrainMaterial : : onAdd ( )
{
if ( ! Parent : : onAdd ( ) )
return false ;
SimSet * set = Sim : : getTerrainMaterialSet ( ) ;
// Make sure we have an internal name set.
2022-03-29 06:40:07 +00:00
if ( ! mInternalName | | ! mInternalName [ 0 ] )
{
Con : : warnf ( " TerrainMaterial::onAdd() - No internal name set! " ) ;
return false ;
}
2012-09-19 15:15:01 +00:00
else
{
SimObject * object = set - > findObjectByInternalName ( mInternalName ) ;
2022-03-29 06:40:07 +00:00
if ( object )
{
Con : : warnf ( " TerrainMaterial::onAdd() - Internal name collision; '%s' already exists! " , mInternalName ) ;
return false ;
}
2020-09-30 18:50:23 +00:00
}
2012-09-19 15:15:01 +00:00
set - > addObject ( this ) ;
return true ;
}
TerrainMaterial * TerrainMaterial : : getWarningMaterial ( )
{
return findOrCreate ( NULL ) ;
}
TerrainMaterial * TerrainMaterial : : findOrCreate ( const char * nameOrPath )
{
SimSet * set = Sim : : getTerrainMaterialSet ( ) ;
if ( ! nameOrPath | | ! nameOrPath [ 0 ] )
nameOrPath = " warning_material " ;
// See if we can just find it.
TerrainMaterial * mat = dynamic_cast < TerrainMaterial * > ( set - > findObjectByInternalName ( StringTable - > insert ( nameOrPath ) ) ) ;
if ( mat )
return mat ;
2022-03-20 21:17:06 +00:00
StringTableEntry assetId = TerrainMaterialAsset : : getAssetIdByMaterialName ( nameOrPath ) ;
if ( assetId ! = StringTable - > EmptyString ( ) )
{
TerrainMaterialAsset * terrMatAsset = AssetDatabase . acquireAsset < TerrainMaterialAsset > ( assetId ) ;
if ( terrMatAsset )
{
mat = terrMatAsset - > getMaterialDefinition ( ) ;
if ( mat )
return mat ;
}
}
2012-09-19 15:15:01 +00:00
// We didn't find it... so see if its a path to a
// file. If it is lets assume its the texture.
if ( GBitmap : : sFindFiles ( nameOrPath , NULL ) )
{
mat = new TerrainMaterial ( ) ;
mat - > setInternalName ( nameOrPath ) ;
2021-07-25 22:18:55 +00:00
mat - > _setDiffuseMap ( nameOrPath ) ;
2012-09-19 15:15:01 +00:00
mat - > registerObject ( ) ;
Sim : : getRootGroup ( ) - > addObject ( mat ) ;
return mat ;
}
2022-03-20 21:17:06 +00:00
// Ok... return a placeholder material then.
2012-09-19 15:15:01 +00:00
mat = new TerrainMaterial ( ) ;
2022-03-20 21:17:06 +00:00
mat - > setInternalName ( nameOrPath ) ;
2021-07-25 22:18:55 +00:00
mat - > _setDiffuseMap ( GFXTextureManager : : getWarningTexturePath ( ) ) ;
2012-09-19 15:15:01 +00:00
mat - > mDiffuseSize = 500 ;
2021-07-25 22:18:55 +00:00
mat - > _setDetailMap ( StringTable - > EmptyString ( ) ) ;
2012-09-19 15:15:01 +00:00
mat - > mDetailSize = 5 ;
2021-07-25 22:18:55 +00:00
mat - > _setMacroMap ( StringTable - > EmptyString ( ) ) ;
2021-07-19 06:07:08 +00:00
mat - > mMacroSize = 200 ;
2012-09-19 15:15:01 +00:00
mat - > registerObject ( ) ;
2022-03-20 21:17:06 +00:00
Sim : : getRootGroup ( ) - > addObject ( mat ) ;
2012-09-19 15:15:01 +00:00
return mat ;
}
2021-07-19 06:07:08 +00:00
//declare general get<entry>, get<entry>Asset and set<entry> methods
//signatures are:
//using DiffuseMap as an example
//material.getDiffuseMap(); //returns the raw file referenced
//material.getDiffuseMapAsset(); //returns the asset id
//material.setDiffuseMap(%texture); //tries to set the asset and failing that attempts a flat file reference
2021-10-03 07:56:26 +00:00
DEF_ASSET_BINDS ( TerrainMaterial , DiffuseMap ) ;
DEF_ASSET_BINDS ( TerrainMaterial , NormalMap ) ;
DEF_ASSET_BINDS ( TerrainMaterial , DetailMap ) ;
DEF_ASSET_BINDS ( TerrainMaterial , ORMConfigMap ) ;
DEF_ASSET_BINDS ( TerrainMaterial , MacroMap ) ;