2
votes

I cannot figure it out how to apply mipmaps for SKTexture texture. I'm using XCode 8.2.1 with Swift.

Here is my method:

override func sceneDidLoad() {
    logo = childNode(withName: "logo") as! SKSpriteNode
    let texture: SKTexture! = SKTexture.init(imageNamed: "logo")
    texture.usesMipmaps = true
    logo.texture = texture
}

I use sample image png 512*512 size (pot both) Full size it looks like this: full size image

Then I want to decrease its size to 128*128 pixels

decrease size

And here is the output with mipmapping enabled: mipmapping enabled output 128*128

And for example output without mipmapping (only default linear filtering)without mipmapping

They both looks the same. So I think mipmaps didn't apply. Help me please, I've spent 3 days trying solve this problem.

UPD: As Confused told me, I made all job in code. Created texture with mipmaping enabled, assign it to SKSpriteNode and then perform scaling by scale method or action.

let texture = SKTexture.init(imageNamed: "logo")
texture.usesMipmaps = true
logo = SKSpriteNode.init(texture: texture)
logo.position = CGPoint(x: 0, y: 0)
self.addChild(logo)
//logo.scale(to: CGSize(width: 128, height: 128))
let scaleAction = SKAction.scale(to: CGSize(width: 128, height: 128), duration: 1)
    logo.run(scaleAction)

Result is the same: enter image description here

1
You're right. They are exactly the same. I dropped them into Photoshop and did a Difference blend. The one without mipmapping is one pixel to the left of where it should be, in your screenshot, but once that's corrected, these are identical.Confused

1 Answers

1
votes

It wouldn't surprise, at all, if the Xcode SpriteKit Scene Editor is doing the resizing of your texture before the mipmapping is activated by your code. Meaning a 128x128 version of your texture has mipmapping on, because that's what Xcode and SpriteKit SceneEditor have conspired to store as a texture.

So instead of setting the scale in the Scene Editor, after turning on mipMapping in your code, use this to scale the SKSpriteNode:

https://developer.apple.com/reference/spritekit/skspritenode/1645445-scale

and/or experiment with the SKAction scaling operations:

https://developer.apple.com/reference/spritekit/skaction

In this way you can be absolutely sure that the sequence of events is

  1. Turn on mipmapping
  2. Perform Scaling

And hopefully this works.