Cleans up ShapeAsset of some unnecessary/redundant elements like extra material and animations tracking

Removed the old SHAPE_ASSET macros
Implements AssetRef struct that acts as a universal wrapper for an templated AssetPtr and AssetId pair
Adds Type handling for AssetRef for ShapeAsset to unify handling in classes that utilize a shapeAsset, so assigning an assetPtr or an assetId will keep a record of the assignment in the event the assetPtr is invalid.
Update all classes that utilized the old SHAPE_ASSET macros to utilize the AssetRef struct and updated the class code to utilize it to provide much more clean and concise code that isn't blocked behind macro definitions
Added a new example class: shapeDatablockExample which allows render of a simple shape object utilizing a simple example datablock.
This commit is contained in:
JeffR 2026-05-31 01:19:26 -05:00
parent c2c5674fe9
commit b44158cb89
52 changed files with 1860 additions and 1086 deletions

View file

@ -74,12 +74,11 @@ PhysicsDebrisData::PhysicsDebrisData()
lifetime = 5.0f;
lifetimeVariance = 0.0f;
mShapeAsset.registerRefreshNotify(this);
shapeAssetRef.assetPtr.registerRefreshNotify(this);
}
PhysicsDebrisData::~PhysicsDebrisData()
{
mShapeAsset.unregisterRefreshNotify();
}
bool PhysicsDebrisData::onAdd()
@ -97,17 +96,21 @@ bool PhysicsDebrisData::preload( bool server, String &errorStr )
if ( server ) return true;
if ( getShape() )
if (!shapeAssetRef.isNull())
{
// Create a dummy shape to force the generation of shaders and materials
// during the level load and not during gameplay.
TSShapeInstance *pDummy = new TSShapeInstance( getShape(), !server);
delete pDummy;
}
else
{
errorStr = String::ToString("PhysicsDebrisData::load: Couldn't load shape asset \"%s\"", _getShapeAssetId());
return false;
Resource<TSShape> shape = shapeAssetRef.assetPtr->getShapeResource();
if (shape)
{
// Create a dummy shape to force the generation of shaders and materials
// during the level load and not during gameplay.
TSShapeInstance* pDummy = new TSShapeInstance(shape, !server);
delete pDummy;
}
else
{
errorStr = String::ToString("PhysicsDebrisData(%s)::preload: Couldn't load shape asset\"%s\"", getName(), shapeAssetRef.assetId);
return false;
}
}
return true;
@ -118,8 +121,9 @@ void PhysicsDebrisData::initPersistFields()
docsURL;
addGroup( "Shapes" );
INITPERSISTFIELD_SHAPEASSET_REFACTOR(Shape, PhysicsDebrisData, "@brief Shape to use with this debris.\n\n"
"Compatable with Live-Asset Reloading.");
ADD_FIELD("shapeAsset", TypeShapeAssetRef, Offset(shapeAssetRef, PhysicsDebrisData))
.doc("@brief Shape to use with this debris.\n\n"
"Compatable with Live-Asset Reloading.");
endGroup( "Shapes" );
@ -218,7 +222,7 @@ void PhysicsDebrisData::packData(BitStream* stream)
stream->write( waterDampingScale );
stream->write( buoyancyDensity );
PACKDATA_ASSET_REFACTOR(Shape);
AssetDatabase.packDataAsset(stream, shapeAssetRef.assetId);
}
void PhysicsDebrisData::unpackData(BitStream* stream)
@ -239,7 +243,7 @@ void PhysicsDebrisData::unpackData(BitStream* stream)
stream->read( &waterDampingScale );
stream->read( &buoyancyDensity );
UNPACKDATA_ASSET_REFACTOR(Shape);
shapeAssetRef = AssetDatabase.unpackDataAsset(stream);
}
DefineEngineMethod( PhysicsDebrisData, preload, void, (), ,
@ -250,7 +254,7 @@ DefineEngineMethod( PhysicsDebrisData, preload, void, (), ,
{
String errorStr;
object->_setShape(object->_getShapeAssetId());
object->shapeAssetRef = object->shapeAssetRef.assetId;
if( !object->preload( false, errorStr ) )
Con::errorf( "PhsysicsDebrisData::preload - error: %s", errorStr.c_str() );
@ -356,6 +360,19 @@ bool PhysicsDebris::onAdd()
return false;
}
if ( !mDataBlock->shapeAssetRef.notNull() )
{
Con::errorf("PhysicsDebris::onAdd - Fail - No shape asset \"%s\"", mDataBlock->shapeAssetRef.assetId);
return false;
}
Resource<TSShape> shape = mDataBlock->shapeAssetRef.assetPtr->getShapeResource();
if (!shape)
{
Con::errorf("PhysicsDebris::onAdd - Fail - Unable to load shape asset \"%s\"", mDataBlock->shapeAssetRef.assetId);
return false;
}
// If it has a fixed lifetime then calculate it.
if ( mDataBlock->lifetime > 0.0f )
{
@ -364,7 +381,7 @@ bool PhysicsDebris::onAdd()
}
// Setup our bounding box
mObjBox = mDataBlock->getShape()->mBounds;
mObjBox = shape->mBounds;
resetWorldBox();
// Add it to the client scene.
@ -627,7 +644,7 @@ void PhysicsDebris::_createFragments()
if ( !mWorld )
return;
TSShape *shape = mDataBlock->getShape();
TSShape *shape = mDataBlock->shapeAssetRef.assetPtr->getShape();
mShapeInstance = new TSShapeInstance( shape, true );
mShapeInstance->animate();
@ -701,7 +718,7 @@ void PhysicsDebris::_findNodes( U32 colNode, Vector<U32> &nodeIds )
// 1. Visible mesh nodes are siblings of the collision node under a common parent dummy node
// 2. Collision node is a child of its visible mesh node
TSShape *shape = mDataBlock->getShape();
TSShape *shape = mDataBlock->shapeAssetRef.assetPtr->getShape();
S32 itr = shape->nodes[colNode].parentIndex;
itr = shape->nodes[itr].firstChild;

View file

@ -86,7 +86,7 @@ public:
/// Is rendererd during shadow passes.
bool castShadows;
DECLARE_SHAPEASSET_REFACTOR(PhysicsDebrisData, Shape)
AssetRef<ShapeAsset> shapeAssetRef;
PhysicsDebrisData();
virtual ~PhysicsDebrisData();

View file

@ -78,12 +78,11 @@ PhysicsShapeData::PhysicsShapeData()
buoyancyDensity( 0.0f ),
simType( SimType_ClientServer )
{
mShapeAsset.registerRefreshNotify(this);
shapeAsset.assetPtr.registerRefreshNotify(this);
}
PhysicsShapeData::~PhysicsShapeData()
{
mShapeAsset.unregisterRefreshNotify();
}
void PhysicsShapeData::initPersistFields()
@ -91,8 +90,9 @@ void PhysicsShapeData::initPersistFields()
docsURL;
addGroup("Shapes");
INITPERSISTFIELD_SHAPEASSET_REFACTOR(Shape, PhysicsShapeData, "@brief Shape asset to be used with this physics object.\n\n"
"Compatable with Live-Asset Reloading. ")
ADD_FIELD("shapeAsset", TypeShapeAssetRef, Offset(shapeAsset, PhysicsShapeData))
. doc("@brief Shape asset to be used with this physics object.\n\n"
"Compatable with Live-Asset Reloading. ");
addField( "debris", TYPEID< SimObjectRef<PhysicsDebrisData> >(), Offset( debris, PhysicsShapeData ),
"@brief Name of a PhysicsDebrisData to spawn when this shape is destroyed (optional)." );
@ -181,7 +181,7 @@ void PhysicsShapeData::packData( BitStream *stream )
{
Parent::packData( stream );
PACKDATA_ASSET_REFACTOR(Shape);
AssetDatabase.packDataAsset(stream, shapeAsset.assetId);
stream->write( mass );
stream->write( dynamicFriction );
@ -205,7 +205,7 @@ void PhysicsShapeData::unpackData( BitStream *stream )
{
Parent::unpackData(stream);
UNPACKDATA_ASSET_REFACTOR(Shape);
shapeAsset = AssetDatabase.unpackDataAsset(stream);
stream->read( &mass );
stream->read( &dynamicFriction );
@ -242,27 +242,28 @@ void PhysicsShapeData::onRemove()
void PhysicsShapeData::_onResourceChanged( const Torque::Path &path )
{
U32 assetStatus = ShapeAsset::getAssetErrCode(mShapeAsset);
U32 assetStatus = ShapeAsset::getAssetErrCode(shapeAsset.assetPtr);
if (assetStatus != AssetBase::Ok && assetStatus != AssetBase::UsingFallback)
{
return;
}
if ( path != Path(mShapeAsset->getShapeFile()) )
if ( path != Path(shapeAsset.assetPtr->getShapeFile()) )
return;
_setShape(_getShapeAssetId());
shapeAsset = shapeAsset.assetId;
// Reload the changed shape.
PhysicsCollisionRef reloadcolShape;
if ( !getShape())
Resource<TSShape> shape = shapeAsset.assetPtr->getShapeResource();
if ( !shape)
{
Con::warnf( ConsoleLogEntry::General, "PhysicsShapeData::_onResourceChanged: Could not reload %s.", path.getFileName().c_str() );
return;
}
// Reload the collision shape.
reloadcolShape = getShape()->buildColShape( false, Point3F::One );
reloadcolShape = shape->buildColShape( false, Point3F::One );
if ( bool(reloadcolShape))
colShape = reloadcolShape;
@ -283,34 +284,41 @@ bool PhysicsShapeData::preload( bool server, String &errorBuffer )
return false;
}
bool shapeError = false;
if (getShape())
if (shapeAsset.isNull())
{
if (!server && !getShape()->preloadMaterialList(getShapeFile()) && NetConnection::filesWereDownloaded())
errorBuffer = String::ToString("PhysicsShapeData::preload(%s) - No shape asset!", getName());
return false;
}
bool shapeError = false;
Resource<TSShape> shape = shapeAsset.assetPtr->getShapeResource();
if (shape)
{
if (!server && !shapeAsset.assetPtr->preloadMaterialList() && NetConnection::filesWereDownloaded())
shapeError = true;
}
else
{
errorBuffer = String::ToString("PhysicsShapeData: Couldn't load shape \"%s\"", _getShapeAssetId());
errorBuffer = String::ToString("PhysicsShapeData: Couldn't load shape! AssetId \"%s\"", shapeAsset.assetId);
return false;
}
// Prepare the shared physics collision shape.
if ( !colShape && getShape())
if ( !colShape && shape)
{
colShape = getShape()->buildColShape( false, Point3F::One );
colShape = shape->buildColShape( false, Point3F::One );
// If we got here and didn't get a collision shape then
// we need to fail... can't have a shape without collision.
if ( !colShape )
{
//no collision so we create a simple box collision shape from the shapes bounds and alert the user
Con::warnf( "PhysicsShapeData::preload - No collision found for shape '%s', auto-creating one", _getShapeAssetId());
Point3F halfWidth = getShape()->mBounds.getExtents() * 0.5f;
Con::warnf( "PhysicsShapeData::preload - No collision found for shape '%s', auto-creating one", shapeAsset.assetId);
Point3F halfWidth = shape->mBounds.getExtents() * 0.5f;
colShape = PHYSICSMGR->createCollision();
MatrixF centerXfm(true);
centerXfm.setPosition(getShape()->mBounds.getCenter());
centerXfm.setPosition(shape->mBounds.getCenter());
colShape->addBox(halfWidth, centerXfm);
return true;
}
@ -703,11 +711,12 @@ bool PhysicsShape::_createShape()
mAmbientSeq = -1;
PhysicsShapeData *db = getDataBlock();
if ( !db || !db->getShape())
Resource<TSShape> shape;
if ( !db || !(shape = db->shapeAsset.assetPtr->getShapeResource()))
return false;
// Set the world box.
mObjBox = db->getShape()->mBounds;
mObjBox = shape->mBounds;
resetWorldBox();
// If this is the server and its a client only simulation
@ -721,11 +730,11 @@ bool PhysicsShape::_createShape()
}
// Create the shape instance.
mShapeInst = new TSShapeInstance( db->getShape(), isClientObject() );
mShapeInst = new TSShapeInstance(shape, isClientObject() );
if ( isClientObject() )
{
mAmbientSeq = db->getShape()->findSequence( "ambient" );
mAmbientSeq = shape->findSequence( "ambient" );
_initAmbient();
}

View file

@ -74,7 +74,7 @@ public:
public:
DECLARE_SHAPEASSET_REFACTOR(PhysicsShapeData, Shape)
AssetRef<ShapeAsset> shapeAsset;
/// The shared unscaled collision shape.
PhysicsCollisionRef colShape;