【问题标题】:Three.js - Make a rotation animation?Three.js - 制作旋转动画?
【发布时间】:2012-01-05 12:54:25
【问题描述】:

我已经构建了这段代码...(javascript)

现在屏幕上有一个红色球体......问题是 如何让它旋转?

var 相机、场景、渲染器、 mouseX = 0, mouseY = 0;

var 几何、材质、网格;

init();

function init() {

// Camera params : 
// field of view, aspect ratio for render output, near and far clipping plane. 
    camera = new THREE.Camera( 75, window.innerWidth / window.innerHeight, 1, 1000 );

// move the camera backwards so we can see stuff! 
// default position is 0,0,0.
camera.position.z = 300;

// the scene contains all the 3D object data
    scene = new THREE.Scene();

// and the CanvasRenderer figures out what the 
// stuff in the scene looks like and draws it!  
    renderer = new THREE.CanvasRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );

// the renderer's canvas domElement is added to the body
    document.body.appendChild( renderer.domElement );

    // creating shapes
makeShapes(); 

// add the mouse move listener
document.addEventListener( 'mousemove', onMouseMove, false );

// render 30 times a second (should also look 
// at requestAnimationFrame) 
setInterval(update,1000/30); 

}

function update(){

updateParticles();

// and render the scene from the perspective of the camera
renderer.render( scene, camera );

}

function makeShapes() { 

    // create a sphere shape        
    geometry = new THREE.SphereGeometry( 50, 16, 16 );

    // give a shape red color
    material = new THREE.MeshLambertMaterial({color: 0xFF1111});    

    // create an object
    mesh = new THREE.Mesh( geometry, material );

    mesh.position.x = 0;

    // add it to the scene
    scene.addObject( mesh );
}



function updateParticles(){

}

// called when the mouse moves
function onMouseMove( event ) {

// store the mouseX and mouseY position 
mouseX = event.clientX;
mouseY = event.clientY;
}

【问题讨论】:

    标签: javascript three.js


    【解决方案1】:

    试试这个:

    var halfWidth = window.innerWidth/2, halfHeight = window.innerHeight/2;
    
    function update(){
       camera.position.x += ( mouseX - camera.position.x ) * 0.05;
       camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
       camera.lookAt( scene.position );
    
       mesh.rotation.y -= 0.005;
    
       renderer.render( scene, camera );
    }
    
    function onMouseMove( event ) {
      mouseX = event.clientX - halfWidth;
      mouseY = event.clientY - halfHeight;
    }
    

    【讨论】:

    • 谢谢老兄!!!这对我帮助很大!我还有一个棘手的问题要问你……现在我在我的场景中设置了 5 个球体……你知道如何让它们可拖动吗?除了threejs.org/io/s 之外,还有关于three.js 的任何文档吗?
    • 关于拖动对象,请查看此示例:mrdoob.github.com/three.js/examples/…。源代码很容易阅读。
    • 再次感谢老兄...文档与我上面发布的链接相同...关于拖动...真可惜...我的电脑,不知何故拒绝运行 webGL。 ..所以我看不到这个例子。到目前为止,我使用的是 CanvasRender 而不是 webGL :-)
    猜你喜欢
    • 1970-01-01
    • 2011-08-29
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多