简体   繁体   English

three.js使用颜色过渡(tween.js)为网格设置动画

[英]three.js animate a Mesh with Color transition (tween.js)

I'm getting crazy trying to achieve this. 我试图实现这个目标的过程变得疯狂。 I want to change the color of a mesh(ConvexGeometry) when I hovered, until here I can do it without any problem, I can change the color of the mesh. 我想在我徘徊时改变网格(ConvexGeometry)的颜色,直到这里我可以毫无问题地做到这一点,我可以改变网格的颜色。

The problem comes when I want to make it with a color transition/interpolation from a to b, right now I'm using tween.js but its not working. 当我想从a到b进行颜色转换/插值时,问题出现了,现在我正在使用tween.js,但它不起作用。 I don't know if the mesh support a material color transition, or the problem is other...I would appreciate any help on this. 我不知道网格是否支持材质颜色过渡,或问题是其他...我会很感激任何帮助。

I can´t find any example doing this...only this similar approach . 我找不到这样做的任何例子...只有这种类似的方法

In any case, when I hovered the object I'm doing the follow: 在任何情况下,当我徘徊对象时,我正在做以下事情:

var tween = new TWEEN.Tween(INTERSECTED.material.materials[0].color)
            .to({r: 0, g: 25, b: 155}, 5000)
            .easing(TWEEN.Easing.Quartic.In)
            .onUpdate(function() {
                INTERSECTED.material.materials[0].color.r = this.r;
                INTERSECTED.material.materials[0].color.g = this.g;
                INTERSECTED.material.materials[0].color.b = this.b;
              }).start()

Basically you need to do tween.update(time) for each requested animation frame. 基本上,您需要为每个请求的动画帧执行tween.update(time)

I hvae modified an example from threejs.org to demonstrate this: http://jsfiddle.net/up1wg1Lo/2/ 我修改了一个来自threejs.org的例子来证明这一点: http//jsfiddle.net/up1wg1Lo/2/

Note that I add parameter to animte and render so that they can know the tick. 请注意,我将参数添加到animterender以便他们可以知道tick。 Also note the usage of tween.update(time) on line 143 另请注意第143行上tween.update(time)的用法

If you want to optimize performance and be 100% accurate you should do the computation step on your tween in each render frame, But its really not necessary to do so for a color tween usually: 如果要优化性能并保持100%准确,则应在每个渲染帧中对补间执行计算步骤,但通常不需要对补间颜色执行此操作:

This is using gsap, which has never failed me: 这是使用gsap,从来没有让我失望:

  var initial = new THREE.Color(target.material.color.getHex());
  var value = new THREE.Color(value.color.getHex());

  TweenLite.to(initial, 1, {
    r: value.r,
    g: value.g,
    b: value.b,

    onUpdate: function () {
      target.material.color = initial;
    }
  });
}

A simple color tween using TweenLite: 使用TweenLite的简单颜色补间:

var col = new THREE.Color('#ff00ff');
TweenLite.to(mesh.material.color, 1, {
  r: col.r,
  g: col.g,
  b: col.b,
});

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

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