2

I have been trying to render a node tree into an SKTexture as a possible solution to another question here. I tried searching for an answer to this, and I came across one of Apple's Developer pages (view it here). It says the command which converts a node tree into an SKTexture is this:

func texture(from node: SKNode) -> SKTexture?

Although, the code is incomplete since there is no code which executes the conversion of a node tree to an SKTexture. Am I doing something wrong or what is the complete code that I should use to render a node tree into an SKTexture.

Thanks for any advice, help, and answers!

Community
  • 1
  • 1
J.Treutlein
  • 963
  • 8
  • 23

1 Answers1

2

texture(from:) is an instance method of SKView that takes a node (with zero or more children) as a parameter and returns an optional SKTexture. Since the returned value is an optional, you'll need to unwrap it before using it.

From didMove(to:), you can use the view parameter to create a texture

if let texture = view.texture(from: node) {
    let sprite = SKSpriteNode(texture:texture)
    addChild(sprite)
}

From other methods in the SKScene subclass, use optional chaining

if let texture = self.view?.texture(from: node) {
    let sprite = SKSpriteNode(texture:texture)
    addChild(sprite)
}

You can also render the portion of the node-tree's contents inside of a rectangle with

let rect = CGRect(x:-width/2, y:-height/2, width:width, height:height)
if let texture = self.view?.texture(from: node, crop: rect) {
    let sprite = SKSpriteNode(texture:texture)
    addChild(sprite)
}
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • This works thanks! Is there anyway to change the contents of the texture during runtime instead always creating a new texture each frame. Right now, the frame rate is so low because I'm creating a new texture each frame. – J.Treutlein Jan 09 '17 at 08:40
  • Since the node tree does not need to be in the scene, you can precompute the textures during initialization or render the node tree into textures in a background thread during run-time. If your node tree is highly dynamic, perhaps you'll need to use something other than `texture(from:)`. It depends on what you're trying to accomplish. – 0x141E Jan 09 '17 at 08:55
  • The texture changes throughout the program and I want to always make a physics body with this texture. How would I render the node tree into textures in a background thread during run-time? – J.Treutlein Jan 09 '17 at 08:57
  • I'll need to know more about the node tree and its children and, specifically, how the contents of the node tree changes over time. – 0x141E Jan 09 '17 at 09:14
  • If you click on the first hyperlink in my original post that leads to another question by me, there is an example code with a parent and children and the parent and the children have the same properties as my actual code. Although, what I haven't explained is that each child moves backwards the same amount each frame, and when the last child goes off the screen, a new child is added in front of all the other children (looks like moving terrain). These moving children create the texture, and as you can tell, it changes each frame. – J.Treutlein Jan 09 '17 at 09:20
  • I've been working on a solution to the larger issue you're trying to resolve. I've concluded that you are probably better off moving the character instead of moving the ground. It's closer to what happens in the real world and very straightforward to implement. – 0x141E Jan 09 '17 at 21:46
  • Before I fully collapse on my idea, can you please take a look at my problem in the first link. I am trying to work out how to keep the rotation of a physicsBody so that when i add it to a list and make a node have a physics body of the list's physicsBodies combined, they will still be rotated. – J.Treutlein Jan 09 '17 at 21:49