以编程方式创建SKTileDefinition [英] Programmatically creating an SKTileDefinition

查看:106
本文介绍了以编程方式创建SKTileDefinition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几个小时以来一直在墙上撞头。我正在尝试使用CIFilter修改应用程序内部的纹理,然后将该新纹理用作新的SKTileDefinition的一部分,以对地图上的图块重新着色。
下面的函数查找玩家拥有的图块,并尝试通过将SKTileDefinition更改为 coloredDefinition 来重新着色。

I've been beating my head against a wall for hours now. I am trying to modify a texture inside my app using a CIFilter and then use that new texture as a part of a new SKTileDefinition to recolor tiles on my map. The function bellow finds tiles that players "own" and attempts to recolor them by changing the SKTileDefinition to the coloredDefinition.

  func updateMapTileColoration(for players: Array<Player>){
    for player in players {
        for row in 0..<mainBoardMap!.numberOfRows {
            for col in 0..<mainBoardMap!.numberOfColumns {
                let rowReal = mainBoardMap!.numberOfRows - 1 - row
                if player.crownLocations!.contains(CGPoint(x: row, y: col)) {
                    if let tile = mainBoardMap!.tileDefinition(atColumn: col, row: rowReal) {
                        let coloredDefinition = colorizeTile(tile: tile, into: player.color!)
                        print(coloredDefinition.name)
                        mainBoardMap!.tileSet.tileGroups[4].rules[0].tileDefinitions.append(coloredDefinition)
                        mainBoardMap!.setTileGroup(crownGroup!, andTileDefinition: crownGroup!.rules[0].tileDefinitions[1], forColumn: col, row: rowReal)
                    }
                }
            }
        }
    }

下面是实际应用CIFilter的函数: colorizeTile

And here is the function that actulaly applies the CIFilter: colorizeTile

func colorizeTile(tile: SKTileDefinition, into color: UIColor) -> SKTileDefinition{
        let texture = tile.textures[0]

        let colorationFilter = CIFilter(name: "CIColorMonochrome")
        colorationFilter!.setValue(CIImage(cgImage: texture.cgImage()), forKey: kCIInputImageKey)
        colorationFilter!.setValue(CIColor(cgColor: color.cgColor), forKey: "inputColor")
        colorationFilter!.setValue(0.25, forKey: "inputIntensity")
        let coloredTexture = texture.applying(colorationFilter!)

        let newDefinition = SKTileDefinition(texture: texture)
        newDefinition.textures[0] = coloredTexture
        newDefinition.name = "meow"

        return newDefinition
    }

我会喜欢弄清楚为什么我无法更改tileDefinition的任何帮助。能够定义一个新的TileDefinition并将其添加到tileGroup,然后将tile组设置为特定的tile定义似乎在直觉上是正确的。但是,这导致出现空白磁贴...

I would love any help in figuring out why I cannot change the tileDefinition like I am trying to do. It seems intuitively correct to be able to define a new TileDefinition and add it to the tileGroup and then set the tile group to the specific tile definition. However, this is leading to blank tiles...

是否有指针?

推荐答案

尝试了一堆东西之后,我终于弄清楚了什么地方出了问题。无法正确创建图块定义,因为我从未真正绘制新纹理。据我了解, CIImage 并不是绘制的纹理,它只是一个配方,我们需要上下文来绘制纹理。进行此更改后,将正确创建SKTileDefinition。问题不是我原本认为的问题所在,所以我有点二手了。我创建 SKTileDefinition 的方法是正确的。

After trying a bunch of things I finally figured out what is wrong. The tile definition wasn't being created correctly because I never actually drew a new texture. As I learned, a CIImage is not the drawn texture its just a recipe and we need a context to draw the texture. After this change, the SKTileDefinition is properly created. The problem wasn't where I thought it was so I am sort-of second hand anwering the question. My method for creating a SKTileDefinition was correct.

   if drawContext == nil{
        drawContext = CIContext()
    }

    let texture = tile.textures[0]

    let colorationFilter = CIFilter(name: "CIColorMonochrome")
    colorationFilter!.setValue(CIImage(cgImage: texture.cgImage()), forKey: kCIInputImageKey)
    colorationFilter!.setValue(CIColor(cgColor: color.cgColor), forKey: "inputColor")
    colorationFilter!.setValue(0.75, forKey: "inputIntensity")

    let result = colorationFilter!.outputImage!
    let output = drawContext!.createCGImage(result, from: result.extent)
    let coloredTexture = SKTexture(cgImage: output!)

    let newDefinition = SKTileDefinition(texture: texture)
    newDefinition.textures[0] = coloredTexture
    newDefinition.name = "meow"

这篇关于以编程方式创建SKTileDefinition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