Generieren Sie ein Textnetz für Babylonjs mit WASM, geschrieben in AssemblyScript.
Beispiel
Mit CDN
https://cdn.jsdelivr.net/gh/ycw/babylon.font@version >/build/babylon.font.js
oder npm
npm i ycw/babylon.font
npm i ycw/babylon.font#{VERSION_TAG}
import * as BABYLON from "babylonjs"
import * as opentype from "opentype.js"
import earcut from "earcut"
import { Compiler , Font , TextMeshBuilder } from "babylon.font" ;
( async function ( ) {
const compiler = await Compiler . Build ( ) ;
const font = await Font . Install ( "a.ttf" , compiler , opentype ) ;
const builder = new TextMeshBuilder ( BABYLON , earcut ) ;
const scene = . . ;
const mesh = builder . create ( { font , text : "foo" } , scene ) ;
} ) ( ) ; Compiler
// (1) auto-probe .wasm url
await Compiler . Build ( ) ;
// (2) specify .wasm url
await Compiler . Build ( wasmUrl ) ;Die Signatur (2) ist bei Verwendung von Bundler / Dev -Server nützlich:
// ex. obtain static asset url for ViteJS/Vite
import wasmUrl from "babylon.font/build/optimized.wasm?url" ;
const compiler = await Compiler . Build ( wasmUrl ) ; Font
await Font . Install ( fontUrl , compiler , opentype ) ;
// fontUrl: font(.otf/.ttf) url
// compiler: a Compiler instance
// opentype: the 'opentype.js' lib.measure(text, fontSize) : Text messen; Metrics zurückgeben. Metrics
.advanceWidth.ascender.descender TextMeshBuilder
Konstruieren Sie einen Text Mesh Builder
new TextMeshBuilder ( BABYLON , earcut ) ;
// BABYLON: the 'babylonjs' lib
// earcut: the 'earcut' lib.create(options, scene) : Erstellen Sie extrudiertes Textnetz.Mesh zurück, wenn ein Netz erstellt wird.null zurück, wenn kein Netz erstellt wird, z. B. Text sind alle Whitespaces.undefined zurück, wenn WASM nicht verarbeitet wurde (z. B. aus MEM) builder . create ( {
font , // Font instance
text , // text
size , // font size; default is 100
ppc , // points per curve, [0, 255], default is 2
eps , // decimation threshold, default is 0.001
// plus `BABYLON.MeshBuilder.CreatePolygon` options
depth ,
sideOrientation ,
faceColors ,
faceUV ,
backUVs ,
frontUVs ,
updatable ,
} , scene ) ;ex. zurückgreifen
var mesh = builder . create ( opt1 , scene )
if ( mesh === undefined ) mesh = builder . create ( opt2 , scene ) MaxGraey - hilft bei Assemblyskript/WASM -Optimierungen
OpenTypeJS - Lesen und Schreiben von Openentyp -Schriftarten mit JavaScript.
Ohrkürzung - Die schnellste und kleinste JavaScript -Polygon -Triangulationsbibliothek für Ihre WebGL -Apps
BABYLONJS - Ein mächtiges, schönes, einfaches und offenes Spiel und die Rendering -Engine in ein freundliches JavaScript -Framework.
AssemblyScript - Kompiliert eine strenge Teilmenge von Typenkripten (im Grunde genommen JavaScript mit Typen) in die WebAssembly mit Binaryen.
MIT