SceneKit - Crossfade material property textures

后端 未结 2 548
我寻月下人不归
我寻月下人不归 2021-02-08 21:33

The documentation for SCNMaterialProperty.contents states that it is an animatable property and indeed I can perform a crossfade between two colors. However I’m unable to crossf

相关标签:
2条回答
  • 2021-02-08 22:21

    Your animation isn't happening because "contents" property is animatable only when set to a color not for image. You can read it on the apple's documentation about contents property.

    0 讨论(0)
  • 2021-02-08 22:25

    From my own testing, it doesn't look like this property is actually animatable when texture values are involved (rather than solid color values). Either that's a bug in SceneKit (i.e. it's intended to be animatable but that's not working) or it's a bug in Apple's docs (i.e. it's not intended to be animatable but they say it is). Either way, you should file that bug so you get notified when Apple fixes it.

    (It also doesn't look like this is a tvOS-specific issue -- I see it on OS X as well.)

    I can understand why animated texture transitions might not be there... from a GL/Metal perspective, that requires binding an extra texture unit and having two texture lookups per pixel (instead of one) during the transition.

    I can think of a couple of decent potential workarounds:

    1. Use a shader modifier. Write a GLSL(ish) snippet that looks something like this:

      uniform sampler2D otherTexture;
      uniform float fadeFactor;
      #pragma body
      vec4 otherTexel = texture2D(otherTexture, _surface.diffuseTexcoord);
      _surface.diffuse = mix(_surface.diffuse, otherTexel, fadeFactor);
      

      Set it on the material you want to animate using the SCNShaderModifierEntryPointSurface entry point. Then use setValue:forKey: to associate a SCNMaterialProperty with the otherTexture and a CABasicAnimation to animate the fadeFactor from 0 to 1.

    2. Use something more animated (like a SpriteKit scene) as your material property contents, and animate that to perform the transition. (As a bonus, when you do it this way, you can use other transition styles.)

    0 讨论(0)
提交回复
热议问题