Skip to content

Commit

Permalink
[MAPS3D-697] support emission strength for 2d layers (#506)
Browse files Browse the repository at this point in the history
* [MAPS3D-697] support emission strength for 2d layers

data-constant emissive strength layers:
- circle
- fill
- line

data-driven emissive strength layers:
- symbol

* new tests: zoom and measure-light tests

* leave sdk support property empty (to be filled later)
  • Loading branch information
alexey-romanov authored Mar 29, 2023
1 parent 6a7126f commit d40fed9
Show file tree
Hide file tree
Showing 27 changed files with 342 additions and 65 deletions.
2 changes: 2 additions & 0 deletions src/data/program_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ const attributeNameExceptions = {
'icon-opacity': ['opacity'],
'text-color': ['fill_color'],
'icon-color': ['fill_color'],
'text-emissive-strength': ['emissive_strength'],
'icon-emissive-strength': ['emissive_strength'],
'text-halo-color': ['halo_color'],
'icon-halo-color': ['halo_color'],
'text-halo-blur': ['halo_blur'],
Expand Down
10 changes: 6 additions & 4 deletions src/render/draw_fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,21 @@ function drawFillTiles(painter, sourceCache, layer, coords, depthMode, colorMode
const tileMatrix = painter.translatePosMatrix(coord.projMatrix, tile,
layer.paint.get('fill-translate'), layer.paint.get('fill-translate-anchor'));

const emissiveStrength = layer.paint.get('fill-emissive-strength');

if (!isOutline) {
indexBuffer = bucket.indexBuffer;
segments = bucket.segments;
uniformValues = image ?
fillPatternUniformValues(tileMatrix, painter, tile) :
fillUniformValues(tileMatrix);
fillPatternUniformValues(tileMatrix, emissiveStrength, painter, tile) :
fillUniformValues(tileMatrix, emissiveStrength);
} else {
indexBuffer = bucket.indexBuffer2;
segments = bucket.segments2;
const drawingBufferSize = (painter.terrain && painter.terrain.renderingToTexture) ? painter.terrain.drapeBufferSize : [gl.drawingBufferWidth, gl.drawingBufferHeight];
uniformValues = (programName === 'fillOutlinePattern' && image) ?
fillOutlinePatternUniformValues(tileMatrix, painter, tile, drawingBufferSize) :
fillOutlineUniformValues(tileMatrix, drawingBufferSize);
fillOutlinePatternUniformValues(tileMatrix, emissiveStrength, painter, tile, drawingBufferSize) :
fillOutlineUniformValues(tileMatrix, emissiveStrength, drawingBufferSize);
}

painter.uploadCommonUniforms(painter.context, program, coord.toUnwrapped());
Expand Down
7 changes: 2 additions & 5 deletions src/render/painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -957,16 +957,13 @@ class Painter {
const defines = [];

if (this.style && this.style.enable3dLights()) {
// The following check is equivalent to emissive strength default of 1
// for paint_symbol internal property in gl-native, to be removed once
// https://mapbox.atlassian.net/browse/MAPS3D-697 is addressed.
// Additionally lighting was moved to a later stage in draped rendering in
// https://mapbox.atlassian.net/browse/MAPS3D-569.
if (!name.startsWith('symbol') && name !== 'globeRaster' && name !== 'terrainRaster') {
if (name !== 'globeRaster' && name !== 'terrainRaster') {
defines.push('LIGHTING_3D_MODE');
defines.push('LIGHTING_3D_MODE_NO_EMISSION');
}
}

if (this.renderPass === 'shadow') {
if (!this._shadowMapDebug) defines.push('DEPTH_TEXTURE');
} else if (this.shadowRenderer) {
Expand Down
5 changes: 4 additions & 1 deletion src/render/program/circle_program.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type CircleUniformsType = {|
'u_tile_id': Uniform3f,
'u_zoom_transition': Uniform1f,
'u_up_dir': Uniform3f,
'u_emissive_strength': Uniform1f,
|};

export type CircleDefinesType = 'PITCH_WITH_MAP' | 'SCALE_WITH_MAP' | 'PROJECTION_GLOBE_VIEW';
Expand All @@ -43,6 +44,7 @@ const circleUniforms = (context: Context): CircleUniformsType => ({
'u_tile_id': new Uniform3f(context),
'u_zoom_transition': new Uniform1f(context),
'u_up_dir': new Uniform3f(context),
'u_emissive_strength': new Uniform1f(context),
});

const identityMatrix = mat4.create();
Expand Down Expand Up @@ -87,7 +89,8 @@ const circleUniformValues = (
'u_merc_center': [0, 0],
'u_tile_id': [0, 0, 0],
'u_zoom_transition': 0,
'u_up_dir': [0, 0, 0]
'u_up_dir': [0, 0, 0],
'u_emissive_strength': layer.paint.get('circle-emissive-strength')
};

if (isGlobe) {
Expand Down
28 changes: 20 additions & 8 deletions src/render/program/fill_program.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ import type Context from '../../gl/context.js';
import type Tile from '../../source/tile.js';

export type FillUniformsType = {|
'u_matrix': UniformMatrix4f
'u_matrix': UniformMatrix4f,
'u_emissive_strength': Uniform1f
|};

export type FillOutlineUniformsType = {|
'u_matrix': UniformMatrix4f,
'u_emissive_strength': Uniform1f,
'u_world': Uniform2f
|};

export type FillPatternUniformsType = {|
'u_matrix': UniformMatrix4f,
'u_emissive_strength': Uniform1f,
// pattern uniforms:
'u_texsize': Uniform2f,
'u_image': Uniform1i,
Expand All @@ -35,6 +38,7 @@ export type FillPatternUniformsType = {|

export type FillOutlinePatternUniformsType = {|
'u_matrix': UniformMatrix4f,
'u_emissive_strength': Uniform1f,
'u_world': Uniform2f,
// pattern uniforms:
'u_texsize': Uniform2f,
Expand All @@ -45,26 +49,29 @@ export type FillOutlinePatternUniformsType = {|
|};

const fillUniforms = (context: Context): FillUniformsType => ({
'u_matrix': new UniformMatrix4f(context)
'u_matrix': new UniformMatrix4f(context),
'u_emissive_strength': new Uniform1f(context)
});

const fillPatternUniforms = (context: Context): FillPatternUniformsType => ({
'u_matrix': new UniformMatrix4f(context),
'u_emissive_strength': new Uniform1f(context),
'u_image': new Uniform1i(context),
'u_texsize': new Uniform2f(context),
'u_pixel_coord_upper': new Uniform2f(context),
'u_pixel_coord_lower': new Uniform2f(context),
'u_tile_units_to_pixels': new Uniform1f(context)

});

const fillOutlineUniforms = (context: Context): FillOutlineUniformsType => ({
'u_matrix': new UniformMatrix4f(context),
'u_emissive_strength': new Uniform1f(context),
'u_world': new Uniform2f(context)
});

const fillOutlinePatternUniforms = (context: Context): FillOutlinePatternUniformsType => ({
'u_matrix': new UniformMatrix4f(context),
'u_emissive_strength': new Uniform1f(context),
'u_world': new Uniform2f(context),
'u_image': new Uniform1i(context),
'u_texsize': new Uniform2f(context),
Expand All @@ -73,34 +80,39 @@ const fillOutlinePatternUniforms = (context: Context): FillOutlinePatternUniform
'u_tile_units_to_pixels': new Uniform1f(context)
});

const fillUniformValues = (matrix: Float32Array): UniformValues<FillUniformsType> => ({
'u_matrix': matrix
const fillUniformValues = (matrix: Float32Array, emissiveStrength: number): UniformValues<FillUniformsType> => ({
'u_matrix': matrix,
'u_emissive_strength': emissiveStrength
});

const fillPatternUniformValues = (
matrix: Float32Array,
emissiveStrength: number,
painter: Painter,
tile: Tile
): UniformValues<FillPatternUniformsType> => extend(
fillUniformValues(matrix),
fillUniformValues(matrix, emissiveStrength),
patternUniformValues(painter, tile)
);

const fillOutlineUniformValues = (
matrix: Float32Array,
emissiveStrength: number,
drawingBufferSize: [number, number]
): UniformValues<FillOutlineUniformsType> => ({
'u_matrix': matrix,
'u_world': drawingBufferSize
'u_world': drawingBufferSize,
'u_emissive_strength': emissiveStrength
});

const fillOutlinePatternUniformValues = (
matrix: Float32Array,
emissiveStrength: number,
painter: Painter,
tile: Tile,
drawingBufferSize: [number, number]
): UniformValues<FillOutlinePatternUniformsType> => extend(
fillPatternUniformValues(matrix, painter, tile),
fillPatternUniformValues(matrix, emissiveStrength, painter, tile),
{
'u_world': drawingBufferSize
}
Expand Down
9 changes: 6 additions & 3 deletions src/render/program/line_program.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export type LineUniformsType = {|
'u_texsize': Uniform2f,
'u_tile_units_to_pixels': Uniform1f,
'u_alpha_discard_threshold': Uniform1f,
'u_trim_offset': Uniform2f
'u_trim_offset': Uniform2f,
'u_emissive_strength': Uniform1f
|};

export type LinePatternUniformsType = {|
Expand All @@ -54,7 +55,8 @@ const lineUniforms = (context: Context): LineUniformsType => ({
'u_texsize': new Uniform2f(context),
'u_tile_units_to_pixels': new Uniform1f(context),
'u_alpha_discard_threshold': new Uniform1f(context),
'u_trim_offset': new Uniform2f(context)
'u_trim_offset': new Uniform2f(context),
'u_emissive_strength': new Uniform1f(context)
});

const linePatternUniforms = (context: Context): LinePatternUniformsType => ({
Expand Down Expand Up @@ -93,7 +95,8 @@ const lineUniformValues = (
'u_texsize': hasDash(layer) ? tile.lineAtlasTexture.size : [0, 0],
'u_tile_units_to_pixels': calculateTileRatio(tile, painter.transform),
'u_alpha_discard_threshold': 0.0,
'u_trim_offset': trimOffset
'u_trim_offset': trimOffset,
'u_emissive_strength': layer.paint.get('line-emissive-strength')
};
};

Expand Down
12 changes: 0 additions & 12 deletions src/shaders/_prelude_lighting.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,8 @@ float calculate_NdotL(vec3 normal) {
return (clamp(dot(normal, u_lighting_directional_dir), -ext, 1.0) + ext) / (1.0 + ext);
}

#ifdef LIGHTING_3D_MODE_NO_EMISSION

// Note: To be removed once emission is supported in GL-JS
// - https://mapbox.atlassian.net/browse/MAPS3D-697
vec4 apply_lighting_with_emission(vec4 color, float emissive_strength) {
return apply_lighting(color);
}

#else

vec4 apply_lighting_with_emission(vec4 color, float emissive_strength) {
return mix(apply_lighting(color), color, emissive_strength);
}

#endif

#endif
6 changes: 3 additions & 3 deletions src/shaders/circle.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ varying float v_visibility;
#pragma mapbox: define highp vec4 stroke_color
#pragma mapbox: define mediump float stroke_width
#pragma mapbox: define lowp float stroke_opacity
#pragma mapbox: define lowp float emissive_strength

uniform float u_emissive_strength;

void main() {
#pragma mapbox: initialize highp vec4 color
Expand All @@ -18,7 +19,6 @@ void main() {
#pragma mapbox: initialize highp vec4 stroke_color
#pragma mapbox: initialize mediump float stroke_width
#pragma mapbox: initialize lowp float stroke_opacity
#pragma mapbox: initialize lowp float emissive_strength
vec2 extrude = v_data.xy;
float extrude_length = length(extrude);

Expand All @@ -36,7 +36,7 @@ void main() {
vec4 out_color = mix(color * opacity, stroke_color * stroke_opacity, color_t);

#ifdef LIGHTING_3D_MODE
out_color = apply_lighting_with_emission(out_color, emissive_strength);
out_color = apply_lighting_with_emission(out_color, u_emissive_strength);
#endif
#ifdef FOG
out_color = fog_apply_premultiplied(out_color, v_fog_pos);
Expand Down
2 changes: 0 additions & 2 deletions src/shaders/circle.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ varying float v_visibility;
#pragma mapbox: define highp vec4 stroke_color
#pragma mapbox: define mediump float stroke_width
#pragma mapbox: define lowp float stroke_opacity
#pragma mapbox: define lowp float emissive_strength

vec2 calc_offset(vec2 extrusion, float radius, float stroke_width, float view_scale) {
return extrusion * (radius + stroke_width) * u_extrude_scale * view_scale;
Expand Down Expand Up @@ -90,7 +89,6 @@ void main(void) {
#pragma mapbox: initialize highp vec4 stroke_color
#pragma mapbox: initialize mediump float stroke_width
#pragma mapbox: initialize lowp float stroke_opacity
#pragma mapbox: initialize lowp float emissive_strength

// unencode the extrusion vector that we snuck into the a_pos vector
vec2 extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0);
Expand Down
5 changes: 2 additions & 3 deletions src/shaders/fill.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#pragma mapbox: define highp vec4 color
#pragma mapbox: define lowp float opacity

#pragma mapbox: define lowp float emissive_strength
uniform float u_emissive_strength;

void main() {
#pragma mapbox: initialize highp vec4 color
#pragma mapbox: initialize lowp float opacity
#pragma mapbox: initialize lowp float emissive_strength

vec4 out_color = color;

#ifdef LIGHTING_3D_MODE
out_color = apply_lighting_with_emission(out_color, emissive_strength);
out_color = apply_lighting_with_emission(out_color, u_emissive_strength);
#endif
#ifdef FOG
out_color = fog_dither(fog_apply_premultiplied(out_color, v_fog_pos));
Expand Down
2 changes: 0 additions & 2 deletions src/shaders/fill.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ uniform mat4 u_matrix;

#pragma mapbox: define highp vec4 color
#pragma mapbox: define lowp float opacity
#pragma mapbox: define lowp float emissive_strength

void main() {
#pragma mapbox: initialize highp vec4 color
#pragma mapbox: initialize lowp float opacity
#pragma mapbox: initialize lowp float emissive_strength

gl_Position = u_matrix * vec4(a_pos, 0, 1);

Expand Down
4 changes: 3 additions & 1 deletion src/shaders/fill_outline.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
varying vec2 v_pos;

uniform float u_emissive_strength;

#pragma mapbox: define highp vec4 outline_color
#pragma mapbox: define lowp float opacity

Expand All @@ -12,7 +14,7 @@ void main() {
vec4 out_color = outline_color;

#ifdef LIGHTING_3D_MODE
out_color = apply_lighting(out_color);
out_color = apply_lighting_with_emission(out_color, u_emissive_strength);
#endif
#ifdef FOG
out_color = fog_dither(fog_apply_premultiplied(out_color, v_fog_pos));
Expand Down
3 changes: 2 additions & 1 deletion src/shaders/fill_outline_pattern.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

uniform vec2 u_texsize;
uniform sampler2D u_image;
uniform float u_emissive_strength;

varying vec2 v_pos;
varying vec2 v_pos_world;
Expand All @@ -26,7 +27,7 @@ void main() {
vec4 out_color = texture2D(u_image, pos);

#ifdef LIGHTING_3D_MODE
out_color = apply_lighting(out_color);
out_color = apply_lighting_with_emission(out_color, u_emissive_strength);
#endif
#ifdef FOG
out_color = fog_dither(fog_apply_premultiplied(out_color, v_fog_pos));
Expand Down
4 changes: 3 additions & 1 deletion src/shaders/fill_pattern.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ uniform sampler2D u_image;

varying vec2 v_pos;

uniform float u_emissive_strength;

#pragma mapbox: define lowp float opacity
#pragma mapbox: define lowp vec4 pattern

Expand All @@ -19,7 +21,7 @@ void main() {
vec4 out_color = texture2D(u_image, pos);

#ifdef LIGHTING_3D_MODE
out_color = apply_lighting(out_color);
out_color = apply_lighting_with_emission(out_color, u_emissive_strength);
#endif
#ifdef FOG
out_color = fog_dither(fog_apply_premultiplied(out_color, v_fog_pos));
Expand Down
6 changes: 3 additions & 3 deletions src/shaders/line.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ float luminance(vec3 c) {
return (c.r + c.r + c.b + c.g + c.g + c.g) * 0.1667;
}

uniform float u_emissive_strength;

#pragma mapbox: define highp vec4 color
#pragma mapbox: define lowp float floorwidth
#pragma mapbox: define lowp vec4 dash
#pragma mapbox: define lowp float blur
#pragma mapbox: define lowp float opacity
#pragma mapbox: define lowp float emissive_strength

float linearstep(float edge0, float edge1, float x) {
return clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
Expand All @@ -40,7 +41,6 @@ void main() {
#pragma mapbox: initialize lowp vec4 dash
#pragma mapbox: initialize lowp float blur
#pragma mapbox: initialize lowp float opacity
#pragma mapbox: initialize lowp float emissive_strength

// Calculate the distance of the pixel from the line in pixels.
float dist = length(v_normal) * v_width2.s;
Expand Down Expand Up @@ -115,7 +115,7 @@ void main() {
#endif

#ifdef LIGHTING_3D_MODE
out_color = apply_lighting_with_emission(out_color, emissive_strength);
out_color = apply_lighting_with_emission(out_color, u_emissive_strength);
#endif

#ifdef FOG
Expand Down
Loading

0 comments on commit d40fed9

Please sign in to comment.