Bug fixes and scene changes #267

Merged
glencelow merged 15 commits from PlayerController into main 2022-11-23 20:53:44 +08:00
8 changed files with 76 additions and 39 deletions
Showing only changes of commit ca60b64a89 - Show all commits

View File

@ -8,6 +8,7 @@ namespace SHADE
SHCameraArmComponent::SHCameraArmComponent()
:pitch(0.0f), yaw(0.0f), armLength(1.0f),offset(), dirty(true), lookAtCameraOrigin(true)
, targetOffset(0.0f)
{
}
@ -33,6 +34,11 @@ namespace SHADE
return armLength;
}
SHVec3 SHCameraArmComponent::GetTargetOffset() const noexcept
{
return targetOffset;
}
void SHCameraArmComponent::SetPitch(float pitch) noexcept
{
this->pitch = pitch;
@ -50,6 +56,11 @@ namespace SHADE
this->armLength = length;
dirty = true;
}
void SHCameraArmComponent::SetTargetOffset(SHVec3 offset) noexcept
{
this->targetOffset = offset;
}
}//namespace SHADE
@ -63,6 +74,8 @@ RTTR_REGISTRATION
.property("Arm Pitch", &SHCameraArmComponent::GetPitch, &SHCameraArmComponent::SetPitch)
.property("Arm Yaw", &SHCameraArmComponent::GetYaw, &SHCameraArmComponent::SetYaw)
.property("Arm Length", &SHCameraArmComponent::GetArmLength, &SHCameraArmComponent::SetArmLength)
.property("Look At Camera Origin", &SHCameraArmComponent::lookAtCameraOrigin);
.property("Look At Camera Origin", &SHCameraArmComponent::lookAtCameraOrigin)
.property("Target Offset", &SHCameraArmComponent::GetTargetOffset, &SHCameraArmComponent::SetTargetOffset)
;
}

View File

@ -17,7 +17,7 @@ namespace SHADE
bool dirty;
SHVec3 offset;
SHVec3 targetOffset;
public:
friend class SHCameraSystem;
SHCameraArmComponent();
@ -30,11 +30,13 @@ namespace SHADE
float GetPitch() const noexcept;
float GetYaw() const noexcept;
float GetArmLength() const noexcept;
SHVec3 GetTargetOffset() const noexcept;
//Setters
void SetPitch(float pitch) noexcept;
void SetYaw(float yaw) noexcept;
void SetArmLength(float length) noexcept;
void SetTargetOffset(SHVec3 offset)noexcept;
protected:

View File

@ -182,7 +182,7 @@ namespace SHADE
{
camera.offset = arm->GetOffset();
if (arm->lookAtCameraOrigin)
CameraLookAt(camera, camera.position);
CameraLookAt(camera, camera.position + arm->GetTargetOffset());
}
}

View File

