Package | starling.styles |
Class | public class DistanceFieldStyle |
Inheritance | DistanceFieldStyle MeshStyle EventDispatcher Object |
Signed distance field rendering allows bitmap fonts and other single colored shapes to be drawn without jagged edges, even at high magnifications. The technique was introduced in the SIGGRAPH paper Improved Alpha-Tested Magnification for Vector Textures and Special Effects by Valve Software.
While bitmap fonts are a great solution to render text in a GPU-friendly way, they don't scale well. For best results, one has to embed the font in all the sizes used within the app. The distance field style solves this issue: instead of providing a standard black and white image of the font, it uses a signed distance field texture as its input (a texture that encodes, for each pixel, the distance to the closest edge of a vector shape). With this data, the shape can be rendered smoothly at almost any scale.
Here are some tools that support creation of such distance field textures:
Another advantage of this rendering technique: it supports very efficient rendering of some popular filter effects, in just one pass, directly on the GPU. You can add an outline around the shape, let it glow in an arbitrary color, or add a drop shadow.
The type of effect currently used is called the 'mode'. Meshes with the same mode will be batched together on rendering.
Property | Defined By | ||
---|---|---|---|
alpha : Number The alpha value with which the inner area (what's rendered in 'basic' mode) is drawn. | DistanceFieldStyle | ||
color : uint Changes the color of all vertices to the same value. | MeshStyle | ||
mode : String The current render mode. | DistanceFieldStyle | ||
outerAlphaEnd : Number The alpha value on the outer side of the outer area's gradient. | DistanceFieldStyle | ||
outerAlphaStart : Number The alpha value on the inner side of the outer area's gradient. | DistanceFieldStyle | ||
outerColor : uint The color with which the outer area (outline, glow, or drop shadow) will be filled. | DistanceFieldStyle | ||
outerThreshold : Number The threshold that determines where the outer area (outline, glow, or drop shadow)
ends. | DistanceFieldStyle | ||
shadowOffsetX : Number The x-offset of the shadow in points. | DistanceFieldStyle | ||
shadowOffsetY : Number The y-offset of the shadow in points. | DistanceFieldStyle | ||
softness : Number Indicates how soft the transition between inside and outside should be rendered. | DistanceFieldStyle | ||
target : Mesh [read-only] The target the style is currently assigned to. | MeshStyle | ||
texture : Texture The texture that is mapped to the mesh (or null, if there is none). | MeshStyle | ||
textureRepeat : Boolean Indicates if pixels at the edges will be repeated or clamped. | MeshStyle | ||
textureSmoothing : String The smoothing filter that is used for the texture. | MeshStyle | ||
threshold : Number The threshold that will separate the inside from the outside of the shape. | DistanceFieldStyle | ||
type : Class [read-only] The actual class of this style. | MeshStyle | ||
vertexFormat : VertexDataFormat [read-only] The format used to store the vertices. | MeshStyle |
Method | Defined By | ||
---|---|---|---|
DistanceFieldStyle(softness:Number = 0.125, threshold:Number = 0.5) Creates a new distance field style. | DistanceFieldStyle | ||
addEventListener(type:String, listener:Function):void [override] | MeshStyle | ||
batchIndexData(targetStyle:MeshStyle, targetIndexID:int = 0, offset:int = 0, indexID:int = 0, numIndices:int = -1):void Copies the index data of the style's current target to the target of another style. | MeshStyle | ||
batchVertexData(targetStyle:MeshStyle, targetVertexID:int = 0, matrix:Matrix = null, vertexID:int = 0, numVertices:int = -1):void Copies the vertex data of the style's current target to the target of another style. | MeshStyle | ||
canBatchWith(meshStyle:MeshStyle):Boolean Indicates if the current instance can be batched with the given style. | MeshStyle | ||
Creates a clone of this instance. | MeshStyle | ||
Copies all properties of the given style to the current instance (or a subset, if the
classes don't match). | MeshStyle | ||
Creates the effect that does the actual, low-level rendering. | MeshStyle | ||
dispatchEvent(event:Event):void Dispatches an event to all objects that have registered listeners for its type. | EventDispatcher | ||
dispatchEventWith(type:String, bubbles:Boolean = false, data:Object = null):void Dispatches an event with the given parameters to all objects that have registered
listeners for the given type. | EventDispatcher | ||
getTexCoords(vertexID:int, out:Point = null):Point Returns the texture coordinates of the vertex at the specified index. | MeshStyle | ||
getVertexAlpha(vertexID:int):Number Returns the alpha value of the vertex at the specified index. | MeshStyle | ||
getVertexColor(vertexID:int):uint Returns the RGB color of the vertex at the specified index. | MeshStyle | ||
getVertexPosition(vertexID:int, out:Point = null):Point The position of the vertex at the specified index, in the mesh's local coordinate
system. | MeshStyle | ||
hasEventListener(type:String, listener:Function = null):Boolean If called with one argument, figures out if there are any listeners registered for
the given event type. | EventDispatcher | ||
removeEventListener(type:String, listener:Function):void [override] | MeshStyle | ||
removeEventListeners(type:String = null):void Removes all event listeners with a certain type, or all of them if type is null. | EventDispatcher | ||
setTexCoords(vertexID:int, u:Number, v:Number):void Sets the texture coordinates of the vertex at the specified index to the given values. | MeshStyle | ||
setupBasic():void Restores basic render mode, i.e. | DistanceFieldStyle | ||
setupDropShadow(blur:Number = 0.2, offsetX:Number = 2, offsetY:Number = 2, color:uint = 0x0, alpha:Number = 0.5):void Sets up shadow rendering mode. | DistanceFieldStyle | ||
setupGlow(blur:Number = 0.2, color:uint = 0xffff00, alpha:Number = 0.5):void Sets up glow rendering mode. | DistanceFieldStyle | ||
setupOutline(width:Number = 0.25, color:uint = 0x0, alpha:Number = 1.0):void Sets up outline rendering mode. | DistanceFieldStyle | ||
setVertexAlpha(vertexID:int, alpha:Number):void Sets the alpha value of the vertex at the specified index to a certain value. | MeshStyle | ||
setVertexColor(vertexID:int, color:uint):void Sets the RGB color of the vertex at the specified index to a certain value. | MeshStyle | ||
setVertexPosition(vertexID:int, x:Number, y:Number):void | MeshStyle | ||
Updates the settings of the given effect to match the current style. | MeshStyle |
Constant | Defined By | ||
---|---|---|---|
MODE_BASIC : String = basic [static] Basic distance field rendering, without additional effects. | DistanceFieldStyle | ||
MODE_GLOW : String = glow [static] Adds a smooth glow effect around the shape. | DistanceFieldStyle | ||
MODE_OUTLINE : String = outline [static] Adds an outline around the edge of the shape. | DistanceFieldStyle | ||
MODE_SHADOW : String = shadow [static] Adds a drop shadow behind the shape. | DistanceFieldStyle | ||
VERTEX_FORMAT : VertexDataFormat [static] The vertex format expected by this style. | DistanceFieldStyle |
alpha | property |
alpha:Number
The alpha value with which the inner area (what's rendered in 'basic' mode) is drawn.
The default value is 1.0
.
public function get alpha():Number
public function set alpha(value:Number):void
mode | property |
mode:String
The current render mode. It's recommended to use one of the 'setup...'-methods to change the mode, as those provide useful standard settings, as well.
The default value is basic
.
public function get mode():String
public function set mode(value:String):void
outerAlphaEnd | property |
outerAlphaEnd:Number
The alpha value on the outer side of the outer area's gradient. Used for outline, glow, and drop shadow modes.
public function get outerAlphaEnd():Number
public function set outerAlphaEnd(value:Number):void
outerAlphaStart | property |
outerAlphaStart:Number
The alpha value on the inner side of the outer area's gradient. Used for outline, glow, and drop shadow modes.
public function get outerAlphaStart():Number
public function set outerAlphaStart(value:Number):void
outerColor | property |
outerColor:uint
The color with which the outer area (outline, glow, or drop shadow) will be filled. Ignored in 'basic' mode.
public function get outerColor():uint
public function set outerColor(value:uint):void
outerThreshold | property |
outerThreshold:Number
The threshold that determines where the outer area (outline, glow, or drop shadow) ends. Ignored in 'basic' mode.
public function get outerThreshold():Number
public function set outerThreshold(value:Number):void
shadowOffsetX | property |
shadowOffsetX:Number
The x-offset of the shadow in points. Note that certain combinations of offset and blur value can lead the shadow to be cut off at the edges. Reduce blur or offset to counteract.
public function get shadowOffsetX():Number
public function set shadowOffsetX(value:Number):void
shadowOffsetY | property |
shadowOffsetY:Number
The y-offset of the shadow in points. Note that certain combinations of offset and blur value can lead the shadow to be cut off at the edges. Reduce blur or offset to counteract.
public function get shadowOffsetY():Number
public function set shadowOffsetY(value:Number):void
softness | property |
softness:Number
Indicates how soft the transition between inside and outside should be rendered.
A value of '0' will lead to a hard, jagged edge; '1' will be just as blurry as the
actual distance field texture. The recommend value should be 1.0 / spread
(you determine the spread when creating the distance field texture).
The default value is 0.125
.
public function get softness():Number
public function set softness(value:Number):void
threshold | property |
threshold:Number
The threshold that will separate the inside from the outside of the shape. On the distance field texture, '0' means completely outside, '1' completely inside; the actual edge runs along '0.5'.
The default value is 0.5
.
public function get threshold():Number
public function set threshold(value:Number):void
DistanceFieldStyle | () | Constructor |
public function DistanceFieldStyle(softness:Number = 0.125, threshold:Number = 0.5)
Creates a new distance field style.
Parameterssoftness:Number (default = 0.125 ) — adds a soft transition between the inside and the outside.
This should typically be 1.0 divided by the spread used when
creating the distance field texture.
| |
threshold:Number (default = 0.5 ) — the value separating the inside from the outside of the shape.
Range: 0 - 1.
|
setupBasic | () | method |
public function setupBasic():void
Restores basic render mode, i.e. smooth rendering of the shape.
setupDropShadow | () | method |
public function setupDropShadow(blur:Number = 0.2, offsetX:Number = 2, offsetY:Number = 2, color:uint = 0x0, alpha:Number = 0.5):void
Sets up shadow rendering mode. The 'blur' determines the threshold where the drop shadow ends; 'offsetX' and 'offsetY' are expected in points.
Beware that the style can only act within the limits of the mesh's vertices. This means that not all combinations of blur and offset are possible; too high values will appear the shadow to be cut off on the sides. Reduce either blur or offset to compensate.
Parameters
blur:Number (default = 0.2 )
| |
offsetX:Number (default = 2 )
| |
offsetY:Number (default = 2 )
| |
color:uint (default = 0x0 )
| |
alpha:Number (default = 0.5 )
|
setupGlow | () | method |
public function setupGlow(blur:Number = 0.2, color:uint = 0xffff00, alpha:Number = 0.5):void
Sets up glow rendering mode. The 'blur' determines the threshold where the blur ends; 'blur + threshold' must not exceed '1.0'.
Parameters
blur:Number (default = 0.2 )
| |
color:uint (default = 0xffff00 )
| |
alpha:Number (default = 0.5 )
|
setupOutline | () | method |
public function setupOutline(width:Number = 0.25, color:uint = 0x0, alpha:Number = 1.0):void
Sets up outline rendering mode. The 'width' determines the threshold where the outline ends; 'width + threshold' must not exceed '1.0'.
Parameters
width:Number (default = 0.25 )
| |
color:uint (default = 0x0 )
| |
alpha:Number (default = 1.0 )
|
MODE_BASIC | Constant |
public static const MODE_BASIC:String = basic
Basic distance field rendering, without additional effects.
MODE_GLOW | Constant |
public static const MODE_GLOW:String = glow
Adds a smooth glow effect around the shape.
MODE_OUTLINE | Constant |
public static const MODE_OUTLINE:String = outline
Adds an outline around the edge of the shape.
MODE_SHADOW | Constant |
public static const MODE_SHADOW:String = shadow
Adds a drop shadow behind the shape.
VERTEX_FORMAT | Constant |
public static const VERTEX_FORMAT:VertexDataFormat
The vertex format expected by this style.