Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions trinity/Eve/EveInstancedMeshManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ class EveInstancedMeshManager

struct DynamicPerInstanceData
{
Vector4 worldTransform[3];
Vector4 prevWorldTransform[3];
Float4x3 worldTransform;
Float4x3 prevWorldTransform;
uint32_t sphereIndex = 0;
};

struct StaticPerInstanceData
{
Vector4 worldTransform[3];
Float4x3 worldTransform;
uint32_t sphereIndex = 0;
};

Expand Down Expand Up @@ -142,14 +142,14 @@ class EveInstancedMeshManager
private:
struct StaticPerInstanceBufferElement
{
Vector4 worldTransform[3];
Float4x3 worldTransform;
uint32_t perObjectDataIndex = 0;
};

struct DynamicPerInstanceBufferElement
{
Vector4 worldTransform[3];
Vector4 prevWorldTransform[3];
Float4x3 worldTransform;
Float4x3 prevWorldTransform;
uint32_t perObjectDataIndex = 0;
};

Expand Down
79 changes: 51 additions & 28 deletions trinity/Eve/SpaceObject/Children/EveChildInstancedMeshes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ void EveChildInstancedMeshes::PushRtGeometry( Tr2RaytracingManager& rtManager )
}
}

XMMATRIX m = *reinterpret_cast<const Matrix*>( instanceTransform.worldTransform );
m.r[3] = XMVectorSet( 0, 0, 0, 1 );
m = XMMatrixMultiply( XMMatrixTranspose( m ), m_worldTransform );
XMMATRIX m = Matrix( instanceTransform.worldTransform );
m = XMMatrixMultiply( m, m_worldTransform );
mesh.rtMeshes[lodIndex].instanceWorldTransforms.push_back( Float4x3( Matrix( m ) ) );
}

