Skip to content

Commit

Permalink
Fixed an issue where the wrong camera would be uploaded to the GPU
Browse files Browse the repository at this point in the history
Added a Keybind to move the character controller using the FreeflyingCamera (Keybind is G)
Fixed an issue where jumping while animations were disabled would brick animations if reenabled
Added extra defaulting to some characterSingleton parameters when the Character Controller is reinitialized
Fixed culling issues for Jolt
Increased Jolt Parameters to allow loading bigger maps
  • Loading branch information
NixAJ committed Jun 24, 2024
1 parent 71ddbef commit b9701c4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Source/Game/Game/ECS/Singletons/JoltState.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace Jolt
{
namespace Settings
{
static constexpr u32 maxBodies = 65536 * 10;
static constexpr u32 maxBodies = 65536 * 100;
static constexpr u32 numBodyMutexes = 0;
static constexpr u32 maxBodyPairs = 65536 * 10;
static constexpr u32 maxBodyPairs = 65536 * 100;
static constexpr u32 maxContactConstraints = 10240 * 10;
}

Expand Down
13 changes: 11 additions & 2 deletions Source/Game/Game/ECS/Systems/CalculateCameraMatrices.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "CalculateCameraMatrices.h"

#include "Game/ECS/Components/Camera.h"
#include "Game/ECS/Singletons/ActiveCamera.h"
#include "Game/ECS/Util/Transforms.h"
#include "Game/Rendering/GameRenderer.h"
#include "Game/Util/ServiceLocator.h"
Expand All @@ -27,10 +28,18 @@ namespace ECS::Systems
GameRenderer* gameRenderer = ServiceLocator::GetGameRenderer();
RenderResources& renderResources = gameRenderer->GetRenderResources();

auto view = registry.view<Components::Transform, Components::Camera>();
entt::registry::context& ctx = registry.ctx();
ECS::Singletons::ActiveCamera& activeCamera = ctx.get<ECS::Singletons::ActiveCamera>();

view.each([&](Components::Transform& transform, Components::Camera& camera)
auto view = registry.view<Components::Transform, Components::Camera>();
view.each([&](entt::entity e, Components::Transform& transform, Components::Camera& camera)
{
if (e != activeCamera.entity)
{
// TODO: Multiple cameras (picture-in-picture I guess?) would need to change this
return;
}

vec2 renderSize = gameRenderer->GetRenderer()->GetRenderSize();
camera.aspectRatio = renderSize.x / renderSize.y;

Expand Down
60 changes: 55 additions & 5 deletions Source/Game/Game/ECS/Systems/CharacterController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <entt/entt.hpp>
#include <GLFW/glfw3.h>
#include <Game/ECS/Util/CameraUtil.h>
#include <Base/CVarSystem/CVarSystem.h>

namespace ECS::Systems
{
Expand Down Expand Up @@ -88,6 +89,9 @@ namespace ECS::Systems

if (activeCamera.entity == orbitalCameraSettings.entity)
{
if (!registry->valid(freeFlyingCameraSettings.entity))
return false;

ECS::Util::CameraUtil::SetCaptureMouse(false); // Uncapture mouse for Orbital Camera when switching to FreeFlying Camera
activeCamera.entity = freeFlyingCameraSettings.entity;

Expand All @@ -103,6 +107,9 @@ namespace ECS::Systems
}
else if (activeCamera.entity == freeFlyingCameraSettings.entity)
{
if (!registry->valid(orbitalCameraSettings.entity))
return false;

TransformSystem& transformSystem = ctx.get<TransformSystem>();

ECS::Components::Transform& transform = registry->get<ECS::Components::Transform>(characterSingleton.entity);
Expand All @@ -127,6 +134,35 @@ namespace ECS::Systems
return true;
});

characterSingleton.cameraToggleKeybindGroup->AddKeyboardCallback("Move Character To Camera", GLFW_KEY_G, KeybindAction::Press, KeybindModifier::Any, [](i32 key, KeybindAction action, KeybindModifier modifier)
{
entt::registry* registry = ServiceLocator::GetEnttRegistries()->gameRegistry;
entt::registry::context& ctx = registry->ctx();

Singletons::CharacterSingleton& characterSingleton = ctx.get<Singletons::CharacterSingleton>();
Singletons::ActiveCamera& activeCamera = ctx.get<Singletons::ActiveCamera>();
Singletons::FreeflyingCameraSettings& freeFlyingCameraSettings = ctx.get<Singletons::FreeflyingCameraSettings>();

if (activeCamera.entity != freeFlyingCameraSettings.entity)
return false;

TransformSystem& transformSystem = ctx.get<TransformSystem>();

ECS::Components::Camera& camera = registry->get<ECS::Components::Camera>(freeFlyingCameraSettings.entity);
ECS::Components::Transform& cameraTransform = registry->get<ECS::Components::Transform>(freeFlyingCameraSettings.entity);
ECS::Components::Transform& characterTransform = registry->get<ECS::Components::Transform>(characterSingleton.entity);

vec3 newPosition = cameraTransform.GetWorldPosition();
characterSingleton.character->SetLinearVelocity(JPH::Vec3::sZero());
characterSingleton.character->SetPosition(JPH::RVec3Arg(newPosition.x, newPosition.y, newPosition.z));
transformSystem.SetWorldPosition(characterSingleton.entity, newPosition);

characterSingleton.pitch = 0.0f;
characterSingleton.yaw = glm::pi<f32>() + glm::radians(camera.yaw);

return true;
});

characterSingleton.cameraToggleKeybindGroup->SetActive(true);
}

Expand Down Expand Up @@ -246,6 +282,13 @@ namespace ECS::Systems
JPH::CharacterVirtual::EGroundState groundState = characterSingleton.character->GetGroundState();
bool isGrounded = groundState == JPH::CharacterVirtual::EGroundState::OnGround;

// Fix for animations bricking when turning off animations while jumping state is not None
bool animationsEnabled = *CVarSystem::Get()->GetIntCVar(CVarCategory::Client | CVarCategory::Rendering, "animationEnabled");
if (!animationsEnabled && characterSingleton.jumpState != ECS::Singletons::JumpState::None)
{
characterSingleton.jumpState = ECS::Singletons::JumpState::None;
}

// TODO : When jumping, we need to incoporate checks from the physics system to handle if jumping ends early
bool isJumping = false;
bool canJump = characterSingleton.jumpState == ECS::Singletons::JumpState::None || characterSingleton.jumpState == ECS::Singletons::JumpState::End;
Expand Down Expand Up @@ -316,7 +359,8 @@ namespace ECS::Systems
bool canPlay = !animationSystem->IsPlaying(instanceID, bone, animationID);
if (canPlay)
{
animationSystem->SetBoneSequence(instanceID, bone, animationID, flags, blendOverride, callback);
if (!animationSystem->SetBoneSequence(instanceID, bone, animationID, flags, blendOverride, callback))
return false;
}

return canPlay;
Expand Down Expand Up @@ -570,6 +614,9 @@ namespace ECS::Systems
characterSettings.mShape = shapeResult.Get();
characterSettings.mBackFaceMode = JPH::EBackFaceMode::IgnoreBackFaces;

if (characterSingleton.character)
delete characterSingleton.character;

characterSingleton.character = new JPH::CharacterVirtual(&characterSettings, JPH::RVec3(0.0f, 0.0f, 0.0f), JPH::Quat::sIdentity(), &joltState.physicsSystem);
characterSingleton.character->SetShapeOffset(JPH::Vec3(0.0f, 0.0f, 0.0f));

Expand All @@ -581,13 +628,16 @@ namespace ECS::Systems
newPosition = JPH::Vec3(0.0f, 0.0f, 0.0f);
}

characterSingleton.character->SetLinearVelocity(JPH::Vec3::sZero());
characterSingleton.character->SetPosition(newPosition);

if (activeCamera.entity == orbitalCameraSettings.entity)
{
transformSystem.ParentEntityTo(characterSingleton.entity, orbitalCameraSettings.entity);
}
characterSingleton.speed = 7.1111f;
characterSingleton.jumpState = ECS::Singletons::JumpState::None;
characterSingleton.pitch = 0.0f;
characterSingleton.yaw = 0.0f;

transformSystem.ParentEntityTo(characterSingleton.entity, orbitalCameraSettings.entity);
transformSystem.SetLocalPosition(orbitalCameraSettings.entity, orbitalCameraSettings.cameraCurrentZoomOffset);
transformSystem.SetWorldPosition(characterSingleton.entity, vec3(newPosition.GetX(), newPosition.GetY(), newPosition.GetZ()));
}
}
2 changes: 1 addition & 1 deletion Source/Game/Game/ECS/Systems/OrbitalCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ namespace ECS::Systems
glm::mat3 rotationMatrix = glm::mat3_cast(resultRotation);
f32 yaw = glm::radians(camera.yaw);

