SDF-font loading example added
This commit is contained in:
parent
735af12e7f
commit
549dbcf4d8
4 changed files with 226 additions and 0 deletions
BIN
examples/text/font_sdf/AnonymousPro-Bold.ttf
Normal file
BIN
examples/text/font_sdf/AnonymousPro-Bold.ttf
Normal file
Binary file not shown.
94
examples/text/font_sdf/OFL.txt
Normal file
94
examples/text/font_sdf/OFL.txt
Normal file
|
@ -0,0 +1,94 @@
|
|||
Copyright (c) 2009, Mark Simonson (http://www.ms-studio.com, mark@marksimonson.com),
|
||||
with Reserved Font Name Anonymous Pro.
|
||||
|
||||
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
||||
This license is copied below, and is also available with a FAQ at:
|
||||
https://openfontlicense.org
|
||||
|
||||
|
||||
-----------------------------------------------------------
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
-----------------------------------------------------------
|
||||
|
||||
PREAMBLE
|
||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||
development of collaborative font projects, to support the font creation
|
||||
efforts of academic and linguistic communities, and to provide a free and
|
||||
open framework in which fonts may be shared and improved in partnership
|
||||
with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves. The
|
||||
fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply
|
||||
to any document created using the fonts or their derivatives.
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such. This may
|
||||
include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software components as
|
||||
distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||
or substituting -- in part or in whole -- any of the components of the
|
||||
Original Version, by changing formats or by porting the Font Software to a
|
||||
new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical
|
||||
writer or other person who contributed to the Font Software.
|
||||
|
||||
PERMISSION & CONDITIONS
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||
redistribute, and sell modified and unmodified copies of the Font
|
||||
Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components,
|
||||
in Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the corresponding
|
||||
Copyright Holder. This restriction only applies to the primary font name as
|
||||
presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole,
|
||||
must be distributed entirely under this license, and must not be
|
||||
distributed under any other license. The requirement for fonts to
|
||||
remain under this license does not apply to any document created
|
||||
using the Font Software.
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are
|
||||
not met.
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
106
examples/text/font_sdf/main.go
Normal file
106
examples/text/font_sdf/main.go
Normal file
|
@ -0,0 +1,106 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
//go:embed AnonymousPro-Bold.ttf
|
||||
var fileData []byte
|
||||
|
||||
func main() {
|
||||
// Initialization
|
||||
const screenWidth, screenHeight = 800, 450
|
||||
rl.InitWindow(screenWidth, screenHeight, "raylib [text] example - SDF fonts")
|
||||
defer rl.CloseWindow() // Close window and OpenGL context
|
||||
|
||||
// NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
|
||||
|
||||
const msg = "Signed Distance Fields"
|
||||
|
||||
// Default font generation from TTF font
|
||||
fontDefault := rl.Font{BaseSize: 16, CharsCount: 95}
|
||||
defer rl.UnloadFont(fontDefault) // Default font unloading
|
||||
|
||||
// Loading font data from memory data
|
||||
// Parameters > font size: 16, no glyphs array provided (nil), glyphs count: 95 (autogenerate chars array)
|
||||
glyphs := rl.LoadFontData(fileData, 16, nil, 95, rl.FontDefault)
|
||||
fontDefault.Chars = &glyphs[0]
|
||||
|
||||
// Parameters > font size: 16, glyphs padding in image: 4 px, pack method: 0 (default)
|
||||
atlas := rl.GenImageFontAtlas(unsafe.Slice(fontDefault.Chars, fontDefault.CharsCount), unsafe.Slice(&fontDefault.Recs, fontDefault.CharsCount), 16, 4, 0)
|
||||
fontDefault.Texture = rl.LoadTextureFromImage(&atlas)
|
||||
rl.UnloadImage(&atlas)
|
||||
|
||||
// SDF font generation from TTF font
|
||||
fontSDF := rl.Font{BaseSize: 16, CharsCount: 95}
|
||||
defer rl.UnloadFont(fontSDF) // SDF font unloading
|
||||
|
||||
// Parameters > font size: 16, no glyphs array provided (nil), glyphs count: 0 (defaults to 95)
|
||||
glyphsSDF := rl.LoadFontData(fileData, 16, nil, 0, rl.FontSdf)
|
||||
fontSDF.Chars = &glyphsSDF[0]
|
||||
// Parameters > font size: 16, glyphs padding in image: 0 px, pack method: 1 (Skyline algorithm)
|
||||
atlas = rl.GenImageFontAtlas(unsafe.Slice(fontSDF.Chars, fontSDF.CharsCount), unsafe.Slice(&fontSDF.Recs, fontSDF.CharsCount), 16, 0, 1)
|
||||
fontSDF.Texture = rl.LoadTextureFromImage(&atlas)
|
||||
rl.UnloadImage(&atlas)
|
||||
|
||||
// Load SDF required shader (we use default vertex shader)
|
||||
shader := rl.LoadShader("", "sdf.fs")
|
||||
defer rl.UnloadShader(shader) // Unload SDF shader
|
||||
rl.SetTextureFilter(fontSDF.Texture, rl.FilterBilinear) // Required for SDF font
|
||||
|
||||
fontPosition := rl.NewVector2(40, screenHeight/2.0-50)
|
||||
textSize := rl.Vector2Zero()
|
||||
fontSize := float32(16)
|
||||
currentFont := 0 // 0 - fontDefault, 1 - fontSDF
|
||||
|
||||
rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
|
||||
|
||||
// Main game loop
|
||||
for !rl.WindowShouldClose() { // Detect window close button or ESC key
|
||||
fontSize += rl.GetMouseWheelMove() * 8.0
|
||||
|
||||
if fontSize < 6 {
|
||||
fontSize = 6
|
||||
}
|
||||
|
||||
if rl.IsKeyDown(rl.KeySpace) {
|
||||
currentFont = 1
|
||||
textSize = rl.MeasureTextEx(fontSDF, msg, fontSize, 0)
|
||||
} else {
|
||||
currentFont = 0
|
||||
textSize = rl.MeasureTextEx(fontDefault, msg, fontSize, 0)
|
||||
}
|
||||
|
||||
fontPosition.X = float32(rl.GetScreenWidth()/2) - textSize.X/2
|
||||
fontPosition.Y = float32(rl.GetScreenHeight()/2) - textSize.Y/2 + 80
|
||||
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.RayWhite)
|
||||
|
||||
if currentFont == 1 {
|
||||
// NOTE: SDF fonts require a custom SDf shader to compute fragment color
|
||||
rl.BeginShaderMode(shader) // Activate SDF font shader
|
||||
rl.DrawTextEx(fontSDF, msg, fontPosition, fontSize, 0, rl.Black)
|
||||
rl.EndShaderMode() // Activate our default shader for next drawings
|
||||
rl.DrawTexture(fontSDF.Texture, 10, 10, rl.Black)
|
||||
rl.DrawText("SDF!", 320, 20, 80, rl.Red)
|
||||
} else {
|
||||
rl.DrawTextEx(fontDefault, msg, fontPosition, fontSize, 0, rl.Black)
|
||||
rl.DrawTexture(fontDefault.Texture, 10, 10, rl.Black)
|
||||
rl.DrawText("default font", 315, 40, 30, rl.Gray)
|
||||
}
|
||||
|
||||
rl.DrawText("FONT SIZE: 16.0", int32(rl.GetScreenWidth()-240), 20, 20, rl.DarkGray)
|
||||
rl.DrawText(fmt.Sprintf("RENDER SIZE: %02.02f", fontSize), int32(rl.GetScreenWidth()-240), 50, 20, rl.DarkGray)
|
||||
rl.DrawText("Use MOUSE WHEEL to SCALE TEXT!", int32(rl.GetScreenWidth()-240), 90, 10, rl.DarkGray)
|
||||
|
||||
rl.DrawText("HOLD SPACE to USE SDF FONT VERSION!", 340, int32(rl.GetScreenHeight()-30), 20, rl.Maroon)
|
||||
|
||||
rl.EndDrawing()
|
||||
|
||||
}
|
||||
}
|
26
examples/text/font_sdf/sdf.fs
Normal file
26
examples/text/font_sdf/sdf.fs
Normal file
|
@ -0,0 +1,26 @@
|
|||
#version 330
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
in vec2 fragTexCoord;
|
||||
in vec4 fragColor;
|
||||
|
||||
// Input uniform values
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
|
||||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
// NOTE: Add here your custom variables
|
||||
|
||||
void main()
|
||||
{
|
||||
// Texel color fetching from texture sampler
|
||||
// NOTE: Calculate alpha using signed distance field (SDF)
|
||||
float distanceFromOutline = texture(texture0, fragTexCoord).a - 0.5;
|
||||
float distanceChangePerFragment = length(vec2(dFdx(distanceFromOutline), dFdy(distanceFromOutline)));
|
||||
float alpha = smoothstep(-distanceChangePerFragment, distanceChangePerFragment, distanceFromOutline);
|
||||
|
||||
// Calculate final fragment color
|
||||
finalColor = vec4(fragColor.rgb, fragColor.a*alpha);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue