Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Multi-channel signed distance field generator

License

NotificationsYou must be signed in to change notification settings

Chlumsky/msdfgen

Repository files navigation

This is a utility for generating signed distance fields from vector shapes and font glyphs,which serve as a texture representation that can be used in real-time graphics to efficiently reproduce said shapes.Although it can also be used to generate conventional signed distance fields best known fromthis Valve paperand perpendicular distance fields, its primary purpose is to generate multi-channel distance fields,using a method I have developed. Unlike monochrome distance fields, they have the abilityto reproduce sharp corners almost perfectly by utilizing all three color channels.

The following comparison demonstrates the improvement in image quality.

demo-msdf16demo-sdf16demo-sdf32

Getting started

The project can be used either as a library or as a console program. It is divided into two parts,coreandextensions. The core module has no dependencies and only uses bare C++. It contains allkey data structures and algorithms, which can be accessed through themsdfgen.h header.Extensions contain utilities for loading fonts and SVG files, as well as saving PNG images.Those are exposed by themsdfgen-ext.h header. This module usesFreeType,TinyXML2,libpng,and (optionally)Skia.

Additionally, there is themain.cpp, which wraps the functionality intoa comprehensive standalone console program. To start using the program immediately,there is a Windows binary available for download in the"Releases" section.To use the project as a library, you may install it via thevcpkg package manager as

vcpkg install msdfgen

Or, to build the project from source, you may use the includedCMake script.In its default configuration, it requiresvcpkg as the provider for third-party library dependencies.If you set the environment variableVCPKG_ROOT to the vcpkg directory,the CMake configuration will take care of fetching all required packages from vcpkg.

Console commands

The standalone program is executed as

msdfgen.exe <mode> <input> <options>

where only the input specification is required.

Mode can be one of:

  • sdf – generates a conventional monochrome (true) signed distance field.
  • psdf – generates a monochrome signed perpendicular distance field.
  • msdf (default) – generates a multi-channel signed distance field using my new method.
  • mtsdf – generates a combined multi-channel and true signed distance field in the alpha channel.

The input can be specified as one of:

  • -font <filename.ttf> <character code> – to load a glyph from a font file.Character code can be expressed as either a decimal (63) or hexadecimal (0x3F) Unicode value, or an ASCII characterin single quotes ('?').
  • -svg <filename.svg> – to load an SVG file. Note that only the last vector path in the file will be used.
  • -shapedesc <filename.txt>, -defineshape <definition>, -stdin – to load a text description of the shapefrom either a file, the next argument, or the standard input, respectively. Its syntax is documented further down.

The complete list of available options can be printed with-help.Some of the important ones are:

  • -o <filename> – specifies the output file name. The desired format will be deduced from the extension(png, bmp, tiff, rgba, fl32, txt, bin). Otherwise, use -format.
  • -dimensions <width> <height> – specifies the dimensions of the output distance field (in pixels).
  • -range <range>,-pxrange <range> – specifies the width of the range around the shapebetween the minimum and maximum representable signed distance in shape units or distance field pixels, respectivelly.
  • -scale <scale> – sets the scale used to convert shape units to distance field pixels.
  • -translate <x> <y> – sets the translation of the shape in shape units. Otherwise the origin (0, 0)lies in the bottom left corner.
  • -autoframe – automatically frames the shape to fit the distance field. If the output must be precisely aligned,you should manually position it using -translate and -scale instead.
  • -angle <angle> – specifies the maximum angle to be considered a corner.Can be expressed in radians (3.0) or degrees with D at the end (171.9D).
  • -testrender <filename.png> <width> <height> - tests the generated distance field by using it to render an imageof the original shape into a PNG file with the specified dimensions. Alternatively, -testrendermulti rendersan image without combining the color channels, and may give you an insight in how the multi-channel distance field works.
  • -exportshape <filename.txt> - saves the text description of the shape with edge coloring to the specified file.This can be later edited and used as input through -shapedesc.
  • -printmetrics – prints some useful information about the shape's layout.

For example,

msdfgen.exe msdf -font C:\Windows\Fonts\arialbd.ttf 'M' -o msdf.png -dimensions 32 32 -pxrange 4 -autoframe -testrender render.png 1024 1024

will take the glyph capital M from the Arial Bold typeface, generate a 32×32 multi-channel distance fieldwith a 4 pixels wide distance range, store it into msdf.png, and create a test render of the glyph as render.png.

Note: Do not use-autoframe to generate character maps! It is intended as a quick preview only.

Library API

