Donnerstag, 4. Mai 2017

subclassing SCNNode and add parameters to the call

class box : SCNNode {

     override init() {
        super.init()
    }
    
    init(x: Float, y: Float) {
        super.init()
        
        // grüner würfel
        let boxmesh = SCNBox(width: 1, height: 1, length:1, chamferRadius: 0 )
        let mat = SCNMaterial()
        mat.diffuse.contents = UIColor(red: 0.3, green: 0.8, blue: 0.3, alpha: 1)
        boxmesh.materials = [mat]
        self.geometry = boxmesh
        self.name = "box"
        self.castsShadow = false
        world.addChildNode(self)
        
        self.position.x = x
        self.position.y = y
    }

    /* Xcode required this */
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}

Dienstag, 6. Dezember 2016

Animate / Fade Intensity

let animation2 = CAKeyframeAnimation()
animation2.keyPath = "geometry.firstMaterial.diffuse.intensity"
animation2.values   =  [2, 0.2, 0.1, 0]
animation2.keyTimes =  [0, 0.5, 0.9, 3]
animation2.duration =  3
animation2.isAdditive = false

self.addAnimation(animation2, forKey: "test2")

Variable im Printtext


Jedesmal muss ich nachgucken. Deshalb schreibe ichs mir hier nieder:

print("Variable: \(variable)")

Mittwoch, 30. November 2016

Scenekit Call Action Every Frame

// ----------------------------
// PER FRAME CODE HERE
// ----------------------------
func renderer(_ aRenderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        // per-frame code here 
        
}


--- snip ---

// Diese zwei Zeilen sind notwendig um einen Handler 
// für jedes Frame zu bekommen
// Ohne playing updated er offensichtlich das Bild nur bei Änderungen...
// Diese Methode geht übrigens voll auf die CPU und die Batterie !!
// und kann gar nicht wirklich empfohlen werden...

scnView.delegate  = self

scnView.isPlaying = true


--- snip ---


SWIFT / SCENEKIT: A SCNNODE removes itself after

// ------------------------------------------------------------------------
// Die SCNNODE entfernt sich nach Zeit selber aus dem Scenegraph
// Ich hoffe SWIFT´s  removeFromParentNode() räumt selber vernünftig auf...
// ------------------------------------------------------------------------
let waitaction   = SCNAction.wait(duration: TimeInterval(<lifetime>))
let removeaction = SCNAction.removeFromParentNode()
let sequence     = SCNAction.sequence([waitaction, removeaction])

<SCNNODE>.runAction(sequence)

Move UV Mapping of a 3D geometry

// Das UV Mapping einer 3D Node skalieren
self.geometry?.firstMaterial?.diffuse.contentsTransform = SCNMatrix4MakeScale(0.1, 0.1, 0)



// Das UV Mapping einer 3D Node verschöben

self.geometry?.firstMaterial?.diffuse.contentsTransform = SCNMatrix4MakeTranslation(0.1, 0.1, 0)

Dienstag, 29. November 2016

Animate UVs / Animate Texture for a SpriteAtlas / Flipbook effekt

let spriteatlasanimation = CAKeyframeAnimation()
        
// We do not directly animate UV´s but a matrix instead that moves the
// UV´s across the texture space
spriteatlasanimation.keyPath = "geometry.firstMaterial.diffuse.contentsTransform"

let pos0 = SCNMatrix4MakeTranslation(0.0, 0.0, 0)
let pos1 = SCNMatrix4MakeTranslation(0.1, 0.1, 0)
let pos2 = SCNMatrix4MakeTranslation(0.2, 0.2, 0)
let pos3 = SCNMatrix4MakeTranslation(0.3, 0.3, 0)
let pos4 = SCNMatrix4MakeTranslation(0.4, 0.4, 0)
let pos5 = SCNMatrix4MakeTranslation(0.5, 0.5, 0)
let pos6 = SCNMatrix4MakeTranslation(0.6, 0.6, 0)
let pos7 = SCNMatrix4MakeTranslation(0.7, 0.7, 0)
let pos8 = SCNMatrix4MakeTranslation(0.8, 0.8, 0)
let pos9 = SCNMatrix4MakeTranslation(0.9, 0.9, 0)

spriteatlasanimation.values   =  [pos0, pos1, pos2, pos3,  pos4,  pos5,  pos6,  pos7,  pos8,  pos9]
spriteatlasanimation.keyTimes =  [0, 0.10.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
spriteatlasanimation.duration0.5
spriteatlasanimation.isAdditive = false
        
// No Interpolation required for Flipbook effect : kCAAnimationDiscrete
spriteatlasanimation.calculationMode = kCAAnimationDiscrete
spriteatlasanimation.repeatCount = .infinity


<SCNNODE>.addAnimation(spriteatlasanimation, forKey: "test3")