Assetifies MeshRoad, Decal Road, and the material slot of GroundCover

Creates a networked and non-networked variant of DECLARE_MATERIALASSET macro
This commit is contained in:
Areloch 2021-01-03 08:58:53 -06:00
parent a0ba345095
commit bf5b26f734
17 changed files with 301 additions and 117 deletions

View file

@ -287,7 +287,9 @@ DecalRoad::DecalRoad()
{
// Setup NetObject.
mTypeMask |= StaticObjectType | StaticShapeObjectType;
mNetFlags.set(Ghostable);
mNetFlags.set(Ghostable);
initMaterialAsset(Material);
}
DecalRoad::~DecalRoad()
@ -303,7 +305,8 @@ void DecalRoad::initPersistFields()
{
addGroup( "DecalRoad" );
addField( "material", TypeMaterialName, Offset( mMaterialName, DecalRoad ), "Material used for rendering." );
addProtectedField("materialAsset", TypeMaterialAssetId, Offset(mMaterialAssetId, DecalRoad), &DecalRoad::_setMaterialAsset, &defaultProtectedGetFn, "Material Asset used for rendering.");
addProtectedField( "material", TypeMaterialName, Offset( mMaterialName, DecalRoad ), &DecalRoad::_setMaterialName, &defaultProtectedGetFn, "Material used for rendering." );
addProtectedField( "textureLength", TypeF32, Offset( mTextureLength, DecalRoad ), &DecalRoad::ptSetTextureLength, &defaultProtectedGetFn,
"The length in meters of textures mapped to the DecalRoad" );
@ -489,7 +492,7 @@ U32 DecalRoad::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
if ( stream->writeFlag( mask & DecalRoadMask ) )
{
// Write Texture Name.
stream->write( mMaterialName );
packMaterialAsset(con, Material);
stream->write( mBreakAngle );
@ -578,24 +581,10 @@ void DecalRoad::unpackUpdate( NetConnection *con, BitStream *stream )
// DecalRoadMask
if ( stream->readFlag() )
{
String matName;
stream->read( &matName );
if ( matName != mMaterialName )
{
mMaterialName = matName;
Material *pMat = NULL;
if ( !Sim::findObject( mMaterialName, pMat ) )
{
Con::printf( "DecalRoad::unpackUpdate, failed to find Material of name %s!", mMaterialName.c_str() );
}
else
{
mMaterial = pMat;
if ( isProperlyAdded() )
_initMaterial();
}
}
unpackMaterialAsset(con, Material);
if (isProperlyAdded())
_initMaterial();
stream->read( &mBreakAngle );
@ -1056,12 +1045,31 @@ bool DecalRoad::addNodeFromField( void *object, const char *index, const char *d
void DecalRoad::_initMaterial()
{
SAFE_DELETE( mMatInst );
if (mMaterialAsset.notNull())
{
if (mMatInst && String(mMaterialAsset->getMaterialDefinitionName()).equal(mMatInst->getMaterial()->getName(), String::NoCase))
return;
if ( mMaterial )
mMatInst = mMaterial->createMatInstance();
else
mMatInst = MATMGR->createMatInstance( "WarningMaterial" );
SAFE_DELETE(mMatInst);
Material* tMat = nullptr;
if (!Sim::findObject(mMaterialAsset->getMaterialDefinitionName(), tMat))
Con::errorf("DecalRoad::_initMaterial - Material %s was not found.", mMaterialAsset->getMaterialDefinitionName());
mMaterial = tMat;
if (mMaterial)
mMatInst = mMaterial->createMatInstance();
else
mMatInst = MATMGR->createMatInstance("WarningMaterial");
if (!mMatInst)
Con::errorf("DecalRoad::_initMaterial - no Material called '%s'", mMaterialAsset->getMaterialDefinitionName());
}
if (!mMatInst)
return;
GFXStateBlockDesc desc;
desc.setZReadWrite( true, false );

View file

@ -36,6 +36,8 @@
#include "collision/clippedPolyList.h"
#endif
#include "T3D/assets/MaterialAsset.h"
class Path;
class TerrainBlock;
struct ObjectRenderInst;
@ -237,7 +239,8 @@ protected:
F32 mBreakAngle;
U32 mSegmentsPerBatch;
F32 mTextureLength;
String mMaterialName;
DECLARE_NET_MATERIALASSET(DecalRoad, Material, DecalRoadMask);
U32 mRenderPriority;
// Static ConsoleVars for editor

View file

@ -96,10 +96,10 @@ GuiMeshRoadEditorCtrl::GuiMeshRoadEditorCtrl()
mSelectedSplineColor( 0,255,0,255 ),
mHoverNodeColor( 255,255,255,255 ),
mHasCopied( false )
{
mMaterialName[Top] = StringTable->insert("DefaultRoadMaterialTop");
mMaterialName[Bottom] = StringTable->insert("DefaultRoadMaterialOther");
mMaterialName[Side] = StringTable->insert("DefaultRoadMaterialOther");
{
mTopMaterialAssetId = Con::getVariable("$MeshRoadEditor::defaultTopMaterialAsset");
mBottomMaterialAssetId = Con::getVariable("$MeshRoadEditor::defaultBottomMaterialAsset");
mSideMaterialAssetId = Con::getVariable("$MeshRoadEditor::defaultSideMaterialAsset");
}
GuiMeshRoadEditorCtrl::~GuiMeshRoadEditorCtrl()
@ -205,6 +205,10 @@ bool GuiMeshRoadEditorCtrl::onAdd()
desc.zEnable = true;
mZEnableSB = GFX->createStateBlock(desc);
bindMaterialAsset(TopMaterial);
bindMaterialAsset(BottomMaterial);
bindMaterialAsset(SideMaterial);
return true;
}
@ -217,12 +221,11 @@ void GuiMeshRoadEditorCtrl::initPersistFields()
addField( "SelectedSplineColor", TypeColorI, Offset( mSelectedSplineColor, GuiMeshRoadEditorCtrl ) );
addField( "HoverNodeColor", TypeColorI, Offset( mHoverNodeColor, GuiMeshRoadEditorCtrl ) );
addField( "isDirty", TypeBool, Offset( mIsDirty, GuiMeshRoadEditorCtrl ) );
addField( "topMaterialName", TypeString, Offset( mMaterialName[Top], GuiMeshRoadEditorCtrl ),
"Default Material used by the Mesh Road Editor on upper surface road creation." );
addField( "bottomMaterialName", TypeString, Offset( mMaterialName[Bottom], GuiMeshRoadEditorCtrl ),
"Default Material used by the Mesh Road Editor on bottom surface road creation." );
addField( "sideMaterialName", TypeString, Offset( mMaterialName[Side], GuiMeshRoadEditorCtrl ),
"Default Material used by the Mesh Road Editor on side surface road creation." );
addField("topMaterial", TypeMaterialAssetId, Offset(mTopMaterialAssetId, GuiMeshRoadEditorCtrl), "Default Material used by the Mesh Road Editor on upper surface road creation.");
addField("bottomMaterial", TypeMaterialAssetId, Offset(mBottomMaterialAssetId, GuiMeshRoadEditorCtrl), "Default Material used by the Mesh Road Editor on bottom surface road creation.");
addField("sideMaterial", TypeMaterialAssetId, Offset(mSideMaterialAssetId, GuiMeshRoadEditorCtrl), "Default Material used by the Mesh Road Editor on side surface road creation.");
//addField( "MoveNodeCursor", TYPEID< SimObject >(), Offset( mMoveNodeCursor, GuiMeshRoadEditorCtrl) );
//addField( "AddNodeCursor", TYPEID< SimObject >(), Offset( mAddNodeCursor, GuiMeshRoadEditorCtrl) );
//addField( "InsertNodeCursor", TYPEID< SimObject >(), Offset( mInsertNodeCursor, GuiMeshRoadEditorCtrl) );
@ -622,10 +625,13 @@ void GuiMeshRoadEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
}
MeshRoad *newRoad = new MeshRoad;
newRoad->mMaterialName[Top] = mMaterialName[Top];
newRoad->mMaterialName[Bottom] = mMaterialName[Bottom];
newRoad->mMaterialName[Side] = mMaterialName[Side];
if(mTopMaterialAsset.notNull())
newRoad->setTopMaterialAssetId(mTopMaterialAssetId);
if (mBottomMaterialAsset.notNull())
newRoad->setBottomMaterialAssetId(mBottomMaterialAssetId);
if (mSideMaterialAsset.notNull())
newRoad->setSideMaterialAssetId(mSideMaterialAssetId);
newRoad->registerObject();

View file

@ -36,6 +36,8 @@
#include "environment/meshRoad.h"
#endif
#include "T3D/assets/MaterialAsset.h"
class GameBase;
class GuiMeshRoadEditorCtrl : public EditTSCtrl
@ -156,8 +158,14 @@ class GuiMeshRoadEditorCtrl : public EditTSCtrl
bool mHasCopied;
public:
StringTableEntry mMaterialName[SurfaceCount];
StringTableEntry mTopMaterialAssetId;
StringTableEntry mBottomMaterialAssetId;
StringTableEntry mSideMaterialAssetId;
AssetPtr<MaterialAsset> mTopMaterialAsset;
AssetPtr<MaterialAsset> mBottomMaterialAsset;
AssetPtr<MaterialAsset> mSideMaterialAsset;
};
class GuiMeshRoadEditorUndoAction : public UndoAction

View file

@ -85,7 +85,7 @@ GuiRoadEditorCtrl::GuiRoadEditorCtrl()
mSavedDrag = false;
mIsDirty = false;
mMaterialName = StringTable->insert("DefaultDecalRoadMaterial");
mMaterialAssetId = Con::getVariable("$DecalRoadEditor::defaultMaterialAsset");
}
GuiRoadEditorCtrl::~GuiRoadEditorCtrl()
@ -100,7 +100,7 @@ void GuiRoadEditorUndoAction::undo()
return;
// Temporarily save the roads current data.
String materialName = road->mMaterialName;
String materialAssetId = road->mMaterialAssetId;
F32 textureLength = road->mTextureLength;
F32 breakAngle = road->mBreakAngle;
F32 segmentsPerBatch = road->mSegmentsPerBatch;
@ -108,7 +108,7 @@ void GuiRoadEditorUndoAction::undo()
nodes.merge( road->mNodes );
// Restore the Road properties saved in the UndoAction
road->mMaterialName = materialName;
road->setMaterialAssetId(materialAssetId);
road->mBreakAngle = breakAngle;
road->mSegmentsPerBatch = segmentsPerBatch;
road->mTextureLength = textureLength;
@ -130,7 +130,7 @@ void GuiRoadEditorUndoAction::undo()
// Now save the previous Road data in this UndoAction
// since an undo action must become a redo action and vice-versa
mMaterialName = materialName;
mMaterialAssetId = materialAssetId;
mBreakAngle = breakAngle;
mSegmentsPerBatch = segmentsPerBatch;
mTextureLength = textureLength;
@ -153,6 +153,8 @@ bool GuiRoadEditorCtrl::onAdd()
mZDisableSB = GFX->createStateBlock(desc);
bindMaterialAsset(Material);
return true;
}
@ -163,8 +165,9 @@ void GuiRoadEditorCtrl::initPersistFields()
addField( "SelectedSplineColor", TypeColorI, Offset( mSelectedSplineColor, GuiRoadEditorCtrl ) );
addField( "HoverNodeColor", TypeColorI, Offset( mHoverNodeColor, GuiRoadEditorCtrl ) );
addField( "isDirty", TypeBool, Offset( mIsDirty, GuiRoadEditorCtrl ) );
addField( "materialName", TypeString, Offset( mMaterialName, GuiRoadEditorCtrl ),
"Default Material used by the Road Editor on road creation." );
addField("material", TypeMaterialAssetId, Offset(mMaterialAssetId, GuiRoadEditorCtrl), "Default Material used by the Road Editor on road creation.");
//addField( "MoveNodeCursor", TYPEID< SimObject >(), Offset( mMoveNodeCursor, GuiRoadEditorCtrl) );
//addField( "AddNodeCursor", TYPEID< SimObject >(), Offset( mAddNodeCursor, GuiRoadEditorCtrl) );
//addField( "InsertNodeCursor", TYPEID< SimObject >(), Offset( mInsertNodeCursor, GuiRoadEditorCtrl) );
@ -405,8 +408,8 @@ void GuiRoadEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
DecalRoad *newRoad = new DecalRoad;
newRoad->mMaterialName = mMaterialName;
if (mMaterialAsset.notNull())
newRoad->setMaterialAssetId(mMaterialAssetId);
newRoad->registerObject();
@ -1027,7 +1030,7 @@ void GuiRoadEditorCtrl::submitUndo( const UTF8 *name )
action->mObjId = mSelRoad->getId();
action->mBreakAngle = mSelRoad->mBreakAngle;
action->mMaterialName = mSelRoad->mMaterialName;
action->mMaterialAssetId = mSelRoad->mMaterialAssetId;
action->mSegmentsPerBatch = mSelRoad->mSegmentsPerBatch;
action->mTextureLength = mSelRoad->mTextureLength;
action->mRoadEditor = this;

View file

@ -103,7 +103,8 @@ class GuiRoadEditorCtrl : public EditTSCtrl
public:
StringTableEntry mMaterialName;
StringTableEntry mMaterialAssetId;
AssetPtr<MaterialAsset> mMaterialAsset;
protected:
void _drawRoadSpline( DecalRoad *road, const ColorI &color );
@ -158,7 +159,7 @@ class GuiRoadEditorUndoAction : public UndoAction
Vector<RoadNode> mNodes;
SimObjectId mObjId;
String mMaterialName;
StringTableEntry mMaterialAssetId;
F32 mBreakAngle;
U32 mSegmentsPerBatch;
F32 mTextureLength;

View file

@ -920,6 +920,10 @@ MeshRoad::MeshRoad()
mTriangleCount[i] = 0;
}
initMaterialAsset(TopMaterial);
initMaterialAsset(BottomMaterial);
initMaterialAsset(SideMaterial);
mSideProfile.mRoad = this;
}
@ -933,14 +937,9 @@ void MeshRoad::initPersistFields()
{
addGroup( "MeshRoad" );
addField( "topMaterial", TypeMaterialName, Offset( mMaterialName[Top], MeshRoad ),
"Material for the upper surface of the road." );
addField( "bottomMaterial", TypeMaterialName, Offset( mMaterialName[Bottom], MeshRoad ),
"Material for the bottom surface of the road." );
addField( "sideMaterial", TypeMaterialName, Offset( mMaterialName[Side], MeshRoad ),
"Material for the left, right, front, and back surfaces of the road." );
scriptBindMaterialAsset(TopMaterial, MeshRoad, "Material for the upper surface of the road.");
scriptBindMaterialAsset(BottomMaterial, MeshRoad, "Material for the bottom surface of the road.");
scriptBindMaterialAsset(SideMaterial, MeshRoad, "Material for the left, right, front, and back surfaces of the road.");
addField( "textureLength", TypeF32, Offset( mTextureLength, MeshRoad ),
"The length in meters of textures mapped to the MeshRoad." );
@ -1265,17 +1264,68 @@ void MeshRoad::prepRenderImage( SceneRenderState* state )
void MeshRoad::_initMaterial()
{
for ( U32 i = 0; i < SurfaceCount; i++ )
if (mTopMaterialAsset.notNull())
{
if ( mMatInst[i] )
SAFE_DELETE( mMatInst[i] );
if (!mMatInst[Top] || !String(mTopMaterialAsset->getMaterialDefinitionName()).equal(mMatInst[Top]->getMaterial()->getName(), String::NoCase))
{
SAFE_DELETE(mMatInst[Top]);
if ( mMaterial[i] )
mMatInst[i] = mMaterial[i]->createMatInstance();
else
mMatInst[i] = MATMGR->createMatInstance( "WarningMaterial" );
Material* tMat = nullptr;
if (!Sim::findObject(mTopMaterialAsset->getMaterialDefinitionName(), tMat))
Con::errorf("MeshRoad::_initMaterial - Material %s was not found.", mTopMaterialAsset->getMaterialDefinitionName());
mMatInst[i]->init( MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>() );
mMaterial[Top] = tMat;
if (mMaterial[Top])
mMatInst[Top] = mMaterial[Top]->createMatInstance();
else
mMatInst[Top] = MATMGR->createMatInstance("WarningMaterial");
mMatInst[Top]->init(MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>());
}
}
if (mBottomMaterialAsset.notNull())
{
if (!mMatInst[Bottom] || !String(mBottomMaterialAsset->getMaterialDefinitionName()).equal(mMatInst[Bottom]->getMaterial()->getName(), String::NoCase))
{
SAFE_DELETE(mMatInst[Bottom]);
Material* tMat = nullptr;
if (!Sim::findObject(mBottomMaterialAsset->getMaterialDefinitionName(), tMat))
Con::errorf("MeshRoad::_initMaterial - Material %s was not found.", mBottomMaterialAsset->getMaterialDefinitionName());
mMaterial[Bottom] = tMat;
if (mMaterial[Bottom])
mMatInst[Bottom] = mMaterial[Bottom]->createMatInstance();
else
mMatInst[Bottom] = MATMGR->createMatInstance("WarningMaterial");
mMatInst[Bottom]->init(MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>());
}
}
if (mSideMaterialAsset.notNull())
{
if (!mMatInst[Side] || !String(mSideMaterialAsset->getMaterialDefinitionName()).equal(mMatInst[Side]->getMaterial()->getName(), String::NoCase))
{
SAFE_DELETE(mMatInst[Side]);
Material* tMat = nullptr;
if (!Sim::findObject(mSideMaterialAsset->getMaterialDefinitionName(), tMat))
Con::errorf("MeshRoad::_initMaterial - Material %s was not found.", mSideMaterialAsset->getMaterialDefinitionName());
mMaterial[Side] = tMat;
if (mMaterial[Side])
mMatInst[Side] = mMaterial[Side]->createMatInstance();
else
mMatInst[Side] = MATMGR->createMatInstance("WarningMaterial");
mMatInst[Side]->init(MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>());
}
}
}
@ -1365,10 +1415,10 @@ U32 MeshRoad::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
// Write Object Transform.
stream->writeAffineTransform( mObjToWorld );
// Write Materials
stream->write( mMaterialName[0] );
stream->write( mMaterialName[1] );
stream->write( mMaterialName[2] );
// Write Materials
packMaterialAsset(con, TopMaterial);
packMaterialAsset(con, BottomMaterial);
packMaterialAsset(con, SideMaterial);
stream->write( mTextureLength );
stream->write( mBreakAngle );
@ -1465,18 +1515,9 @@ void MeshRoad::unpackUpdate(NetConnection * con, BitStream * stream)
stream->readAffineTransform(&ObjectMatrix);
Parent::setTransform(ObjectMatrix);
// Read Materials...
Material *pMat = NULL;
for ( U32 i = 0; i < SurfaceCount; i++ )
{
stream->read( &mMaterialName[i] );
if ( !Sim::findObject( mMaterialName[i], pMat ) )
Con::printf( "DecalRoad::unpackUpdate, failed to find Material of name %s", mMaterialName[i].c_str() );
else
mMaterial[i] = pMat;
}
unpackMaterialAsset(con, TopMaterial);
unpackMaterialAsset(con, BottomMaterial);
unpackMaterialAsset(con, SideMaterial);
if ( isProperlyAdded() )
_initMaterial();

View file

@ -52,6 +52,8 @@
#include "math/util/decomposePoly.h"
#include "T3D/assets/MaterialAsset.h"
//extern U32 gIdxArray[6][2][3];
struct MeshRoadHitSegment
@ -619,7 +621,11 @@ protected:
GFXVertexBufferHandle<GFXVertexPNTT> mVB[SurfaceCount];
GFXPrimitiveBufferHandle mPB[SurfaceCount];
String mMaterialName[SurfaceCount];
DECLARE_NET_MATERIALASSET(MeshRoad, TopMaterial, MeshRoadMask);
DECLARE_NET_MATERIALASSET(MeshRoad, BottomMaterial, MeshRoadMask);
DECLARE_NET_MATERIALASSET(MeshRoad, SideMaterial, MeshRoadMask);
//String mMaterialName[SurfaceCount];
SimObjectPtr<Material> mMaterial[SurfaceCount];
BaseMatInstance *mMatInst[SurfaceCount];