does anyone know how I could use tween.js library to smoothly scale objects in three.js? Like for instance if I had a 3d cube how could I make it shrink and then have it return back to its normal size all with one smooth animation. I would appreciate any help or examples you could provide. Thanks
How to use Tween.js to scale a 3d object in Three.js
2.3k Views Asked by FestiveHydra235 At
2
There are 2 best solutions below
0
On
Use the following basic example as a code template.
var camera, scene, renderer;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 1;
scene = new THREE.Scene();
var geometry = new THREE.PlaneGeometry( 0.2, 0.2 );
var material = new THREE.MeshBasicMaterial();
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var targetPosition = new THREE.Vector3( 0, 0, 0 );
// create animation
new TWEEN.Tween( mesh.scale )
.to( targetPosition )
.repeat( Infinity )
.yoyo( true )
.easing( TWEEN.Easing.Cubic.InOut )
.start();
}
function animate() {
requestAnimationFrame( animate );
TWEEN.update();
renderer.render( scene, camera );
}
body {
margin: 0;
}
canvas {
display: block;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tweenjs/[email protected]/dist/tween.umd.js"></script>
And something more funny: