我基本上是按照this Codrops example为我的网站添加置换图效果。但是我想使其带有方向的幻灯片放映,例如单击“下一个”将使位移图向右移动并转换图像,而单击“上一个”则相反。以下是着色器代码:
片段着色器:
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;
}
现在,我只能获得左右交替的方向。为此,我跟踪幻灯片中显示的当前纹理。为了过渡,我设置了另一个纹理均匀(如果正在显示texture2
,则显示texture1
,反之亦然),然后在dispFactor
和0
之间来回切换1
。因此,如果我继续单击“下一步”,则动画将向右,向左,向右,向左滑动等等。
我正在努力解决这个问题,并想出一种输入方向的方法,但是我认为这就是我Three.js知识的最高点。
我找到了解决办法。
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: funtion(){...}
更新纹理。
https://gist.github.com/MadebyAe/9d5314568cd0f156d74d6c128993c0e0
享受吧! :D