Create a flickering/variable SKLightNode in SpriteKit - simulate campfire lighting - ios

I have an animated campfire using an texture atlas in SpriteKit, I am trying to simulate the variable lighting that a fire would produce. I was able to achieve a flicker by varying the falloff by passing in a random number form 0...1.5. It works but is a little too crazy - looking for a suggestion on smoothing it out to be more subtle and realistic - maybe pass an array of set values thru - not sure how I would do that? Or some sort of easing?
func buildCampfire() {
let campfireAtlas = SKTextureAtlas(named: "Campfire")
var fireFrames: [SKTexture] = []
let numImages = campfireAtlas.textureNames.count
for i in 1...numImages {
let fireTextureName = "campfire\(i)"
fireFrames.append(campfireAtlas.textureNamed(fireTextureName))
}
animatedCampfire = fireFrames
let firstFrameTexture = animatedCampfire[0]
campfire = SKSpriteNode(texture: firstFrameTexture)
campfire.size.height = 300
campfire.size.width = 300
campfire.position = CGPoint(x: 108, y: -188)
addChild(campfire)
}
func animateCampfire() {
campfire.run(SKAction.repeatForever(SKAction.animate(with: animatedCampfire, timePerFrame: 0.1, resize: false, restore: true)), withKey: "campfireAnimated")
}
func flickerCampfire() {
if let campfireLight = self.childNode(withName: "//campfireLight") as? SKLightNode {
campfireLight.falloff = CGFloat.random(in: 0..<1.5)
} else {
print("cannot find light node")
}
}
override func update(_ currentTime: TimeInterval) {
flickerCampfire()
}
}

Related

ARKit: Tracking VisonCoreML detected object

I'm new to iOS and I am currently refactoring a code I got from a tutorial on VisionCoreML and ARKit that adds a node to the detected object.
currently, if the I move the object the node does not move and follow the object. I can see from Apple's sample code for Recognizing Objects in Live Capture they use layers and repositions this each time Vision detects the object at a new position which is what I was hoping to replicate with an ARObject.
Is there a way I can achieve this with ARKit?
Any help around this would be greatly appreciated.
Thanks.
EDIT: Working code with solution
#IBOutlet var sceneView: ARSCNView!
private var viewportSize: CGSize!
private var previousAnchor: ARAnchor?
private var trackingNode: SCNNode!
lazy var objectDetectionRequest: VNCoreMLRequest = {
do {
let model = try VNCoreMLModel(for: yolov5s(configuration: MLModelConfiguration()).model)
let request = VNCoreMLRequest(model: model) { [weak self] request, error in
self?.processDetections(for: request, error: error)
}
return request
} catch {
fatalError("Failed to load Vision ML model.")
}
}()
func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {
guard let capturedImage = sceneView.session.currentFrame?.capturedImage
else { return }
let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: capturedImage, orientation: .leftMirrored, options: [:])
do {
try imageRequestHandler.perform([objectDetectionRequest])
} catch {
print("Failed to perform image request.")
}
}
func processDetections(for request: VNRequest, error: Error?) {
guard error == nil else {
print("Object detection error: \(error!.localizedDescription)")
return
}
guard let results = request.results else { return }
for observation in results where observation is VNRecognizedObjectObservation {
let objectObservation = observation as! VNRecognizedObjectObservation
let topLabelObservation = objectObservation.labels.first
print(topLabelObservation!.identifier + " " + "\(Int(topLabelObservation!.confidence * 100))%")
guard recognisedObject(topLabelObservation!.identifier) && topLabelObservation!.confidence > 0.9
else { continue }
let rect = VNImageRectForNormalizedRect(
objectObservation.boundingBox,
Int(self.sceneView.bounds.width),
Int(self.sceneView.bounds.height))
let midPoint = CGPoint(x: rect.midX, y: rect.midY)
let raycastQuery = self.sceneView.raycastQuery(from: midPoint,
allowing: .estimatedPlane,
alignment: .any)
let raycastArray = self.sceneView.session.raycast(raycastQuery!)
guard let raycastResult = raycastArray.first else { return }
let position = SCNVector3(raycastResult.worldTransform.columns.3.x,
raycastResult.worldTransform.columns.3.y,
raycastResult.worldTransform.columns.3.z)
if let _ = trackingNode {
trackingNode!.worldPosition = position
} else {
trackingNode = createNode()
trackingNode!.worldPosition = position
self.sceneView.scene.rootNode.addChildNode(trackingNode!)
}
}
}
private func recognisedObject(_ identifier: String) -> Bool {
return identifier == "remote" || identifier == "mouse"
}
private func createNode() -> SCNNode {
let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.01))
sphereNode.geometry?.firstMaterial?.diffuse.contents = UIColor.purple
return sphereNode
}
private func loadSession() {
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = []
sceneView.session.run(configuration)
}
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
viewportSize = sceneView.frame.size
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadSession()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
To be honest, the technologies you're using here cannot do that out of the box. YOLO (and any other object detection model you swapped out for it) have no built in concept of tracking the same object in a video. They look for objects in a 2D bitmap, and return 2D bounding boxes for them. As either the camera or object moves, and you pass in the next capturedImage buffer, it will give you a new bounding box in the correct position, but it has no way of knowing whether or not it's the same instance of the object detected in a previous frame.
To make this work, you'll need to do some post processing of those Vision results to determine whether or not it's the same object, and if so, manually move the anchor/mesh to match the new position. If you're confident there should only be one object in view at any given time, then it's pretty straightforward. If there will be multiple objects, you're venturing into complex (but still achievable) territory.
You could try to incorporate Vision Tracking, which might work though would depend on the nature and behavior of the tracked object.
Also, sceneView.hitTest() is deprecated. You should probably port that over to use ARSession.raycast()

