THREE.js中的定向位移贴图是什么?

3
我将会翻译以下内容:

我基本上是按照这个Codrops示例来为我的网站添加位移图效果的。然而,我想把它变成一个带有方向的幻灯片,比如点击“下一个”会使位移图向右移动并转换图像,点击“上一个”则相反。以下是着色器代码:

片段着色器:

varying vec2 vUv;

uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D disp;

uniform float dispFactor;
uniform float effectFactor;

void main() {

    vec4 disp = texture2D(disp, vUv);
    vec2 distortedPosition1 = vec2(uv.x + dispFactor * (disp.r*effectFactor), uv.y);
    vec2 distortedPosition2 = vec2(uv.x - (1.0 - dispFactor) * (disp.r*effectFactor), uv.y);

    vec4 _texture1 = texture2D(texture1, distortedPosition1);
    vec4 _texture2 = texture2D(texture2, distortedPosition2);

    vec4 finalTexture = mix(_texture1, _texture2, dispFactor);

    gl_FragColor = finalTexture;
}

目前我只能让方向交替变化,即左右交替。为此,我跟踪正在幻灯片上显示的当前纹理。要进行过渡,我设置另一个纹理统一(如果正在显示texture1,则为texture2,反之亦然),然后在dispFactor之间来回缓动01。因此,如果我不断点击“下一个”,动画将向右滑动、向左滑动、向右滑动、向左滑动,依此类推。

我试图理解这个并想出一种输入方向的方法,但我认为这就是我Three.js知识的极限所在。

1个回答

0

好的,我找到了解决方案。

setInterval(function(){
    if(i == 0){
        TweenMax.to(mat1.uniforms.dispFactor, speedIn, {
            value: 1.0,
            ease: 'Expo.easeInOut',
            onComplete: function () {
                mat1.uniforms.texture1.value = textures[j];
                mat1.uniforms.texture1.needsUpdate = true;
                j = (j < 4) ? ++j : 0;
            }
        });
        i++;
    }else{
        TweenMax.to(mat1.uniforms.dispFactor, speedOut, {
            value: 0.0,
            ease: 'Expo.easeInOut',
            onComplete: function () {
                mat1.uniforms.texture2.value = textures[j];
                mat1.uniforms.texture2.needsUpdate = true;
                j = (j < 4) ? ++j : 0;
            }
        });
        i--;
    }

}, 4000);

我在这个项目中找到了解决方案,我使用他的函数 onComplete:function(){...} 来更新纹理。

https://gist.github.com/MadebyAe/9d5314568cd0f156d74d6c128993c0e0

享受它!! :D


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接