Skip to content

Commit

Permalink
Port model shader changes for height-based emissive strength (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksigron authored Apr 4, 2023
1 parent add11df commit 6843d07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
11 changes: 8 additions & 3 deletions 3d-style/shaders/model.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ varying float v_depth_shadows;

#ifdef HAS_ATTRIBUTE_a_pbr
varying lowp vec4 v_roughness_metallic_emissive_alpha;
varying mediump vec4 v_height_based_emission_params;
#endif

#ifdef HAS_TEXTURE_u_baseColorTexture
Expand Down Expand Up @@ -342,7 +343,7 @@ vec3 computeLightContribution(Material mat, vec3 lightPosition, vec3 lightColor)
float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);
// Just to make happy generate-metal-shaders
float NdotL = 0.0;
#ifdef LIGHTING_3D_MODE
#ifdef LIGHTING_3D_MODE
// For landmarks use the extended NdotL range to mimic
// fill extrusion lighting
NdotL = calculate_NdotL(n, l);
Expand Down Expand Up @@ -458,12 +459,16 @@ vec4 finalColor;
// Apply transparency
float opacity = mat.baseColor.w * u_opacity;
#ifdef HAS_ATTRIBUTE_a_pbr
color = mix(color, v_color_mix.rgb, min(1.0, v_roughness_metallic_emissive_alpha.z));
float resEmission = v_roughness_metallic_emissive_alpha.z;

resEmission *= v_height_based_emission_params.z + v_height_based_emission_params.w * pow(clamp(v_height_based_emission_params.x, 0.0, 1.0), v_height_based_emission_params.y);

color = mix(color, v_color_mix.rgb, min(1.0, resEmission));
#ifdef HAS_ATTRIBUTE_a_color_4f
// pbr includes color. If pbr is used, color_4f is used to pass information about light geometry.
// calculate distance to line segment, multiplier 1.3 additionally deattenuates towards extruded corners.
float distance = length(vec2(1.3 * max(0.0, abs(color_4f.x) - color_4f.z), color_4f.y));
distance += mix(0.5, 0.0, clamp(v_roughness_metallic_emissive_alpha.z - 1.0, 0.0, 1.0));
distance += mix(0.5, 0.0, clamp(resEmission - 1.0, 0.0, 1.0));
opacity *= v_roughness_metallic_emissive_alpha.w * saturate(1.0 - distance * distance);
#endif
#endif
Expand Down
30 changes: 29 additions & 1 deletion 3d-style/shaders/model.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ attribute vec3 a_pos_3f;
#pragma mapbox: define-attribute highp vec3 color_3f
#pragma mapbox: define-attribute highp vec4 color_4f
#pragma mapbox: define-attribute-vertex-shader-only highp vec4 pbr
#pragma mapbox: define-attribute-vertex-shader-only highp vec3 heightBasedEmissiveStrength

// pbr
// .xy - color.rgba (4 bytes)
// .z - emissive strength (1 byte) | roughness (4 bits) | metallic (4 bits)
// .w - heightBasedEmissionMultiplier value at interpolation Begin and Finish points (2 bytes)

// heightBasedEmissiveStrength
// .xy - interpolation parameters
// .z - interpolation curve power
// i.e.
// interpolatedHeight = pow(pos_z * .x + .y, .z)

uniform mat4 u_matrix;
uniform mat4 u_lighting_matrix;
Expand Down Expand Up @@ -37,6 +49,11 @@ varying highp float v_depth;

#ifdef HAS_ATTRIBUTE_a_pbr
varying lowp vec4 v_roughness_metallic_emissive_alpha;
varying mediump vec4 v_height_based_emission_params;
// .x - height-based interpolation factor
// .y - interpolation power
// .z - min value
// .w - max - min
#endif

void main() {
Expand All @@ -45,6 +62,7 @@ void main() {
#pragma mapbox: initialize-attribute highp vec3 color_3f
#pragma mapbox: initialize-attribute highp vec4 color_4f
#pragma mapbox: initialize-attribute-custom highp vec4 pbr
#pragma mapbox: initialize-attribute-custom highp vec3 heightBasedEmissiveStrength

highp mat4 normal_matrix;
#ifdef INSTANCED_ARRAYS
Expand Down Expand Up @@ -99,6 +117,16 @@ void main() {
v_color_mix = vec4(albedo_c.rgb, 1.0); // vertex color is computed on CPU
v_roughness_metallic_emissive_alpha = vec4(vec3(r_m, e_r_m.x) / 255.0, albedo_c.a);
v_roughness_metallic_emissive_alpha.z *= 2.0; // range [0..2] was shrank to fit [0..1]

float heightBasedRelativeIntepolation = a_pos_3f.z * heightBasedEmissiveStrength.x + heightBasedEmissiveStrength.y;

v_height_based_emission_params.x = heightBasedRelativeIntepolation;
v_height_based_emission_params.y = heightBasedEmissiveStrength.z;

vec2 emissionMultiplierValues = unpack_float(pbr.w) / 256.0;

v_height_based_emission_params.z = emissionMultiplierValues.x;
v_height_based_emission_params.w = emissionMultiplierValues.y - emissionMultiplierValues.x;
#endif
#ifdef FOG
v_fog_pos = fog_position(local_pos);
Expand All @@ -124,7 +152,7 @@ void main() {
#ifdef HAS_ATTRIBUTE_a_pbr
#ifdef HAS_ATTRIBUTE_a_color_4f
v_roughness_metallic_emissive_alpha.w = clamp(color_4f.a * v_roughness_metallic_emissive_alpha.w * (v_roughness_metallic_emissive_alpha.z - 1.0), 0.0, 1.0);
#endif
#endif
#endif

#ifdef RENDER_SHADOWS
Expand Down

0 comments on commit 6843d07

Please sign in to comment.