简体   繁体   English

使用TimelineMax在Three.js中同时补间两个属性

[英]Tween two properties at once in Three.js with TimelineMax

TweenMax doesn't allow you to tween 2 or more properties at once in Three.js on the same tween. TweenMax不允许您在同一补间中的Three.js中一次补间2个或更多属性。 For example you can't tween rotation and position at once. 例如,您无法一次补间rotationposition You can only tween the rotation in one tween or the position but not both. 您只能补间一个补间中的rotationposition而不是两者。

I've managed to do it by pushing 2 tweens in an array and calling tl.insertMultiple(array) . 我已经设法通过在数组中推送2个补间并调用tl.insertMultiple(array)

Unfortunately, for some unknown to me reason, it only plays fine the first time . 不幸的是,对于一些我不知道的原因, 它只是第一次播放正常 After restart() , the tween/timeline is broken/stutters/skips. restart() ,补间/时间线被破坏/断断续续/跳过。

Notice when the color changes to red it's no longer smooth animation. 请注意,当颜色变为红色时,它不再是平滑动画。

jsFiddle Demo jsFiddle演示

/* TWEENMAX ANIMATION STARTS HERE
 p = position
 r = rotation
 t = time
*/

var miroKeyframes = JSON.parse(`[
{"t":"0"},
{"p":{"x":"0.050","y":"0.220"},"r":{"x":"0.246","y":"-0.444","z":"0.014"},"t":"0.29"},
{"p":{"x":"0.010","y":"0.060"},"r":{"x":"0.109","y":"-0.150","z":"0.150"},"t":"1.01"},
{"p":{"x":"0.746","y":"0.738"},"r":{"x":"0.109","y":"-0.050","z":"0.013"},"t":"1.67"},
{"p":{"x":"-0.495","y":"0.804"},"r":{"x":"0.097","y":"-0.040","z":"0.105"},"t":"2.63"},
...
]`);

// Setup a timeline object. Restart on complete.
var tl = new TimelineMax({ onComplete:restart }),

tweens = [];


for (var i = 1; i < miroKeyframes.length ; i++) {

  var keyframe = miroKeyframes[i]; //current keyframe
  var dur = keyframe.t - miroKeyframes[i-1].t ; //auto-duration


  tweens.push( TweenMax.to( obj.rotation, dur, { x:keyframe.r.x, y:keyframe.r.y, z:keyframe.r.z, delay:keyframe.t, ease:Sine.easeIn} ));
  tweens.push( TweenMax.to( obj.position, dur, { x:keyframe.p.x*20, y:keyframe.p.y*20, delay:keyframe.t, ease:Sine.easeIn} ));

  //Works with either one of these but not both. It will execute each consequently. I need both at the same time.
  //tl.add( TweenMax.to( obj.position, dur, { x:keyframe.p.x, y:keyframe.p.y, ease:Sine.easeIn } ));
  //tl.add( TweenMax.to( obj.rotation, dur, { x:keyframe.p.x, y:keyframe.p.y, ease:Sine.easeIn } ));

}

tl.insertMultiple(tweens);

Please let me know how to fix this and what's going on. 请让我知道如何解决这个问题以及发生了什么。 I don't want to use 2 objects - 1 for rotation and 1 for position. 我不想使用2个对象 - 1个用于旋转,1个用于位置。

The solution came from the GSAP forums by PointC and Dipscom . 解决方案来自PointCDipscom 的GSAP论坛 Special thanks! 特别感谢!

The first and easier solution is to use labels or position parameters . 第一个也是更简单的解决方案是使用标签或position parameters Check the video tutorial. 查看视频教程。

 tl.to( obj.position, dur, { x:keyframe.p.x*10, y:keyframe.p.y*10, ease:Sine.easeIn }, "label" + i );
 tl.to( obj.rotation, dur, { x:keyframe.r.x, y:keyframe.r.y, ease:Sine.easeIn }, "label" + i );

Working demo 工作演示

The second solution is to use 2 timelines - 1 main time line and 1 mini-timeline to add both properties at the same time: 第二种解决方案是使用2个时间轴 - 1个主时间线和1个迷你时间线同时添加两个属性:

  tl2.to( obj.rotation, dur, { x:keyframe.r.x, y:keyframe.r.y, z:keyframe.r.z, ease:Sine.easeIn}, 0 );
  tl2.to( obj.position, dur, { x:keyframe.p.x*20, y:keyframe.p.y*20,  ease:Sine.easeIn}, 0);

  tl.add(tl2);

Working Demo 工作演示

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM