Three.js - 如何反复从左向右移动球

时间:2015-09-25 05:24:28

标签: javascript animation three.js rendering

我想将Three.js中的一个球移到右边,然后它会移动到起始位置,然后反复向右移动。

var geometry = new THREE.SphereGeometry( 5, 32, 32);
var material = new THREE.MeshPhongMaterial( {specular: "#fdfb57", 
color: "#d8d613", emissive: "#6b6a0d", side: THREE.DoubleSide} );
var ball = new THREE.Mesh(geometry, material);

var render = function () {
    requestAnimationFrame( render );
    renderer.render(scene, camera);
    renderer.setSize(window.innerWidth, window.innerHeight);

// What programming code I have to put on here? 
};

render();   

但我应该如何实施移动球?

1 个答案:

答案 0 :(得分:2)

通过更改每一帧上的ball.position或使用Tween.js等库来获取您想要移动的两个点之间的运动路径。

对于简单的从左到右反向重复的动画,只需调整位置就可以了。

尝试将其粘贴到avgp.github.io/h2g2three

var geometry = new THREE.SphereGeometry( 5, 32, 32);
var material = new THREE.MeshPhongMaterial( {specular: "#fdfb57", color: "#d8d613", emissive: "#6b6a0d", side: THREE.DoubleSide} );
var ball = new THREE.Mesh(geometry, material);
var dxPerFrame = 1; // how to move in a single frame

scene.add(ball);

onRender = function() {
  ball.position.x += dxPerFrame; // move ball
  if(ball.position.x >  100) dxPerFrame = -1; // if we're too far right, move towards the left
  if(ball.position.x < -100) dxPerFrame =  1; // if we're too far left, move towards the right again
};

// Alt+Return run the code