Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Grass Collision to VR #56

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

cbuffer GrassCollisionPerFrame : register(b5)
{
float3 boundCentre;
float4 boundCentre[2];
float boundRadius;
bool EnableGrassCollision;
float RadiusMultiplier;
Expand All @@ -10,7 +10,7 @@ cbuffer GrassCollisionPerFrame : register(b5)

struct StructuredCollision
{
float3 centre;
float3 centre[2];
float radius;
};

Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const std::vector<Feature*>& Feature::GetFeatureList()

static std::vector<Feature*> featuresVR = {
GrassLighting::GetSingleton(),
GrassCollision::GetSingleton(),
ExtendedMaterials::GetSingleton(),
LightLimitFix::GetSingleton()
};
Expand Down
37 changes: 19 additions & 18 deletions src/Features/GrassCollision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Features/GrassCollision.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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();
Expand Down