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,32 +81,26 @@ 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<SHVec3> positions{};
std::vector<SHQuaternion> quats{}; std::vector<SHQuaternion> quats{};
physicsSystem->SimulateBody physicsSystem->SimulateBody
(positions, quats, (positions, quats,
SHPhysicsSystem::SimulateBodyInfo SHPhysicsSystem::SimulateBodyInfo
{ {
.bodyEID = comp.GetEID(), .bodyEID = entityToSimulate,
.force = SHVec3 {30.0f, 100.0f, 30.0f}, .force = comp.GetSimulationForce(),
.continuousForce = false, .continuousForce = false,
.timeStep = 0.1f, .timeStep = comp.GetSimulationTimestep(),
.maxSteps = 100, .maxSteps = static_cast<int>(comp.GetSimulationMaxSteps()),
} }
); );
comp.SetPositions(positions); comp.ResetSimulationInfo();
// If has positions, feed data to buffer. // If has positions, feed data to buffer.
if (comp.HasPositions()) if (!positions.empty())
{ {
auto meshHandle = comp.GetMesh(); auto meshHandle = comp.GetMesh();
@ -134,8 +128,6 @@ namespace SHADE
// Will be used for baseInstance later // Will be used for baseInstance later
uint32_t oldTransformDataSize = transformData.size(); uint32_t oldTransformDataSize = transformData.size();
auto const& positions = comp.GetPositions();
for (auto& pos : positions) for (auto& pos : positions)
{ {
// modify position and reuse matrix // modify position and reuse matrix
@ -166,9 +158,8 @@ namespace SHADE
}); });
} }
} }
}
// 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);
}; };
} }