2

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

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 定义,这在直观上似乎是正确的。然而,这会导致空白瓷砖......

任何指针?

4

1 回答 1

0

在尝试了一堆东西之后,我终于弄清楚了哪里出了问题。没有正确创建平铺定义,因为我从未真正绘制过新纹理。据我所知,aCIImage不是绘制的纹理,它只是一个配方,我们需要一个上下文来绘制纹理。在此更改之后,正确创建了 SKTileDefinition。问题不在我认为的地方,所以我有点像第二手回答这个问题。我的创建方法SKTileDefinition是正确的。

   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"
于 2017-10-16T22:08:33.153 回答