javascript - Three.js - 制作旋转动画?

标签 javascript three.js

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

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

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

var 几何、 Material 、网格;

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;
}

最佳答案

试试这个:

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;
}

关于javascript - Three.js - 制作旋转动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255619/

相关文章:

javascript - 如何消除 NetBeans 中绿色 Javascript 突出显示?

javascript - 使用 Three.js 将图像映射到球体上

javascript - 在 Three.js 中加载随机纹理

javascript - 将 .obj 转换为 .js 文件

javascript - 从 HTML 表中删除行

javascript - jQuery 将事件类从 a 更改为 li

javascript - 未捕获的类型错误 : undefined is not a function 'appendTo'

javascript - 工具提示被 POPUP WIndow 阻止

javascript - Threejs Canvas 不会调整到标签 div

javascript - 基于键盘trijs改变物体旋转动画方向