You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebGL顶点着色器实现翻页效果时出现纹理伪影求助

Fixing Texture Artifacts in WebGL Page Flip Shader

Hey there! Let's break down why adding that curve adjustment is causing those annoying texture artifacts—even with mipmapping enabled. Here are targeted fixes and debugging steps to get your page flip looking smooth again:

1. Fix Mipmap Derivative Mismatch

The core issue is likely incorrect texture coordinate derivatives. When you adjust rotation based on aTextureCoord.y, you’re creating a non-linear transformation of your UVs. The default texture2D function uses screen-space derivatives to pick the right mipmap level, but these derivatives don’t account for your custom y-dependent rotation. This means it’s selecting the wrong mipmap, leading to aliasing or blurry artifacts.

To fix this:

  • In your fragment shader, calculate derivatives of your transformed texture coordinates explicitly with dFdx and dFdy, then use texture2DGrad (if your WebGL setup supports the EXT_shader_texture_lod extension) to sample with these custom gradients. Example:
    // After calculating your rotated texture coordinate (vTexCoord)
    vec2 dx = dFdx(vTexCoord);
    vec2 dy = dFdy(vTexCoord);
    vec4 color = texture2DGrad(uTexture, vTexCoord, dx, dy);
    
  • Alternatively, pass both the original and rotated UVs from your vertex shader, so you can compute gradients relative to the original texture space instead of screen space.

2. Tweak Texture Filtering & Wrapping

Even with mipmapping, your texture settings might not handle the distortion well:

  • Double-check you generated mipmaps after uploading texture data (this is easy to forget!):
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, imageData);
    gl.generateMipmap(gl.TEXTURE_2D); // Critical step
    
  • Set texture wrapping to CLAMP_TO_EDGE to prevent edge repetition when UVs go outside the [0,1] range during rotation:
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
    
  • Use LINEAR_MIPMAP_LINEAR for the minification filter if you haven’t already—it gives the smoothest mipmap blending:
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
    

3. Smooth Out the Rotation Curve

Your current curve adjustment might create an abrupt jump in rotation speed across the y-axis, causing extreme texture stretching in the bottom area. Instead of adding curve directly to ease_out, tie rotation speed to distance from a pivot point (like the bottom of the page) for more natural physics:

// Define pivot at the bottom of the page (y=1.0)
float pivotY = 1.0;
// Calculate distance from each pixel to the pivot
float distanceFromPivot = abs(aTextureCoord.y - pivotY);
// Smoothly adjust rotation step based on pivot distance
float adjustedStep = uAnimationStep + (curve * distanceFromPivot);
// Compute rotation with this gradual adjustment
float y_rot = mix(adjustedStep, ease_out, aTextureCoord.x) * -PI;

This makes rotation speed increase gradually toward the bottom, reducing sudden texture distortion that causes artifacts.

4. Boost Shader Precision

Low precision in fragment shaders can amplify artifacts from non-linear transformations. Try upgrading the precision declaration at the top of your fragment shader:

precision highp float; // Replace mediump if you were using it

This gives your texture coordinate calculations more precision, cutting down on rounding errors that lead to visual glitches.

5. Verify Texture Coordinate Transformation

Double-check that you’re applying the rotation correctly to your texture coordinates. If you’re calculating UVs manually after rotation, ensure your rotation matrix uses the correct origin point (like the edge of the page, not the center). A small mistake here can cause skewed sampling and artifacts.

内容的提问来源于stack exchange,提问作者cboe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 12:38:12