Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@croquet/hybrid-msdf-text

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@croquet/hybrid-msdf-text

renders BMFont files in ThreeJS with hybrid MSDF and bitmap renderer

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Hybrid MSDF and bitmap text

Derived from three-bmfont-text but modified to support a hybrid MSDF approach so that text look fine from near and far. That is, when a character is magnified, it looks totally crisp, and when a character is minified, it is anti-aliased.

The package is designed to work best with Croquet Worldcore, but the implementation is generally usable for any Three.js based application.

Loading font is done something like this with load-bmfont:

let path = "./assets/fonts";
let image = `${path}/${name}.png`;

return new Promise((resolve, reject) => {
    loadFont(`${path}/${name}.json`, (err, font) => {
        if (err) throw err;
        let loader = new THREE.TextureLoader();
        loader.load(
            image,
            (tex) => {
                let preprocessor = new MSDFFontPreprocessor();
                let img = new Image(font.common.scaleW, font.common.scaleH);
                let canvas = document.createElement("canvas");
                canvas.width = font.common.scaleW;
                canvas.height = font.common.scaleH;
                let ctx = canvas.getContext("2d");
                ctx.drawImage(tex.image, 0, 0);
                let inBitmap = ctx.getImageData(0, 0, canvas.width, canvas.height);
                let outImage = preprocessor.process(font, inBitmap.data);
                ctx.putImageData(outImage, 0, 0);

                let processedTexture = new THREE.Texture(outImage);
                processedTexture.minFilter = THREE.LinearMipMapLinearFilter;
                processedTexture.magFilter = THREE.LinearFilter;
                processedTexture.generateMipmaps = true;
                processedTexture.anisotropy = 1; // maxAni

                img.src = canvas.toDataURL("image/png");
                img.onload = () => {
                    processedTexture.needsUpdate = true;
                };

                this.fonts[name] = {font, texture: processedTexture};
                resolve(this.fonts[name]);
            },
            null,
            () => {
                reject(new Error(`failed to load font: ${name}`));
            });
    });
});

and updating geometry can be done:

let TextLayout = getTextLayout(THREE);
let layout = new TextLayout({font});
let glyphs = layout.computeGlyphs({font, {x: 0, y: 0, string: "Test", style: "blue"}});
let extent = {width: 600, height: 600};

this.textGeometry.update({font, glyphs});
let bounds = {left: 0, top: 0, bottom: extent.height, right: extent.width};
this.fonts[fontName].material.uniforms.corners.value = new THREE.Vector4(bounds.left, bounds.top, bounds.right, bounds.bottom);

License

MIT, see LICENSE.md.

Keywords

FAQs

Package last updated on 27 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc