2017-10-15 164 views
0

我一直在牆上撞擊我的頭幾個小時。我正嘗試使用CIFilter修改我的應用程序中的紋理,然後將該新紋理作爲新SKTileDefinition的一部分在我的地圖上重新着色。 函數波紋管發現玩家「擁有」的圖塊,並嘗試通過將SKTileDefinition更改爲coloredDefinition來重新着色。以編程方式創建一個SKTileDefinition

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) 
        } 
       } 
      } 
     } 
    } 

這裏是actulaly應用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定義。但是,這導致瓷磚空白...

任何指針?

回答

0

在嘗試了一堆東西之後,我終於明白了什麼是錯的。瓷磚定義沒有正確創建,因爲我從來沒有真正畫過一個新的紋理。據我所知,CIImage不是繪製的紋理,它只是一個配方,我們需要一個上下文來繪製紋理。在此更改之後,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" 
相關問題