Merge pull request #424 from lukaspj/feature/new-terrain-blending

Height based terrain texture blending
This commit is contained in:
Brian Roberts 2021-01-04 05:36:50 -06:00 committed by GitHub
commit 27641b16ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
49 changed files with 3628 additions and 1280 deletions

File diff suppressed because it is too large Load diff

View file

@ -44,9 +44,9 @@ public:
Var* _getInMacroCoord(Vector<ShaderComponent*> &componentList );
Var* _getNormalMapTex();
Var* _getORMConfigMapTex();
Var* _getDetailMapSampler();
Var* _getNormalMapSampler();
Var* _getOrmMapSampler();
static Var* _getUniformVar( const char *name, const char *type, ConstantSortPosition csp );
@ -151,17 +151,6 @@ public:
virtual String getName() { return "Terrain Lightmap Texture"; }
};
class TerrainAdditiveFeatGLSL : public TerrainFeatGLSL
{
public:
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual String getName() { return "Terrain Additive"; }
};
class TerrainORMMapFeatGLSL : public TerrainFeatGLSL
{
public:
@ -189,4 +178,17 @@ public:
virtual String getName() { return "Blank Matinfo map"; }
};
class TerrainHeightMapBlendGLSL : public TerrainFeatGLSL
{
public:
virtual void processVert(Vector<ShaderComponent*>& componentList,
const MaterialFeatureData& fd);
virtual void processPix(Vector<ShaderComponent*>& componentList,
const MaterialFeatureData& fd);
virtual String getName() { return "Terrain Heightmap Blend"; }
};
#endif // _TERRFEATUREGLSL_H_

File diff suppressed because it is too large Load diff

View file

@ -45,6 +45,12 @@ public:
Var* _getInMacroCoord(Vector<ShaderComponent*> &componentList );
Var* _getDetailMapSampler();
Var* _getDetailMapArray();
Var* _getNormalMapSampler();
Var* _getNormalMapArray();
Var* _getOrmMapSampler();
Var* _getOrmMapArray();
Var* _getNormalMapTex();
Var* _getORMConfigMapTex();
@ -151,17 +157,6 @@ public:
virtual String getName() { return "Terrain Lightmap Texture"; }
};
class TerrainAdditiveFeatHLSL : public TerrainFeatHLSL
{
public:
virtual void processPix( Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd );
virtual String getName() { return "Terrain Additive"; }
};
class TerrainORMMapFeatHLSL : public TerrainFeatHLSL
{
public:
@ -189,4 +184,17 @@ public:
virtual String getName() { return "Blank Matinfo map"; }
};
class TerrainHeightMapBlendHLSL : public TerrainFeatHLSL
{
public:
virtual void processVert(Vector<ShaderComponent*>& componentList,
const MaterialFeatureData& fd);
virtual void processPix(Vector<ShaderComponent*>& componentList,
const MaterialFeatureData& fd);
virtual String getName() { return "Terrain Heightmap Blend"; }
};
#endif // _TERRFEATUREHLSL_H_

File diff suppressed because it is too large Load diff

View file

