SCNMaterialProperty.contents
的文档声明它是一个动画属性,实际上我可以在两种颜色之间执行交叉淡入淡出。但是我无法在两个图像之间交叉淡入淡出。
所以我开始怀疑这是否可能,或者我是否需要为此创建一个自定义着色器?
我尝试了一个隐式动画,在这种情况下它会立即显示“后”图像:
node.geometry.firstMaterial.diffuse.contents = [UIImage imageNamed:@"before"];
[SCNTransaction begin];
[SCNTransaction setAnimationDuration:5];
node.geometry.firstMaterial.diffuse.contents = [UIImage imageNamed:@"after"];
[SCNTransaction commit];
一个明确的动画,它什么都不做:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"contents"];
animation.fromValue = (__bridge id)[UIImage imageNamed:@"before"].CGImage;
animation.toValue = (__bridge id)[UIImage imageNamed:@"after"].CGImage;
animation.duration = 5;
[node.geometry.firstMaterial.diffuse addAnimation:animation forKey:nil];
以及通过
CALayer
,什么都不做:CALayer *textureLayer = [CALayer layer];
textureLayer.frame = CGRectMake(0, 0, 793, 1006);
textureLayer.contents = (__bridge id)[UIImage imageNamed:@"before"].CGImage;
node.geometry.firstMaterial.diffuse.contents = textureLayer;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"contents"];
animation.fromValue = (__bridge id)[UIImage imageNamed:@"before"].CGImage;
animation.toValue = (__bridge id)[UIImage imageNamed:@"after"].CGImage;
animation.duration = 5;
[textureLayer addAnimation:animation forKey:nil];
最佳答案
从我自己的测试来看,当涉及纹理值(而不是纯色值)时,这个属性实际上并不是可动画的。要么是 SceneKit 中的错误(即它旨在动画化但不起作用),要么它是 Apple 文档中的错误(即它不打算动画化但他们说它是)。无论哪种方式,您都应该 file that bug所以当 Apple 修复它时你会收到通知。
(看起来这也不是 tvOS 特有的问题——我在 OS X 上也看到了。)
我可以理解为什么动画纹理过渡可能不存在......从 GL/Metal 的角度来看,这需要绑定(bind)一个额外的纹理单元并在过渡期间每个像素(而不是一个)有两个纹理查找。
我可以想到几个不错的潜在解决方法:
uniform sampler2D otherTexture;
uniform float fadeFactor;
#pragma body
vec4 otherTexel = texture2D(otherTexture, _surface.diffuseTexcoord);
_surface.diffuse = mix(_surface.diffuse, otherTexel, fadeFactor);
使用
SCNShaderModifierEntryPointSurface
将其设置在您要设置动画的 Material 上入口点。然后使用 setValue:forKey:
关联 SCNMaterialProperty
与 otherTexture
和 CABasicAnimation
为 fadeFactor
设置动画从 0 到 1。关于opengl-es - SceneKit - 交叉淡入淡出 Material 属性纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33235058/