If you choose to use this utility inside your own program, there are a few simple steps you need to performin order to generate a distance field. Please note that all classes and functions are in themsdfgen namespace.

  • Acquire aShape object. You can either load it vialoadGlyph orloadSvgShape, or construct it manually.It consists of closed contours, which in turn consist of edges. An edge is represented by aLinearEdge,QuadraticEdge,orCubicEdge. You can construct them from two endpoints and 0 to 2 Bézier control points.
  • Normalize the shape using itsnormalize method and assign colors to edges if you need a multi-channel SDF.This can be performed automatically using theedgeColoringSimple (or other) heuristic, or manually by setting each edge'scolor member. Keep in mind that at least two color channels must be turned on in each edge.
  • CallgenerateSDF,generatePSDF,generateMSDF, orgenerateMTSDF to generate a distance field into a floating pointBitmap object. This can then be worked with further or saved to a file usingsaveBmp,savePng,saveTiff, etc.
  • You may also render an image from the distance field usingrenderSDF. Consider callingsimulate8biton the distance field beforehand to simulate the standard 8 bits/channel image format.

Example:

#include<msdfgen.h>#include<msdfgen-ext.h>usingnamespacemsdfgen;intmain() {if (FreetypeHandle *ft =initializeFreetype()) {if (FontHandle *font =loadFont(ft,"C:\\Windows\\Fonts\\arialbd.ttf")) {            Shape shape;if (loadGlyph(shape, font,'A', FONT_SCALING_EM_NORMALIZED)) {                shape.normalize();//                      max. angleedgeColoringSimple(shape,3.0);//          output width, height                Bitmap<float,3>msdf(32,32);//                            scale, translation (in em's)                SDFTransformationt(Projection(32.0,Vector2(0.125,0.125)),Range(0.125));generateMSDF(msdf, shape, t);savePng(msdf,"output.png");            }destroyFont(font);        }deinitializeFreetype(ft);    }return0;}

Using a multi-channel distance field

Using a multi-channel distance field generated by this program is similarly simple to how a monochrome distance field is used.The only additional operation is computing themedian of the three channels inside the fragment shader,right after sampling the distance field. This signed distance value can then be used the same way as usual.

The following is an example GLSL fragment shader with anti-aliasing:

invec2 texCoord;outvec4 color;uniformsampler2D msdf;uniformvec4 bgColor;uniformvec4 fgColor;float median(float r,float g,float b) {returnmax(min(r, g),min(max(r, g), b));}void main() {vec3 msd= texture(msdf, texCoord).rgb;float sd= median(msd.r, msd.g, msd.b);float screenPxDistance= screenPxRange()*(sd-0.5);float opacity=clamp(screenPxDistance+0.5,0.0,1.0);    color=mix(bgColor, fgColor, opacity);}

Here,screenPxRange() represents the distance field range in output screen pixels. For example, if the pixel range was set to 2when generating a 32x32 distance field, and it is used to draw a quad that is 72x72 pixels on the screen,it should return 4.5 (because 72/32 * 2 = 4.5).For 2D rendering, this can generally be replaced by a precomputed uniform value.

For rendering in a3D perspective only, where the texture scale varies across the screen,you may want to implement this function with fragment derivatives in the following way.I would suggest precomputingunitRange as a uniform variable instead ofpxRange for better performance.

uniformfloat pxRange;// set to distance field's pixel rangefloat screenPxRange() {vec2 unitRange=vec2(pxRange)/vec2(textureSize(msdf,0));vec2 screenTexSize=vec2(1.0)/fwidth(texCoord);returnmax(0.5*dot(unitRange, screenTexSize),1.0);}

screenPxRange() must never be lower than 1. If it is lower than 2, there is a high probability that the anti-aliasing will failand you may want to re-generate your distance field with a wider range.

Shape description syntax

The text shape description has the following syntax.

  • Each closed contour is enclosed by braces:{ <contour 1> } { <contour 2> }
  • Each point (and control point) is written as two real numbers separated by a comma.
  • Points in a contour are separated with semicolons.
  • The last point of each contour must be equal to the first, or the symbol# can be used, which represents the first point.
  • There can be an edge segment specification between any two points, also separated by semicolons.This can include the edge's color (c,m,y orw) and/or one or two Bézier curve control points inside parentheses.

For example,

{ -1, -1; m; -1, +1; y; +1, +1; m; +1, -1; y; # }

would represent a square with magenta and yellow edges,

{ 0, 1; (+1.6, -0.8; -1.6, -0.8); # }

is a teardrop shape formed by a single cubic Bézier curve.

About

Multi-channel signed distance field generator

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages


[8]ページ先頭

©2009-2025 Movatter.jp