three.js 2D Text sprite labels

后端 未结 3 1202
萌比男神i
萌比男神i 2020-12-06 04:50

I\'m new to three.js and have some issues belonging 2D Text:

What I want: I want some Labels for eg. the x,y and z-axis. The labels should always lo

相关标签:
3条回答
  • 2020-12-06 05:35

    I've published a module on NPM that does that for you: https://github.com/gamestdio/three-text2d

    It allows you to have an Object3D with rendered text without dealing with canvas manually.

    Example:

    var Text2D = require('three-text2d').Text2D
    
    var text = new Text2D("Hello world!", { align: textAlign.right, font: '30px Arial', fillStyle: '#000000', antialias: true })
    scene.add(text) 
    
    0 讨论(0)
  • 2020-12-06 05:47

    You can use my SpriteText object. See example of using sprites_text.

    Example of code:

    var spriteText = new THREE.SpriteText({ text: 'Hello world!' });
    scene.add( spriteText );
    

    Source.

    0 讨论(0)
  • 2020-12-06 05:51

    Text Sprites are nice, but if you use more then thousand of it it can slow down everything because of the GPU.

    There is a "better" way, create the Sprite as Canvas, here is a function that I use too for it:

        function makeTextSprite( message, parameters )
        {
            if ( parameters === undefined ) parameters = {};
            var fontface = parameters.hasOwnProperty("fontface") ? parameters["fontface"] : "Arial";
            var fontsize = parameters.hasOwnProperty("fontsize") ? parameters["fontsize"] : 18;
            var borderThickness = parameters.hasOwnProperty("borderThickness") ? parameters["borderThickness"] : 4;
            var borderColor = parameters.hasOwnProperty("borderColor") ?parameters["borderColor"] : { r:0, g:0, b:0, a:1.0 };
            var backgroundColor = parameters.hasOwnProperty("backgroundColor") ?parameters["backgroundColor"] : { r:255, g:255, b:255, a:1.0 };
            var textColor = parameters.hasOwnProperty("textColor") ?parameters["textColor"] : { r:0, g:0, b:0, a:1.0 };
    
            var canvas = document.createElement('canvas');
            var context = canvas.getContext('2d');
            context.font = "Bold " + fontsize + "px " + fontface;
            var metrics = context.measureText( message );
            var textWidth = metrics.width;
    
            context.fillStyle   = "rgba(" + backgroundColor.r + "," + backgroundColor.g + "," + backgroundColor.b + "," + backgroundColor.a + ")";
            context.strokeStyle = "rgba(" + borderColor.r + "," + borderColor.g + "," + borderColor.b + "," + borderColor.a + ")";
    
            context.lineWidth = borderThickness;
            roundRect(context, borderThickness/2, borderThickness/2, (textWidth + borderThickness) * 1.1, fontsize * 1.4 + borderThickness, 8);
    
            context.fillStyle = "rgba("+textColor.r+", "+textColor.g+", "+textColor.b+", 1.0)";
            context.fillText( message, borderThickness, fontsize + borderThickness);
    
            var texture = new THREE.Texture(canvas) 
            texture.needsUpdate = true;
    
            var spriteMaterial = new THREE.SpriteMaterial( { map: texture, useScreenCoordinates: false } );
            var sprite = new THREE.Sprite( spriteMaterial );
            sprite.scale.set(0.5 * fontsize, 0.25 * fontsize, 0.75 * fontsize);
            return sprite;  
        }
    
    0 讨论(0)
提交回复
热议问题