@ -40,6 +40,7 @@
#endif
class GFXTextureArray;
class SceneRenderState;
struct SceneData;
class TerrainMaterial;
@ -58,8 +59,7 @@ protected:
public:
MaterialInfo()
:mat(NULL), layerId(0), detailTexConst(NULL), macroTexConst(NULL), normalTexConst(NULL),
ormTexConst(NULL), detailInfoVConst(NULL), detailInfoPConst(NULL), macroInfoVConst(NULL), macroInfoPConst(NULL)
:mat(NULL), layerId(0)
{
}
@ -69,95 +69,64 @@ protected:
TerrainMaterial *mat;
U32 layerId;
GFXShaderConstHandle *detailTexConst;
GFXTexHandle detailTex;
GFXShaderConstHandle *macroTexConst;
GFXTexHandle macroTex;
GFXShaderConstHandle *normalTexConst;
GFXTexHandle normalTex;
GFXShaderConstHandle *ormTexConst;
GFXTexHandle ormTex;
GFXShaderConstHandle *detailInfoVConst;
GFXShaderConstHandle *detailInfoPConst;
GFXShaderConstHandle *macroInfoVConst;
GFXShaderConstHandle *macroInfoPConst;
GFXShaderConstHandle* mBlendDepthConst;
GFXShaderConstHandle* mBlendContrastConst;
};
class Pass
{
public:
///
GFXShader *mShader;
Pass()
: shader( NULL ),
modelViewProjConst(NULL), worldViewOnly(NULL), viewToObj(NULL),
eyePosWorldConst(NULL), eyePosConst(NULL),
objTransConst(NULL), worldToObjConst(NULL), vEyeConst(NULL),
layerSizeConst(NULL), lightParamsConst(NULL), lightInfoBufferConst(NULL),
baseTexMapConst(NULL), layerTexConst(NULL),
lightMapTexConst(NULL),
squareSize(NULL), oneOverTerrainSize(NULL),
fogDataConst(NULL), fogColorConst(NULL)
{
}
GFXShaderConstBufferRef mConsts;
~Pass()
{
for ( U32 i=0; i < materials.size(); i++ )
delete materials[i];
}
GFXStateBlockRef mStateBlock;
GFXStateBlockRef mWireframeStateBlock;
GFXStateBlockRef mReflectionStateBlock;
Vector<MaterialInfo*> materials;
GFXShaderConstHandle *mModelViewProjConst;
GFXShaderConstHandle *mWorldViewOnlyConst;
GFXShaderConstHandle *mViewToObjConst;
///
GFXShader *shader;
GFXShaderConstHandle *mEyePosWorldConst;
GFXShaderConstHandle *mEyePosConst;
GFXShaderConstBufferRef consts;
GFXShaderConstHandle *mObjTransConst;
GFXShaderConstHandle *mWorldToObjConst;
GFXShaderConstHandle *mVEyeConst;
GFXStateBlockRef stateBlock;
GFXStateBlockRef wireframeStateBlock;
GFXStateBlockRef reflectionStateBlock;
GFXShaderConstHandle *mLayerSizeConst;
GFXShaderConstHandle *mLightParamsConst;
GFXShaderConstHandle *mLightInfoBufferConst;
GFXShaderConstHandle *modelViewProjConst;
GFXShaderConstHandle *worldViewOnly;
GFXShaderConstHandle *viewToObj;
GFXShaderConstHandle *mBaseTexMapConst;
GFXShaderConstHandle *mLayerTexConst;
GFXShaderConstHandle *eyePosWorldConst;
GFXShaderConstHandle *eyePosConst;
GFXShaderConstHandle *mLightMapTexConst;
GFXShaderConstHandle *objTransConst;
GFXShaderConstHandle *worldToObjConst;
GFXShaderConstHandle *vEyeConst;
GFXShaderConstHandle *mSquareSizeConst;
GFXShaderConstHandle *mOneOverTerrainSizeConst;
GFXShaderConstHandle *layerSizeConst;
GFXShaderConstHandle *lightParamsConst;
GFXShaderConstHandle *lightInfoBufferConst;
GFXShaderConstHandle* mDetailInfoVArrayConst;
GFXShaderConstHandle* mDetailInfoPArrayConst;
GFXShaderConstHandle* mMacroInfoVArrayConst;
GFXShaderConstHandle* mMacroInfoPArrayConst;
GFXShaderConstHandle *baseTexMapConst;
GFXShaderConstHandle *layerTexConst;
GFXShaderConstHandle *mFogDataConst;
GFXShaderConstHandle *mFogColorConst;
GFXShaderConstHandle *lightMapTexConst;
GFXShaderConstHandle *mDetailTexArrayConst;
GFXShaderConstHandle *mMacroTexArrayConst;
GFXShaderConstHandle *mNormalTexArrayConst;
GFXShaderConstHandle *mOrmTexArrayConst;
GFXShaderConstHandle *squareSize;
GFXShaderConstHandle *oneOverTerrainSize;
GFXShaderConstHandle *fogDataConst;
GFXShaderConstHandle *fogColorConst;
};
GFXShaderConstHandle* mBlendDepthConst;
TerrainBlock *mTerrain;
U64 mMaterials;
Vector<Pass> mPasses;
U32 mCurrPass;
U64 mMaterials;
Vector<MaterialInfo*> mMaterialInfos;
static const Vector<String> mSamplerNames;
GFXTexHandle mBaseMapTexture;
@ -175,14 +144,11 @@ protected:
/// A vector of all terrain cell materials loaded in the system.
static Vector<TerrainCellMaterial*> smAllMaterials;
bool _createPass( Vector<MaterialInfo*> *materials,
Pass *pass,
bool firstPass,
bool deferredMat,
bool _initShader( bool deferredMat,
bool reflectMat,
bool baseOnly );
void _updateMaterialConsts( Pass *pass );
void _updateMaterialConsts();
public:

View file

@ -47,11 +47,13 @@
#include "materials/baseMatInstance.h"
#include "gfx/gfxTextureManager.h"
#include "gfx/gfxCardProfile.h"
#include "gfx/gfxAPI.h"
#include "core/resourceManager.h"
#include "T3D/physics/physicsPlugin.h"
#include "T3D/physics/physicsBody.h"
#include "T3D/physics/physicsCollision.h"
#include "console/engineAPI.h"
#include "core/util/safeRelease.h"
#include "T3D/assets/TerrainMaterialAsset.h"
using namespace Torque;
@ -203,7 +205,11 @@ TerrainBlock::TerrainBlock()
mScreenError( 16 ),
mCastShadows( true ),
mZoningDirty( false ),
mUpdateBasetex ( true )
mUpdateBasetex ( true ),
mDetailTextureArray( NULL ),
mMacroTextureArray( NULL ),
mOrmTextureArray( NULL ),
mNormalTextureArray( NULL )
{
mTypeMask = TerrainObjectType | StaticObjectType | StaticShapeObjectType;
mNetFlags.set(Ghostable | ScopeAlways);
@ -231,6 +237,11 @@ TerrainBlock::~TerrainBlock()
editor->detachTerrain(this);
#endif
deleteZodiacPrimitiveBuffer();
SAFE_RELEASE(mDetailTextureArray);
SAFE_RELEASE(mMacroTextureArray);
SAFE_RELEASE(mNormalTextureArray);
SAFE_RELEASE(mOrmTextureArray);
}
void TerrainBlock::_onTextureEvent( GFXTexCallbackCode code )
@ -1461,6 +1472,11 @@ DefineEngineMethod(TerrainBlock, saveAsset, bool, (), ,
return static_cast<TerrainBlock*>(object)->saveAsset();
}
DefineEngineMethod( TerrainBlock, setMaterialsDirty, void, (),, "")
{
static_cast<TerrainBlock*>(object)->setMaterialsDirty();
}
//ConsoleMethod(TerrainBlock, save, bool, 3, 3, "(string fileName) - saves the terrain block's terrain file to the specified file name.")
//{
// char filename[256];

View file

@ -135,6 +135,11 @@ protected:
///
Vector<GFXTexHandle> mBaseTextures;
GFXTextureArrayHandle mDetailTextureArray;
GFXTextureArrayHandle mMacroTextureArray;
GFXTextureArrayHandle mNormalTextureArray;
GFXTextureArrayHandle mOrmTextureArray;
///
GFXTexHandle mLayerTex;
@ -308,6 +313,8 @@ public:
/// Deletes all the materials on the terrain.
void deleteAllMaterials();
void setMaterialsDirty() { mDetailsDirty = true; };
//void setMaterialName( U32 index, const String &name );
/// Accessors and mutators for TerrainMaterialUndoAction.
@ -324,6 +331,11 @@ public:
U32 getMaterialCount() const;
GFXTextureArrayHandle getDetailTextureArray() const { return mDetailTextureArray; }
GFXTextureArrayHandle getMacroTextureArray() const { return mMacroTextureArray; }
GFXTextureArrayHandle getNormalTextureArray() const { return mNormalTextureArray; }
GFXTextureArrayHandle getOrmTextureArray() const { return mOrmTextureArray; }
//BaseMatInstance* getMaterialInst( U32 x, U32 y );
void setHeight( const Point2I &pos, F32 height );

View file

@ -33,7 +33,7 @@ ImplementFeatureType( MFT_TerrainNormalMap, MFG_Texture, 103.0f, false );
ImplementFeatureType( MFT_TerrainMacroMap, MFG_Texture, 104.0f, false );
ImplementFeatureType( MFT_TerrainLightMap, MFG_Texture, 105.0f, false );
ImplementFeatureType( MFT_TerrainSideProject, MFG_Texture, 106.0f, false );
ImplementFeatureType( MFT_TerrainAdditive, MFG_PostProcess, 999.0f, false );
ImplementFeatureType( MFT_TerrainHeightBlend, MFG_PreLighting, 200.0f, false );
//Deferred Shading
ImplementFeatureType( MFT_DeferredTerrainBlankInfoMap, MFG_Texture, 104.1f, false);
ImplementFeatureType( MFT_TerrainORMMap, MFG_Texture, 104.2f, false);

View file

@ -34,7 +34,7 @@ DeclareFeatureType( MFT_TerrainNormalMap );
DeclareFeatureType( MFT_TerrainParallaxMap );
DeclareFeatureType( MFT_TerrainLightMap );
DeclareFeatureType( MFT_TerrainSideProject );
DeclareFeatureType( MFT_TerrainAdditive );
DeclareFeatureType( MFT_TerrainHeightBlend );
//Deferred Shading
DeclareFeatureType( MFT_TerrainORMMap );
DeclareFeatureType( MFT_DeferredTerrainBlankInfoMap );

View file

@ -68,6 +68,8 @@ TerrainMaterial::TerrainMaterial()
mMacroStrength( 0.7f ),
mMacroDistance( 500.0f ),
mParallaxScale( 0.0f ),
mBlendDepth( 0.0f ),
mBlendContrast( 1.0f ),
mIsSRGB(false),
mInvertRoughness(false)
{
@ -91,6 +93,12 @@ void TerrainMaterial::initPersistFields()
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" );
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."
"Higher numbers = larger blend radius.");
scriptBindMapSlot(DetailMap, TerrainMaterial, "Raises and lowers the RGB result of the Base Albedo up close.");
addField( "detailSize", TypeF32, Offset( mDetailSize, TerrainMaterial ), "Used to scale the detail map to the material square" );
addField( "detailStrength", TypeF32, Offset( mDetailStrength, TerrainMaterial ), "Exponentially sharpens or lightens the detail map rendering on the material" );

View file

@ -85,6 +85,13 @@ protected:
///
F32 mParallaxScale;
/// Depth for blending the textures using the new
/// blending method. Higher numbers = larger blend
/// radius.
F32 mBlendDepth;
F32 mBlendContrast;
public:
TerrainMaterial();
@ -122,6 +129,10 @@ public:
F32 getParallaxScale() const { return mParallaxScale; }
F32 getBlendDepth() const { return mBlendDepth; }
F32 getBlendContrast() const { return mBlendContrast; }
bool getIsSRGB() const { return mIsSRGB; }
bool getInvertRoughness() const { return mInvertRoughness; }

View file

@ -96,7 +96,7 @@ void TerrainBlock::_updateMaterials()
{
TerrainMaterial *mat = mFile->mMaterials[i];
if (!mat->getDiffuseMap().isEmpty())
if (mat->getDiffuseMap().isNotEmpty())
{
mBaseTextures[i].set(mat->getDiffuseMap(), &GFXStaticTextureSRGBProfile,
"TerrainBlock::_updateMaterials() - DiffuseMap");
@ -114,6 +114,133 @@ void TerrainBlock::_updateMaterials()
mMaxDetailDistance = mat->getMacroDistance();
}
Vector<GFXTexHandle> detailTexArray;
detailTexArray.setSize(mFile->mMaterials.size());
Vector<GFXTexHandle> macroTexArray;
macroTexArray.setSize(mFile->mMaterials.size());
Vector<GFXTexHandle> normalTexArray;
normalTexArray.setSize(mFile->mMaterials.size());
Vector<GFXTexHandle> ormTexArray;
ormTexArray.setSize(mFile->mMaterials.size());
for (U32 i = 0; i < mFile->mMaterials.size(); i++)
{
TerrainMaterial* mat = mFile->mMaterials[i];
GFXTextureProfile* profile = &GFXStaticTextureProfile;
if (mat->getIsSRGB())
profile = &GFXStaticTextureSRGBProfile;
if (mat->getDetailMap().isNotEmpty())
detailTexArray[i] = TEXMGR->createTexture(mat->getDetailMap(), profile);
if (mat->getMacroMap().isNotEmpty())
macroTexArray[i] = TEXMGR->createTexture(mat->getMacroMap(), profile);
if (mat->getNormalMap().isNotEmpty())
normalTexArray[i] = TEXMGR->createTexture(mat->getNormalMap(), profile);
if (mat->getORMConfigMap().isNotEmpty())
ormTexArray[i] = TEXMGR->createTexture(mat->getORMConfigMap(), profile);
}
if (mDetailTextureArray.isNull())
{
mDetailTextureArray = GFX->createTextureArray();
}
if (mMacroTextureArray.isNull())
{
mMacroTextureArray = GFX->createTextureArray();
}
if (mNormalTextureArray.isNull())
{
mNormalTextureArray = GFX->createTextureArray();
}
if (mOrmTextureArray.isNull())
{
mOrmTextureArray = GFX->createTextureArray();
}
U32 detailTexArraySize = detailTexArray.size();
U32 macroTexArraySize = macroTexArray.size();
U32 normalTexArraySize = normalTexArray.size();
U32 ormTexArraySize = ormTexArray.size();
#ifdef TORQUE_TOOLS
// For performance improvement when adding terrain layers, we always allocate at least 32 textures to the arrays in tool builds
detailTexArraySize = mMax(32, detailTexArraySize);
macroTexArraySize = mMax(32, macroTexArraySize);
normalTexArraySize = mMax(32, normalTexArraySize);
ormTexArraySize = mMax(32, ormTexArraySize);
#endif
// Format has been explicitly set
const U32 detailTexSize = Con::getIntVariable("Terrain::DetailTextureSize");
const GFXFormat detailTexFormat = static_cast<GFXFormat>(Con::getIntVariable("Terrain::DetailTextureFormat"));
if (detailTexSize != 0)
{
GFXFormat format = GFXFormatR8G8B8A8;
if (detailTexFormat < GFXFormat_COUNT)
{
format = detailTexFormat;
}
mDetailTextureArray->set(detailTexSize, detailTexSize, detailTexArraySize, format);
}
const U32 macroTexSize = Con::getIntVariable("Terrain::MacroTextureSize");
const GFXFormat macroTexFormat = static_cast<GFXFormat>(Con::getIntVariable("Terrain::MacroTextureFormat"));
if (macroTexSize != 0)
{
GFXFormat format = GFXFormatR8G8B8A8;
if (macroTexFormat < GFXFormat_COUNT)
{
format = macroTexFormat;
}
mMacroTextureArray->set(macroTexSize, macroTexSize, macroTexArraySize, format);
}
const U32 normalTexSize = Con::getIntVariable("Terrain::NormalTextureSize");
const GFXFormat normalTexFormat = static_cast<GFXFormat>(Con::getIntVariable("Terrain::NormalTextureFormat"));
if (normalTexSize != 0)
{
GFXFormat format = GFXFormatR8G8B8A8;
if (normalTexFormat < GFXFormat_COUNT)
{
format = normalTexFormat;
}
mNormalTextureArray->set(normalTexSize, normalTexSize, normalTexArraySize, format);
}
const U32 ormTexSize = Con::getIntVariable("Terrain::OrmTextureSize");
const GFXFormat ormTexFormat = static_cast<GFXFormat>(Con::getIntVariable("Terrain::OrmTextureFormat"));
if (ormTexSize != 0)
{
GFXFormat format = GFXFormatR8G8B8A8;
if (ormTexFormat < GFXFormat_COUNT)
{
format = ormTexFormat;
}
mOrmTextureArray->set(ormTexSize, ormTexSize, ormTexArraySize, format);
}
if (!mDetailTextureArray->fromTextureArray(detailTexArray, detailTexArraySize))
{
Con::errorf("TerrainBlock::_updateMaterials - an issue with the diffuse terrain materials was detected. Please ensure they are all of the same size and format!");
}
if (!mMacroTextureArray->fromTextureArray(macroTexArray, macroTexArraySize))
{
Con::errorf("TerrainBlock::_updateMaterials - an issue with the detail terrain materials was detected. Please ensure they are all of the same size and format!");
}
if (!mNormalTextureArray->fromTextureArray(normalTexArray, normalTexArraySize))
{
Con::errorf("TerrainBlock::_updateMaterials - an issue with the normal terrain materials was detected. Please ensure they are all of the same size and format!");
}
if (!mOrmTextureArray->fromTextureArray(ormTexArray, ormTexArraySize))
{
Con::errorf("TerrainBlock::_updateMaterials - an issue with the orm terrain materials was detected. Please ensure they are all of the same size and format!");
}
if ( mCell )
mCell->deleteMaterials();
}