Expand Down Expand Up @@ -275,10 +274,11 @@ void EveChildInstancedMeshes::UpdateAsyncronous( const EveUpdateContext& updateC

for( const auto& instance : mesh.instances )
{
Vector3 position = Vector3( instance.worldTransform[0].w, instance.worldTransform[1].w, instance.worldTransform[2].w );
float scale = std::sqrtf( std::max( { LengthSq( instance.worldTransform[0].GetXYZ() ),
LengthSq( instance.worldTransform[1].GetXYZ() ),
LengthSq( instance.worldTransform[2].GetXYZ() ) } ) );
Matrix m = instance.worldTransform;
Vector3 position = m.GetTranslation();
float scale = std::sqrtf( std::max( { LengthSq( Vector3( m._11, m._12, m._13 ) ),
LengthSq( Vector3( m._21, m._22, m._23 ) ),
LengthSq( Vector3( m._31, m._32, m._33 ) ) } ) );
position = TransformCoord( position, m_worldTransform );
scale *= worldScale;
mesh.instanceSpheres[&instance - mesh.instances.data()] = CcpMath::Sphere( position, radius * scale );
Expand Down Expand Up @@ -449,10 +449,7 @@ void EveChildInstancedMeshes::AddMesh(
for( size_t i = 0; i < count; ++i )
{
EveInstancedMeshManager::StaticPerInstanceData instanceData;
auto& mat = instanceTransforms[i];
instanceData.worldTransform[0] = Vector4( mat._11, mat._21, mat._31, mat._41 );
instanceData.worldTransform[1] = Vector4( mat._12, mat._22, mat._32, mat._42 );
instanceData.worldTransform[2] = Vector4( mat._13, mat._23, mat._33, mat._43 );
instanceData.worldTransform = Float4x3( instanceTransforms[i] );
instanceData.sphereIndex = static_cast<uint32_t>( existingCount + i );
mesh.instances.push_back( instanceData );
mesh.partTags.push_back( partTag );
Expand Down Expand Up @@ -493,10 +490,7 @@ void EveChildInstancedMeshes::AddMesh(
for( size_t i = 0; i < count; ++i )
{
EveInstancedMeshManager::StaticPerInstanceData instanceData;
auto& mat = instanceTransforms[i];
instanceData.worldTransform[0] = Vector4( mat._11, mat._21, mat._31, mat._41 );
instanceData.worldTransform[1] = Vector4( mat._12, mat._22, mat._32, mat._42 );
instanceData.worldTransform[2] = Vector4( mat._13, mat._23, mat._33, mat._43 );
instanceData.worldTransform = Float4x3( instanceTransforms[i] );
instanceData.sphereIndex = static_cast<uint32_t>( i );
mesh.instances.push_back( instanceData );
mesh.partTags.push_back( partTag );
Expand Down Expand Up @@ -594,6 +588,21 @@ void EveChildInstancedMeshes::RemoveInstancesByPartTag( EveSpaceObjectChild::Par
}
}
}
void EveChildInstancedMeshes::SetInstanceTransformByPartTag( PartTag partTag, const Vector3& translation, const Quaternion& rotation, Vector3 scale )
{
Matrix m = TransformationMatrix( scale, rotation, translation );
const Float4x3 packedTransform( m );
for( auto& mesh : m_meshes )
{
for( size_t i = 0; i < mesh.instances.size(); ++i )
{
if( mesh.partTags[i] == partTag )
{
mesh.instances[i].worldTransform = packedTransform;
}
}
}
}

void EveChildInstancedMeshes::ReleaseCachedData( BlueAsyncRes* p )
{
Expand Down Expand Up @@ -769,6 +778,32 @@ uint32_t EveChildInstancedMeshes::GetMeshCount() const
{
return static_cast<uint32_t>( m_meshes.size() );
}
BluePy EveChildInstancedMeshes::GetInstancesTransforms( uint32_t meshId ) const
{
if( meshId >= m_meshes.size() )
{
PyErr_SetString( PyExc_IndexError, "Mesh index out of range" );
return {};
}

auto& mesh = m_meshes[meshId];
BluePy result( PyTuple_New( mesh.instances.size() ) );
int i = 0;
for( auto& instance : mesh.instances )
{
Vector3 scale, translation;
Quaternion rotation;
Decompose( scale, rotation, translation, instance.worldTransform );

PyObject* transform = PyTuple_New( 3 );
PyTuple_SetItem( transform, 0, ToPython( translation ) );
Comment thread
Ikreb1 marked this conversation as resolved.
PyTuple_SetItem( transform, 1, ToPython( rotation ) );
PyTuple_SetItem( transform, 2, ToPython( scale ) );
PyTuple_SetItem( result, i++, transform );
}
Comment thread
Ikreb1 marked this conversation as resolved.

return result;
}

BluePy EveChildInstancedMeshes::GetMeshInfo( uint32_t meshId ) const
{
Expand Down Expand Up @@ -1040,19 +1075,7 @@ void EveChildInstancedMeshes::UpdateOverlayInstanceData( const EveSpaceObjectVSD
const auto& wt = mesh.instances[i].worldTransform;
OverlayInstancePod& pod = ( *mesh.overlayPods )[i];

Matrix local = IdentityMatrix();
local._11 = wt[0].x;
local._12 = wt[1].x;
local._13 = wt[2].x;
local._21 = wt[0].y;
local._22 = wt[1].y;
local._23 = wt[2].y;
local._31 = wt[0].z;
local._32 = wt[1].z;
local._33 = wt[2].z;
local._41 = wt[0].w;
local._42 = wt[1].w;
local._43 = wt[2].w;
Matrix local = mesh.instances[i].worldTransform;

Matrix worldTransform = Transpose( local * m_worldTransform );
Matrix worldTransformLast = Transpose( local * prevWorldTransform );
Expand Down
2 changes: 2 additions & 0 deletions trinity/Eve/SpaceObject/Children/EveChildInstancedMeshes.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ BLUE_CLASS( EveChildInstancedMeshes ) :
EveSpaceObjectChild::PartTag partTag = EveSpaceObjectChild::NO_PART_TAG );

void RemoveInstancesByPartTag( EveSpaceObjectChild::PartTag partTag );
void SetInstanceTransformByPartTag( PartTag partTag, const Vector3& translation, const Quaternion& rotation, Vector3 scale );

BluePy GetSofSourceLocator( uint32_t areaId ) const;
uint32_t GetMeshCount() const;
BluePy GetMeshInfo( uint32_t meshId ) const;
BluePy GetAreaInfo( uint32_t meshId, uint32_t areaId ) const;
BluePy GetInstancesTransforms( uint32_t meshId ) const;
BluePy GetMeshDisplay( uint32_t meshId ) const;
BluePy SetMeshDisplay( uint32_t meshId, bool display );
BluePy GetMeshInheritOverlayEffects( uint32_t meshId ) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ const Be::ClassInfo* EveChildInstancedMeshes::ExposeToBlue()
":param meshId: Index of the mesh to query\n"
":param areaId: Index of the area to query\n"
":rtype: (trinity.Tr2Effect, int, int, int)" )
MAP_METHOD_AND_WRAP(
"GetInstancesTransforms",
GetInstancesTransforms,
"Returns the object-local transform of each instance of an instanced mesh, decomposed\n"
"into translation, rotation and scale. Returns one (translation, rotation, scale)\n"
"tuple per instance\n\n"
":param meshId: Index of the mesh to query\n"
":rtype: (((float, float, float), (float, float, float, float), (float, float, float)), ...)" )
MAP_METHOD_AND_WRAP(
"GetMeshDisplay",
GetMeshDisplay,
Expand Down
3 changes: 1 addition & 2 deletions trinity/Eve/SpaceObject/Children/EveChildPartData_Blue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ const Be::ClassInfo* EveChildPartData::ExposeToBlue()
EXPOSURE_BEGIN( EveChildPartData, "Persistent state of a modular space object (per-part transforms and bounds). Edit through EveModularObjectModifier" )
MAP_INTERFACE( EveSpaceObjectChild );
MAP_INTERFACE( IEveSpaceObjectChild )
MAP_ATTRIBUTE( "name", m_name, "Name of the space object child", Be::READWRITE | Be::PERSIST )
EXPOSURE_END()
EXPOSURE_CHAINTO( EveSpaceObjectChild )
}
4 changes: 4 additions & 0 deletions trinity/Eve/SpaceObject/Children/EveModularObjectModifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ BlueStdResult EveModularObjectModifier::SetTransform( EveSpaceObjectChild::PartT
{
child->Setup( &scale, &rotation, &position, Tr2Lod::TR2_LOD_LOW );
}
if( EveChildInstancedMeshesPtr instancedMeshes = BlueCastPtr( child ) )
{
instancedMeshes->SetInstanceTransformByPartTag( partId, position, rotation, scale );
}
}
m_object->InvalidateMergedLocators( LocatorInvalidationReason::PartMoved );
return BlueStdResultType::BLUE_STD_RESULT_OK;
Expand Down