反转 Tween.js 动画的效果

Reverse the effects of a Tween.js animation

我正在尝试使用 tween.js.

为 three.js 块设置动画,使其在动画结束时 returns 回到其原始位置

有没有办法 tween.js 只使用一个补间

我已经完成了如下所示的工作:

var position = {x: -200, y: 150, width: 1, height: 1, depth: 1, rotx: -0.5, roty: 0.7, rotz: 0.9};
var target = {x: 200, y: -100, width: 0.4, height: 3, depth: 8, rotx: 0.3, roty: -0.4, rotz: -0.6};
var position2 = {x: -200, y: 150, width: 1, height: 1, depth: 1, rotx: -0.5, roty: 0.7, rotz: 0.9};

var mesh = new THREE.Mesh(
  new THREE.CubeGeometry(190, 45, 30),
  new THREE.MeshBasicMaterial({color: 0x444444}),
  0
);
mesh.position.set(position.x, position.y, 0);
mesh.rotation.set(position.rotx, position.roty, position.rotz);
scene.add(mesh);

var t1 = new TWEEN.Tween(position).to(target, 2000);
t1.onUpdate(function() {
  mesh.position.set(position.x, position.y, 0);
  mesh.scale.set(position.width, position.height, position.depth);
  mesh.rotation.set(position.rotx, position.roty, position.rotz);
});
t1.easing(TWEEN.Easing.Quadratic.Out);
t1.onComplete(function() {t2.start();});

var t2 = new TWEEN.Tween(target).to(position2, 2000);
t2.onUpdate(function() {
  mesh.position.set(target.x, target.y, 0);
  mesh.scale.set(target.width, target.height, target.depth);
  mesh.rotation.set(target.rotx, target.roty, target.rotz);
});
t2.easing(TWEEN.Easing.Quadratic.In);

t1.start();

我的动画函数中有补间更新:

function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  mesh.__dirtyPosition = true;
  mesh.__dirtyRotation = true;
  TWEEN.update();
}
animate();

这正如我所期望的那样工作,但显然效率很低,而且很难变通。

我们将不胜感激。

x, y, z 属性重命名为 width, height, depthrotx, roty, rotz 会使事情变得过于复杂。这仅意味着您必须在执行 scale.x = position.widthrotation.x = position.rotx 时手动转换这些属性 onUpdate。我建议您保留 x, y, z,以避免这些重复分配。

// We set our start and target pos using the THREE.js "x, y, z" nomenclature
var startPos = {x: -200, y: 150, z: 0};
var targetPos = {x: 200, y: -100, z: 0};

// Scale also is defined in "x, y, z"
var startScale = {x: 1, y: 1, z: 1};
var targetScale = {x: 0.4, y: 3, z: 8};

// Rotation also has "x, y, z" degrees in Euler angles
var startRot = {x: -0.5, y: 0.7, z: 0.9};
var targetRot = {x: 0.3, y: -0.4, z: -0.6};

// Standard mesh setup
var mesh = new THREE.Mesh(
  new THREE.CubeGeometry(190, 45, 30),
  new THREE.MeshBasicMaterial({color: 0x444444})
);
mesh.position.copy(startPos);
mesh.rotation.copy(startRot);
scene.add(mesh);

// Create shortcuts for shorter easing names
var QuadOut = TWEEN.Easing.Quadratic.Out;
var QuadIn = TWEEN.Easing.Quadratic.In;

// Create one tween for position
// Notice that you can chain the animation 
// back to startPos by doing double ".to().to()""
var t1 = new TWEEN.Tween(mesh.position)
    .to(targetPos, 2000, QuadOut)
    .to(startPos, 2000, QuadIn);

// Second, we tween the mesh's rotation
var t2 = new TWEEN.Tween(mesh.rotation)
    .to(targetRot, 2000, QuadOut)
    .to(startRot, 2000, QuadIn);

// Third, we tween the mesh's scale
var t3 = new TWEEN.Tween(mesh.scale)
    .to(targetScale, 2000, QuadOut)
    .to(startScale, 2000, QuadIn);

t1.start();
t2.start();
t3.start();

最后,在 animate() 期间,您不再需要更改 __dirtyPosition 或任何其他内容,因为补间正在直接更新网格的属性。

function animate() {
    requestAnimationFrame(animate);
    TWEEN.update();
    renderer.render(scene, camera);
}
animate();