使用片段着色器映射的纹理中重复的边缘像素

时间:2018-07-07 08:14:08

标签: three.js glsl fragment-shader

我正在尝试使用片段着色器复制笔触。 here is my demo。在动画开始时查看拉伸边缘。

我使用three.js进行了此设置:

material = new THREE.ShaderMaterial( {
    side: THREE.DoubleSide,
    uniforms: {
        time: { type: 'f', value: 0 },
        uvRate: {
            value: new THREE.Vector2(1,3.7) // aspect ratio of image
        },
        texture: {
            value: THREE.ImageUtils.loadTexture('img/stroke.png')
        },
    },
    vertexShader: vertex,
    fragmentShader: fragment
});

plane = new THREE.Mesh(new THREE.PlaneGeometry( 1,1, 1, 1 ),material);

顶点着色器以计算纵横比:

uniform vec2 uvRate1;
void main() {
    vUv1 = uv - 0.5;
    vUv1 *= uvRate.xy;
    vUv1 += 0.5;
    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}`

还有带有一些数学运算功能的片段着色器,可以缩放UV以进行“刷涂”

// math behind this https://www.desmos.com/calculator/8qdmw3a91w
float scale1(float coord,float progress){
    coord = coord/progress;
    if(coord<0.88) {
        final = coord*progress;
    } else{
        final = pow( (3.*coord - 2.4),3.) + 0.75 + coord/8.;
        final *= progress;
    }
    return final;
}


void main() {
    float p = clamp(fract(time/20.) + 0.3, 0.,1.);  // 0.3 -> 1.0
    vec2 newuv = vUv1;
    newuv.x = scale1(vUv1.x,p);

    gl_FragColor = texture2D(texture,newuv);
}

但是我有这种优势:

有人知道这是什么原因吗?以及如何解决?

1 个答案:

答案 0 :(得分:0)

它可能与纹理环绕有关,因为默认情况下它会夹紧到边缘。 (THREE.ClampToEdgeWrapping)

尝试在初始化后更改纹理的wrapS和wrapT属性,类似。

plane.material.uniforms.texture.wrapS =飞机.material.uniforms.texture.wrapT = 3.RepeatWrapping;