为什么我的文字网格不会不断旋转?

时间:2015-01-15 14:21:35

标签: javascript three.js

我需要我的文本网格不断旋转,无论TrackballControls(用鼠标移动)。使用以下代码,我的网格仅在移动鼠标时旋转。有谁能告诉我出了什么问题?

代码:

//Basic Three components 
var scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.z = 500;

//Set camera controls
var controls = new THREE.TrackballControls( camera );
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.keys = [ 65, 83, 68 ];
controls.addEventListener( 'change', render );

//Set the renderer
var renderer = new THREE.WebGLRenderer( { antialias: false } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

window.addEventListener( 'resize', onWindowResize, false );

//Let's add a text
var materialT1 = new THREE.MeshNormalMaterial();
var textGeom = new THREE.TextGeometry( 'Sitescope', {
    font: 'optimer',
    weight: 'normal',
    size: 20                
});

var textMesh = new THREE.Mesh( textGeom, materialT1 );
textMesh.position.set(-40,60,50)
scene.add( textMesh );

function onWindowResize() {

    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize( window.innerWidth, window.innerHeight );
    controls.handleResize();
    render();

}

function animate() {
    requestAnimationFrame( animate );
    controls.update();

}

function render() {
    textMesh.rotation.x += 0.05;
    renderer.render( scene, camera );
} 

animate();
render();

1 个答案:

答案 0 :(得分:1)

操作顺序导致您的问题。试试这个:

function animate() {
    requestAnimationFrame( animate );
    render();
}

function render() {
    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;
    textMesh.rotation.x += 0.05;
    controls.update();
    renderer.render( scene, camera );
}

animate();

同时删除此电话:

controls.addEventListener( 'change', render );

您可以在此处看到fiddle