Three.js Rotate 3D Text by its Center Point

It took me a few hours to solve the issue of how to rotate text generated by three.js and the THREE.TextGeometry function. That is why I am posting the final solution here for others to find when they have the same issue. When you simply create a 3D text model with THREE.TextGeometry and add rotation to it it will rotate on it's default pivot point which will be on the first letter of the text. That means it will not rotate according to a center pivot which is what I needed. I tried several types of three.js functions and mods to get it to work that all failed except one. I eded up creating a new pivot object to contain the text object and rotating that. Here is the complete code followed by an explanation of how it works.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Jafty 3D Gallery</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background: transparent;
padding: 0;
margin: 0;
font-family:sans-serif;
}

#canvas {
margin:10px auto;
width:800px;
height:350px;
margin-top:-44px;
}

</style>
</head>
<body>
<div id="canvas"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r69/three.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/janda_manatee_solid_regular.js"></script>
<script>
// standard global variables
var container, scene, camera, renderer, controls, textMesh2;
//var axis = new THREE.Vector3(0,1,1);

function init() {
// SCENE
scene = new THREE.Scene();

// CAMERA
var SCREEN_WIDTH = 800, SCREEN_HEIGHT = 300;
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
scene.add(camera);
camera.position.set(0,0,1000);
camera.lookAt(scene.position);
renderer = new THREE.WebGLRenderer({antialias:true, alpha:true});
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
container = document.getElementById( 'canvas' );
container.appendChild( renderer.domElement );

// CONTROLS
controls = new THREE.OrbitControls( camera, renderer.domElement );

// LIGHT
var light = new THREE.PointLight(0xffffff);
light.position.set(0,0,1000);
scene.add(light);

// add 3D text beveled and sized
var material2 = new THREE.MeshPhongMaterial({
    color:0xff4444,//default is 0xffffff
    //vertexColors: THREE.FaceColors,//THREE.VertexColors,
    //morphTargets: true,//? doesn't work here
    shading: THREE.FlatShading,//comment out for other default shading type
    //shading:THREE.NoShading,//uncomment for no shading at all.
    wireframe: false,
    wireframeLinewidth: 1,//windows is always 1, but macs/linux will change line width
    //emissive: 0xFF0300,//color of the material, essentially a solid color unaffected by other lighting. Default is black.
    specular: 0xFF0300,//color of shine
    shininess: 65,//default is 30, higher the shinier
    //reflectivity: 100,//not sure what values are, but didn't make diff
    //opacity: 0.4,//0.0 - 1.0
    transparent: true,
});

var textGeom2 = new THREE.TextGeometry( 'Jafty 3D Gallery', {
    size: 120, //text size
    height: 80, //thicknes of text's extrude!
    curveSegments: 3,
    font: 'janda manatee solid',
    weight: 'normal',//normal or bold(both don't always work with all fonts)
    style: 'normal',//normal or italics(both don't always work with all fonts)
    bevelThickness: 12, //how deep bevel goes into text
    bevelSize: 8, //dist of bevel from text outline
    bevelEnabled: true
});
textMesh2 = new THREE.Mesh( textGeom2, material2 );

textGeom2.computeBoundingBox();
var textWidth2 = textGeom2.boundingBox.max.x - textGeom2.boundingBox.min.x;

textMesh2.position.set( -0.5 * textWidth2, 0, 60 );//left-rt/high/far
scene.add( textMesh2 );

pivot = new THREE.Object3D();
pivot.add( textMesh2 );
scene.add( pivot );
}//end init function

function render() {
renderer.render(scene, camera);
}

function animate() {
window.requestAnimationFrame(animate);
render();
pivot.rotation.y += 0.015;
}

init();
animate();
</script>
</body>
</html>

What part of the above code makes it work?

Good question. All goes according to a normal 3D text reduring until after where I added the textMesh2 object to the scene. Look at the line that starts with the word piviot. Here's the functional code that allows us to rotate the text by it's center axis instead of an axis off to the left of center:

Put this in your init function:

pivot = new THREE.Object3D();
pivot.add( textMesh2 );
scene.add( pivot );

Then, place this in your animate or update function:

pivot.rotation.y += 0.015;

What we are doing here is adding a new Object3D to the scene and then adding our textMesh2 object to the pivot object and finally in our update or animate function, we rotate the pivot object containing the text object which rotates on it's center axis now! You can change the no. in pivot.rotation.y += 0.015; to make it rotate faster or slower. Make it a negative no. to rotate the other direction.

Leave a Reply

Your email address will not be published. Required fields are marked *