Skip to content

Commit

Permalink
Merge pull request #7730 from jasonbeverage/sdf-labels
Browse files Browse the repository at this point in the history
SDF Text Labels!
  • Loading branch information
lilleyse authored Jul 2, 2019
2 parents 7a4d461 + 07b7d89 commit 32fc04d
Show file tree
Hide file tree
Showing 14 changed files with 791 additions and 49 deletions.
154 changes: 154 additions & 0 deletions Apps/Sandcastle/gallery/Labels SDF.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="description" content="Create and style textual labels.">
<meta name="cesium-sandcastle-labels" content="Showcases">
<title>Cesium Demo</title>
<script type="text/javascript" src="../Sandcastle-header.js"></script>
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
<script type="text/javascript">
require.config({
baseUrl: '../../../Source',
waitSeconds: 60
});
</script>
</head>

<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
<style>
@import url(../templates/bucket.css);

#toolbar {
background: rgba(42, 42, 42, 0.8);
padding: 4px;
border-radius: 4px;
}
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay">
<h1>Loading...</h1>
</div>
<div id="toolbar">
<table>
<tbody>
<tr>
<td>Outline</td>
<td>
<input type="range" min="0.0" max="8.0" step="0.01"
data-bind="value: outlineWidth, valueUpdate: 'input'">
<input type="text" size="5" data-bind="value: outlineWidth">
</td>
</tr>
<tr>
<td>Scale</td>
<td>
<input type="range" min="0.1" max="20.0" step="0.01"
data-bind="value: scale, valueUpdate: 'input'">
<input type="text" size="5" data-bind="value: scale">
</td>
</tr>
<tr>
<td class="header">Outline Color</td>
</tr>
<tr>
<td>Color</td>
<td><select data-bind="options: outlineColors, value: outlineColor"></select></td>
</tr>
</tbody>
</table>
</div>
<script id="cesium_sandcastle_script">
function startup(Cesium) {
'use strict';
//Sandcastle_Begin
function getColor(colorName, alpha) {
var color = Cesium.Color[colorName.toUpperCase()];
return Cesium.Color.fromAlpha(color, parseFloat(alpha));
}

var viewer = new Cesium.Viewer('cesiumContainer');

// The viewModel tracks the state of our mini application.
var viewModel = {
outlineWidth: 1.0,
scale: 1.0,
outlineColor: 'Red',
outlineColors: ['Red', 'Green', 'Blue', 'Yellow', 'Gray', 'Black']
};

// Convert the viewModel members into knockout observables.
Cesium.knockout.track(viewModel);
// Bind the viewModel to the DOM elements of the UI that call for it.
var toolbar = document.getElementById('toolbar');
Cesium.knockout.applyBindings(viewModel, toolbar);

// Create a label collection with two labels
var labels = viewer.scene.primitives.add(new Cesium.LabelCollection());
var label = labels.add({
position: Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222),
text: 'Philadelphia',
fillColor: Cesium.Color.WHITE,
outlineColor: getColor(viewModel.outlineColor, 1.0),
font: '48px Calibri',
outlineWidth: 1.0,
scale: viewModel.scale,
style: Cesium.LabelStyle.FILL_AND_OUTLINE
});

var morgantown = labels.add({
position: Cesium.Cartesian3.fromDegrees(-79.9559, 39.6295),
text: 'Morgantown',
fillColor: Cesium.Color.YELLOW,
font: '24px Calibri',
outlineColor: Cesium.Color.BLUE,
outlineWidth: 2.0,
style: Cesium.LabelStyle.FILL_AND_OUTLINE
});

Cesium.knockout.getObservable(viewModel, 'outlineWidth').subscribe(function(newValue) {
label.outlineWidth = parseFloat(newValue);
});

Cesium.knockout.getObservable(viewModel, 'scale').subscribe(function(newValue) {
label.scale = parseFloat(newValue);
});

Cesium.knockout.getObservable(viewModel, 'outlineColor').subscribe(function(newValue) {
label.outlineColor = getColor(newValue, 1.0);
});

var outlineDelta = 0.1;
var fontDelta = -1.0;
var fontSize = 48.0;
var minFontSize = 1.0;
var maxFontSize = 48.0;

