admin管理员组文章数量:1415664
I am new to Tween JS, and trying to make a simple animation of moving to the right using Tween.
Below is my code in the init function ( I am using Three JS):
var geometry = new THREE.CylinderGeometry( 200,200, 200, 4, 0 );
var material = new THREE.MeshPhongMaterial( { ambient: 0xf0f0f0, color: 0x006699, specular: 0x006699, shininess: 60, shading: THREE.FlatShading } );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = 0;
mesh.position.y = 0;
mesh.position.z = 0;
mesh.updateMatrix();
mesh.matrixAutoUpdate = false;
scene.add( mesh );
var tween = new TWEEN.Tween( { x: 0, y: 0 } )
.to( { x: 400 }, 2000 )
.easing( TWEEN.Easing.Elastic.InOut )
.onUpdate( function () {
mesh.position.x = Math.round( this.x );
} ).start();
And my animate function:
requestAnimationFrame(animate);
renderer.render(scene, camera);
TWEEN.update();
update();
The cube is there but the tween is not working. Is there something I miss?
I am new to Tween JS, and trying to make a simple animation of moving to the right using Tween.
Below is my code in the init function ( I am using Three JS):
var geometry = new THREE.CylinderGeometry( 200,200, 200, 4, 0 );
var material = new THREE.MeshPhongMaterial( { ambient: 0xf0f0f0, color: 0x006699, specular: 0x006699, shininess: 60, shading: THREE.FlatShading } );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = 0;
mesh.position.y = 0;
mesh.position.z = 0;
mesh.updateMatrix();
mesh.matrixAutoUpdate = false;
scene.add( mesh );
var tween = new TWEEN.Tween( { x: 0, y: 0 } )
.to( { x: 400 }, 2000 )
.easing( TWEEN.Easing.Elastic.InOut )
.onUpdate( function () {
mesh.position.x = Math.round( this.x );
} ).start();
And my animate function:
requestAnimationFrame(animate);
renderer.render(scene, camera);
TWEEN.update();
update();
The cube is there but the tween is not working. Is there something I miss?
Share Improve this question asked Sep 9, 2014 at 8:30 RickyHalimRickyHalim 2951 gold badge7 silver badges22 bronze badges1 Answer
Reset to default 4The following is what I did to my scene.
Create a separate render() function
Put the TWEEN code into a function that you can call. You want to remove all TWEENs at beginning of this function. I am not entirely sure why, but I learned that from tutorial code.
In TWEEN function, call render function on update.
Call TWEEN.update in your animation through your non-stop animation loop. Note: render() will be called every time you update the TWEEN.
The following is the skeleton code. Check if that could apply to your program:
//TWEEN function
function moveObject( ) {
TWEEN.removeAll();
new TWEEN.Tween( { x: 0, y: 0 } )
.to( { x: 400 }, 2000 )
.easing( TWEEN.Easing.Elastic.InOut )
.onUpdate( render )
.start();
};
//NON-STOP animation loop
function animation(){
requestAnimationFrame(animate);
TWEEN.update();
}
//Render function
function render(){
renderer.render(scene, camera);
}
Hope it helps.
本文标签: javascriptTween JS basics on three JS cubeStack Overflow
版权声明:本文标题:javascript - Tween JS basics on three JS cube - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745216252a2648184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论