KRCollider is now using KRMeshBinding

This commit is contained in:
2025-11-11 23:20:00 -08:00
parent 82019987e6
commit 51b8bcbc78
4 changed files with 25 additions and 29 deletions

View File

@@ -48,7 +48,6 @@ void KRCollider::InitNodeInfo(KrNodeInfo* nodeInfo)
KRCollider::KRCollider(KRScene& scene, std::string name)
: KRNode(scene, name)
, m_model(nullptr)
, m_layer_mask(0xffff)
, m_audio_occlusion(1.0f)
{
@@ -57,11 +56,10 @@ KRCollider::KRCollider(KRScene& scene, std::string name)
KRCollider::KRCollider(KRScene& scene, std::string collider_name, std::string model_name, unsigned int layer_mask, float audio_occlusion)
: KRNode(scene, collider_name)
, m_layer_mask(layer_mask)
, m_audio_occlusion(audio_occlusion)
{
m_model_name = model_name;
m_layer_mask = layer_mask;
m_audio_occlusion = audio_occlusion;
m_model = nullptr;
m_model.setName(model_name);
}
KRCollider::~KRCollider()
@@ -77,7 +75,7 @@ std::string KRCollider::getElementName()
tinyxml2::XMLElement* KRCollider::saveXML(tinyxml2::XMLNode* parent)
{
tinyxml2::XMLElement* e = KRNode::saveXML(parent);
e->SetAttribute("mesh", m_model_name.c_str());
e->SetAttribute("mesh", m_model.getName().c_str());
e->SetAttribute("layer_mask", m_layer_mask);
e->SetAttribute("audio_occlusion", m_audio_occlusion);
return e;
@@ -86,8 +84,7 @@ tinyxml2::XMLElement* KRCollider::saveXML(tinyxml2::XMLNode* parent)
void KRCollider::loadXML(tinyxml2::XMLElement* e)
{
KRNode::loadXML(e);
m_model_name = e->Attribute("mesh");
m_model.setName(e->Attribute("mesh"));
m_layer_mask = 65535;
if (e->QueryUnsignedAttribute("layer_mask", &m_layer_mask) != tinyxml2::XML_SUCCESS) {
@@ -102,19 +99,18 @@ void KRCollider::loadXML(tinyxml2::XMLElement* e)
void KRCollider::loadModel()
{
if (m_model == nullptr) {
m_model = m_pContext->getMeshManager()->getMesh(m_model_name.c_str());
if (m_model) {
getScene().notify_sceneGraphModify(this);
}
KRMesh* prevModel = m_model.get();
m_model.load(&getContext());
if (m_model.get() != prevModel) {
getScene().notify_sceneGraphModify(this);
}
}
AABB KRCollider::getBounds()
{
loadModel();
if (m_model) {
return AABB::Create(m_model->getMinPoint(), m_model->getMaxPoint(), getModelMatrix());
if (m_model.isLoaded()) {
return AABB::Create(m_model.get()->getMinPoint(), m_model.get()->getMaxPoint(), getModelMatrix());
} else {
return AABB::Infinite();
}
@@ -124,7 +120,7 @@ bool KRCollider::lineCast(const Vector3& v0, const Vector3& v1, HitInfo& hitinfo
{
if (layer_mask & m_layer_mask) { // Only test if layer masks have a common bit set
loadModel();
if (m_model) {
if (m_model.isLoaded()) {
if (getBounds().intersectsLine(v0, v1)) {
Vector3 v0_model_space = Matrix4::Dot(getInverseModelMatrix(), v0);
Vector3 v1_model_space = Matrix4::Dot(getInverseModelMatrix(), v1);
@@ -134,7 +130,7 @@ bool KRCollider::lineCast(const Vector3& v0, const Vector3& v1, HitInfo& hitinfo
hitinfo_model_space = HitInfo(hit_position_model_space, Matrix4::DotNoTranslate(getInverseModelMatrix(), hitinfo.getNormal()), (hit_position_model_space - v0_model_space).magnitude(), hitinfo.getNode());
}
if (m_model->lineCast(v0_model_space, v1_model_space, hitinfo_model_space)) {
if (m_model.get()->lineCast(v0_model_space, v1_model_space, hitinfo_model_space)) {
Vector3 hit_position_world_space = Matrix4::Dot(getModelMatrix(), hitinfo_model_space.getPosition());
hitinfo = HitInfo(hit_position_world_space, Vector3::Normalize(Matrix4::DotNoTranslate(getModelMatrix(), hitinfo_model_space.getNormal())), (hit_position_world_space - v0).magnitude(), this);
return true;
@@ -149,7 +145,7 @@ bool KRCollider::rayCast(const Vector3& v0, const Vector3& dir, HitInfo& hitinfo
{
if (layer_mask & m_layer_mask) { // Only test if layer masks have a common bit set
loadModel();
if (m_model) {
if (m_model.isLoaded()) {
if (getBounds().intersectsRay(v0, dir)) {
Vector3 v0_model_space = Matrix4::Dot(getInverseModelMatrix(), v0);
Vector3 dir_model_space = Vector3::Normalize(Matrix4::DotNoTranslate(getInverseModelMatrix(), dir));
@@ -159,7 +155,7 @@ bool KRCollider::rayCast(const Vector3& v0, const Vector3& dir, HitInfo& hitinfo
hitinfo_model_space = HitInfo(hit_position_model_space, Vector3::Normalize(Matrix4::DotNoTranslate(getInverseModelMatrix(), hitinfo.getNormal())), (hit_position_model_space - v0_model_space).magnitude(), hitinfo.getNode());
}
if (m_model->rayCast(v0_model_space, dir_model_space, hitinfo_model_space)) {
if (m_model.get()->rayCast(v0_model_space, dir_model_space, hitinfo_model_space)) {
Vector3 hit_position_world_space = Matrix4::Dot(getModelMatrix(), hitinfo_model_space.getPosition());
hitinfo = HitInfo(hit_position_world_space, Vector3::Normalize(Matrix4::DotNoTranslate(getModelMatrix(), hitinfo_model_space.getNormal())), (hit_position_world_space - v0).magnitude(), this);
return true;
@@ -174,14 +170,14 @@ bool KRCollider::sphereCast(const Vector3& v0, const Vector3& v1, float radius,
{
if (layer_mask & m_layer_mask) { // Only test if layer masks have a common bit set
loadModel();
if (m_model) {
if (m_model.isLoaded()) {
AABB sphereCastBounds = AABB::Create( // TODO - Need to cache this; perhaps encasulate within a "spherecast" class to be passed through these functions
Vector3::Create(KRMIN(v0.x, v1.x) - radius, KRMIN(v0.y, v1.y) - radius, KRMIN(v0.z, v1.z) - radius),
Vector3::Create(KRMAX(v0.x, v1.x) + radius, KRMAX(v0.y, v1.y) + radius, KRMAX(v0.z, v1.z) + radius)
);
if (getBounds().intersects(sphereCastBounds)) {
if (m_model->sphereCast(getModelMatrix(), v0, v1, radius, hitinfo)) {
if (m_model.get()->sphereCast(getModelMatrix(), v0, v1, radius, hitinfo)) {
hitinfo = HitInfo(hitinfo.getPosition(), hitinfo.getNormal(), hitinfo.getDistance(), this);
return true;
}
@@ -220,7 +216,7 @@ void KRCollider::render(RenderInfo& ri)
if (ri.renderPass->getType() == RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT && ri.camera->settings.debug_display == KRRenderSettings::KRENGINE_DEBUG_DISPLAY_COLLIDERS) {
loadModel();
if (m_model) {
if (m_model.isLoaded()) {
GL_PUSH_GROUP_MARKER("Debug Overlays");
@@ -233,14 +229,14 @@ void KRCollider::render(RenderInfo& ri)
info.spot_lights = &ri.spot_lights;
info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditive;
info.modelFormat = m_model->getModelFormat();
info.vertexAttributes = m_model->getVertexAttributes();
info.modelFormat = m_model.get()->getModelFormat();
info.vertexAttributes = m_model.get()->getVertexAttributes();
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
pShader->bind(ri, getModelMatrix());
m_model->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "visualize_overlay", 1.0f);
m_model.get()->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "visualize_overlay", 1.0f);
GL_POP_GROUP_MARKER;
}

View File

@@ -41,6 +41,7 @@
#include "KRModel.h"
#include "KRCamera.h"
#include "resources/mesh/KRMeshManager.h"
#include "resources/mesh/KRMeshBinding.h"
#include "KRNode.h"
#include "KRContext.h"
#include "resources/mesh/KRMesh.h"
@@ -73,8 +74,7 @@ public:
void render(RenderInfo& ri) override;
private:
KRMesh* m_model;
std::string m_model_name;
KRMeshBinding m_model;
unsigned int m_layer_mask;
float m_audio_occlusion;

View File

@@ -33,7 +33,7 @@
#include "KRResourceBinding.h"
#include "KRContext.h"
KRResourceBinding::KRResourceBinding(std::string& name)
KRResourceBinding::KRResourceBinding(const std::string& name)
: m_name(name)
, m_resource(nullptr)
{

View File

@@ -40,7 +40,7 @@ class KRContext;
class KRResourceBinding
{
public:
KRResourceBinding(std::string& name);
KRResourceBinding(const std::string& name);
KRResourceBinding();
~KRResourceBinding();