viewer.scene.preUpdate.addEventListener(function(scene, time) {
morgantown.outlineWidth += outlineDelta;
if (morgantown.outlineWidth >= 4.0 || morgantown.outlineWidth <= 0.0) {
outlineDelta *= -1.0;
}

fontSize += fontDelta;
if (fontSize >= maxFontSize || fontSize <= minFontSize) {
fontDelta *= -1.0;
}
morgantown.font = fontSize + 'px Calibri';
});
//Sandcastle_End
Sandcastle.finishedLoading();
}
if (typeof Cesium !== 'undefined') {
startup(Cesium);
} else if (typeof require === 'function') {
require(['Cesium'], startup);
}
</script>
</body>

</html>
Binary file added Apps/Sandcastle/gallery/Labels SDF.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Change Log
==========

### 1.60 - 2019-08-01

##### Additions :tada:
* Reworked label rendering to use signed distance fields (SDF) for crisper text. [#7730](https://github.com/AnalyticalGraphicsInc/cesium/pull/7730)
* Added a [new Sandcastle example](https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/?src=Labels%20SDF.html) to showcase the new SDF labels.
* Added `totalScale` property to `Label` which is the total scale of the label taking into account the label's scale and the relative size of the desired font compared to the generated glyph size.

### 1.59 - 2019-07-01

##### Additions :tada:
Expand Down
8 changes: 8 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,14 @@ http://www.jshint.com/
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. JSHint was forked from the 2010-12-16 edition of JSLint.
### bitmap-sdf

https://github.com/dy/bitmap-sdf

> (c) 2017 Dima Yv. MIT License
>
> Development supported by plot.ly.
### Public domain data from Natural Earth

Free vector and raster map data @ naturalearthdata.com
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/writeTextToCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ define([
//While the height of the letter is correct, we need to adjust
//where we start drawing it so that letters like j and y properly dip
//below the line.

var height = dimensions.height + doublePadding;
var baseline = height - dimensions.ascent + doublePadding;
var baseline = height - dimensions.ascent + padding;
var y = height - baseline + doublePadding;

canvas.width = width;
Expand Down
48 changes: 47 additions & 1 deletion Source/Scene/Billboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ define([
this._mode = SceneMode.SCENE3D;

this._clusterShow = true;
this._outlineColor = Color.clone(defaultValue(options.outlineColor, Color.BLACK));
this._outlineWidth = defaultValue(options.outlineWidth, 0.0);

this._updateClamping();
}
Expand All @@ -216,7 +218,8 @@ define([
var DISTANCE_DISPLAY_CONDITION = Billboard.DISTANCE_DISPLAY_CONDITION = 14;
var DISABLE_DEPTH_DISTANCE = Billboard.DISABLE_DEPTH_DISTANCE = 15;
Billboard.TEXTURE_COORDINATE_BOUNDS = 16;
Billboard.NUMBER_OF_PROPERTIES = 17;
var SDF_INDEX = Billboard.SDF_INDEX = 17;
Billboard.NUMBER_OF_PROPERTIES = 18;

function makeDirty(billboard, propertyChanged) {
var billboardCollection = billboard._billboardCollection;
Expand Down Expand Up @@ -944,6 +947,49 @@ define([
makeDirty(this, SHOW_INDEX);
}
}
},

/**
* The outline color of this Billboard. Effective only for SDF billboards like Label glyphs.
* @memberof Billboard.prototype
* @type {Color}
* @private
*/
outlineColor : {
get : function() {
return this._outlineColor;
},
set : function(value) {
//>>includeStart('debug', pragmas.debug);
if (!defined(value)) {
throw new DeveloperError('value is required.');
}
//>>includeEnd('debug');

var outlineColor = this._outlineColor;
if (!Color.equals(outlineColor, value)) {
Color.clone(value, outlineColor);
makeDirty(this, SDF_INDEX);
}
}
},

/**
* The outline width of this Billboard in pixels. Effective only for SDF billboards like Label glyphs.
* @memberof Billboard.prototype
* @type {Number}
* @private
*/
outlineWidth : {
get : function() {
return this._outlineWidth;
},
set : function(value) {
if (this._outlineWidth !== value) {
this._outlineWidth = value;
makeDirty(this, SDF_INDEX);
}
}
}
});

Expand Down
Loading

0 comments on commit 32fc04d

Please sign in to comment.