Gere malha de texto para babylonjs usando o WASM, escrito no AssemblyScript.
Exemplo
Com cdn
https://cdn.jsdelivr.net/gh/ycw/babylon.font@{versioning }/build/babylon.font.js
ou 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 ) ;A assinatura (2) é útil ao usar o Bundler / Dev Server:
// 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) : meça o texto; devolver uma Metrics . Metrics
.advanceWidth.ascender.descender TextMeshBuilder
Construir um construtor de malha de texto
new TextMeshBuilder ( BABYLON , earcut ) ;
// BABYLON: the 'babylonjs' lib
// earcut: the 'earcut' lib.create(options, scene) : Crie malha de texto extrudada.Mesh se uma malha for criada.null Se nenhuma malha for criada, por exemplo, o texto é todos espaços em branco.undefined se o WASM falhou em processar (por exemplo, fora do 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. cair pra trás
var mesh = builder . create ( opt1 , scene )
if ( mesh === undefined ) mesh = builder . create ( opt2 , scene ) Maxgraey - Ajuda com otimizações AssemblyScript/WASM
OpenTypeJS - Leia e escreva fontes OpenType usando JavaScript.
Earcut - a biblioteca de triangulação de polígono JavaScript mais rápida e mais
Babylonjs - Um jogo poderoso, bonito, simples e aberto e o motor de renderização em uma estrutura amigável de JavaScript.
Assemblycript - compila um subconjunto estrito do TypeScript (basicamente JavaScript com tipos) para a montagem da Web usando o Binaryen.
Mit