Skip to content

Commit

Permalink
Remove attribute-naming magic, improve shader var names
Browse files Browse the repository at this point in the history
  • Loading branch information
anandthakker committed Feb 3, 2017
1 parent 2a7245a commit 52653a5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
8 changes: 4 additions & 4 deletions js/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ const symbolInterfaces = {
layoutVertexArrayType: layoutVertexArrayType,
elementArrayType: elementArrayType,
paintAttributes: [
{property: 'text-color', type: 'Uint8'},
{property: 'text-halo-color', type: 'Uint8'}
{name: 'a_fill_color', property: 'text-color', type: 'Uint8'},
{name: 'a_halo_color', property: 'text-halo-color', type: 'Uint8'}
]
},
icon: {
layoutVertexArrayType: layoutVertexArrayType,
elementArrayType: elementArrayType,
paintAttributes: [
{property: 'icon-color', type: 'Uint8'},
{property: 'icon-halo-color', type: 'Uint8'}
{name: 'a_fill_color', property: 'icon-color', type: 'Uint8'},
{name: 'a_halo_color', property: 'icon-halo-color', type: 'Uint8'}
]
},
collisionBox: {
Expand Down
11 changes: 9 additions & 2 deletions js/data/program_configuration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const assert = require('assert');
const createVertexArrayType = require('./vertex_array_type');
const util = require('../util/util');

Expand Down Expand Up @@ -34,6 +35,7 @@ class ProgramConfiguration {

for (const attributeConfig of attributes) {
const attribute = normalizePaintAttribute(attributeConfig, layer);
assert(/^a_/.test(attribute.name));
const name = attribute.name.slice(2);

if (layer.isPaintValueFeatureConstant(attribute.property)) {
Expand Down Expand Up @@ -210,8 +212,13 @@ function getPaintAttributeValue(attribute, layer, globalProperties, featurePrope
}

function normalizePaintAttribute(attribute, layer) {
const layerTypePrefix = layer.type === 'symbol' ? /text-|icon-/ : `${layer.type}-`;
const name = attribute.property.replace(layerTypePrefix, '').replace(/-/g, '_');
let name = attribute.name;

// by default, construct the shader variable name for paint attribute
// `layertype-some-property` as `some_property`
if (!name) {
name = attribute.property.replace(`${layer.type}-`, '').replace(/-/g, '_');
}
const isColor = layer._paintSpecifications[attribute.property].type === 'color';

return util.extend({
Expand Down
12 changes: 6 additions & 6 deletions shaders/symbol_sdf.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
#pragma mapbox: define lowp vec4 color
#pragma mapbox: define lowp vec4 fill_color
#pragma mapbox: define lowp vec4 halo_color
uniform bool u_is_halo;

uniform sampler2D u_texture;
uniform sampler2D u_fadetexture;
uniform lowp float u_opacity;
uniform lowp float u_buffer;
uniform lowp float u_gamma;
uniform bool u_is_halo;

varying vec2 v_tex;
varying vec2 v_fade_tex;
varying float v_gamma_scale;

void main() {
#pragma mapbox: initialize lowp vec4 color
#pragma mapbox: initialize lowp vec4 fill_color
#pragma mapbox: initialize lowp vec4 halo_color

lowp vec4 color_to_use = color;
lowp vec4 color = fill_color;
if (u_is_halo) {
color_to_use = halo_color;
color = halo_color;
}

lowp float dist = texture2D(u_texture, v_tex).a;
lowp float fade_alpha = texture2D(u_fadetexture, v_fade_tex).a;
lowp float gamma = u_gamma * v_gamma_scale;
lowp float alpha = smoothstep(u_buffer - gamma, u_buffer + gamma, dist) * fade_alpha;

gl_FragColor = color_to_use * (alpha * u_opacity);
gl_FragColor = color * (alpha * u_opacity);

#ifdef OVERDRAW_INSPECTOR
gl_FragColor = vec4(1.0);
Expand Down
4 changes: 2 additions & 2 deletions shaders/symbol_sdf.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ attribute vec2 a_offset;
attribute vec2 a_texture_pos;
attribute vec4 a_data;

#pragma mapbox: define lowp vec4 color
#pragma mapbox: define lowp vec4 fill_color
#pragma mapbox: define lowp vec4 halo_color

// matrix is for the vertex position.
Expand All @@ -26,7 +26,7 @@ varying vec2 v_fade_tex;
varying float v_gamma_scale;

void main() {
#pragma mapbox: initialize lowp vec4 color
#pragma mapbox: initialize lowp vec4 fill_color
#pragma mapbox: initialize lowp vec4 halo_color

vec2 a_tex = a_texture_pos.xy;
Expand Down

0 comments on commit 52653a5

Please sign in to comment.