首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扭转Tween.js动画的效果

扭转Tween.js动画的效果
EN

Stack Overflow用户
提问于 2018-08-27 19:41:16
回答 1查看 1.2K关注 0票数 0

我试图用这样的方式动画一个three.js块,当动画结束时,它会返回到原来的位置,使用tween.js。

是否有一种方法可以在tween.js 中实现这一目的,只使用一个

我的工作如下所示:

代码语言:javascript
复制
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();

我在我的动画功能中更新了tweens:

代码语言:javascript
复制
function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  mesh.__dirtyPosition = true;
  mesh.__dirtyRotation = true;
  TWEEN.update();
}
animate();

这是我所期望的,但它显然是非常低效的,而且很难解决。

任何和所有的帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-27 21:47:07

通过将x, y, z属性重新命名为width, height, depthrotx, roty, rotz,事情变得过于复杂了。这只意味着在执行onUpdate和scale.x = position.width时,您必须手动转换这些属性。我建议您保留x, y, z,以避免重复作业。

代码语言:javascript
复制
// 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或其他任何东西,因为tween直接更新网格的属性。

代码语言:javascript
复制
function animate() {
    requestAnimationFrame(animate);
    TWEEN.update();
    renderer.render(scene, camera);
}
animate();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52046009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档