Skip to content

Instantly share code, notes, and snippets.

@amitlzkpa
Last active May 16, 2020 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitlzkpa/f4b4af9870830399f49f8ce67920d4af to your computer and use it in GitHub Desktop.
Save amitlzkpa/f4b4af9870830399f49f8ce67920d4af to your computer and use it in GitHub Desktop.
3D texts in threejs scenes
// ref: https://bocoup.com/blog/learning-three-js-with-real-world-challenges-that-have-already-been-solved
function makeTextSprite(message, opts) {
let parameters = opts || {};
let fontface = parameters.fontface || 'Helvetica';
let fontsize = parameters.fontsize || 20;
let canvas = document.createElement('canvas');
let context = canvas.getContext('2d');
context.font = fontsize + "px " + fontface;
context.textAlign = "center";
context.textBaseline = "middle";
// text color
context.fillStyle = 'rgba(255, 255, 255, 1.0)';
context.fillText(message, canvas.width/2, canvas.height/2);
// canvas contents will be used for a texture
let texture = new THREE.Texture(canvas)
texture.minFilter = THREE.LinearFilter;
texture.needsUpdate = true;
let spriteMaterial = new THREE.SpriteMaterial({
map: texture,
useScreenCoordinates: false
});
let sprite = new THREE.Sprite(spriteMaterial);
sprite.scale.set(100, 50, 1.0);
return sprite;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment