Wrote scripting interface for trajectory rendering

This commit is contained in:
Brandon Mak 2023-03-09 10:45:57 +08:00
parent 4012eb97ba
commit e3a552f983
5 changed files with 128 additions and 107 deletions

View File

@ -22,19 +22,32 @@ namespace SHADE
} }
void SHTrajectoryRenderableComponent::ClearPositions(void) noexcept void SHTrajectoryRenderableComponent::SimulateTrajectory(EntityID eid, SHVec3 force, float timestep, uint32_t maxSteps) noexcept
{ {
positions.clear(); entityToSimulate = eid;
simulationForce = force;
simulationTimestep = timestep;
simulationMaxSteps = maxSteps;
} }
bool SHTrajectoryRenderableComponent::HasPositions(void) const noexcept float SHTrajectoryRenderableComponent::GetSimulationTimestep(void) const noexcept
{ {
return !positions.empty(); return simulationTimestep;
} }
std::vector<SHVec3> SHTrajectoryRenderableComponent::GetPositions(void) const noexcept void SHTrajectoryRenderableComponent::ResetSimulationInfo(void) noexcept
{ {
return positions; entityToSimulate = MAX_EID;
}
uint32_t SHTrajectoryRenderableComponent::GetSimulationMaxSteps(void) const noexcept
{
return simulationMaxSteps;
}
SHVec3 SHTrajectoryRenderableComponent::GetSimulationForce(void) const noexcept
{
return simulationForce;
} }
Handle<SHMesh> SHTrajectoryRenderableComponent::GetMesh(void) const noexcept Handle<SHMesh> SHTrajectoryRenderableComponent::GetMesh(void) const noexcept
@ -67,16 +80,16 @@ namespace SHADE
return colorEvolveRate; return colorEvolveRate;
} }
EntityID SHTrajectoryRenderableComponent::GetEntityToSimulate(void) const noexcept
{
return entityToSimulate;
}
void SHTrajectoryRenderableComponent::SetMesh(Handle<SHMesh> newMesh) noexcept void SHTrajectoryRenderableComponent::SetMesh(Handle<SHMesh> newMesh) noexcept
{ {
mesh = newMesh; mesh = newMesh;
} }
void SHTrajectoryRenderableComponent::SetPositions(std::vector<SHVec3> const& inPositions) noexcept
{
positions = inPositions;
}
void SHTrajectoryRenderableComponent::SetStartColor(SHVec3 color) noexcept void SHTrajectoryRenderableComponent::SetStartColor(SHVec3 color) noexcept
{ {
startColor = color; startColor = color;

View File

@ -17,9 +17,6 @@ namespace SHADE
//! Mesh used to render the trajectory //! Mesh used to render the trajectory
Handle<SHMesh> mesh; Handle<SHMesh> mesh;
//! positions to plot for rendering. Will be cleared every frame.
std::vector<SHVec3> positions;
//! Starting color of the trajectory //! Starting color of the trajectory
SHVec3 startColor; SHVec3 startColor;
@ -35,36 +32,48 @@ namespace SHADE
//! evolving rate of the color //! evolving rate of the color
float colorEvolveRate; float colorEvolveRate;
//! Used for the trajectory simulation. Indicates the time to pass before
//! plotting a point in the simulation
float simulationTimestep;
//! Entity to simulate trajectory of //! Entity to simulate trajectory of
//EntityID EntityID entityToSimulate;
//! Force to use during simulation of
SHVec3 simulationForce;
//! max points to be plotted in the simulation before stopping.
//! Note that the plotting might still be halted if the simulation
//! detects a raycast hit with a collider.
uint32_t simulationMaxSteps;
public: public:
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* PRIVATE MEMBER FUNCTIONS */ /* PRIVATE MEMBER FUNCTIONS */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
void SetMesh(Handle<SHMesh> newMesh) noexcept; void SetMesh(Handle<SHMesh> newMesh) noexcept;
void SetPositions (std::vector<SHVec3> const& inPositions) noexcept;
void SetStartColor(SHVec3 startColor) noexcept; void SetStartColor(SHVec3 startColor) noexcept;
void SetEndColor (SHVec3 endColor) noexcept; void SetEndColor (SHVec3 endColor) noexcept;
void SetStartAlpha(float a) noexcept; void SetStartAlpha(float a) noexcept;
void SetEndAlpha (float a) noexcept; void SetEndAlpha (float a) noexcept;
void SetColorEvolveRate(float rate) noexcept; void SetColorEvolveRate(float rate) noexcept;
std::vector<SHVec3> GetPositions (void) const noexcept; Handle<SHMesh> GetMesh (void) const noexcept;
Handle<SHMesh> GetMesh (void) const noexcept; SHVec3 const& GetStartColor (void) const noexcept;
SHVec3 const& GetStartColor (void) const noexcept; SHVec3 const& GetEndColor (void) const noexcept;
SHVec3 const& GetEndColor (void) const noexcept; float GetStartAlpha (void) const noexcept;
float GetStartAlpha (void) const noexcept; float GetEndAlpha (void) const noexcept;
float GetEndAlpha (void) const noexcept; float GetColorEvolveRate (void) const noexcept;
float GetColorEvolveRate (void) const noexcept; EntityID GetEntityToSimulate (void) const noexcept;
SHVec3 GetSimulationForce (void) const noexcept;
uint32_t GetSimulationMaxSteps (void) const noexcept;
float GetSimulationTimestep (void) const noexcept;
void ResetSimulationInfo (void) noexcept;
void OnCreate(void) override final; void OnCreate(void) override final;
void OnDestroy(void) override final; void OnDestroy(void) override final;
void ClearPositions(void) noexcept; void SimulateTrajectory (EntityID eid, SHVec3 force, float timestep, uint32_t maxSteps) noexcept;
bool HasPositions(void) const noexcept;
void SimulateTrajectory (EntityID eid) noexcept;
RTTR_ENABLE() RTTR_ENABLE()

View File

@ -81,94 +81,85 @@ namespace SHADE
auto& comps = SHComponentManager::GetDense<SHTrajectoryRenderableComponent>(); auto& comps = SHComponentManager::GetDense<SHTrajectoryRenderableComponent>();
for (auto& comp : comps) for (auto& comp : comps)
{ {
//std::vector<SHVec3> test{}; if (EntityID entityToSimulate = comp.GetEntityToSimulate(); entityToSimulate != MAX_EID)
//test.resize(10);
//float x = 0.0f;
//for (auto& vec : test)
//{
// vec = SHVec3(x, 5.0f, 0.0f);
// x += 0.5f;
//}
std::vector<SHVec3> positions{};
std::vector<SHQuaternion> quats{};
physicsSystem->SimulateBody
(positions, quats,
SHPhysicsSystem::SimulateBodyInfo
{
.bodyEID = comp.GetEID(),
.force = SHVec3 {30.0f, 100.0f, 30.0f},
.continuousForce = false,
.timeStep = 0.1f,
.maxSteps = 100,
}
);
comp.SetPositions(positions);
// If has positions, feed data to buffer.
if (comp.HasPositions())
{ {
auto meshHandle = comp.GetMesh(); std::vector<SHVec3> positions{};
std::vector<SHQuaternion> quats{};
// dont do anything if no mesh physicsSystem->SimulateBody
if (!meshHandle) (positions, quats,
continue; SHPhysicsSystem::SimulateBodyInfo
SHTransformComponent* transform = SHComponentManager::GetComponent_s<SHTransformComponent>(comp.GetEID());
if (transform)
{
// convenient variable
SHVec3 const& startColor = comp.GetStartColor();
SHVec3 const& endColor = comp.GetEndColor();
float colorEvolveRate = comp.GetColorEvolveRate();
// trs to be reused
SHMatrix trs = transform->GetTRS();
// starting color of trajectory
SHVec4 currentColor = comp.GetStartColor();
// Start from 0 and slowly evolve to 1
float lerpValue = 0.0f;
// Will be used for baseInstance later
uint32_t oldTransformDataSize = transformData.size();
auto const& positions = comp.GetPositions();
for (auto& pos : positions)
{ {
// modify position and reuse matrix .bodyEID = entityToSimulate,
trs.m[3][0] = pos.x; .force = comp.GetSimulationForce(),
trs.m[3][1] = pos.y; .continuousForce = false,
trs.m[3][2] = pos.z; .timeStep = comp.GetSimulationTimestep(),
.maxSteps = static_cast<int>(comp.GetSimulationMaxSteps()),
transformData.push_back(trs);
colorData.push_back(currentColor);
// evolve lerp value and clamp to 1
lerpValue = std::min (1.0f, lerpValue + colorEvolveRate);
// evolve color
currentColor = SHVec3::Lerp(startColor, endColor, lerpValue);
currentColor.w = SHMath::Lerp (comp.GetStartAlpha(), comp.GetEndAlpha(), lerpValue);
} }
);
// add draw data for this trajectory comp.ResetSimulationInfo();
drawData.push_back(vk::DrawIndexedIndirectCommand
// If has positions, feed data to buffer.
if (!positions.empty())
{
auto meshHandle = comp.GetMesh();
// dont do anything if no mesh
if (!meshHandle)
continue;
SHTransformComponent* transform = SHComponentManager::GetComponent_s<SHTransformComponent>(comp.GetEID());
if (transform)
{
// convenient variable
SHVec3 const& startColor = comp.GetStartColor();
SHVec3 const& endColor = comp.GetEndColor();
float colorEvolveRate = comp.GetColorEvolveRate();
// trs to be reused
SHMatrix trs = transform->GetTRS();
// starting color of trajectory
SHVec4 currentColor = comp.GetStartColor();
// Start from 0 and slowly evolve to 1
float lerpValue = 0.0f;
// Will be used for baseInstance later
uint32_t oldTransformDataSize = transformData.size();
for (auto& pos : positions)
{ {
.indexCount = meshHandle->IndexCount, // modify position and reuse matrix
.instanceCount = static_cast<uint32_t>(transformData.size()) - oldTransformDataSize, trs.m[3][0] = pos.x;
.firstIndex = meshHandle->FirstIndex, trs.m[3][1] = pos.y;
.vertexOffset = meshHandle->FirstVertex, trs.m[3][2] = pos.z;
.firstInstance = oldTransformDataSize
}); transformData.push_back(trs);
colorData.push_back(currentColor);
// evolve lerp value and clamp to 1
lerpValue = std::min(1.0f, lerpValue + colorEvolveRate);
// evolve color
currentColor = SHVec3::Lerp(startColor, endColor, lerpValue);
currentColor.w = SHMath::Lerp(comp.GetStartAlpha(), comp.GetEndAlpha(), lerpValue);
}
// add draw data for this trajectory
drawData.push_back(vk::DrawIndexedIndirectCommand
{
.indexCount = meshHandle->IndexCount,
.instanceCount = static_cast<uint32_t>(transformData.size()) - oldTransformDataSize,
.firstIndex = meshHandle->FirstIndex,
.vertexOffset = meshHandle->FirstVertex,
.firstInstance = oldTransformDataSize
});
}
} }
} }
// clear at the end of every frame since data is already in buffers
comp.ClearPositions();
} }
if (!transformData.empty()) if (!transformData.empty())

View File

@ -13,6 +13,11 @@ namespace SHADE
} }
void TrajectoryRenderable::SimulateTrajectory(EntityID eid, Vector3 force, float timestep, uint32_t maxSteps)
{
GetNativeComponent()->SimulateTrajectory(eid, Convert::ToNative(force), timestep, maxSteps);
}
MeshAsset TrajectoryRenderable::Mesh::get() MeshAsset TrajectoryRenderable::Mesh::get()
{ {
auto mesh = GetNativeComponent()->GetMesh(); auto mesh = GetNativeComponent()->GetMesh();

View File

@ -82,6 +82,9 @@ namespace SHADE
float get(); float get();
void set(float val); void set(float val);
} }
void SimulateTrajectory(EntityID eid, Vector3 force, float timestep, uint32_t maxSteps);
}; };
} }