characterSingleton.yaw = yaw + glm::pi<f32>();;
characterSingleton.yaw = yaw + glm::pi<f32>();
}

tSystem.SetWorldRotation(activeCamera.entity, resultRotation);
Expand Down
2 changes: 0 additions & 2 deletions Source/Game/Game/Rendering/Model/ModelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@ bool ModelLoader::LoadRequest(const LoadRequestInternal& request)

// Generate Jolt Shape
{
// Disabled on purpose for now
i32 physicsEnabled = *CVarSystem::Get()->GetIntCVar(CVarCategory::Client | CVarCategory::Physics, "enabled"_h);
u32 numPhysicsBytes = static_cast<u32>(model->physicsData.size());

Expand All @@ -684,7 +683,6 @@ bool ModelLoader::LoadRequest(const LoadRequestInternal& request)
JPH::Shape::IDToMaterialMap materialMap;

JPH::MeshShapeSettings::ShapeResult shapeResult = JPH::Shape::sRestoreWithChildren(streamIn, shapeMap, materialMap);
JPH::ShapeRefC shape = shapeResult.Get();

_nameHashToJoltShape[request.placement.nameHash] = shapeResult.Get();
discoveredModel.hasShape = true;
Expand Down
4 changes: 2 additions & 2 deletions Source/Shaders/Shaders/Utils/CullingInstanced.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ CullingData LoadCullingData(uint instanceIndex)

bool SphereIsForwardPlane(float4 plane, float4 sphere)
{
return dot(plane.xyz, sphere.xyz) + plane.w > -sphere.w;
return (dot(plane.xyz, sphere.xyz) - plane.w) > -sphere.w;
}

bool IsSphereInsideFrustum(float4 frustum[6], float4 sphere)
Expand Down Expand Up @@ -298,7 +298,7 @@ void main(CSInput input)
// Calculate bounding sphere from AABB
float4 sphere;
sphere.xyz = (aabb.min + aabb.max) / 2.0f;
sphere.w = distance(aabb.min, aabb.max);
sphere.w = distance(aabb.min, aabb.max) / 2.0f;

// Load DrawCalls instanceCount
uint instanceCount = GetDrawCallInstanceCount(instanceRef.drawID);
Expand Down

0 comments on commit b9701c4

Please sign in to comment.