Genere la malla de texto para Babillonjs usando WASM, escrito en AssemblyScript.
Ejemplo
Con CDN
https://cdn.jsdelivr.net/gh/ycw/babilon.font@{versionh /build/babylon.font.js
o 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 ) ;La firma (2) es útil cuando se usa servidor Bundler / Dev:
// 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) : mida el texto; devolver una Metrics . Metrics
.advanceWidth.ascender.descender TextMeshBuilder
Construya un constructor de malla de texto
new TextMeshBuilder ( BABYLON , earcut ) ;
// BABYLON: the 'babylonjs' lib
// earcut: the 'earcut' lib.create(options, scene) : cree malla de texto extruida.Mesh si se crea una malla.null Si no se crea una malla, por ejemplo, el texto es todos los espacios en blanco.undefined si WASM no se procesó (por ejemplo, fuera de 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. retroceder
var mesh = builder . create ( opt1 , scene )
if ( mesh === undefined ) mesh = builder . create ( opt2 , scene ) Maxgraey - Ayuda con las optimizaciones de ensamblaje/WASM
OpenTypeJS: lea y escriba fuentes OpenType usando JavaScript.
Earcut: la biblioteca de triangulación de polígono JavaScript más rápida y más pequeña para sus aplicaciones WebGL
Babylonjs: un juego poderoso, hermoso, simple y abierto y el motor de renderizado en un marco de JavaScript amigable.
AssemblyScript: compila un subconjunto estricto de TypeScript (básicamente JavaScript con tipos) a WebAssembly usando binaryen.
MIT