What is causing node to slow down despite velocity remaining constant?

I have a SCNSphere that is climbing a 45 degree hill.
The node maintains a consistent speed until the same point at every level, at which point it unexpectedly drops in speed, here is a 10 second clip of the issue.
The drop in speed occurs at 8 seconds in this clip.
When the node reaches a z position of -240, it seems as though the entire game speed is cut in half.
I have tested this in the following ways always without success.
Tried testing without gravity.
Tried testing without colliding with the hill.
Tried testing without damping or friction.
Tried printing the nodes velocity to notice any changes, although the velocity remains at -5.0 on the z axis for the duration of the
level despite the significant drop in speed.
Tried printing the physicsWorld speed to notice any changes, although the speed remains at 1.0 for the duration of the
level despite the significant drop in speed.
Checked for a drop in frame rate although it maintains a frame rate of 60 fps, making only 14 draw calls in total with a poly count under 15k.
The sphere's velocity is updated at every frame in the renderer using the following function.
func updatePositions() {
if let playerPhysicsBod = playerNode.physicsBody {
playerPhysicsBod.velocity.x = (lastXPosition - playerNode.position.x) * 8
playerPhysicsBod.velocity.z = -5
print("player velocity is \(playerNode.physicsBody!.velocity)")
}
}
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
updatePositions()
updateSegments()
}
This project is a fresh one, and so there aren't many lines, I will include the entire code base below.
class GameViewController: UIViewController {
let scene = SCNScene(named: "art.scnassets/gameScene.scn")!
let jump = SCNScene(named: "art.scnassets/jump.scn")!.rootNode.childNode(withName: "jump", recursively: true)!
let box = SCNScene(named: "art.scnassets/box.scn")!.rootNode.childNode(withName: "box", recursively: true)!
var playerNode = SCNNode()
var cameraNode = SCNNode()
var lastXPosition = Float()
var floorSegments = [SCNNode]()
override func viewDidLoad() {
super.viewDidLoad()
// retrieve the SCNView
let scnView = self.view as! SCNView
// scnView.isJitteringEnabled = true
scnView.scene = scene
scnView.delegate = self
scnView.showsStatistics = true
setupCamera()
setupPlayer()
setupSegments()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let tLocationX = touches.first?.location(in: self.view).x else { return }
let ratio = tLocationX / self.view.frame.maxX
lastXPosition = Float((5 * ratio) - 2.5)
}
func setupCamera() {
if let camera = scene.rootNode.childNode(withName: "camera", recursively: true) {
cameraNode = camera
cameraNode.name = "camera"
}
}
func setupPlayer() {
if let player = scene.rootNode.childNode(withName: "player", recursively: true) {
playerNode = player
playerNode.name = "player"
}
}
func setupSegments() {
if let segment = scene.rootNode.childNode(withName: "segment", recursively: true) {
floorSegments.append(segment)
}
}
}
extension GameViewController: SCNSceneRendererDelegate {
func updateSegments() {
playerNode.position = playerNode.presentation.position
if let lastSegmentClone = floorSegments.last?.clone() {
lastSegmentClone.childNodes.forEach { (node) in
node.removeFromParentNode()
}
if abs(playerNode.position.z - lastSegmentClone.position.z) < 30 {
// set up next segment
lastSegmentClone.position = SCNVector3(lastSegmentClone.position.x, lastSegmentClone.position.y + 4, lastSegmentClone.position.z - 4)
floorSegments.append(lastSegmentClone)
// Add falling blocks to the segment
for _ in 0...2 {
let boxClone = box.clone()
let randomX = Int.random(in: -2...2)
let randomY = Int.random(in: 1...3)
boxClone.eulerAngles.z = Float(GLKMathDegreesToRadians(-45))
boxClone.position = SCNVector3(randomX, randomY, -randomY)
lastSegmentClone.addChildNode(boxClone)
}
// Add falling blocks to the segment
for (index,segment) in floorSegments.enumerated().reversed() {
if segment.position.z > playerNode.position.z + 5 {
floorSegments.remove(at: index)
segment.childNodes.forEach { (node) in
node.removeFromParentNode()
}
segment.removeFromParentNode()
}
}
scene.rootNode.addChildNode(lastSegmentClone)
}
}
}
func updatePositions() {
if let playerPhysicsBod = playerNode.physicsBody {
playerPhysicsBod.velocity.x = (lastXPosition - playerNode.position.x) * 8
playerPhysicsBod.velocity.z = -5
print("player velocity is \(playerNode.physicsBody!.velocity)")
}
}
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
updatePositions()
updateSegments()
}
}
It is difficult to judge w/o seeing scn files.
I hope there is no collision between the player and floor nodes...
Player's Position is a float, It might be inexact comparison for equality. I think you should avoid player's position adjustment, as it should be almost the same:
playerNode.position = playerNode.presentation.position
Why don't you keep reference on an empty segment node?
In the method setupSegments clone the segment and after removing children nodes, keep it. No point to always remove children nodes:
lastSegmentClone.childNodes.forEach { (node) in
node.removeFromParentNode()
}
Plus I think you should "reverse" the order in updateSegments:
if let lastSegment = floorSegments.last { // no cloning here....
if abs(playerNode.position.z - lastSegment.position.z) < 30 {
let lastSegmentClone = lastSegment.clone() // better to use emptySegmentNode
/* If empty segment then it's not needed to remove children nodes...
lastSegmentClone.childNodes.forEach { (node) in
node.removeFromParentNode() */
Also if you're removing parent node, children nodes are going to be removed automatically....
// Avoid commented code
/*segment.childNodes.forEach { (node) in
node.removeFromParentNode()
}*/
segment.removeFromParentNode()
No sure, but perhaps for floor segments it is better to use custom action for removal:
// calculate somehow wait duration, based on the player's position, velocity or use pure constant..
let removeAction = SCNAction.sequence([SCNAction.waitForDuration(2.0), SCNAction.removeFromParent()])
lastSegmentClone.run(removeAction)
In such case you just need a reference on the last floor node, and empty floor node.

Why can't I call a function in function update?

Please help me! Im trying to call a function I have declared in the GameScene class, within the update function. But it doesn't recognise the function, I'm wondering if this is something to do with the class or something because I want to run the function every frame (but has to update for each individual spriteCopy, depending on its own movement) to make sure the sprite copies all follow the function and continue to, infinitely.
Thank you in advance for any help.
here is the code for the function that sort of works to an extent:
func touchUp(atPoint pos : CGPoint) {
if let spriteCopy = self.sprite?.copy() as! SKShapeNode? {
spriteCopy.fillColor = UIColor.white
spriteCopy.position = initialTouch
spriteCopy.physicsBody?.restitution = 0.5
spriteCopy.physicsBody?.friction = 0
spriteCopy.physicsBody?.affectedByGravity = false
spriteCopy.physicsBody?.linearDamping = 0
spriteCopy.physicsBody?.angularDamping = 0
spriteCopy.physicsBody?.angularVelocity = 0
spriteCopy.physicsBody?.isDynamic = true
spriteCopy.physicsBody?.categoryBitMask = 1 //active
spriteCopy.isHidden = false
touchUp = pos
xAxisLength = initialTouch.x - touchUp.x
yAxisLength = initialTouch.y - touchUp.y
xUnitVector = xAxisLength / distanceBetweenTouch * power * 300
yUnitVector = yAxisLength / distanceBetweenTouch * power * 300
spriteCopy.physicsBody?.velocity = CGVector(dx: xUnitVector, dy: yUnitVector)
func directionRotation() {
if let body = spriteCopy.physicsBody {
if (body.velocity.speed() > 0.01) {
spriteCopy.zRotation = body.velocity.angle()
}
}
}
directionRotation() //When I run the function with this line, the spriteCopy
//is spawned initially with the right angle (in the direction
//of movement) but doesn't stay updating the angle
sprite?.isHidden = true
self.addChild(spriteCopy)
}
}
and here is the function not being recognised in function update:
override func update(_ currentTime: TimeInterval) {
directionRotation() //this line has error saying "use of unresolved identifier"
// Called before each frame is rendered
}
EDIT: I was thinking maybe there could be a way to spawn multiple spriteCopy's without the "copy()" method that will not restrict the access to the spriteCopy's properties after they have been spawned? Whilst remembering they still must have to be individual SpriteNodes so that the directionRotation function could be applied independently to each of them (FYI: The user can spawn upwards of 50+ sprite nodes)
You have specified local function. You need move out from touchUp function realisation directionRotation
func directionRotation() {
if let body = spriteCopy.physicsBody {
if (body.velocity.speed() > 0.01) {
spriteCopy.zRotation = body.velocity.angle()
}
}
}
func touchUp(atPoint pos : CGPoint) {
...
}
EDIT
I mean you need do some think like this:
func directionRotation(node:SKNode) {
if let body = node.physicsBody {
if (body.velocity.speed() > 0.01) {
node.zRotation = body.velocity.angle()
}
}
}
override func update(_ currentTime: TimeInterval) {
for node in self.children
{
directionRotation(node)
}
}

SpriteKit reference nodes from level editor

I'm using the scene editor in SpriteKit to place color sprites and assign them textures using the Attributes Inspector. My problem is trying to figure out how to reference those sprites from my GameScene file. For example, I'd like to know when a sprite is a certain distance from my main character.
Edit - code added
I'm adding the code because for some reason, appzYourLife's answer worked great in a simple test project, but not in my code. I was able to use Ron Myschuk's answer which I also included in the code below for reference. (Though, as I look at it now I think the array of tuples was overkill on my part.) As you can see, I have a Satellite class with some simple animations. There's a LevelManager class that replaces the nodes from the scene editor with the correct objects. And finally, everything gets added to the world node in GameScene.swift.
Satellite Class
func spawn(parentNode:SKNode, position: CGPoint, size: CGSize = CGSize(width: 50, height: 50)) {
parentNode.addChild(self)
createAnimations()
self.size = size
self.position = position
self.name = "satellite"
self.runAction(satAnimation)
self.physicsBody = SKPhysicsBody(circleOfRadius: size.width / 2)
self.physicsBody?.affectedByGravity = false
self.physicsBody?.categoryBitMask = PhysicsCategory.satellite.rawValue
self.physicsBody?.contactTestBitMask = PhysicsCategory.laser.rawValue
self.physicsBody?.collisionBitMask = 0
}
func createAnimations() {
let flyFrames:[SKTexture] = [textureAtlas.textureNamed("sat1.png"),
textureAtlas.textureNamed("sat2.png")]
let flyAction = SKAction.animateWithTextures(flyFrames, timePerFrame: 0.14)
satAnimation = SKAction.repeatActionForever(flyAction)
let warningFrames:[SKTexture] = [textureAtlas.textureNamed("sat8.png"),
textureAtlas.textureNamed("sat1.png")]
let warningAction = SKAction.animateWithTextures(warningFrames, timePerFrame: 0.14)
warningAnimation = SKAction.repeatActionForever(warningAction)
}
func warning() {
self.runAction(warningAnimation)
}
Level Manager Class
import SpriteKit
class LevelManager
{
let levelNames:[String] = ["Level1"]
var levels:[SKNode] = []
init()
{
for levelFileName in levelNames {
let level = SKNode()
if let levelScene = SKScene(fileNamed: levelFileName) {
for node in levelScene.children {
switch node.name! {
case "satellite":
let satellite = Satellite()
satellite.spawn(level, position: node.position)
default: print("Name error: \(node.name)")
}
}
}
levels.append(level)
}
}
func addLevelsToWorld(world: SKNode)
{
for index in 0...levels.count - 1 {
levels[index].position = CGPoint(x: -2000, y: index * 1000)
world.addChild(levels[index])
}
}
}
GameScene.swift - didMoveToView
world = SKNode()
world.name = "world"
addChild(world)
physicsWorld.contactDelegate = self
levelManager.addLevelsToWorld(self.world)
levelManager.levels[0].position = CGPoint(x:0, y: 0)
//This does not find the satellite nodes
let satellites = children.flatMap { $0 as? Satellite }
//This does work
self.enumerateChildNodesWithName("//*") {
node, stop in
if (node.name == "satellite") {
self.satTuple.0 = node.position
self.satTuple.1 = (node as? SKSpriteNode)!
self.currentSatellite.append(self.satTuple)
}
}
The Obstacle class
First of all you should create an Obstacle class like this.
class Obstacle: SKSpriteNode { }
Now into the scene editor associate the Obstacle class to your obstacles images
The Player class
Do the same for Player, create a class
class Player: SKSpriteNode { }
and associate it to your player sprite.
Checking for collisions
Now into GameScene.swift change the updated method like this
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
let obstacles = children.flatMap { $0 as? Obstacle }
let player = childNodeWithName("player") as! Player
let obstacleNearSprite = obstacles.contains { (obstacle) -> Bool in
let distance = hypotf(Float(player.position.x) - Float(obstacle.position.x), Float(player.position.y) - Float(obstacle.position.y))
return distance < 100
}
if obstacleNearSprite {
print("Oh boy!")
}
}
What does it do?
The first line retrieves all your obstacles into the scene.
the second line retrieves the player (and does crash if it's not present).
Next it put into the obstacleNearSprite constant the true value if there is at least one Obstacle at no more then 100 points from Player.
And finally use the obstacleNearSprite to print something.
Optimizations
The updated method gets called 60 times per second. We put these 2 lines into it
let obstacles = children.flatMap { $0 as? Obstacle }
let player = childNodeWithName("player") as! Player
in order to retrieves the sprites we need. With the modern hardware it is not a problem but you should save references to Obstacle and Player instead then searching for them in every frame.
Build a nice game ;)
you will have to loop through the children of the scene and assign them to local objects to use in your code
assuming your objects in your SKS file were named Obstacle1, Obstacle2, Obstacle3
Once in local objects you can check and do whatever you want with them
let obstacle1 = SKSpriteNode()
let obstacle2 = SKSpriteNode()
let obstacle3 = SKSpriteNode()
let obstacle3Location = CGPointZero
func setUpScene() {
self.enumerateChildNodesWithName("//*") {
node, stop in
if (node.name == "Obstacle1") {
self.obstacle1 = node
}
else if (node.name == "Obstacle2") {
self.obstacle2 = node
}
else if (node.name == "Obstacle3") {
self.obstacle3Location = node.position
}
}
}

SpriteKit fps drops at first animation call

I have the function that moves an object and runs animation on move:
func animateMove(move: MoveTo, completion: () -> ()) {
let object = move.object
let spriteName = "\(object.spriteName)\(move.direction.name)"
let textures = TextureCache.loadTextures(spriteName)
let animate = SKAction.animateWithTextures(textures, timePerFrame: moveDuration/NSTimeInterval(textures.count))
let point = pointForColumn(move.column, row: move.row)
let move = SKAction.moveTo(point, duration: moveDuration)
move.timingMode = .Linear
let group = SKAction.group([move, animate])
object.sprite!.removeAllActions()
object.sprite!.runAction(group, completion: completion)
}
Also I have the cacher:
class TextureCache {
...
static func loadTextures(name: String) -> [SKTexture] {
let atlas = "\(name).atlas"
return TextureCache.sharedInstance.loadTexturesFromAtlas(atlas, name: name)
}
private func loadTexturesFromAtlas(atlas: String, name: String) -> [SKTexture] {
if let textures = textureDictionary["\(atlas):\(name)"] {
return textures
}
let textureAtlas = SKTextureAtlas(named: atlas)
var textures = [SKTexture]()
for i in 0..<textureAtlas.textureNames.count {
textures.append(SKTexture(imageNamed: "\(name)\(i)"))
}
textureDictionary["\(atlas):\(name)"] = textures
return textures
}
So, the problem is that during first call fps drops significantly and CPU time increases, for example: move object to the left - from 30 fps it drops to 8 fps.
The problem was on my side in cacher, was:
let textureAtlas = SKTextureAtlas(named: atlas)
var textures = [SKTexture]()
for i in 0..<textureAtlas.textureNames.count {
textures.append(SKTexture(imageNamed: "\(name)\(i)"))
}
now:
let textureAtlas = SKTextureAtlas(named: atlas)
var textures = [SKTexture]()
for i in 0..<textureAtlas.textureNames.count {
let texture = textureAtlas.textureNamed("\(name)\(i)")
texture.size()
textures.append(texture)
}

Resources