從文檔爲SKMutableTexture.modifyPixelDataWithBlock
:
紋理被假定字節將被存儲爲緊密包裝32 BPP,8bpc(無符號整數)RGBA像素數據。您提供的顏色成分應該已經乘以alpha值。
所以,雖然你給了void*
,底層數據的形式是一個4x8位流。
你可以操縱這樣的結構,像這樣:
// struct of 4 bytes
struct RGBA {
var r: UInt8
var g: UInt8
var b: UInt8
var a: UInt8
}
let tex = SKMutableTexture(size: CGSize(width: 10, height: 10))
tex.modifyPixelDataWithBlock { voidptr, len in
// convert the void pointer into a pointer to your struct
let rgbaptr = UnsafeMutablePointer<RGBA>(voidptr)
// next, create a collection-like structure from that pointer
// (this second part isn’t necessary but can be nicer to work with)
// note the length you supply to create the buffer is the number of
// RGBA structs, so you need to convert the supplied length accordingly...
let pixels = UnsafeMutableBufferPointer(start: rgbaptr, count: Int(len/sizeof(RGBA))
// now, you can manipulate the pixels buffer like any other mutable collection type
for i in indices(pixels) {
pixels[i].r = 0x00
pixels[i].g = 0xff
pixels[i].b = 0x00
pixels[i].a = 0x20
}
}
剛剛在Xcode中嘗試過使用SpriteKit示例項目,似乎做了正確的事情 - 但我知道_nothing_關於SpriteKit,因此無法保證這是做到這一點的「方式」。 – 2015-02-08 20:42:43
謝謝,這是工作。我現在在屏幕上看到了一個方塊,但我期待每幀調用一次modifyPixelDataWithBlock方法 - 就像片段着色器的一種替代方法,但塊只調用一次。更多挖掘... – Bret 2015-02-08 20:45:51
請注意,您需要傳入RGB部分的預乘alpha值。從[文檔](https://developer.apple。com/library/ios/documentation/SpriteKit/Reference/SKMutableTexture_Ref /#// apple_ref/occ/instm/SKMutableTexture/modifyPixelDataWithBlock :)'假設紋理字節存儲爲緊密堆棧32 bpp,8bpc(無符號整數)RGBA像素數據。您提供的顏色成分應該已經乘以alpha值 – Benzi 2016-05-03 19:45:27