Skip to content

Commit

Permalink
[text] Add .negate property for msdfgen 1.4.
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored and dmarcos committed Feb 10, 2018
1 parent db8ee9f commit 5f4179d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/components/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ module.exports.Component = registerComponent('text', {
letterSpacing: {type: 'number', default: 0},
// `lineHeight` defaults to font's `lineHeight` value.
lineHeight: {type: 'number'},
// `negate` must be true for fonts generated with older versions of msdfgen (white background).
negate: {type: 'boolean', default: true},
opacity: {type: 'number', default: 1.0},
shader: {default: 'sdf', oneOf: shaders},
side: {default: 'front', oneOf: ['front', 'back', 'double']},
Expand Down Expand Up @@ -160,7 +162,8 @@ module.exports.Component = registerComponent('text', {
map: this.texture,
opacity: data.opacity,
side: parseSide(data.side),
transparent: data.transparent
transparent: data.transparent,
negate: data.negate
};

// Shader has not changed, do an update.
Expand Down
7 changes: 6 additions & 1 deletion src/shaders/msdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports.Shader = registerShader('msdf', {
alphaTest: {type: 'number', is: 'uniform', default: 0.5},
color: {type: 'color', is: 'uniform', default: 'white'},
map: {type: 'map', is: 'uniform'},
negate: {type: 'boolean', is: 'uniform', default: true},
opacity: {type: 'number', is: 'uniform', default: 1.0}
},

Expand Down Expand Up @@ -42,13 +43,17 @@ module.exports.Shader = registerShader('msdf', {
'uniform vec3 color;',
'uniform float opacity;',
'uniform float alphaTest;',
'uniform bool negate;',
'varying vec2 vUV;',

'float median(float r, float g, float b) {',
' return max(min(r, g), min(max(r, g), b));',
'}',
'void main() {',
' vec3 sample = 1.0 - texture2D(map, vUV).rgb;',
' vec3 sample = texture2D(map, vUV).rgb;',
' if (negate) {',
' sample = 1.0 - sample;',
' }',
' float sigDist = median(sample.r, sample.g, sample.b) - 0.5;',
' float alpha = clamp(sigDist/fwidth(sigDist) + 0.5, 0.0, 1.0);',
' float dscale = 0.353505;',
Expand Down
7 changes: 7 additions & 0 deletions tests/components/text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ suite('text', function () {
value = el.getObject3D('text').material.side;
assert.equal(value, THREE.DoubleSide);
});

test('updates material negate', function () {
var value;
el.setAttribute('text', 'negate', false);
value = el.getObject3D('text').material.uniforms.negate.value;
assert.equal(value, 0.0);
});
});

suite('updateFont', function () {
Expand Down

0 comments on commit 5f4179d

Please sign in to comment.