Fixed Shadow bugs and implemented scripting for trajectory rendering #407

Merged
Xenosas1337 merged 5 commits from SP3-1-Rendering into main 2023-03-09 15:10:59 +08:00
5 changed files with 128 additions and 107 deletions
Showing only changes of commit e3a552f983 - Show all commits

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
@ -67,16 +80,16 @@ namespace SHADE
return colorEvolveRate;
}
EntityID SHTrajectoryRenderableComponent::GetEntityToSimulate(void) const noexcept
{
return entityToSimulate;
}
void SHTrajectoryRenderableComponent::SetMesh(Handle<SHMesh> newMesh) noexcept
{
mesh = newMesh;
}
void SHTrajectoryRenderableComponent::SetPositions(std::vector<SHVec3> const& inPositions) noexcept
{
positions = inPositions;
}
void SHTrajectoryRenderableComponent::SetStartColor(SHVec3 color) noexcept
{
startColor = color;

View File

@ -17,9 +17,6 @@ namespace SHADE
//! Mesh used to render the trajectory
Handle<SHMesh> mesh;
//! positions to plot for rendering. Will be cleared every frame.
std::vector<SHVec3> positions;
//! Starting color of the trajectory
SHVec3 startColor;
@ -35,36 +32,48 @@ namespace SHADE
//! evolving rate of the color
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
//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:
/*-----------------------------------------------------------------------*/
/* PRIVATE MEMBER FUNCTIONS */
/*-----------------------------------------------------------------------*/
void SetMesh(Handle<SHMesh> newMesh) noexcept;
void SetPositions (std::vector<SHVec3> const& inPositions) noexcept;
void SetStartColor(SHVec3 startColor) noexcept;
void SetEndColor (SHVec3 endColor) noexcept;
void SetStartAlpha(float a) noexcept;
void SetEndAlpha (float a) noexcept;
void SetColorEvolveRate(float rate) noexcept;
std::vector<SHVec3> GetPositions (void) const noexcept;
Handle<SHMesh> GetMesh (void) const noexcept;
SHVec3 const& GetStartColor (void) const noexcept;
SHVec3 const& GetEndColor (void) const noexcept;
float GetStartAlpha (void) const noexcept;
float GetEndAlpha (void) const noexcept;
float GetColorEvolveRate (void) const noexcept;
Handle<SHMesh> GetMesh (void) const noexcept;
SHVec3 const& GetStartColor (void) const noexcept;
SHVec3 const& GetEndColor (void) const noexcept;
float GetStartAlpha (void) const noexcept;
float GetEndAlpha (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 OnDestroy(void) override final;
void ClearPositions(void) noexcept;
bool HasPositions(void) const noexcept;
void SimulateTrajectory (EntityID eid) noexcept;
void SimulateTrajectory (EntityID eid, SHVec3 force, float timestep, uint32_t maxSteps) noexcept;
RTTR_ENABLE()

View File

@ -81,94 +81,85 @@ namespace SHADE
auto& comps = SHComponentManager::GetDense<SHTrajectoryRenderableComponent>();
for (auto& comp : comps)
{
//std::vector<SHVec3> test{};
//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())
if (EntityID entityToSimulate = comp.GetEntityToSimulate(); entityToSimulate != MAX_EID)
{
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();
auto const& positions = comp.GetPositions();
for (auto& pos : positions)
std::vector<SHVec3> positions{};
std::vector<SHQuaternion> quats{};
physicsSystem->SimulateBody
(positions, quats,
SHPhysicsSystem::SimulateBodyInfo
{
// modify position and reuse matrix
trs.m[3][0] = pos.x;
trs.m[3][1] = pos.y;
trs.m[3][2] = pos.z;
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);
.bodyEID = entityToSimulate,
.force = comp.GetSimulationForce(),
.continuousForce = false,
.timeStep = comp.GetSimulationTimestep(),
.maxSteps = static_cast<int>(comp.GetSimulationMaxSteps()),
}
);
// add draw data for this trajectory
drawData.push_back(vk::DrawIndexedIndirectCommand
comp.ResetSimulationInfo();
// 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,
.instanceCount = static_cast<uint32_t>(transformData.size()) - oldTransformDataSize,
.firstIndex = meshHandle->FirstIndex,
.vertexOffset = meshHandle->FirstVertex,
.firstInstance = oldTransformDataSize
});
// modify position and reuse matrix
trs.m[3][0] = pos.x;
trs.m[3][1] = pos.y;
trs.m[3][2] = pos.z;
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())

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()
{
auto mesh = GetNativeComponent()->GetMesh();

View File

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