rework label shader

This commit is contained in:
ohyzha
2024-08-28 11:12:59 +03:00
parent e2ae1687ac
commit 6305cbfe1e
7 changed files with 231 additions and 129 deletions

View File

@@ -28,16 +28,17 @@ namespace OpenVulkano::Scene
SetLabelSettings(settings);
SetupShaders();
SetupBuffers();
m_backgroundGeometry.SetFreeAfterUpload(false);
}
void LabelDrawable::SetLabelSettings(const LabelDrawableSettings& settings)
{
m_settings = settings;
m_labelData.color = settings.backgroundColor;
m_labelData.hasRoundedCorners = settings.hasRoundedCorners;
m_labelData.hasArrow = settings.hasArrow;
m_labelData.cornerRadius = settings.cornerRadius;
m_labelData.cornerRadius = settings.cornerRadius * settings.cornerRadius;
m_labelData.arrowLength = settings.arrowLength;
m_labelData.arrowWidth = settings.arrowWidth;
}
void LabelDrawable::AddText(const std::string& text, const TextConfig& config)
@@ -47,8 +48,8 @@ namespace OpenVulkano::Scene
return;
}
// do not render glyph's background
TextDrawable& textDrawable = m_texts.emplace_back(m_atlasData, config);
// do not render glyph's background
textDrawable.GetConfig().backgroundColor.a = 0;
textDrawable.SetShader(&m_textShader);
double lineHeight = m_atlasData->meta.lineHeight;
@@ -58,72 +59,20 @@ namespace OpenVulkano::Scene
// update position for next text entry
m_position.y = m_bbox.GetMin().y - lineHeight;
if (!m_settings.hasArrow)
{
if (m_backgroundGeometry.vertexCount != 4)
{
m_backgroundGeometry.Init(4, 6);
uint32_t indices[6] = { 0, 1, 2, 0, 2, 3 };
m_backgroundGeometry.SetIndices(indices, 6);
}
}
else
{
if (m_backgroundGeometry.vertexCount != 7)
{
m_backgroundGeometry.Init(7, 9);
uint32_t indices[9] = { 0, 1, 2, 0, 2, 3, 4, 5, 6 };
m_backgroundGeometry.SetIndices(indices, 9);
}
}
const auto& min = m_bbox.GetMin();
const auto& max = m_bbox.GetMax();
Vertex v, v2, v3, v4;
v.color = v2.color = v3.color = v4.color = m_settings.backgroundColor;
const float offset = 0.001;
v.position = Vector3f(min.x - m_settings.horizontalOffset, min.y - m_settings.verticalOffset, min.z - offset);
v2.position = Vector3f(max.x + m_settings.horizontalOffset, min.y - m_settings.verticalOffset, min.z - offset);
const float yOffset = m_settings.hasArrow ? m_settings.arrowLength : 0;
v.position = Vector3f(min.x - m_settings.horizontalOffset, min.y - m_settings.verticalOffset - yOffset, min.z - offset);
v2.position = Vector3f(max.x + m_settings.horizontalOffset, min.y - m_settings.verticalOffset - yOffset, min.z - offset);
v3.position = Vector3f(max.x + m_settings.horizontalOffset, max.y + m_settings.verticalOffset, min.z - offset);
v4.position = Vector3f(min.x - m_settings.horizontalOffset, max.y + m_settings.verticalOffset, min.z - offset);
if (!m_settings.hasArrow)
{
v.textureCoordinates = Vector3f(0, 0, 0);
v2.textureCoordinates = Vector3f(1, 0, 0);
v3.textureCoordinates = Vector3f(1, 1, 0);
v4.textureCoordinates = Vector3f(0, 1, 0);
}
else
{
const float w = v2.position.x - v.position.x;
const float h = v3.position.y - v2.position.y + m_settings.arrowLength;
v.textureCoordinates = Vector3f(0, v.position.y / h, 0);
v2.textureCoordinates = Vector3f(1, v2.position.y / h, 0);
v3.textureCoordinates = Vector3f(1, 1, 0);
v4.textureCoordinates = Vector3f(0, 1, 0);
// arrow vertices
Vertex a, b, c;
a.position = Vector3f(v.position.x + w / 3, v.position.y, v.position.z);
b.position = Vector3f(v.position.x + w / 2, v.position.y - m_settings.arrowLength, v.position.z);
c.position = Vector3f(v2.position.x - w / 3, v.position.y, v.position.z);
a.color = b.color = c.color = m_settings.backgroundColor;
a.textureCoordinates = Vector3f(a.position.x / w, a.position.y / h, 0);
b.textureCoordinates = Vector3f(b.position.x / w, 0, 0);
c.textureCoordinates = Vector3f(c.position.x / w, c.position.y / h, 0);
m_backgroundGeometry.vertices[4] = a;
m_backgroundGeometry.vertices[5] = b;
m_backgroundGeometry.vertices[6] = c;
}
m_backgroundGeometry.vertices[0] = v;
m_backgroundGeometry.vertices[1] = v2;
m_backgroundGeometry.vertices[2] = v3;
m_backgroundGeometry.vertices[3] = v4;
if (m_settings.hasRoundedCorners)
{
m_labelData.textSize.x = v2.position.x - v.position.x;
m_labelData.textSize.y = v3.position.y - v.position.y;
}
m_labelData.textSize.x = v2.position.x - v.position.x;
m_labelData.textSize.y = v3.position.y - v.position.y;
m_labelData.bboxCenter.x = (v2.position.x + v.position.x) / 2;
m_labelData.bboxCenter.y = (v3.position.y + v.position.y) / 2;
}
void LabelDrawable::SetBillboardSettings(const BillboardControlBlock& settings)
@@ -150,23 +99,22 @@ namespace OpenVulkano::Scene
void LabelDrawable::SetupShaders()
{
DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
if (!m_isBillboard)
{
m_backgroundShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/basic");
m_backgroundShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/label");
}
else
{
m_backgroundShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/billboard");
m_backgroundShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/labelBillboard");
// binding for billboard's buffer
DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
binding.stageFlags = ShaderProgramType::Type::VERTEX;
m_backgroundShader.AddDescriptorSetLayoutBinding(binding, 4);
}
m_backgroundShader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/label");
m_backgroundShader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
m_backgroundShader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING, 2);
binding.stageFlags = ShaderProgramType::Type::FRAGMENT;
m_backgroundShader.AddDescriptorSetLayoutBinding(binding, 5);
m_backgroundShader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING, 5);
m_backgroundShader.topology = Topology::TRIANGLE_STRIP;
m_backgroundShader.cullMode = CullMode::NONE;
SetShader(&m_backgroundShader);
@@ -198,17 +146,16 @@ namespace OpenVulkano::Scene
void LabelDrawable::SetupBuffers()
{
DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
binding.stageFlags = ShaderProgramType::Type::VERTEX;
m_billboardBuffer.size = sizeof(BillboardControlBlock);
m_billboardBuffer.data = &m_billboardSettings;
m_billboardBuffer.setId = 4;
DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
binding.stageFlags = ShaderProgramType::Type::VERTEX;
m_billboardBuffer.binding = binding;
binding.stageFlags = ShaderProgramType::Type::FRAGMENT;
m_labelBuffer.size = sizeof(LabelUniformData);
m_labelBuffer.data = &m_labelData;
m_labelBuffer.setId = 5;
m_labelBuffer.binding = binding;
m_labelBuffer.binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
}
}