Three.js - 如何将球从左向右重复移动
Three.js - How to move a Ball from left to right repeatedly
我想将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();
但是我应该如何实现小球的移动呢?
通过在每一帧上更改 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
我想将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();
但是我应该如何实现小球的移动呢?
通过在每一帧上更改 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