From f6e79d70735361438655f279c8699a786d25458c Mon Sep 17 00:00:00 2001 From: Lauren Budorick Date: Thu, 27 Apr 2017 15:56:55 -0700 Subject: [core] Render fill-extrusion layers (#8431) --- src/mbgl/shaders/extrusion_texture.cpp | 39 ++++++++++ src/mbgl/shaders/extrusion_texture.hpp | 16 ++++ src/mbgl/shaders/fill_extrusion.cpp | 98 ++++++++++++++++++++++++ src/mbgl/shaders/fill_extrusion.hpp | 16 ++++ src/mbgl/shaders/fill_extrusion_pattern.cpp | 111 ++++++++++++++++++++++++++++ src/mbgl/shaders/fill_extrusion_pattern.hpp | 16 ++++ src/mbgl/shaders/preludes.cpp | 5 +- 7 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 src/mbgl/shaders/extrusion_texture.cpp create mode 100644 src/mbgl/shaders/extrusion_texture.hpp create mode 100644 src/mbgl/shaders/fill_extrusion.cpp create mode 100644 src/mbgl/shaders/fill_extrusion.hpp create mode 100644 src/mbgl/shaders/fill_extrusion_pattern.cpp create mode 100644 src/mbgl/shaders/fill_extrusion_pattern.hpp (limited to 'src/mbgl/shaders') diff --git a/src/mbgl/shaders/extrusion_texture.cpp b/src/mbgl/shaders/extrusion_texture.cpp new file mode 100644 index 0000000000..c756db181c --- /dev/null +++ b/src/mbgl/shaders/extrusion_texture.cpp @@ -0,0 +1,39 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +namespace shaders { + +const char* extrusion_texture::name = "extrusion_texture"; +const char* extrusion_texture::vertexSource = R"MBGL_SHADER( +uniform mat4 u_matrix; +uniform vec2 u_world; +attribute vec2 a_pos; +varying vec2 v_pos; + +void main() { + gl_Position = u_matrix * vec4(a_pos * u_world, 0, 1); + + v_pos.x = a_pos.x; + v_pos.y = 1.0 - a_pos.y; +} + +)MBGL_SHADER"; +const char* extrusion_texture::fragmentSource = R"MBGL_SHADER( +uniform sampler2D u_image; +uniform float u_opacity; +varying vec2 v_pos; + +void main() { + gl_FragColor = texture2D(u_image, v_pos) * u_opacity; + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(0.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/extrusion_texture.hpp b/src/mbgl/shaders/extrusion_texture.hpp new file mode 100644 index 0000000000..05e54c5499 --- /dev/null +++ b/src/mbgl/shaders/extrusion_texture.hpp @@ -0,0 +1,16 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#pragma once + +namespace mbgl { +namespace shaders { + +class extrusion_texture { +public: + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_extrusion.cpp b/src/mbgl/shaders/fill_extrusion.cpp new file mode 100644 index 0000000000..ea1c80545e --- /dev/null +++ b/src/mbgl/shaders/fill_extrusion.cpp @@ -0,0 +1,98 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +namespace shaders { + +const char* fill_extrusion::name = "fill_extrusion"; +const char* fill_extrusion::vertexSource = R"MBGL_SHADER( +uniform mat4 u_matrix; +uniform vec3 u_lightcolor; +uniform lowp vec3 u_lightpos; +uniform lowp float u_lightintensity; + +attribute vec2 a_pos; +attribute vec3 a_normal; +attribute float a_edgedistance; + +varying vec4 v_color; + +uniform lowp float a_base_t; +attribute lowp vec2 a_base; +varying lowp float base; +uniform lowp float a_height_t; +attribute lowp vec2 a_height; +varying lowp float height; + +uniform lowp float a_color_t; +attribute highp vec4 a_color; +varying highp vec4 color; + +void main() { + base = unpack_mix_vec2(a_base, a_base_t); + height = unpack_mix_vec2(a_height, a_height_t); + color = unpack_mix_vec4(a_color, a_color_t); + + base = max(0.0, base); + height = max(0.0, height); + + float ed = a_edgedistance; // use each attrib in order to not trip a VAO assert + float t = mod(a_normal.x, 2.0); + + gl_Position = u_matrix * vec4(a_pos, t > 0.0 ? height : base, 1); + + // Relative luminance (how dark/bright is the surface color?) + float colorvalue = color.r * 0.2126 + color.g * 0.7152 + color.b * 0.0722; + + v_color = vec4(0.0, 0.0, 0.0, 1.0); + + // Add slight ambient lighting so no extrusions are totally black + vec4 ambientlight = vec4(0.03, 0.03, 0.03, 1.0); + color += ambientlight; + + // Calculate cos(theta), where theta is the angle between surface normal and diffuse light ray + float directional = clamp(dot(a_normal / 16384.0, u_lightpos), 0.0, 1.0); + + // Adjust directional so that + // the range of values for highlight/shading is narrower + // with lower light intensity + // and with lighter/brighter surface colors + directional = mix((1.0 - u_lightintensity), max((1.0 - colorvalue + u_lightintensity), 1.0), directional); + + // Add gradient along z axis of side surfaces + if (a_normal.y != 0.0) { + directional *= clamp((t + base) * pow(height / 150.0, 0.5), mix(0.7, 0.98, 1.0 - u_lightintensity), 1.0); + } + + // Assign final color based on surface + ambient light color, diffuse light directional, and light color + // with lower bounds adjusted to hue of light + // so that shading is tinted with the complementary (opposite) color to the light color + v_color.r += clamp(color.r * directional * u_lightcolor.r, mix(0.0, 0.3, 1.0 - u_lightcolor.r), 1.0); + v_color.g += clamp(color.g * directional * u_lightcolor.g, mix(0.0, 0.3, 1.0 - u_lightcolor.g), 1.0); + v_color.b += clamp(color.b * directional * u_lightcolor.b, mix(0.0, 0.3, 1.0 - u_lightcolor.b), 1.0); +} + +)MBGL_SHADER"; +const char* fill_extrusion::fragmentSource = R"MBGL_SHADER( +varying vec4 v_color; +varying lowp float base; +varying lowp float height; +varying highp vec4 color; + +void main() { + + + + + gl_FragColor = v_color; + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_extrusion.hpp b/src/mbgl/shaders/fill_extrusion.hpp new file mode 100644 index 0000000000..949ae57f12 --- /dev/null +++ b/src/mbgl/shaders/fill_extrusion.hpp @@ -0,0 +1,16 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#pragma once + +namespace mbgl { +namespace shaders { + +class fill_extrusion { +public: + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_extrusion_pattern.cpp b/src/mbgl/shaders/fill_extrusion_pattern.cpp new file mode 100644 index 0000000000..e2de5c20b2 --- /dev/null +++ b/src/mbgl/shaders/fill_extrusion_pattern.cpp @@ -0,0 +1,111 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +namespace shaders { + +const char* fill_extrusion_pattern::name = "fill_extrusion_pattern"; +const char* fill_extrusion_pattern::vertexSource = R"MBGL_SHADER( +uniform mat4 u_matrix; +uniform vec2 u_pattern_size_a; +uniform vec2 u_pattern_size_b; +uniform vec2 u_pixel_coord_upper; +uniform vec2 u_pixel_coord_lower; +uniform float u_scale_a; +uniform float u_scale_b; +uniform float u_tile_units_to_pixels; +uniform float u_height_factor; + +uniform vec3 u_lightcolor; +uniform lowp vec3 u_lightpos; +uniform lowp float u_lightintensity; + +attribute vec2 a_pos; +attribute vec3 a_normal; +attribute float a_edgedistance; + +varying vec2 v_pos_a; +varying vec2 v_pos_b; +varying vec4 v_lighting; +varying float v_directional; + +uniform lowp float a_base_t; +attribute lowp vec2 a_base; +varying lowp float base; +uniform lowp float a_height_t; +attribute lowp vec2 a_height; +varying lowp float height; + +void main() { + base = unpack_mix_vec2(a_base, a_base_t); + height = unpack_mix_vec2(a_height, a_height_t); + + base = max(0.0, base); + height = max(0.0, height); + + float t = mod(a_normal.x, 2.0); + float z = t > 0.0 ? height : base; + + gl_Position = u_matrix * vec4(a_pos, z, 1); + + vec2 pos = a_normal.x == 1.0 && a_normal.y == 0.0 && a_normal.z == 16384.0 + ? a_pos // extrusion top + : vec2(a_edgedistance, z * u_height_factor); // extrusion side + + v_pos_a = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_a * u_pattern_size_a, u_tile_units_to_pixels, pos); + v_pos_b = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_b * u_pattern_size_b, u_tile_units_to_pixels, pos); + + v_lighting = vec4(0.0, 0.0, 0.0, 1.0); + float directional = clamp(dot(a_normal / 16383.0, u_lightpos), 0.0, 1.0); + directional = mix((1.0 - u_lightintensity), max((0.5 + u_lightintensity), 1.0), directional); + + if (a_normal.y != 0.0) { + directional *= clamp((t + base) * pow(height / 150.0, 0.5), mix(0.7, 0.98, 1.0 - u_lightintensity), 1.0); + } + + v_lighting.rgb += clamp(directional * u_lightcolor, mix(vec3(0.0), vec3(0.3), 1.0 - u_lightcolor), vec3(1.0)); +} + +)MBGL_SHADER"; +const char* fill_extrusion_pattern::fragmentSource = R"MBGL_SHADER( +uniform vec2 u_pattern_tl_a; +uniform vec2 u_pattern_br_a; +uniform vec2 u_pattern_tl_b; +uniform vec2 u_pattern_br_b; +uniform float u_mix; + +uniform sampler2D u_image; + +varying vec2 v_pos_a; +varying vec2 v_pos_b; +varying vec4 v_lighting; + +varying lowp float base; +varying lowp float height; + +void main() { + + + + vec2 imagecoord = mod(v_pos_a, 1.0); + vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord); + vec4 color1 = texture2D(u_image, pos); + + vec2 imagecoord_b = mod(v_pos_b, 1.0); + vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, imagecoord_b); + vec4 color2 = texture2D(u_image, pos2); + + vec4 mixedColor = mix(color1, color2, u_mix); + + gl_FragColor = mixedColor * v_lighting; + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_extrusion_pattern.hpp b/src/mbgl/shaders/fill_extrusion_pattern.hpp new file mode 100644 index 0000000000..e9ae001d4e --- /dev/null +++ b/src/mbgl/shaders/fill_extrusion_pattern.hpp @@ -0,0 +1,16 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#pragma once + +namespace mbgl { +namespace shaders { + +class fill_extrusion_pattern { +public: + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/preludes.cpp b/src/mbgl/shaders/preludes.cpp index cca0f3e3f1..95fa624e8d 100644 --- a/src/mbgl/shaders/preludes.cpp +++ b/src/mbgl/shaders/preludes.cpp @@ -48,8 +48,9 @@ vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 v // packed like so: // packedValue = floor(input[0]) * 256 + input[1], vec2 unpack_float(const float packedValue) { - float v0 = floor(packedValue / 256.0); - return vec2(v0, packedValue - v0 * 256.0); + int packedIntValue = int(packedValue); + int v0 = packedIntValue / 256; + return vec2(v0, packedIntValue - v0 * 256); } -- cgit v1.2.1