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 NPC grass collision #79

Merged
merged 6 commits into from
Sep 10, 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,2 +1,2 @@
[Info]
Version = 1-1-0
Version = 1-2-0
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cbuffer GrassCollisionPerFrame : register(b5)
bool EnableGrassCollision;
float RadiusMultiplier;
float DisplacementMultiplier;
float maxDistance;
}

struct StructuredCollision
Expand All @@ -21,10 +22,10 @@ float3 GetDisplacedPosition(float3 position, float alpha, uint eyeIndex = 0)
float3 worldPosition = mul(World[eyeIndex], float4(position, 1)).xyz;
float3 displacement = 0;

// Player bound culling
// Player bound culling and distance from player culling
{
float dist = distance(boundCentre[eyeIndex].xyz, worldPosition);
if (dist > boundRadius) {
if ((dist > boundRadius) && (dist > maxDistance)) {
return 0;
}
}
Expand Down
114 changes: 84 additions & 30 deletions src/Features/GrassCollision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ void GrassCollision::DrawSettings()
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::Text("Distance from collision centres to apply collision");
ImGui::Text("Distance from collision centres to apply collision.");
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}

ImGui::SliderFloat("Max Distance from Player", &settings.maxDistance, 0.0f, 1500.0f);
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::Text("Distance from player to apply collision (NPCs). 0 to disable NPC collisions.");
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
Expand All @@ -45,6 +54,23 @@ void GrassCollision::DrawSettings()
ImGui::EndTooltip();
}

if (ImGui::SliderInt("Calculation Frame Interval", (int*)&settings.frameInterval, 0, 30)) {
if (settings.frameInterval) // increment so mod math works (e.g., skip 1 frame means frame % 2).
settings.frameInterval++;
}
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::Text("How many frames to skip before calculating positions again. 0 means calculate every frame (most smooth/costly).");
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}

ImGui::TreePop();
}
if (ImGui::TreeNodeEx("Statistics", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::Text(std::format("Active/Total Actors : {}/{}", activeActorCount, totalActorCount).c_str());
ImGui::Text(std::format("Total Collisions : {}", currentCollisionCount).c_str());
ImGui::TreePop();
}
}
Expand Down Expand Up @@ -131,46 +157,74 @@ void GrassCollision::UpdateCollisions()
{
auto state = RE::BSGraphics::RendererShadowState::GetSingleton();

std::uint32_t currentCollisionCount = 0;

std::vector<CollisionSData> collisionsData{};

if (auto player = RE::PlayerCharacter::GetSingleton()) {
if (auto root = player->Get3D(false)) {
auto position = player->GetPosition();
RE::BSVisit::TraverseScenegraphCollision(root, [&](RE::bhkNiCollisionObject* a_object) -> RE::BSVisit::BSVisitControl {
RE::NiPoint3 centerPos;
float radius;
if (GetShapeBound(a_object, centerPos, radius)) {
radius *= settings.RadiusMultiplier;
CollisionSData data{};
RE::NiPoint3 eyePosition{};
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;
auto frameCount = RE::BSGraphics::State::GetSingleton()->uiFrameCount;

if (settings.frameInterval == 0 || frameCount % settings.frameInterval == 0) { // only calculate actor positions on some frames
currentCollisionCount = 0;
totalActorCount = 0;
activeActorCount = 0;
actorList.clear();
collisionsData.clear();
// actor query code from po3 under MIT
// https://github.com/powerof3/PapyrusExtenderSSE/blob/7a73b47bc87331bec4e16f5f42f2dbc98b66c3a7/include/Papyrus/Functions/Faction.h#L24C7-L46
if (const auto processLists = RE::ProcessLists::GetSingleton(); processLists && settings.maxDistance > 0.0f) {
std::vector<RE::BSTArray<RE::ActorHandle>*> actors;
actors.push_back(&processLists->highActorHandles); // high actors are in combat or doing something interesting
for (auto array : actors) {
for (auto& actorHandle : *array) {
auto actorPtr = actorHandle.get();
if (actorPtr && actorPtr.get() && actorPtr.get()->Is3DLoaded()) {
actorList.push_back(actorPtr.get());
totalActorCount++;
}
data.radius = radius;
currentCollisionCount++;
collisionsData.push_back(data);
}
return RE::BSVisit::BSVisitControl::kContinue;
});
}
}
}

RE::NiPoint3 playerPosition;
if (auto player = RE::PlayerCharacter::GetSingleton()) {
actorList.push_back(player);
playerPosition = player->GetPosition();
}

for (const auto actor : actorList) {
if (auto root = actor->Get3D(false)) {
if (playerPosition.GetDistance(actor->GetPosition()) > settings.maxDistance) { // npc too far so skip
continue;
}
activeActorCount++;
RE::BSVisit::TraverseScenegraphCollision(root, [&](RE::bhkNiCollisionObject* a_object) -> RE::BSVisit::BSVisitControl {
RE::NiPoint3 centerPos;
float radius;
if (GetShapeBound(a_object, centerPos, radius)) {
radius *= settings.RadiusMultiplier;
CollisionSData data{};
RE::NiPoint3 eyePosition{};
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);
}
return RE::BSVisit::BSVisitControl::kContinue;
});
}
}
}
if (!currentCollisionCount) {
CollisionSData data{};
ZeroMemory(&data, sizeof(data));
collisionsData.push_back(data);
currentCollisionCount = 1;
}

static std::uint32_t colllisionCount = 0;
bool collisionCountChanged = currentCollisionCount != colllisionCount;

if (!collisions || collisionCountChanged) {
Expand Down
9 changes: 9 additions & 0 deletions src/Features/GrassCollision.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ struct GrassCollision : Feature
std::uint32_t EnableGrassCollision = 1;
float RadiusMultiplier = 2;
float DisplacementMultiplier = 16;
float maxDistance = 1000.0;
std::uint32_t frameInterval = 0;
};

struct alignas(16) PerFrame
{
Vector4 boundCentre[2];
float boundRadius;
Settings Settings;
float pad01[2];
};

struct CollisionSData
Expand All @@ -35,6 +38,12 @@ struct GrassCollision : Feature
};

std::unique_ptr<Buffer> collisions = nullptr;
std::uint32_t totalActorCount = 0;
std::uint32_t activeActorCount = 0;
std::uint32_t currentCollisionCount = 0;
std::vector<RE::Actor*> actorList{};
std::vector<CollisionSData> collisionsData{};
std::uint32_t colllisionCount = 0;

Settings settings;

Expand Down