#pragma once #include #include #include #include namespace mbgl { namespace shaders { template <> struct ShaderSource { static constexpr auto name = "CollisionBoxShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; static const std::array uniforms; static const std::array attributes; static constexpr std::array instanceAttributes{}; static const std::array textures; static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(2)]]; short2 anchor_pos [[attribute(3)]]; short2 extrude [[attribute(4)]]; ushort2 placed [[attribute(5)]]; float2 shift [[attribute(6)]]; }; struct FragmentStage { float4 position [[position, invariant]]; float placed; float notUsed; }; struct alignas(16) CollisionBoxUBO { float4x4 matrix; float2 extrude_scale; float overscale_factor; float pad1; }; FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const GlobalPaintParamsUBO& paintParams [[buffer(0)]], device const CollisionBoxUBO& drawable [[buffer(1)]]) { 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) * drawable.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]], device const CollisionBoxUBO& drawable [[buffer(1)]]) { 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); } )"; }; } // namespace shaders } // namespace mbgl