2019-01-11 00:48:33 -08:00
|
|
|
//
|
|
|
|
|
// KRReverbZone.cpp
|
2021-08-16 16:35:36 -07:00
|
|
|
// Kraken Engine
|
2019-01-11 00:48:33 -08:00
|
|
|
//
|
2026-05-17 12:19:35 -07:00
|
|
|
// Copyright 2026 Kearwood Gilbert. All rights reserved.
|
2021-08-16 16:35:36 -07:00
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
|
//
|
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other materials
|
|
|
|
|
// provided with the distribution.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
|
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
|
|
|
|
|
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
|
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
|
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
|
|
|
|
// The views and conclusions contained in the software and documentation are those of the
|
|
|
|
|
// authors and should not be interpreted as representing official policies, either expressed
|
|
|
|
|
// or implied, of Kearwood Gilbert.
|
2019-01-11 00:48:33 -08:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "KRReverbZone.h"
|
|
|
|
|
#include "KRContext.h"
|
2024-01-21 18:34:36 -08:00
|
|
|
#include "KRRenderPass.h"
|
2019-01-11 00:48:33 -08:00
|
|
|
|
2023-08-05 21:14:53 -07:00
|
|
|
using namespace hydra;
|
|
|
|
|
|
2020-06-21 22:42:10 -07:00
|
|
|
/* static */
|
|
|
|
|
void KRReverbZone::InitNodeInfo(KrNodeInfo* nodeInfo)
|
|
|
|
|
{
|
|
|
|
|
KRNode::InitNodeInfo(nodeInfo);
|
2025-11-23 16:07:31 -08:00
|
|
|
nodeInfo->reverb_zone.gain = decltype(m_reverb_gain)::defaultVal;
|
|
|
|
|
nodeInfo->reverb_zone.gradient = decltype(m_gradient_distance)::defaultVal;
|
2020-06-21 22:42:10 -07:00
|
|
|
nodeInfo->reverb_zone.sample = -1;
|
2025-11-23 16:07:31 -08:00
|
|
|
nodeInfo->reverb_zone.pZoneName = decltype(m_zone)::defaultVal;
|
2020-06-21 22:42:10 -07:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
KRReverbZone::KRReverbZone(KRScene& scene, std::string name) : KRNode(scene, name)
|
2019-01-11 00:48:33 -08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KRReverbZone::~KRReverbZone()
|
2022-08-08 01:07:26 -07:00
|
|
|
{}
|
2019-01-11 00:48:33 -08:00
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
std::string KRReverbZone::getElementName()
|
|
|
|
|
{
|
|
|
|
|
return "reverb_zone";
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
tinyxml2::XMLElement* KRReverbZone::saveXML(tinyxml2::XMLNode* parent)
|
2019-01-11 00:48:33 -08:00
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
tinyxml2::XMLElement* e = KRNode::saveXML(parent);
|
2025-11-23 16:07:31 -08:00
|
|
|
m_zone.save(e);
|
|
|
|
|
m_reverb.save(e);
|
|
|
|
|
m_reverb_gain.save(e);
|
|
|
|
|
m_gradient_distance.save(e);
|
2022-08-08 01:07:26 -07:00
|
|
|
return e;
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
void KRReverbZone::loadXML(tinyxml2::XMLElement* e)
|
2019-01-11 00:48:33 -08:00
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
KRNode::loadXML(e);
|
|
|
|
|
|
2025-11-23 16:07:31 -08:00
|
|
|
m_zone.load(e);
|
|
|
|
|
m_gradient_distance.load(e);
|
|
|
|
|
m_reverb.load(e);
|
|
|
|
|
m_reverb_gain.load(e);
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
2025-11-23 16:07:31 -08:00
|
|
|
KRAudioSample* KRReverbZone::getReverb()
|
2019-01-11 00:48:33 -08:00
|
|
|
{
|
2025-11-23 16:07:31 -08:00
|
|
|
m_reverb.val.bind(&getContext());
|
|
|
|
|
return m_reverb.val.get();
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
void KRReverbZone::setReverb(const std::string& reverb)
|
2019-01-11 00:48:33 -08:00
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
m_reverb = reverb;
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float KRReverbZone::getReverbGain()
|
|
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
return m_reverb_gain;
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KRReverbZone::setReverbGain(float reverb_gain)
|
|
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
m_reverb_gain = reverb_gain;
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string KRReverbZone::getZone()
|
|
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
return m_zone;
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
void KRReverbZone::setZone(const std::string& zone)
|
2019-01-11 00:48:33 -08:00
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
m_zone = zone;
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-04-06 01:00:13 -07:00
|
|
|
void KRReverbZone::render(RenderInfo& ri)
|
2019-01-11 00:48:33 -08:00
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
KRNode::render(ri);
|
|
|
|
|
|
|
|
|
|
bool bVisualize = ri.camera->settings.debug_display == KRRenderSettings::KRENGINE_DEBUG_DISPLAY_SIREN_REVERB_ZONES;
|
|
|
|
|
|
2024-01-21 18:34:36 -08:00
|
|
|
if (ri.renderPass->getType()== RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT && bVisualize) {
|
2025-11-11 23:07:04 -08:00
|
|
|
KRMesh* sphereModel = getContext().getMeshManager()->getMesh("__sphere");
|
2022-08-08 01:07:26 -07:00
|
|
|
if (sphereModel) {
|
|
|
|
|
Matrix4 sphereModelMatrix = getModelMatrix();
|
|
|
|
|
PipelineInfo info{};
|
|
|
|
|
std::string shader_name("visualize_overlay");
|
|
|
|
|
info.shader_name = &shader_name;
|
|
|
|
|
info.pCamera = ri.camera;
|
|
|
|
|
info.point_lights = &ri.point_lights;
|
|
|
|
|
info.directional_lights = &ri.directional_lights;
|
|
|
|
|
info.spot_lights = &ri.spot_lights;
|
|
|
|
|
info.renderPass = ri.renderPass;
|
|
|
|
|
info.rasterMode = RasterMode::kAlphaBlend;
|
2026-06-05 01:58:11 -07:00
|
|
|
info.topology = sphereModel->getTopology();
|
2022-08-08 01:07:26 -07:00
|
|
|
info.vertexAttributes = sphereModel->getVertexAttributes();
|
|
|
|
|
|
|
|
|
|
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
|
2026-06-01 00:06:28 -07:00
|
|
|
if (pShader && pShader->bind(ri, sphereModelMatrix)) {
|
2026-05-31 15:52:11 -07:00
|
|
|
sphereModel->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "visualize_overlay", 1.0f);
|
|
|
|
|
}
|
2022-08-08 01:07:26 -07:00
|
|
|
} // sphereModel
|
|
|
|
|
}
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float KRReverbZone::getGradientDistance()
|
|
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
return m_gradient_distance;
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KRReverbZone::setGradientDistance(float gradient_distance)
|
|
|
|
|
{
|
2022-08-08 01:07:26 -07:00
|
|
|
m_gradient_distance = gradient_distance;
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
AABB KRReverbZone::getBounds()
|
|
|
|
|
{
|
|
|
|
|
// Reverb zones always have a -1, -1, -1 to 1, 1, 1 bounding box
|
|
|
|
|
return AABB::Create(-Vector3::One(), Vector3::One(), getModelMatrix());
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
float KRReverbZone::getContainment(const Vector3& pos)
|
|
|
|
|
{
|
|
|
|
|
AABB bounds = getBounds();
|
|
|
|
|
if (bounds.contains(pos)) {
|
|
|
|
|
Vector3 size = bounds.size();
|
|
|
|
|
Vector3 diff = pos - bounds.center();
|
|
|
|
|
diff = diff * 2.0f;
|
|
|
|
|
diff = Vector3::Create(diff.x / size.x, diff.y / size.y, diff.z / size.z);
|
|
|
|
|
float d = diff.magnitude();
|
|
|
|
|
|
|
|
|
|
if (m_gradient_distance <= 0.0f) {
|
|
|
|
|
// Avoid division by zero
|
|
|
|
|
d = d > 1.0f ? 0.0f : 1.0f;
|
2019-01-11 00:48:33 -08:00
|
|
|
} else {
|
2022-08-08 01:07:26 -07:00
|
|
|
d = (1.0f - d) / m_gradient_distance;
|
2026-05-17 14:25:14 -07:00
|
|
|
d = std::clamp(d, 0.0f, 1.0f);
|
2019-01-11 00:48:33 -08:00
|
|
|
}
|
2022-08-08 01:07:26 -07:00
|
|
|
return d;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
2024-01-21 18:34:36 -08:00
|
|
|
}
|