Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render SKScene to SKTexture?

Is there a way in Sprite Kit that I can capture the screen (all the current SKScene rendered nodes) to an SKTexture so that I can apply a CIFilter and then assign the SKTexture back to a new SKSpriteNode?

I know that I can set an SKEffectNode, as the parent of my node tree, apply a filter etc and get the result that way but I really need to have a filtered SKTexture (or SKSpriteNode) that I can reuse later?

EDIT:

Possible solution:

textureFromNode:
Renders and returns a Sprite Kit texture that contains the node’s contents.

Yup that works:

   SKTexture *texture = [[self view] textureFromNode:[self scene]];
   [blurSprite setTexture:texture];
like image 271
fuzzygoat Avatar asked Jan 10 '23 12:01

fuzzygoat


2 Answers

From Apple docs:

textureFromNode: Renders and returns a Sprite Kit texture that contains the node’s contents.

Code example:

   SKTexture *texture = [[self view] textureFromNode:[self scene]];
   [blurSprite setTexture:texture];
like image 68
fuzzygoat Avatar answered Jan 22 '23 20:01

fuzzygoat


You should try with snapshotViewAfterScreenUpdates: method from UIView.

Your SKScene is inside an SKView that has this method.

After that you can extract the image from the view, and create a SKNode with it.

like image 36
gabuh Avatar answered Jan 22 '23 22:01

gabuh