随着时间的推移,在 SceneKit 中使用着色器修改器为纹理设置动画会导致纹理抖动

Using shader modifiers to animate texture in SceneKit leads to jittery textures over time

我正在为 iOS.

在 SceneKit 中编写一个场景

我正在尝试使用 sprite sheet 将纹理应用于对象。我使用以下代码遍历 sheet 中的图像:

happyMaterial = [SCNMaterial new];
happyMaterial.diffuse.contents = happyImage;
happyMaterial.diffuse.wrapS = SCNWrapModeRepeat;
happyMaterial.diffuse.wrapT = SCNWrapModeRepeat;
happyMaterial.shaderModifiers = @{ SCNShaderModifierEntryPointGeometry : @"_geometry.texcoords[0] = vec2((_geometry.texcoords[0].x+floor(u_time*30.0))/10.0, (_geometry.texcoords[0].y+floor(u_time*30.0/10.0))/7.0);" };

一切都很好。除了随着时间的推移,纹理开始出现随机抖动,尤其是沿着 x 轴。

有人提到这可能是因为 "floating-point precision issues," 但我不确定如何诊断或解决这个问题。

另外:我不确定如何从着色器代码中记录数据。如果能够查看 "u_time" 之类的变量并确切了解发生了什么,那就太棒了。

肯定是浮点精度问题。您可能应该尝试对 (u_time*30.0) 进行取模,以便它在合理的范围内循环。

如果您想遍历图像,您的纹理坐标必须在短时间内保持不变(例如 1 秒)。

u_timeCACurrentMediaTime()类似,都是以秒为单位的时间。

现在假设您有 N 个纹理。然后 mod(u_time, N) 将每秒增加,从 0N-1 然后再回到 0。如果你将它除以 N 你就得到了你的纹理坐标,你不需要 SCNWrapModeRepeat.

如果您希望图像每 0.04 秒更改一次(每秒 25 次),请使用 mod(25 * u_time, N) / N