cntrl/src/render/models.cpp
dandovlatov a28dc30b41 FBC: NIGHT SHIFT - removed player stagger on enemy contact
Damage no longer changes the player's animation or applies the full-body shader recoil. Hits only tint the actor; the walk cycle and movement continue uninterrupted. Removed the unused flinch overlay and added a regression test for movement through enemy contact.
2026-11-24 22:05:04 +04:00

498 lines
22 KiB
C++

#include "render/models.hpp"
#include "render/profiler.hpp"
#include "raymath.h"
#include "rlgl.h"
#define DrawModelEx(...) do { PROF_DRAW_CALL(); DrawModelEx(__VA_ARGS__); } while(0)
#define DrawCube(...) do { PROF_DRAW_CALL(); DrawCube(__VA_ARGS__); } while(0)
#define DrawCylinder(...) do { PROF_DRAW_CALL(); DrawCylinder(__VA_ARGS__); } while(0)
#define DrawSphere(...) do { PROF_DRAW_CALL(); DrawSphere(__VA_ARGS__); } while(0)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
namespace {
std::string resolve(const char* rel) {
if (FileExists(rel)) return rel;
char path[512];
std::snprintf(path, sizeof(path), "%s../%s", GetApplicationDirectory(), rel);
return FileExists(path) ? path : std::string();
}
// hipY, shoulderY, legX, armX — rough rig pivots per actor (model-local metres).
struct Rig {
float hip_y = 0.0f;
float shoulder_y = 0.0f;
float leg_x = 0.0f;
float arm_x = 0.0f;
float scale = 0.0f;
float mode = 0.0f;
};
Rig rig_for(ActorModel id) {
switch (id) {
// The S17 agent is skinned in Blender; the old shader rig would double
// deform it, so its procedural mode is off.
case ActorModel::Guard: return {0.90f, 1.45f, 0.18f, 0.43f, 1.0f, 0.0f};
case ActorModel::HissStaff: return {0.60f, 1.40f, 0.20f, 0.38f, 0.85f, 1.0f};
case ActorModel::HissBrute: return {1.05f, 2.10f, 0.40f, 0.95f, 1.0f, 1.0f};
case ActorModel::HissGhost: return {0.60f, 1.35f, 0.22f, 0.36f, 0.9f, 1.0f};
case ActorModel::MoldSpore: return {0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 2.0f};
case ActorModel::Shard: return {0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f};
case ActorModel::RoundBasic:
case ActorModel::RoundTuned:
case ActorModel::RoundOvercharge:
return {0.0f, 0.0f, 0.0f, 0.0f, 0.7f, 3.0f};
default: return {};
}
}
float hiss_for(ActorModel id) {
switch (id) {
case ActorModel::Debris: return 0.5f;
case ActorModel::Hotline:
case ActorModel::TV:
case ActorModel::Pedestal:
case ActorModel::Switch: return 0.6f;
default: return 0.0f;
}
}
} // namespace
void ModelBank::init() {
const std::string vs = resolve("assets/shaders/actor.vs");
const std::string fs = resolve("assets/shaders/actor.fs");
if (!vs.empty() && !fs.empty()) {
shader_ = LoadShader(vs.c_str(), fs.c_str());
shader_ok_ = shader_.id != 0;
if (shader_ok_) {
loc_time_ = GetShaderLocation(shader_, "uTime");
loc_flash_ = GetShaderLocation(shader_, "uFlash");
loc_corruption_ = GetShaderLocation(shader_, "uCorruption");
loc_camera_ = GetShaderLocation(shader_, "uCameraPos");
loc_outline_ = GetShaderLocation(shader_, "uOutline");
loc_glow_ = GetShaderLocation(shader_, "uGlow");
loc_ramp_ = GetShaderLocation(shader_, "actorRamp");
loc_walk_ = GetShaderLocation(shader_, "uWalk");
loc_stagger_ = GetShaderLocation(shader_, "uStagger");
loc_phase_ = GetShaderLocation(shader_, "uPhase");
loc_rig_ = GetShaderLocation(shader_, "uRig");
loc_rig_scale_ = GetShaderLocation(shader_, "uRigScale");
loc_rig_mode_ = GetShaderLocation(shader_, "uRigMode");
loc_hiss_ = GetShaderLocation(shader_, "uHiss");
const std::string ramp = resolve("assets/textures/actor/actor_ramp.png");
if (!ramp.empty() && loc_ramp_ >= 0) {
ramp_ = LoadTexture(ramp.c_str());
SetTextureWrap(ramp_, TEXTURE_WRAP_CLAMP);
SetTextureFilter(ramp_, TEXTURE_FILTER_BILINEAR);
// raylib binds material maps to texture units on every model
// draw, but ad-hoc SetShaderValueTexture samplers are only
// bound for 2D batches. Ride the emissive map slot (unit 3).
const int unit = MATERIAL_MAP_EMISSION;
SetShaderValue(shader_, loc_ramp_, &unit, SHADER_UNIFORM_INT);
} else {
TraceLog(LOG_WARNING, "ACTOR: ramp LUT missing, using flat diffuse");
}
} else {
TraceLog(LOG_WARNING, "ACTOR: shader failed to compile, using flat shading");
}
} else {
TraceLog(LOG_WARNING, "ACTOR: shader files missing, using flat shading");
}
for (int i = 0; i < static_cast<int>(ActorModel::Count); ++i) {
const ActorModel id = static_cast<ActorModel>(i);
const std::string path = resolve(file_name(id));
if (path.empty()) {
TraceLog(LOG_WARNING, "ACTOR: %s missing, using primitive fallback", file_name(id));
continue;
}
Model model = LoadModel(path.c_str());
if (model.meshCount == 0) {
TraceLog(LOG_WARNING, "ACTOR: %s failed to load", path.c_str());
UnloadModel(model);
continue;
}
if (shader_ok_) {
for (int m = 0; m < model.materialCount; ++m) {
model.materials[m].shader = shader_;
}
}
entries_[i].model = model;
entries_[i].loaded = true;
if (id == ActorModel::Guard && model.skeleton.boneCount > 0) {
Entry& e = entries_[i];
e.animations = LoadModelAnimations(path.c_str(), &e.animation_count);
int valid = 0;
for (int a = 0; a < e.animation_count; ++a) {
if (IsModelAnimationValid(e.model, e.animations[a])) ++valid;
}
TraceLog(LOG_INFO,
"ACTOR: skinned agent — %d bones, %d animations (%d valid)",
model.skeleton.boneCount, e.animation_count, valid);
}
TraceLog(LOG_INFO, "ACTOR: %s (%d meshes, %d materials)", file_name(id),
model.meshCount, model.materialCount);
}
ready_ = true;
}
void ModelBank::shutdown() {
for (Entry& e : entries_) {
if (e.animations != nullptr && e.animation_count > 0)
UnloadModelAnimations(e.animations, e.animation_count);
e.animations = nullptr;
e.animation_count = 0;
e.current_animation = -1;
e.animation_clock = 0.0f;
e.blend_from = -1;
e.blend_from_clock = 0.0f;
e.blend_time = 0.0f;
e.blend_duration = 0.0f;
if (e.loaded) UnloadModel(e.model);
e.loaded = false;
}
if (ramp_.id != 0) UnloadTexture(ramp_);
ramp_.id = 0;
if (shader_ok_) UnloadShader(shader_);
shader_ok_ = false;
ready_ = false;
}
void ModelBank::begin_frame(float time, float corruption, Vector3 camera_pos) {
if (!shader_ok_) return;
if (ramp_.id != 0) {
rlActiveTextureSlot(MATERIAL_MAP_EMISSION);
rlEnableTexture(ramp_.id);
}
SetShaderValue(shader_, loc_time_, &time, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader_, loc_corruption_, &corruption, SHADER_UNIFORM_FLOAT);
if (loc_camera_ >= 0) {
const float cam[3] = {camera_pos.x, camera_pos.y, camera_pos.z};
SetShaderValue(shader_, loc_camera_, cam, SHADER_UNIFORM_VEC3);
}
}
void ModelBank::draw(ActorModel id, Vector3 pos, float yaw, float scale,
Color tint, float flash, bool outline, float walk,
float stagger, float phase, float hiss, Vector3 stretch) {
const Vector3 draw_scale = stretch.x > 0.0f
? stretch : Vector3{scale, scale, scale};
const float yaw_degrees = yaw * RAD2DEG;
const int index = static_cast<int>(id);
if (index < 0 || index >= static_cast<int>(ActorModel::Count)) return;
if (!entries_[index].loaded) {
draw_fallback(id, pos, yaw, scale, tint);
return;
}
if (shader_ok_) {
const Rig rig = rig_for(id);
const float r[4] = {rig.hip_y, rig.shoulder_y, rig.leg_x, rig.arm_x};
if (loc_rig_ >= 0) SetShaderValue(shader_, loc_rig_, r, SHADER_UNIFORM_VEC4);
if (loc_rig_scale_ >= 0)
SetShaderValue(shader_, loc_rig_scale_, &rig.scale, SHADER_UNIFORM_FLOAT);
if (loc_rig_mode_ >= 0)
SetShaderValue(shader_, loc_rig_mode_, &rig.mode, SHADER_UNIFORM_FLOAT);
if (loc_walk_ >= 0) SetShaderValue(shader_, loc_walk_, &walk, SHADER_UNIFORM_FLOAT);
if (loc_stagger_ >= 0)
SetShaderValue(shader_, loc_stagger_, &stagger, SHADER_UNIFORM_FLOAT);
if (loc_phase_ >= 0)
SetShaderValue(shader_, loc_phase_, &phase, SHADER_UNIFORM_FLOAT);
const bool resonant_round = id >= ActorModel::RoundBasic
&& id <= ActorModel::RoundOvercharge;
const float glow = id == ActorModel::Shard ? 0.9f
: (resonant_round ? 1.15f : 0.0f);
if (loc_glow_ >= 0) SetShaderValue(shader_, loc_glow_, &glow, SHADER_UNIFORM_FLOAT);
const float model_hiss = hiss_for(id);
const float hiss_value = hiss > model_hiss ? hiss : model_hiss;
if (loc_hiss_ >= 0)
SetShaderValue(shader_, loc_hiss_, &hiss_value, SHADER_UNIFORM_FLOAT);
}
// Ink silhouette: expanded back faces first, then the lit model over them.
// One extra draw call only for the guard / future bosses — never the horde.
if (outline && shader_ok_ && loc_outline_ >= 0) {
const float on = 1.0f;
const float off = 0.0f;
SetShaderValue(shader_, loc_outline_, &on, SHADER_UNIFORM_FLOAT);
rlSetCullFace(RL_CULL_FACE_FRONT);
DrawModelEx(entries_[index].model, pos, Vector3{0.0f, 1.0f, 0.0f}, yaw_degrees,
draw_scale, tint);
rlSetCullFace(RL_CULL_FACE_BACK);
SetShaderValue(shader_, loc_outline_, &off, SHADER_UNIFORM_FLOAT);
}
if (shader_ok_ && loc_flash_ >= 0) {
const float f = flash > 0.0f ? 1.0f : 0.0f;
SetShaderValue(shader_, loc_flash_, &f, SHADER_UNIFORM_FLOAT);
}
DrawModelEx(entries_[index].model, pos, Vector3{0.0f, 1.0f, 0.0f}, yaw_degrees,
draw_scale, tint);
}
void ModelBank::update_animation(ActorModel id, const char* name, float dt,
float blend_seconds) {
const int index = static_cast<int>(id);
if (index < 0 || index >= static_cast<int>(ActorModel::Count)) return;
Entry& e = entries_[index];
if (!e.loaded || e.animations == nullptr || e.animation_count <= 0) return;
int found = -1;
for (int a = 0; a < e.animation_count; ++a) {
if (std::strcmp(e.animations[a].name, name) == 0) { found = a; break; }
}
if (found < 0) return;
if (found != e.current_animation) {
if (e.blend_duration > 0.0f) {
// Retargeted mid-fade: carry the dominant pose forward instead of
// snapping to frame zero of the new clip.
if (e.blend_time / e.blend_duration >= 0.5f) {
e.blend_from = e.current_animation;
e.blend_from_clock = e.animation_clock;
}
} else {
e.blend_from = e.current_animation;
e.blend_from_clock = e.animation_clock;
}
e.current_animation = found;
e.animation_clock = 0.0f;
e.blend_time = 0.0f;
e.blend_duration = (e.blend_from >= 0 && blend_seconds > 0.0f)
? blend_seconds : 0.0f;
}
e.animation_clock += dt;
const ModelAnimation& to = e.animations[found];
const float to_frame = std::fmod(e.animation_clock * 60.0f,
static_cast<float>(to.keyframeCount));
if (e.blend_duration <= 0.0f) {
UpdateModelAnimation(e.model, to, to_frame);
return;
}
e.blend_time += dt;
const float w = std::min(1.0f, e.blend_time / e.blend_duration);
const float eased = w * w * (3.0f - 2.0f * w);
e.blend_from_clock += dt;
const ModelAnimation& from = e.animations[e.blend_from];
const float from_frame = std::fmod(e.blend_from_clock * 60.0f,
static_cast<float>(from.keyframeCount));
UpdateModelAnimationEx(e.model, from, from_frame, to, to_frame, eased);
if (w >= 1.0f) {
e.blend_from = -1;
e.blend_duration = 0.0f;
}
}
float ModelBank::height(ActorModel id) {
switch (id) {
case ActorModel::Guard: return 1.9f;
case ActorModel::HissStaff: return 1.9f;
case ActorModel::MoldSpore: return 1.3f;
case ActorModel::HissBrute: return 2.9f;
case ActorModel::HissGhost: return 1.9f;
case ActorModel::Shard: return 0.55f;
case ActorModel::Hotline: return 1.3f;
case ActorModel::TV: return 2.3f;
case ActorModel::Pedestal: return 1.5f;
case ActorModel::Duck: return 1.5f;
case ActorModel::Cabinet: return 1.8f;
case ActorModel::Crate: return 0.9f;
case ActorModel::Debris: return 1.0f;
case ActorModel::Switch: return 0.6f;
case ActorModel::OfficeDesk: return 0.8f;
case ActorModel::MeetingTable: return 0.85f;
case ActorModel::OfficeChair: return 1.1f;
case ActorModel::Computer: return 0.7f;
case ActorModel::Plant: return 1.5f;
case ActorModel::Sofa: return 1.1f;
case ActorModel::FloorLamp: return 1.7f;
case ActorModel::Partition: return 1.6f;
case ActorModel::GarbageCan: return 0.72f;
case ActorModel::DocumentStack: return 0.06f;
case ActorModel::LooseSheet: return 0.02f;
case ActorModel::ArchiveShelf: return 2.8f;
case ActorModel::ThermoFrame: return 4.6f;
case ActorModel::ThermoPanel: return 4.0f;
case ActorModel::RoundBasic: return 0.4f;
case ActorModel::RoundTuned: return 0.5f;
case ActorModel::RoundOvercharge: return 0.6f;
case ActorModel::MuzzleFlash: return 0.6f;
default: return 1.0f;
}
}
const char* ModelBank::file_name(ActorModel id) {
switch (id) {
case ActorModel::Guard: return "assets/models/agent.glb";
case ActorModel::HissStaff: return "assets/models/hiss_staff.glb";
case ActorModel::MoldSpore: return "assets/models/mold_spore.glb";
case ActorModel::HissBrute: return "assets/models/hiss_brute.glb";
case ActorModel::HissGhost: return "assets/models/hiss_ghost.glb";
case ActorModel::Shard: return "assets/models/shard.glb";
case ActorModel::Hotline: return "assets/models/props/hotline.glb";
case ActorModel::TV: return "assets/models/props/tv.glb";
case ActorModel::Pedestal: return "assets/models/props/pedestal.glb";
case ActorModel::Duck: return "assets/models/props/duck.glb";
case ActorModel::Cabinet: return "assets/models/props/cabinet.glb";
case ActorModel::Crate: return "assets/models/props/crate.glb";
case ActorModel::Debris: return "assets/models/props/debris.glb";
case ActorModel::Switch: return "assets/models/props/switch.glb";
case ActorModel::OfficeDesk: return "assets/models/props/desk.glb";
case ActorModel::MeetingTable: return "assets/models/props/meeting_table.glb";
case ActorModel::OfficeChair: return "assets/models/props/chair.glb";
case ActorModel::Computer: return "assets/models/props/computer.glb";
case ActorModel::Plant: return "assets/models/props/plant.glb";
case ActorModel::Sofa: return "assets/models/props/sofa.glb";
case ActorModel::FloorLamp: return "assets/models/props/lamp.glb";
case ActorModel::Partition: return "assets/models/props/partition.glb";
case ActorModel::GarbageCan: return "assets/models/props/garbage_can.glb";
case ActorModel::DocumentStack: return "assets/models/props/document_stack.glb";
case ActorModel::LooseSheet: return "assets/models/props/loose_sheet.glb";
case ActorModel::ArchiveShelf: return "assets/models/props/archive_shelf.glb";
case ActorModel::ThermoFrame: return "assets/models/props/thermo_frame.glb";
case ActorModel::ThermoPanel: return "assets/models/props/thermo_panel.glb";
case ActorModel::RoundBasic: return "assets/models/projectile_1.glb";
case ActorModel::RoundTuned: return "assets/models/projectile_2.glb";
case ActorModel::RoundOvercharge: return "assets/models/projectile_3.glb";
case ActorModel::MuzzleFlash: return "assets/models/muzzle_flash.glb";
default: return "";
}
}
bool ModelBank::actor_from_name(const std::string& name, ActorModel& id) {
static const char* const kNames[] = {
"Guard", "HissStaff", "MoldSpore", "HissBrute", "HissGhost", "Shard",
"Hotline", "TV", "Pedestal", "Duck", "Cabinet", "Crate", "Debris", "Switch",
"OfficeDesk", "MeetingTable", "OfficeChair", "Computer", "Plant", "Sofa",
"FloorLamp", "Partition", "GarbageCan", "DocumentStack", "LooseSheet",
"ArchiveShelf", "ThermoFrame", "ThermoPanel",
"RoundBasic", "RoundTuned", "RoundOvercharge", "MuzzleFlash",
};
static_assert(sizeof(kNames) / sizeof(kNames[0])
== static_cast<size_t>(ActorModel::Count));
for (size_t i = 0; i < sizeof(kNames) / sizeof(kNames[0]); ++i) {
if (name == kNames[i]) {
id = static_cast<ActorModel>(i);
return true;
}
}
return false;
}
void ModelBank::draw_fallback(ActorModel id, Vector3 pos, float yaw,
float scale, Color tint) {
rlPushMatrix();
rlTranslatef(pos.x, pos.y, pos.z);
rlRotatef(yaw * RAD2DEG, 0.0f, 1.0f, 0.0f);
switch (id) {
case ActorModel::Guard:
DrawCylinder(Vector3{0.0f, 0.0f, 0.0f}, 0.38f * scale, 0.3f * scale,
1.5f * scale, 7, tint);
DrawCube(Vector3{0.0f, 1.7f * scale, 0.0f},
0.4f * scale, 0.35f * scale, 0.35f * scale, tint);
break;
case ActorModel::HissStaff:
DrawCylinder(Vector3{0.0f, 0.0f, 0.0f}, 0.15f * scale, 0.42f * scale,
1.5f * scale, 7, tint);
break;
case ActorModel::MoldSpore:
DrawSphere(Vector3{0.0f, 0.0f, 0.0f}, 0.55f * scale, tint);
break;
case ActorModel::HissBrute:
DrawCube(Vector3{0.0f, 1.2f * scale, 0.0f},
1.5f * scale, 2.4f * scale, 1.0f * scale, tint);
break;
case ActorModel::Shard:
DrawSphere(Vector3{0.0f, 0.0f, 0.0f}, 0.3f * scale, tint);
break;
case ActorModel::Switch: {
// Small wall plate at the lightswitch, near the dead beam strip (§2.3).
DrawCube(Vector3{0.0f, 0.45f, 0.0f}, 0.12f * scale,
0.6f * scale, 0.4f * scale, tint);
break;
}
case ActorModel::OfficeDesk:
DrawCube({0.0f, 0.72f * scale, 0.0f}, 2.2f * scale,
0.06f * scale, 1.0f * scale, tint);
break;
case ActorModel::MeetingTable:
DrawCube({0.0f, 0.77f * scale, 0.0f}, 4.9f * scale,
0.07f * scale, 1.8f * scale, tint);
break;
case ActorModel::OfficeChair:
DrawCube({0.0f, 0.50f * scale, 0.0f}, 0.6f * scale,
0.08f * scale, 0.6f * scale, tint);
DrawCube({0.0f, 0.85f * scale, 0.25f * scale}, 0.5f * scale,
0.6f * scale, 0.08f * scale, tint);
break;
case ActorModel::Computer:
DrawCube({0.0f, 0.44f * scale, 0.0f}, 0.68f * scale,
0.42f * scale, 0.05f * scale, tint);
break;
case ActorModel::Plant:
DrawCylinder({0.0f, 0.25f * scale, 0.0f}, 0.25f * scale,
0.29f * scale, 0.5f * scale, 10, tint);
break;
case ActorModel::Sofa:
DrawCube({0.0f, 0.45f * scale, 0.0f}, 2.4f * scale,
0.8f * scale, 0.9f * scale, tint);
break;
case ActorModel::FloorLamp:
DrawCylinder({0.0f, 0.85f * scale, 0.0f}, 0.28f * scale,
0.1f * scale, 1.7f * scale, 10, tint);
break;
case ActorModel::Partition:
DrawCube({0.0f, 0.85f * scale, 0.0f}, 3.0f * scale,
1.4f * scale, 0.08f * scale, tint);
break;
case ActorModel::GarbageCan:
DrawCylinder({0.0f, 0.36f * scale, 0.0f}, 0.22f * scale,
0.16f * scale, 0.72f * scale, 10, tint);
break;
case ActorModel::DocumentStack:
DrawCube({0.0f, 0.03f * scale, 0.0f}, 0.42f * scale,
0.06f * scale, 0.30f * scale, tint);
break;
case ActorModel::LooseSheet:
DrawCube({0.0f, 0.01f * scale, 0.0f}, 0.40f * scale,
0.012f * scale, 0.30f * scale, tint);
break;
case ActorModel::RoundBasic:
case ActorModel::RoundTuned:
case ActorModel::RoundOvercharge:
DrawCylinder({0.0f, 0.0f, -0.3f * scale}, 0.08f * scale,
0.16f * scale, 0.6f * scale, 6, tint);
break;
case ActorModel::MuzzleFlash:
DrawSphere({0.0f, 0.0f, 0.0f}, 0.22f * scale, tint);
break;
case ActorModel::ArchiveShelf:
DrawCube({0.0f, 1.4f * scale, 0.0f}, 14.1f * scale,
2.8f * scale, 0.92f * scale, tint);
break;
case ActorModel::ThermoFrame:
for (const float sx : {-3.85f, 3.85f})
DrawCube({sx * scale, 2.3f * scale, 0.0f}, 0.3f * scale,
4.6f * scale, 1.2f * scale, tint);
DrawCube({0.0f, 4.3f * scale, 0.0f}, 8.0f * scale,
0.55f * scale, 1.2f * scale, tint);
break;
case ActorModel::ThermoPanel:
DrawCube({0.0f, 2.0f * scale, 0.0f}, 3.7f * scale,
4.0f * scale, 0.34f * scale, tint);
break;
default:
break;
}
rlPopMatrix();
}