2015-10-20 68 views
7

SCNMaterialProperty.contents的文檔聲明它是一個動畫屬性,實際上我可以在兩種顏色之間執行交叉淡入淡出。不過,我無法在兩張圖像之間交叉淡入淡出。SceneKit - 交叉淡化材質屬性紋理

所以我開始懷疑這是否可能,或者如果我需要爲此創建自定義着色器?


我試過一個隱含的動畫,在這種情況下,它會立即顯示 '後' 的形象:

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]; 
+0

您是否嘗試過在兩種材質之間改變動畫,而不是在相同材質屬性的兩個內容值之間進行更改? – rickster

回答

6

從我自己的測試看,當涉及到紋理值(而不是純色值)時,它看起來不像這個屬性實際上是可動畫的。無論是在SceneKit中的錯誤(即它的目的是爲了動畫,但不起作用),或者這是蘋果文檔中的錯誤(即它不打算成爲可動畫的,但他們說這是錯誤的)。無論哪種方式,您應該file that bug,以便在Apple修復它時收到通知。

(它也不會像這是一個特定tvOS-問題 - 我看到它在OS X以及)

我可以理解爲什麼動畫紋理過渡可能不存在......從一個GL /金屬透視圖,它需要綁定一個額外的紋理單元,並且在過渡期間每個像素(而不是一個)有兩個紋理查找。

我能想到幾個像樣的潛在解決方法:

  1. 使用着色劑。寫GLSL(ISH)片斷,它看起來是這樣的:

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

    把它放在你想使用SCNShaderModifierEntryPointSurface切入點,以動畫的材料。然後使用setValue:forKey:SCNMaterialPropertyotherTextureCABasicAnimation關聯到從0 fadeFactor動畫爲1

  2. 使用更多的東西動畫(如SpriteKit場景)爲您的材料特性的內容,和動畫,爲執行轉變。 (作爲獎勵,當你做這種方式,您可以使用其他的過渡方式。)

+0

非常感謝,完美的作品!我之前曾嘗試通過KVC綁定紋理,但是我錯誤地直接分配了圖像,而沒有使用'SCNMaterialProperty',這在回想起來似乎很愚蠢。我將立即將該雷達存檔。 – alloy

+0

雷達:https://openradar.appspot。com/23283570 – alloy

0

動畫是不會發生,因爲只有當設置爲顏色不是像「內容」屬性設置動畫。你可以閱讀關於內容屬性的蘋果文檔。

+0

我只是再看看文檔,但我找不到適合動畫的圖像。 – alloy

+0

來自文檔: 屬性內容: 指定接收者的內容。這可以是顏色(NSColor/UIColor),圖像(NSImage/CGImageRef),圖層(CALayer),路徑(NSString或NSURL),SpriteKit場景(SKScene)或紋理(SKTexture,編號爲或GLKTextureInfo) 。設置爲顏色時可以設置動畫。 –

+0

呵呵,即使當我搜索「設置爲顏色時的動畫效果」我也無法在此頁面上找到它:https://developer.apple.com/library/prerelease/ios/documentation/SceneKit/Reference/ SCNMaterialProperty_Class/index.html的#// apple_ref/OCC/instp/SCNMaterialProperty /內容。你在哪裏看到這個? – alloy