From acc0333bb5ea42805d3cf5cc0df9d9f252bc0261 Mon Sep 17 00:00:00 2001 From: Alan Tse Date: Mon, 28 Aug 2023 00:42:55 -0700 Subject: [PATCH 1/2] feat: add Grass Collision to VR Note: VR Address Library must be bumped to 0.103.0. --- .../GrassCollision/GrassCollision.hlsli | 10 ++--- src/Feature.cpp | 1 + src/Features/GrassCollision.cpp | 37 ++++++++++--------- src/Features/GrassCollision.h | 6 +-- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/features/Grass Collision/Shaders/GrassCollision/GrassCollision.hlsli b/features/Grass Collision/Shaders/GrassCollision/GrassCollision.hlsli index 550b0d322..8d0e9f44d 100644 --- a/features/Grass Collision/Shaders/GrassCollision/GrassCollision.hlsli +++ b/features/Grass Collision/Shaders/GrassCollision/GrassCollision.hlsli @@ -1,7 +1,7 @@ cbuffer GrassCollisionPerFrame : register(b5) { - float3 boundCentre; + float4 boundCentre[2]; float boundRadius; bool EnableGrassCollision; float RadiusMultiplier; @@ -10,7 +10,7 @@ cbuffer GrassCollisionPerFrame : register(b5) struct StructuredCollision { - float3 centre; + float3 centre[2]; float radius; }; @@ -23,7 +23,7 @@ float3 GetDisplacedPosition(float3 position, float alpha, uint eyeIndex = 0) // Player bound culling { - float dist = distance(boundCentre, worldPosition); + float dist = distance(boundCentre[eyeIndex].xyz, worldPosition); if (dist > boundRadius) { return 0; } @@ -36,9 +36,9 @@ float3 GetDisplacedPosition(float3 position, float alpha, uint eyeIndex = 0) for (uint collision_index = 0; collision_index < collision_count; collision_index++) { StructuredCollision collision = collisions[collision_index]; - float dist = distance(collision.centre, worldPosition); + float dist = distance(collision.centre[eyeIndex], worldPosition); float power = smoothstep(collision.radius, 0.0, dist); - float3 direction = worldPosition - collision.centre; + float3 direction = worldPosition - collision.centre[eyeIndex]; direction.y = 0; // stops expanding/stretching direction = normalize(direction); float3 shift = power * direction; diff --git a/src/Feature.cpp b/src/Feature.cpp index 1f26d0bad..a4df33c28 100644 --- a/src/Feature.cpp +++ b/src/Feature.cpp @@ -82,6 +82,7 @@ const std::vector& Feature::GetFeatureList() static std::vector featuresVR = { GrassLighting::GetSingleton(), + GrassCollision::GetSingleton(), ExtendedMaterials::GetSingleton(), LightLimitFix::GetSingleton() }; diff --git a/src/Features/GrassCollision.cpp b/src/Features/GrassCollision.cpp index 1733e8a43..917ff29a2 100644 --- a/src/Features/GrassCollision.cpp +++ b/src/Features/GrassCollision.cpp @@ -126,15 +126,15 @@ void GrassCollision::UpdateCollisions() radius *= settings.RadiusMultiplier; CollisionSData data{}; RE::NiPoint3 eyePosition{}; - if (REL::Module::IsVR()) { - // find center of eye position - eyePosition = state->GetVRRuntimeData().posAdjust.getEye() + state->GetVRRuntimeData().posAdjust.getEye(1); - eyePosition /= 2; - } else - eyePosition = state->GetRuntimeData().posAdjust.getEye(); - data.centre.x = centerPos.x - eyePosition.x; - data.centre.y = centerPos.y - eyePosition.y; - data.centre.z = centerPos.z - eyePosition.z; + for (int eyeIndex = 0; eyeIndex < eyeCount; eyeIndex++) { + if (!REL::Module::IsVR()) { + eyePosition = state->GetRuntimeData().posAdjust.getEye(); + } else + eyePosition = state->GetVRRuntimeData().posAdjust.getEye(eyeIndex); + data.centre[eyeIndex].x = centerPos.x - eyePosition.x; + data.centre[eyeIndex].y = centerPos.y - eyePosition.y; + data.centre[eyeIndex].z = centerPos.z - eyePosition.z; + } data.radius = radius; currentCollisionCount++; collisionsData.push_back(data); @@ -200,15 +200,16 @@ void GrassCollision::ModifyGrass(const RE::BSShader*, const uint32_t) auto bound = shaderState.cachedPlayerBound; RE::NiPoint3 eyePosition{}; - if (REL::Module::IsVR()) { - // find center of eye position - eyePosition = state->GetVRRuntimeData().posAdjust.getEye() + state->GetVRRuntimeData().posAdjust.getEye(1); - eyePosition /= 2; - } else - eyePosition = state->GetRuntimeData().posAdjust.getEye(); - perFrameData.boundCentre.x = bound.center.x - eyePosition.x; - perFrameData.boundCentre.y = bound.center.y - eyePosition.y; - perFrameData.boundCentre.z = bound.center.z - eyePosition.z; + for (int eyeIndex = 0; eyeIndex < eyeCount; eyeIndex++) { + if (!REL::Module::IsVR()) { + eyePosition = state->GetRuntimeData().posAdjust.getEye(); + } else + eyePosition = state->GetVRRuntimeData().posAdjust.getEye(eyeIndex); + perFrameData.boundCentre[eyeIndex].x = bound.center.x - eyePosition.x; + perFrameData.boundCentre[eyeIndex].y = bound.center.y - eyePosition.y; + perFrameData.boundCentre[eyeIndex].z = bound.center.z - eyePosition.z; + perFrameData.boundCentre[eyeIndex].w = 0.0f; + } perFrameData.boundRadius = bound.radius * settings.RadiusMultiplier; perFrameData.Settings = settings; diff --git a/src/Features/GrassCollision.h b/src/Features/GrassCollision.h index 77b01c400..6b1a92d12 100644 --- a/src/Features/GrassCollision.h +++ b/src/Features/GrassCollision.h @@ -23,15 +23,14 @@ struct GrassCollision : Feature struct alignas(16) PerFrame { - DirectX::XMFLOAT3 boundCentre; + Vector4 boundCentre[2]; float boundRadius; Settings Settings; - float pad0; }; struct CollisionSData { - DirectX::XMFLOAT3 centre; + Vector3 centre[2]; float radius; }; @@ -41,6 +40,7 @@ struct GrassCollision : Feature bool updatePerFrame = false; ConstantBuffer* perFrame = nullptr; + int eyeCount = !REL::Module::IsVR() ? 1 : 2; virtual void SetupResources(); virtual void Reset(); From a50fe6cddb51affefaa5dc536adf7d1de78deaef Mon Sep 17 00:00:00 2001 From: alandtse Date: Mon, 28 Aug 2023 07:45:42 +0000 Subject: [PATCH 2/2] =?UTF-8?q?style:=20=F0=9F=8E=A8=20apply=20clang-forma?= =?UTF-8?q?t=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Features/GrassCollision.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Features/GrassCollision.cpp b/src/Features/GrassCollision.cpp index 917ff29a2..487a58e1e 100644 --- a/src/Features/GrassCollision.cpp +++ b/src/Features/GrassCollision.cpp @@ -202,7 +202,7 @@ void GrassCollision::ModifyGrass(const RE::BSShader*, const uint32_t) RE::NiPoint3 eyePosition{}; for (int eyeIndex = 0; eyeIndex < eyeCount; eyeIndex++) { if (!REL::Module::IsVR()) { - eyePosition = state->GetRuntimeData().posAdjust.getEye(); + eyePosition = state->GetRuntimeData().posAdjust.getEye(); } else eyePosition = state->GetVRRuntimeData().posAdjust.getEye(eyeIndex); perFrameData.boundCentre[eyeIndex].x = bound.center.x - eyePosition.x;