Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
203 lines
6.9 KiB
C++
203 lines
6.9 KiB
C++
#pragma once
|
|
|
|
#include <mbgl/shaders/collision_layer_ubo.hpp>
|
|
#include <mbgl/shaders/shader_source.hpp>
|
|
#include <mbgl/shaders/mtl/shader_program.hpp>
|
|
|
|
namespace mbgl {
|
|
namespace shaders {
|
|
|
|
#define COLLISION_SHADER_COMMON \
|
|
R"(
|
|
|
|
enum {
|
|
idCollisionDrawableUBO = idDrawableReservedVertexOnlyUBO,
|
|
idCollisionTilePropsUBO = drawableReservedUBOCount,
|
|
collisionUBOCount
|
|
};
|
|
|
|
struct alignas(16) CollisionDrawableUBO {
|
|
/* 0 */ float4x4 matrix;
|
|
/* 64 */
|
|
};
|
|
static_assert(sizeof(CollisionDrawableUBO) == 4 * 16, "wrong size");
|
|
|
|
struct alignas(16) CollisionTilePropsUBO {
|
|
/* 0 */ float2 extrude_scale;
|
|
/* 8 */ float overscale_factor;
|
|
/* 12 */ float pad1;
|
|
/* 16 */
|
|
};
|
|
static_assert(sizeof(CollisionTilePropsUBO) == 16, "wrong size");
|
|
|
|
)"
|
|
|
|
template <>
|
|
struct ShaderSource<BuiltIn::CollisionBoxShader, gfx::Backend::Type::Metal> {
|
|
static constexpr auto name = "CollisionBoxShader";
|
|
static constexpr auto vertexMainFunction = "vertexMain";
|
|
static constexpr auto fragmentMainFunction = "fragmentMain";
|
|
|
|
static const std::array<AttributeInfo, 5> attributes;
|
|
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
|
|
static const std::array<TextureInfo, 0> textures;
|
|
|
|
static constexpr auto source = COLLISION_SHADER_COMMON R"(
|
|
|
|
struct VertexStage {
|
|
short2 pos [[attribute(collisionUBOCount + 0)]];
|
|
short2 anchor_pos [[attribute(collisionUBOCount + 1)]];
|
|
short2 extrude [[attribute(collisionUBOCount + 2)]];
|
|
ushort2 placed [[attribute(collisionUBOCount + 3)]];
|
|
float2 shift [[attribute(collisionUBOCount + 4)]];
|
|
};
|
|
|
|
struct FragmentStage {
|
|
float4 position [[position, invariant]];
|
|
float placed;
|
|
float notUsed;
|
|
};
|
|
|
|
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
|
|
device const GlobalPaintParamsUBO& paintParams [[buffer(idGlobalPaintParamsUBO)]],
|
|
device const CollisionDrawableUBO& drawable [[buffer(idCollisionDrawableUBO)]],
|
|
device const CollisionTilePropsUBO& tileProps [[buffer(idCollisionTilePropsUBO)]]) {
|
|
|
|
float4 projectedPoint = drawable.matrix * float4(float2(vertx.anchor_pos), 0, 1);
|
|
float camera_to_anchor_distance = projectedPoint.w;
|
|
float collision_perspective_ratio = clamp(
|
|
0.5 + 0.5 * (paintParams.camera_to_center_distance / camera_to_anchor_distance),
|
|
0.0, // Prevents oversized near-field boxes in pitched/overzoomed tiles
|
|
4.0);
|
|
|
|
float4 position = drawable.matrix * float4(float2(vertx.pos), 0.0, 1.0);
|
|
position.xy += (float2(vertx.extrude) + vertx.shift) * tileProps.extrude_scale * position.w * collision_perspective_ratio;
|
|
|
|
float placed = float(vertx.placed.x);
|
|
float notUsed = float(vertx.placed.y);
|
|
|
|
return {
|
|
.position = position,
|
|
.placed = placed,
|
|
.notUsed = notUsed,
|
|
};
|
|
}
|
|
|
|
half4 fragment fragmentMain(FragmentStage in [[stage_in]]) {
|
|
|
|
float alpha = 0.5;
|
|
|
|
// Red = collision, hide label
|
|
float4 color = float4(1.0, 0.0, 0.0, 1.0) * alpha;
|
|
|
|
// Blue = no collision, label is showing
|
|
if (in.placed > 0.5) {
|
|
color = float4(0.0, 0.0, 1.0, 0.5) * alpha;
|
|
}
|
|
|
|
if (in.notUsed > 0.5) {
|
|
// This box not used, fade it out
|
|
color *= 0.1;
|
|
}
|
|
|
|
return half4(color);
|
|
}
|
|
)";
|
|
};
|
|
|
|
template <>
|
|
struct ShaderSource<BuiltIn::CollisionCircleShader, gfx::Backend::Type::Metal> {
|
|
static constexpr auto name = "CollisionCircleShader";
|
|
static constexpr auto vertexMainFunction = "vertexMain";
|
|
static constexpr auto fragmentMainFunction = "fragmentMain";
|
|
|
|
static const std::array<AttributeInfo, 4> attributes;
|
|
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
|
|
static const std::array<TextureInfo, 0> textures;
|
|
|
|
static constexpr auto source = COLLISION_SHADER_COMMON R"(
|
|
|
|
struct VertexStage {
|
|
short2 pos [[attribute(collisionUBOCount + 0)]];
|
|
short2 anchor_pos [[attribute(collisionUBOCount + 1)]];
|
|
short2 extrude [[attribute(collisionUBOCount + 2)]];
|
|
ushort2 placed [[attribute(collisionUBOCount + 3)]];
|
|
};
|
|
|
|
struct FragmentStage {
|
|
float4 position [[position, invariant]];
|
|
float placed;
|
|
float notUsed;
|
|
float radius;
|
|
float2 extrude;
|
|
float2 extrude_scale;
|
|
};
|
|
|
|
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
|
|
device const GlobalPaintParamsUBO& paintParams [[buffer(idGlobalPaintParamsUBO)]],
|
|
device const CollisionDrawableUBO& drawable [[buffer(idCollisionDrawableUBO)]],
|
|
device const CollisionTilePropsUBO& tileProps [[buffer(idCollisionTilePropsUBO)]]) {
|
|
|
|
float4 projectedPoint = drawable.matrix * float4(float2(vertx.anchor_pos), 0, 1);
|
|
float camera_to_anchor_distance = projectedPoint.w;
|
|
float collision_perspective_ratio = clamp(
|
|
0.5 + 0.5 * (paintParams.camera_to_center_distance / camera_to_anchor_distance),
|
|
0.0, // Prevents oversized near-field circles in pitched/overzoomed tiles
|
|
4.0);
|
|
|
|
float4 position = drawable.matrix * float4(float2(vertx.pos), 0.0, 1.0);
|
|
|
|
float padding_factor = 1.2; // Pad the vertices slightly to make room for anti-alias blur
|
|
position.xy += float2(vertx.extrude) * tileProps.extrude_scale * padding_factor * position.w * collision_perspective_ratio;
|
|
|
|
float placed = float(vertx.placed.x);
|
|
float notUsed = float(vertx.placed.y);
|
|
float radius = abs(float(vertx.extrude.y)); // We don't pitch the circles, so both units of the extrusion vector are equal in magnitude to the radius
|
|
|
|
float2 extrude = float2(vertx.extrude) * padding_factor;
|
|
float2 extrude_scale = tileProps.extrude_scale * paintParams.camera_to_center_distance * collision_perspective_ratio;
|
|
|
|
return {
|
|
.position = position,
|
|
.placed = placed,
|
|
.notUsed = notUsed,
|
|
.radius = radius,
|
|
.extrude = extrude,
|
|
.extrude_scale = extrude_scale,
|
|
};
|
|
}
|
|
|
|
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
|
|
device const CollisionTilePropsUBO& tileProps [[buffer(idCollisionTilePropsUBO)]]) {
|
|
|
|
float alpha = 0.5;
|
|
|
|
// Red = collision, hide label
|
|
float4 color = float4(1.0, 0.0, 0.0, 1.0) * alpha;
|
|
|
|
// Blue = no collision, label is showing
|
|
if (in.placed > 0.5) {
|
|
color = float4(0.0, 0.0, 1.0, 0.5) * alpha;
|
|
}
|
|
|
|
if (in.notUsed > 0.5) {
|
|
// This box not used, fade it out
|
|
color *= 0.2;
|
|
}
|
|
|
|
float extrude_scale_length = length(in.extrude_scale);
|
|
float extrude_length = length(in.extrude) * extrude_scale_length;
|
|
float stroke_width = 15.0 * extrude_scale_length / tileProps.overscale_factor;
|
|
float radius = in.radius * extrude_scale_length;
|
|
|
|
float distance_to_edge = abs(extrude_length - radius);
|
|
float opacity_t = smoothstep(-stroke_width, 0.0, -distance_to_edge);
|
|
|
|
return half4(opacity_t * color);
|
|
}
|
|
)";
|
|
};
|
|
|
|
} // namespace shaders
|
|
} // namespace mbgl
|