From b6c32207975ce6f167e4ac873c1a69a4bae50b7c Mon Sep 17 00:00:00 2001 From: antopilo Date: Sun, 1 Dec 2024 13:06:13 -0500 Subject: [PATCH] Depth of field no unit accurate --- Nuake/src/Rendering/Camera.cpp | 2 +- Nuake/src/Rendering/Camera.h | 6 ++++ Nuake/src/Rendering/SceneRenderer.cpp | 3 ++ .../src/Scripting/NetModules/SceneNetAPI.cpp | 13 ++++++++ NuakeNet/src/Environment.cs | 31 +++++++++++++++++++ Resources/Shaders/dof.shader | 2 +- 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 NuakeNet/src/Environment.cs diff --git a/Nuake/src/Rendering/Camera.cpp b/Nuake/src/Rendering/Camera.cpp index f19427f3..af5fcbcf 100644 --- a/Nuake/src/Rendering/Camera.cpp +++ b/Nuake/src/Rendering/Camera.cpp @@ -67,7 +67,7 @@ namespace Nuake Matrix4 Camera::GetPerspective() { //TODO: Add perspective options - m_Perspective = glm::perspectiveFov(glm::radians(Fov), 9.0f * AspectRatio, 9.0f, 0.001f, 400.0f); + m_Perspective = glm::perspectiveFov(glm::radians(Fov), 9.0f * AspectRatio, 9.0f, Near, Far); return m_Perspective; } diff --git a/Nuake/src/Rendering/Camera.h b/Nuake/src/Rendering/Camera.h index 9a9371be..333e19f8 100644 --- a/Nuake/src/Rendering/Camera.h +++ b/Nuake/src/Rendering/Camera.h @@ -27,6 +27,9 @@ namespace Nuake Matrix4 m_Perspective; Matrix4 m_View; + float Near = 0.001f; + float Far = 400.0f; + public: float AspectRatio = 16.0f / 9.0f; @@ -48,6 +51,9 @@ namespace Nuake void SetDirection(Vector3 direction); void SetDirection(const Quat& direction); + float GetNear() const { return Near; } + float GetFar() const { return Far; } + Vector3 GetTranslation(); Vector3 GetDirection(); Matrix4 GetPerspective(); diff --git a/Nuake/src/Rendering/SceneRenderer.cpp b/Nuake/src/Rendering/SceneRenderer.cpp index 1760488b..d9a992d0 100644 --- a/Nuake/src/Rendering/SceneRenderer.cpp +++ b/Nuake/src/Rendering/SceneRenderer.cpp @@ -447,6 +447,9 @@ namespace Nuake Shader* shader = ShaderManager::GetShader("Resources/Shaders/dof.shader"); shader->Bind(); + auto cam = scene.GetCurrentCamera(); + shader->SetUniform("znear", cam->GetNear()); + shader->SetUniform("zfar", cam->GetFar()); shader->SetUniform("focalDepth", sceneEnv->DOFFocalDepth); shader->SetUniform("focalLength", sceneEnv->DOFFocalLength); shader->SetUniform("fstop", sceneEnv->DOFFstop); diff --git a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp index 14b12c8a..95af392b 100644 --- a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp @@ -625,6 +625,16 @@ namespace Nuake { } } + void EnvironmentDepthOfFieldSetFocusDistanceIcall(float distance) + { + Engine::GetCurrentScene()->GetEnvironment()->DOFFocalDepth = distance; + } + + float EnvironmentDepthOfFieldGetFocusDistanceIcall() + { + return Engine::GetCurrentScene()->GetEnvironment()->DOFFocalDepth; + } + void Nuake::SceneNetAPI::RegisterMethods() { // Entity @@ -680,6 +690,9 @@ namespace Nuake { // Audio Emitter RegisterMethod("AudioEmitterComponent.GetIsPlayingIcall", &AudioEmitterGetIsPlaying); RegisterMethod("AudioEmitterComponent.SetIsPlayingIcall", &AudioEmitterSetIsPlaying); + + RegisterMethod("Environment.SetFocusDistanceIcall", &EnvironmentDepthOfFieldSetFocusDistanceIcall); + RegisterMethod("Environment.GetFocusDistanceIcall", &EnvironmentDepthOfFieldGetFocusDistanceIcall); } } diff --git a/NuakeNet/src/Environment.cs b/NuakeNet/src/Environment.cs new file mode 100644 index 00000000..88199579 --- /dev/null +++ b/NuakeNet/src/Environment.cs @@ -0,0 +1,31 @@ +using Coral.Managed.Interop; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Nuake.Net +{ + public class Environment + { + internal static unsafe delegate* SetFocusDistanceIcall; + internal static unsafe delegate* GetFocusDistanceIcall; + + public static void SetFocusDistance(float distance) + { + unsafe + { + SetFocusDistanceIcall(distance); + } + } + + public static float GetFocusDistance() + { + unsafe + { + return GetFocusDistanceIcall(); + } + } + } +} diff --git a/Resources/Shaders/dof.shader b/Resources/Shaders/dof.shader index bc0c13ec..53b79fa9 100644 --- a/Resources/Shaders/dof.shader +++ b/Resources/Shaders/dof.shader @@ -83,7 +83,7 @@ uniform bool showFocus = false; make sure that these two values are the same for your camera, otherwise distances will be wrong. */ -uniform float znear = 0.1f; //camera clipping start +uniform float znear = 0.001f; //camera clipping start uniform float zfar = 1000.0; //camera clipping end //------------------------------------------