Skip to content

Commit

Permalink
namespace "private" shader variables with _ to reduce collisions wi…
Browse files Browse the repository at this point in the history
…th user-defined styles
  • Loading branch information
bcamper committed May 4, 2019
1 parent a24a7a7 commit a3ab5d4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 64 deletions.
19 changes: 8 additions & 11 deletions src/styles/points/points_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,19 @@ void main (void) {
color.rgb /= max(color.a, 0.001); // un-multiply canvas texture
}
else if (u_point_type == TANGRAM_POINT_TYPE_SHADER) { // shader point
float outline_edge = v_outline_edge;
vec4 outlineColor = v_outline_color;

// Mask of outermost circle, either outline or point boundary
float l = length(v_texcoord); // distance to this fragment from the point center
float outer_alpha = _tangram_antialias(l, 1.);
float fill_alpha = _tangram_antialias(l, 1. - (v_outline_edge * 0.5)) * color.a;
float stroke_alpha = (outer_alpha - _tangram_antialias(l, 1. - v_outline_edge)) * outlineColor.a;
float _d = length(v_texcoord); // distance to this fragment from the point center
float _outer_alpha = _tangram_antialias(_d, 1.);
float _fill_alpha = _tangram_antialias(_d, 1. - (v_outline_edge * 0.5)) * color.a;
float _stroke_alpha = (_outer_alpha - _tangram_antialias(_d, 1. - v_outline_edge)) * v_outline_color.a;

// Apply alpha compositing with stroke 'over' fill.
#ifdef TANGRAM_BLEND_ADD
color.a = stroke_alpha + fill_alpha;
color.rgb = color.rgb * fill_alpha + outlineColor.rgb * stroke_alpha;
color.a = _stroke_alpha + _fill_alpha;
color.rgb = color.rgb * _fill_alpha + v_outline_color.rgb * _stroke_alpha;
#else // TANGRAM_BLEND_OVERLAY (and fallback for not implemented blending modes)
color.a = stroke_alpha + fill_alpha * (1. - stroke_alpha);
color.rgb = mix(color.rgb * fill_alpha, outlineColor.rgb, stroke_alpha) / max(color.a, 0.001); // avoid divide by zero
color.a = _stroke_alpha + _fill_alpha * (1. - _stroke_alpha);
color.rgb = mix(color.rgb * _fill_alpha, v_outline_color.rgb, _stroke_alpha) / max(color.a, 0.001); // avoid divide by zero
#endif
}
#else
Expand Down
63 changes: 31 additions & 32 deletions src/styles/points/points_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -104,44 +104,43 @@ void main() {

if (u_point_type == TANGRAM_POINT_TYPE_SHADER) { // shader point
// use point dimensions for UVs instead (ignore attribute), add antialiasing info for fragment shader
float size = abs(a_shape.x / 128.); // radius in pixels
v_texcoord = sign(a_shape.xy) * (size + 1.) / size;
size += 2.;
v_aa_offset = 2. / size;
float _size = abs(a_shape.x / 128.); // radius in pixels
v_texcoord = sign(a_shape.xy) * (_size + 1.) / _size;
_size += 2.;
v_aa_offset = 2. / _size;
}
#endif

// Position
vec4 position = u_modelView * vec4(a_position.xyz, 1.);

// Apply positioning and scaling in screen space
vec2 shape = a_shape.xy / 256.; // values have an 8-bit fraction
vec2 offset = vec2(a_offset.x, -a_offset.y); // flip y to make it point down

float zoom = clamp(u_map_position.z - u_tile_origin.z, 0., 1.); //fract(u_map_position.z);
float theta = a_shape.z / 4096.;
vec2 _shape = a_shape.xy / 256.; // values have an 8-bit fraction
vec2 _offset = vec2(a_offset.x, -a_offset.y); // flip y to make it point down
float _theta = a_shape.z / 4096.;

#ifdef TANGRAM_CURVED_LABEL
//TODO: potential bug? null is passed in for non-curved labels, otherwise the first offset will be 0
if (a_offsets[0] != 0.){
vec4 angles_scaled = (TANGRAM_PI / 16384.) * a_angles;
vec4 pre_angles_scaled = (TANGRAM_PI / 128.) * a_pre_angles;
vec4 offsets_scaled = (1. / 64.) * a_offsets;

float pre_angle = mix4linear(pre_angles_scaled, zoom);
float angle = mix4linear(angles_scaled, zoom);
float offset_curve = mix4linear(offsets_scaled, zoom);

shape = rotate2D(shape, pre_angle); // rotate in place
shape.x += offset_curve; // offset for curved label segment
shape = rotate2D(shape, angle); // rotate relative to curved label anchor
shape += rotate2D(offset, theta); // offset if specified in the scene file
vec4 _angles_scaled = (TANGRAM_PI / 16384.) * a_angles;
vec4 _pre_angles_scaled = (TANGRAM_PI / 128.) * a_pre_angles;
vec4 _offsets_scaled = (1. / 64.) * a_offsets;

float _zoom = clamp(u_map_position.z - u_tile_origin.z, 0., 1.); //fract(u_map_position.z);
float _pre_angle = mix4linear(_pre_angles_scaled, _zoom);
float _angle = mix4linear(_angles_scaled, _zoom);
float _offset_curve = mix4linear(_offsets_scaled, _zoom);

_shape = rotate2D(_shape, _pre_angle); // rotate in place
_shape.x += _offset_curve; // offset for curved label segment
_shape = rotate2D(_shape, _angle); // rotate relative to curved label anchor
_shape += rotate2D(_offset, _theta); // offset if specified in the scene file
}
else {
shape = rotate2D(shape + offset, theta);
_shape = rotate2D(_shape + _offset, _theta);
}
#else
shape = rotate2D(shape + offset, theta);
_shape = rotate2D(_shape + _offset, _theta);
#endif

// Fade in (if requested) based on time mesh has been visible.
Expand All @@ -154,7 +153,7 @@ void main() {

// World coordinates for 3d procedural textures
v_world_position = u_model * position;
v_world_position.xy += shape * u_meters_per_pixel;
v_world_position.xy += _shape * u_meters_per_pixel;
v_world_position = wrapWorldPosition(v_world_position);

// Modify position before camera projection
Expand All @@ -170,29 +169,29 @@ void main() {
// Apply pixel offset in screen-space
// Multiply by 2 is because screen is 2 units wide Normalized Device Coords (and u_resolution device pixels wide)
// Device pixel ratio adjustment is because shape is in logical pixels
position.xy += shape * position.w * 2. * u_device_pixel_ratio / u_resolution;
position.xy += _shape * position.w * 2. * u_device_pixel_ratio / u_resolution;
#ifdef TANGRAM_HAS_SHADER_POINTS
if (u_point_type == TANGRAM_POINT_TYPE_SHADER) { // shader point
// enlarge by 1px to catch missed MSAA fragments
position.xy += sign(shape) * position.w * u_device_pixel_ratio / u_resolution;
position.xy += sign(_shape) * position.w * u_device_pixel_ratio / u_resolution;
}
#endif

// Snap to pixel grid
// Only applied to fully upright sprites/labels (not shader-drawn points), while panning is not active
#ifdef TANGRAM_HAS_SHADER_POINTS
if (!u_view_panning && (abs(theta) < TANGRAM_EPSILON) && u_point_type != TANGRAM_POINT_TYPE_SHADER) {
if (!u_view_panning && (abs(_theta) < TANGRAM_EPSILON) && u_point_type != TANGRAM_POINT_TYPE_SHADER) {
#else
if (!u_view_panning && (abs(theta) < TANGRAM_EPSILON)) {
if (!u_view_panning && (abs(_theta) < TANGRAM_EPSILON)) {
#endif
vec2 position_fract = fract((((position.xy / position.w) + 1.) * .5) * u_resolution);
vec2 position_snap = position.xy + ((step(0.5, position_fract) - position_fract) * position.w * 2. / u_resolution);
vec2 _position_fract = fract((((position.xy / position.w) + 1.) * .5) * u_resolution);
vec2 _position_snap = position.xy + ((step(0.5, _position_fract) - _position_fract) * position.w * 2. / u_resolution);

// Animate the snapping to smooth the transition and make it less noticeable
#ifdef TANGRAM_VIEW_PAN_SNAP_RATE
position.xy = mix(position.xy, position_snap, clamp(u_view_pan_snap_timer * TANGRAM_VIEW_PAN_SNAP_RATE, 0., 1.));
position.xy = mix(position.xy, _position_snap, clamp(u_view_pan_snap_timer * TANGRAM_VIEW_PAN_SNAP_RATE, 0., 1.));
#else
position.xy = position_snap;
position.xy = _position_snap;
#endif
}

Expand Down
12 changes: 5 additions & 7 deletions src/styles/polygons/polygons_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ void main (void) {

// Apply raster to vertex color
#ifdef TANGRAM_RASTER_TEXTURE_COLOR
{ // enclose in scope to avoid leakage of internal variables
vec4 raster_color = sampleRaster(0);
vec4 _raster_color = sampleRaster(0);

#if defined(TANGRAM_BLEND_OPAQUE) || defined(TANGRAM_BLEND_TRANSLUCENT) || defined(TANGRAM_BLEND_MULTIPLY)
// Raster sources can optionally mask by the alpha channel, which will render with only full or no alpha.
Expand All @@ -64,22 +63,21 @@ void main (void) {
{
#endif
#if defined(TANGRAM_BLEND_TRANSLUCENT) || defined(TANGRAM_BLEND_MULTIPLY)
if (raster_color.a < TANGRAM_EPSILON) {
if (_raster_color.a < TANGRAM_EPSILON) {
discard;
}
#else // TANGRAM_BLEND_OPAQUE
if (raster_color.a < 1. - TANGRAM_EPSILON) {
if (_raster_color.a < 1. - TANGRAM_EPSILON) {
discard;
}
// only allow full alpha in opaque blend mode (avoids artifacts blending w/canvas tile background)
raster_color.a = 1.;
_raster_color.a = 1.;
#endif
}
#endif
#endif

color *= raster_color; // multiplied to tint texture color
}
color *= _raster_color; // multiplied to tint texture color
#endif

// Apply line texture
Expand Down
28 changes: 14 additions & 14 deletions src/styles/polygons/polygons_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -89,40 +89,40 @@ void main() {
vec4 position = vec4(a_position.xy, TANGRAM_POSITION_Z / TANGRAM_HEIGHT_SCALE, 1.); // convert height back to meters

#ifdef TANGRAM_EXTRUDE_LINES
vec2 extrude = a_extrude.xy;
vec2 offset = a_offset.xy;
vec2 _extrude = a_extrude.xy;
vec2 _offset = a_offset.xy;

// Adjust line width based on zoom level, to prevent proxied lines
// from being either too small or too big.
// "Flattens" the zoom between 1-2 to peg it to 1 (keeps lines from
// prematurely shrinking), then interpolate and clamp to 4 (keeps lines
// from becoming too small when far away).
float dz = clamp(u_map_position.z - u_tile_origin.z, 0., 4.);
dz += step(1., dz) * (1. - dz) + mix(0., 2., clamp((dz - 2.) / 2., 0., 1.));
float _dz = clamp(u_map_position.z - u_tile_origin.z, 0., 4.);
_dz += step(1., _dz) * (1. - _dz) + mix(0., 2., clamp((_dz - 2.) / 2., 0., 1.));

// Interpolate line width between zooms
float mdz = (dz - 0.5) * 2.; // zoom from mid-point
extrude -= extrude * TANGRAM_UNPACK_SCALING(TANGRAM_WIDTH_SCALING) * mdz;
float _mdz = (_dz - 0.5) * 2.; // zoom from mid-point
_extrude -= _extrude * TANGRAM_UNPACK_SCALING(TANGRAM_WIDTH_SCALING) * _mdz;

// Interpolate line offset between zooms
// Scales from the larger value to the smaller one
float dwdz = TANGRAM_UNPACK_SCALING(TANGRAM_OFFSET_SCALING);
float sdwdz = sign(step(0., dwdz) - 0.5); // sign indicates "direction" of scaling
offset -= offset * abs(dwdz) * ((1.-step(0., sdwdz)) - (dz * -sdwdz)); // scale "up" or "down"
float _dwdz = TANGRAM_UNPACK_SCALING(TANGRAM_OFFSET_SCALING);
float _sdwdz = sign(step(0., _dwdz) - 0.5); // sign indicates "direction" of scaling
_offset -= _offset * abs(_dwdz) * ((1.-step(0., _sdwdz)) - (_dz * -_sdwdz)); // scale "up" or "down"

// Scale line width and offset to be consistent in screen space
float ssz = exp2(-dz - (u_tile_origin.z - u_tile_origin.w));
extrude *= ssz;
offset *= ssz;
float _ssz = exp2(-_dz - (u_tile_origin.z - u_tile_origin.w));
_extrude *= _ssz;
_offset *= _ssz;

// Modify line width before extrusion
#ifdef TANGRAM_BLOCK_WIDTH
float width = 1.;
#pragma tangram: width
extrude *= width;
_extrude *= width;
#endif

position.xy += extrude + offset;
position.xy += _extrude + _offset;
#endif

// World coordinates for 3d procedural textures
Expand Down

0 comments on commit a3ab5d4

Please sign in to comment.