Skip to content

Instantly share code, notes, and snippets.

@tomhoag
Last active June 11, 2017 14:00
Show Gist options
  • Save tomhoag/d025373df039cfc422f6de027da3751c to your computer and use it in GitHub Desktop.
Save tomhoag/d025373df039cfc422f6de027da3751c to your computer and use it in GitHub Desktop.
SceneKit particleSystem.handle Swift3 implementation of Objective-C code @ https://developer.apple.com/documentation/scenekit/scnparticlesystem/1523251-handle
particleSystem.handle(SCNParticleEvent.birth, forProperties [SCNParticleSystem.ParticleProperty.color]) {
(data:UnsafeMutablePointer<UnsafeMutableRawPointer>, dataStride:UnsafeMutablePointer<Int>, indicies:UnsafeMutablePointer<UInt32>?, count:Int) in
for i in 0..<count {
// get an UnsafeMutableRawPointer to the i-th rgba element in the data
let colorsPointer:UnsafeMutableRawPointer = data[0] + dataStride[0] * i
// convert the UnsafeMutableRawPointer to a typed pointer by binding it to a type:
let floatPtr = colorsPointer.bindMemory(to: Float.self, capacity: dataStride[0])
// convert that to a an UnsafeMutableBufferPointer
var rgbaBuffer = UnsafeMutableBufferPointer(start: floatPtr, count: dataStride[0])
// At this point, I could convert the buffer to an Array, but doing so copies the data into the array
// and any changes made in the array are not reflected in the original data.
// about half the time, mess with the red and green components
if(arc4random_uniform(2) == 1) {
rgbaBuffer[0] = rgbaBuffer[1]
rgbaBuffer[1] = 0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment