[英]In a MTKView How to get pixel information ( RGB and Alpha values ) of a given point in 3D (x,y,z)?
好的,我們已經在MTKView
繪制了幾件事。我們可以使用Metal,MetalKit函數移動和旋轉它們,但是我們無法獲得3D(x,y,z)中給定點的像素信息。 我們搜索了幾個小時,沒有找到任何解決方案。
這似乎是一個經典案例,試圖向您的視圖(在術語“模型-視圖-控制器”范式的意義上)咨詢模型所擁有的信息。 直接咨詢您的模型。
像Metal這樣的3D渲染技術就是將3D信息展平為2D,並有效地丟棄與該2D表示無關的信息。 渲染結束時,Metal和MTKView
都沒有有關3D點的任何信息。
另請注意,您的模型可能沒有任意3D點的信息。 您的所有3D模型(換句話說,最有可能)都是外殼。 他們沒有內部,只有表面。 因此,除非3D點恰好落在表面上,而不是完全不在模型的內部或外部,否則它將沒有顏色。
無法從3D模型空間獲得顏色,但可能從2D視圖空間獲得顏色。
func getColor(x: CGFloat, y: CGFloat) -> UIColor? {
if let curDrawable = self.currentDrawable {
var pixel: [CUnsignedChar] = [0, 0, 0, 0] // bgra
let textureScale = CGFloat(curDrawable.texture.width) / self.bounds.width
let bytesPerRow = curDrawable.texture.width * 4
let y = self.bounds.height - y
curDrawable.texture.getBytes(&pixel, bytesPerRow: bytesPerRow, from: MTLRegionMake2D(Int(x * textureScale), Int(y * textureScale), 1, 1), mipmapLevel: 0)
let red: CGFloat = CGFloat(pixel[2]) / 255.0
let green: CGFloat = CGFloat(pixel[1]) / 255.0
let blue: CGFloat = CGFloat(pixel[0]) / 255.0
let alpha: CGFloat = CGFloat(pixel[3]) / 255.0
let color = UIColor(red:red, green: green, blue:blue, alpha:alpha)
return color
}
return nil
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.