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

爷,独闯天下 提交于 2019-12-17 20:39:55

问题


I'm writing a scene in SceneKit for iOS.

I'm trying to apply a texture to an object using a sprite sheet. I iterate through the images in that sheet with this code:

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);" };

All is good. Except over time, the texture starts to get random jitteriness in it, especially along the x-axis.

Someone mentioned it could be because of "floating-point precision issues," but I'm not sure how to diagnose or fix this.

Also: I'm not sure how to log data from the shader code. Would be awesome to be able to look into variables like "u_time" and see exactly what's going on.


回答1:


It's definitely a floating point precision issue. you should probably try to do a modulo on (u_time*30.0) so that it loops within a reasonable range.




回答2:


if you want to iterate over images your texture coordinate must stay the same for a short period of time (1 second for instance).

u_time is similar to CACurrentMediaTime(), it's a time in seconds.

Now let's say you have N textures. Then mod(u_time, N) will increase every second from 0 to N-1 and then go back to 0. If you divide this by N you've got your texture coordinate, and you don't need SCNWrapModeRepeat.

If you want your image to change every 0.04 second (25 times per second), then use mod(25 * u_time, N) / N.



来源:https://stackoverflow.com/questions/28886111/using-shader-modifiers-to-animate-texture-in-scenekit-leads-to-jittery-textures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!