@ -340,7 +340,7 @@ namespace SHADE
}
// Transfer to GPU
if (transformDataBuffer[frameIndex])
if (transformDataBuffer[frameIndex] && !drawData.empty())
transformDataBuffer[frameIndex]->WriteToMemory(transformData.data(), static_cast<uint32_t>(transformData.size() * sizeof(SHMatrix)), 0, 0);
}
@ -369,7 +369,7 @@ namespace SHADE
}
// Transfer to GPU
if (instancedIntegerBuffer[frameIndex])
if (instancedIntegerBuffer[frameIndex] && !drawData.empty())
instancedIntegerBuffer[frameIndex]->WriteToMemory(instancedIntegerData.data(), static_cast<uint32_t>(instancedIntegerData.size() * sizeof(SHInstancedIntegerData)), 0, 0);
}
@ -507,33 +507,36 @@ namespace SHADE
isCPUBuffersDirty = false;
}
// Send all buffered data to the GPU buffers
using BuffUsage = vk::BufferUsageFlagBits;
// - Draw Data
const uint32_t DRAW_DATA_BYTES = static_cast<uint32_t>(drawData.size() * sizeof(vk::DrawIndexedIndirectCommand));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, drawDataBuffer[frameIndex], drawData.data(), DRAW_DATA_BYTES,
BuffUsage::eIndirectBuffer,
"Batch Draw Data Buffer"
);
// - Transform Buffer
const uint32_t TF_DATA_BYTES = static_cast<uint32_t>(transformData.size() * sizeof(SHMatrix));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, transformDataBuffer[frameIndex], transformData.data(), TF_DATA_BYTES,
BuffUsage::eVertexBuffer,
"Batch Transform Buffer"
);
const uint32_t EID_DATA_BYTES = static_cast<uint32_t>(instancedIntegerData.size() * sizeof(SHInstancedIntegerData));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, instancedIntegerBuffer[frameIndex], instancedIntegerData.data(), EID_DATA_BYTES,
BuffUsage::eVertexBuffer,
"Batch Instance Data Buffer"
);
// - Material Properties Buffer
rebuildMaterialBuffers(frameIndex, descPool);
// Send all buffered data to the GPU buffers if there is anything to render
if (!drawData.empty())
{
using BuffUsage = vk::BufferUsageFlagBits;
// - Draw Data
const uint32_t DRAW_DATA_BYTES = static_cast<uint32_t>(drawData.size() * sizeof(vk::DrawIndexedIndirectCommand));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, drawDataBuffer[frameIndex], drawData.data(), DRAW_DATA_BYTES,
BuffUsage::eIndirectBuffer,
"Batch Draw Data Buffer"
);
// - Transform Buffer
const uint32_t TF_DATA_BYTES = static_cast<uint32_t>(transformData.size() * sizeof(SHMatrix));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, transformDataBuffer[frameIndex], transformData.data(), TF_DATA_BYTES,
BuffUsage::eVertexBuffer,
"Batch Transform Buffer"
);
const uint32_t EID_DATA_BYTES = static_cast<uint32_t>(instancedIntegerData.size() * sizeof(SHInstancedIntegerData));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, instancedIntegerBuffer[frameIndex], instancedIntegerData.data(), EID_DATA_BYTES,
BuffUsage::eVertexBuffer,
"Batch Instance Data Buffer"
);
// - Material Properties Buffer
rebuildMaterialBuffers(frameIndex, descPool);
}
// Mark this frame as no longer dirty
isDirty[frameIndex] = false;
@ -551,7 +554,7 @@ namespace SHADE
}
// Nothing to draw
if (subBatches.empty())
if (drawData.empty())
return;
// Bind all required objects before drawing
@ -586,7 +589,7 @@ namespace SHADE
void SHBatch::rebuildMaterialBuffers(uint32_t frameIndex, Handle<SHVkDescriptorPool> descPool)
{
if (matPropsData)
if (matPropsData && !drawData.empty())
{
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(

View File

@ -219,10 +219,11 @@ namespace SHADE
static void Exit() noexcept;
static std::string GetSceneName() noexcept;
static void SetCurrentSceneName(std::string const& sceneName) noexcept;
static AssetID GetCurrentSceneAssetID() noexcept;
//Only if scene doesn't exist, and scene asset id needs to be updated to the new one
static void SetCurrentSceneAssetID(AssetID const& newAssetID);
static void SetCurrentSceneName(std::string const& sceneName) noexcept;
static AssetID GetCurrentSceneAssetID() noexcept;
//Only if scene doesn't exist, and scene asset id needs to be updated to the new one
static void SetCurrentSceneAssetID(AssetID const& newAssetID);
static bool HasSceneChanged() noexcept { return sceneChanged; }
};

View File

@ -48,4 +48,15 @@ namespace SHADE
{
GetNativeComponent()->lookAtCameraOrigin = val;
}
Vector3 CameraArm::TargetOffset::get()
{
return Convert::ToCLI(GetNativeComponent()->GetTargetOffset());
}
void CameraArm::TargetOffset::set(Vector3 val)
{
GetNativeComponent()->SetTargetOffset(Convert::ToNative(val));
}
}

View File

@ -36,5 +36,11 @@ namespace SHADE
void set(bool val);
}
property Vector3 TargetOffset
{
Vector3 get();
void set(Vector3 val);
}
};
}

View File

@ -74,8 +74,9 @@ namespace SHADE
// Add the script in
script->Initialize(GameObject(entity));
entityScriptList->Insert(System::Math::Clamp(index, 0, entityScriptList->Count), script);
if (Application::IsPlaying)
if (Application::IsPlaying && !SHSceneManager::HasSceneChanged())
{
// Only call immediately if we are in game and is not loading another scene
script->Awake();
script->Start();
}