-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom Drawable Layer: Symbol Icon (#1908)
- Loading branch information
1 parent
612b256
commit d6db72e
Showing
32 changed files
with
788 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include <mbgl/shaders/layer_ubo.hpp> | ||
|
||
namespace mbgl { | ||
namespace shaders { | ||
|
||
/// Custom Symbol Icon matrix | ||
struct alignas(16) CustomSymbolIconDrawableUBO { | ||
/* 0 */ std::array<float, 4 * 4> matrix; | ||
/* 64 */ | ||
}; | ||
static_assert(sizeof(CustomSymbolIconDrawableUBO) == 4 * 16); | ||
|
||
/// Custom Symbol Icon Parameters | ||
struct alignas(16) CustomSymbolIconParametersUBO { | ||
/* 0 */ std::array<float, 2> extrude_scale; | ||
/* 8 */ std::array<float, 2> anchor; | ||
/* 16 */ float angle_degrees; | ||
/* 20 */ uint32_t scale_with_map; | ||
/* 24 */ uint32_t pitch_with_map; | ||
/* 28 */ float camera_to_center_distance; | ||
/* 32 */ float aspect_ratio; | ||
/* 36 */ float pad0, pad1, pad2; | ||
/* 48 */ | ||
}; | ||
static_assert(sizeof(CustomSymbolIconParametersUBO) == 3 * 16); | ||
|
||
enum { | ||
idCustomSymbolIconDrawableUBO, | ||
idCustomSymbolIconParametersUBO, | ||
customDrawableUBOCount | ||
}; | ||
|
||
} // namespace shaders | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Generated code, do not modify this file! | ||
#pragma once | ||
#include <mbgl/shaders/shader_source.hpp> | ||
|
||
namespace mbgl { | ||
namespace shaders { | ||
|
||
template <> | ||
struct ShaderSource<BuiltIn::CustomSymbolIconShader, gfx::Backend::Type::OpenGL> { | ||
static constexpr const char* name = "CustomSymbolIconShader"; | ||
static constexpr const char* vertex = R"(layout(location = 0) in vec2 a_pos; | ||
layout(location = 1) in vec2 a_tex; | ||
layout(std140) uniform CustomSymbolIconDrawableUBO { | ||
highp mat4 u_matrix; | ||
}; | ||
layout(std140) uniform CustomSymbolIconParametersUBO { | ||
highp vec2 u_extrude_scale; | ||
highp vec2 u_anchor; | ||
highp float u_angle_degrees; | ||
bool u_scale_with_map; | ||
bool u_pitch_with_map; | ||
highp float u_camera_to_center_distance; | ||
highp float u_aspect_ratio; | ||
highp float pad0, pad1, pad3; | ||
}; | ||
out vec2 v_tex; | ||
vec2 rotateVec2(vec2 v, float angle) { | ||
float cosA = cos(angle); | ||
float sinA = sin(angle); | ||
return vec2(v.x * cosA - v.y * sinA, v.x * sinA + v.y * cosA); | ||
} | ||
vec2 ellipseRotateVec2(vec2 v, float angle, float radiusRatio /* A/B */) { | ||
float cosA = cos(angle); | ||
float sinA = sin(angle); | ||
float invRatio = 1.0 / radiusRatio; | ||
return vec2(v.x * cosA - radiusRatio * v.y * sinA, invRatio * v.x * sinA + v.y * cosA); | ||
} | ||
void main() { | ||
// decode the extrusion vector (-1, -1) to (1, 1) | ||
vec2 extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0); | ||
// make anchor relative to (0.5, 0.5) and corners in range (-1, -1) to (1, 1) | ||
vec2 anchor = (u_anchor - vec2(0.5, 0.5)) * 2.0; | ||
// decode center | ||
vec2 center = floor(a_pos * 0.5); | ||
// rotate extrusion around anchor | ||
float angle = radians(-u_angle_degrees); | ||
vec2 corner = extrude - anchor; | ||
// compute | ||
if (u_pitch_with_map) { | ||
if (u_scale_with_map) { | ||
corner *= u_extrude_scale; | ||
} else { | ||
vec4 projected_center = u_matrix * vec4(center, 0, 1); | ||
corner *= u_extrude_scale * (projected_center.w / u_camera_to_center_distance); | ||
} | ||
corner = center + rotateVec2(corner, angle); | ||
gl_Position = u_matrix * vec4(corner, 0, 1); | ||
} else { | ||
gl_Position = u_matrix * vec4(center, 0, 1); | ||
if (u_scale_with_map) { | ||
gl_Position.xy += ellipseRotateVec2(corner * u_extrude_scale * u_camera_to_center_distance, angle, u_aspect_ratio); | ||
} else { | ||
gl_Position.xy += ellipseRotateVec2(corner * u_extrude_scale * gl_Position.w, angle, u_aspect_ratio); | ||
} | ||
} | ||
// texture coordinates | ||
v_tex = a_tex; | ||
} | ||
)"; | ||
static constexpr const char* fragment = R"(uniform sampler2D u_texture; | ||
in vec2 v_tex; | ||
void main() { | ||
fragColor = texture(u_texture, v_tex); | ||
} | ||
)"; | ||
}; | ||
|
||
} // namespace shaders | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#pragma once | ||
|
||
#include <mbgl/shaders/mtl/common.hpp> | ||
#include <mbgl/shaders/mtl/shader_program.hpp> | ||
#include <mbgl/shaders/shader_source.hpp> | ||
#include <mbgl/shaders/custom_drawable_layer_ubo.hpp> | ||
|
||
namespace mbgl { | ||
namespace shaders { | ||
|
||
template <> | ||
struct ShaderSource<BuiltIn::CustomSymbolIconShader, gfx::Backend::Type::Metal> { | ||
static constexpr auto name = "CustomSymbolIconShader"; | ||
static constexpr auto vertexMainFunction = "vertexMain"; | ||
static constexpr auto fragmentMainFunction = "fragmentMain"; | ||
|
||
static const std::array<AttributeInfo, 2> attributes; | ||
static const std::array<UniformBlockInfo, 2> uniforms; | ||
static const std::array<TextureInfo, 1> textures; | ||
|
||
static constexpr auto source = R"( | ||
struct alignas(16) CustomSymbolIconDrawableUBO { | ||
float4x4 matrix; | ||
}; | ||
struct alignas(16) CustomSymbolIconParametersUBO { | ||
float2 extrude_scale; | ||
float2 anchor; | ||
float angle_degrees; | ||
int scale_with_map; | ||
int pitch_with_map; | ||
float camera_to_center_distance; | ||
float aspect_ratio; | ||
float pad0, pad1, pad3; | ||
}; | ||
struct VertexStage { | ||
float2 a_pos [[attribute(0)]]; | ||
float2 a_tex [[attribute(1)]]; | ||
}; | ||
struct FragmentStage { | ||
float4 position [[position, invariant]]; | ||
half2 tex; | ||
}; | ||
float2 rotateVec2(float2 v, float angle) { | ||
float cosA = cos(angle); | ||
float sinA = sin(angle); | ||
return float2(v.x * cosA - v.y * sinA, v.x * sinA + v.y * cosA); | ||
} | ||
float2 ellipseRotateVec2(float2 v, float angle, float radiusRatio /* A/B */) { | ||
float cosA = cos(angle); | ||
float sinA = sin(angle); | ||
float invRatio = 1.0 / radiusRatio; | ||
return float2(v.x * cosA - radiusRatio * v.y * sinA, invRatio * v.x * sinA + v.y * cosA); | ||
} | ||
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], | ||
device const CustomSymbolIconDrawableUBO& drawable [[buffer(2)]], | ||
device const CustomSymbolIconParametersUBO& parameters [[buffer(3)]]) { | ||
const float2 extrude = glMod(float2(vertx.a_pos), 2.0) * 2.0 - 1.0; | ||
const float2 anchor = (parameters.anchor - float2(0.5, 0.5)) * 2.0; | ||
const float2 center = floor(float2(vertx.a_pos) * 0.5); | ||
const float angle = radians(-parameters.angle_degrees); | ||
float2 corner = extrude - anchor; | ||
float4 position; | ||
if (parameters.pitch_with_map) { | ||
if (parameters.scale_with_map) { | ||
corner *= parameters.extrude_scale; | ||
} else { | ||
float4 projected_center = drawable.matrix * float4(center, 0, 1); | ||
corner *= parameters.extrude_scale * (projected_center.w / parameters.camera_to_center_distance); | ||
} | ||
corner = center + rotateVec2(corner, angle); | ||
position = drawable.matrix * float4(corner, 0, 1); | ||
} else { | ||
position = drawable.matrix * float4(center, 0, 1); | ||
const float factor = parameters.scale_with_map ? parameters.camera_to_center_distance : position.w; | ||
position.xy += ellipseRotateVec2(corner * parameters.extrude_scale * factor, angle, parameters.aspect_ratio); | ||
} | ||
return { | ||
.position = position, | ||
.tex = half2(vertx.a_tex) | ||
}; | ||
} | ||
half4 fragment fragmentMain(FragmentStage in [[stage_in]], | ||
texture2d<float, access::sample> image [[texture(0)]], | ||
sampler image_sampler [[sampler(0)]]) { | ||
#if defined(OVERDRAW_INSPECTOR) | ||
return half4(1.0); | ||
#endif | ||
return half4(image.sample(image_sampler, float2(in.tex))); | ||
} | ||
)"; | ||
}; | ||
|
||
} // namespace shaders | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.