gpt4 book ai didi

swift - SceneKit Cocoa快照声明失败

转载 作者:行者123 更新时间:2023-12-03 07:35:30 25 4
gpt4 key购买 nike

我正在开发Swift/Cocoa/Xcode应用程序。

该应用程序包含一个SceneKit View 。渲染API设置为默认(我认为这是金属)。

如果在此SceneKit View 对象上运行snapshot(),则会收到此错误消息。我想要做的是捕获从相机查看的场景的UIImage

Texture PixelFormat MTLPixelFormatBGRA8Unorm does not match Resolve PixelFormat MTLPixelFormatRGBA8Unorm

如果将渲染API设置为 OpenGL,则没有错误,一切正常。

我曾在iOS应用程序上尝试过相同的操作,但在两种情况下(金属或OpenGL)均可使用。

我不明白为什么会收到此错误以及如何避免该错误。

这是示例代码:

    import SceneKit
import Cocoa

class ViewController: NSViewController {

@IBOutlet weak var vue_scene: SCNView!
@IBOutlet weak var img_snapshot: NSImageView!

let camera_node = SCNNode()
var box_node:SCNNode = SCNNode()

override func viewDidLoad() {
super.viewDidLoad()

let scene = SCNScene()
vue_scene.scene = scene

vue_scene.backgroundColor = NSColor.clear

vue_scene.showsStatistics = false
vue_scene.allowsCameraControl = false
vue_scene.autoenablesDefaultLighting = true

camera_node.camera = SCNCamera()
camera_node.camera?.zNear = 0.01
camera_node.camera?.zFar = 1000000.0
vue_scene.pointOfView = camera_node
vue_scene.scene!.rootNode.addChildNode(camera_node)

let box = SCNBox(width: 10.0,
height: 10.0,
length: 10.0,
chamferRadius: 0.0)
box.firstMaterial?.diffuse.contents = NSColor.red

box.firstMaterial?.isDoubleSided = true
box_node = SCNNode(geometry:box)
box_node.position = SCNVector3Make(0,0,0)
box_node.opacity = 1.0
vue_scene.scene!.rootNode.addChildNode(box_node)

camera_node.position = SCNVector3Make(0.0,
0.0,
70.0)
}

@IBAction func on_btn(_ sender: Any) {
// signal SIGABRT here:
// /Library/Caches/com.apple.xbs/Sources/Metal/Metal-56.6.1/ToolsLayers/Debug/MTLDebugCommandBuffer.mm:215: failed assertion `Texture PixelFormat MTLPixelFormatBGRA8Unorm does not match Resolve PixelFormat MTLPixelFormatRGBA8Unorm'
let image = vue_scene.snapshot()
img_snapshot.image = image;
}
}

最佳答案

它适用于金属。
在macOS中,使用Tab View(例如)容纳SceneViewNSImageView
我在macOS 10.15.4 Catalina上使用了Xcode 11.4。

import SceneKit
import Cocoa

class ViewController: NSViewController {

@IBOutlet var sceneView: SCNView!
@IBOutlet var imageView: NSImageView!

override func viewDidLoad() {
super.viewDidLoad()

sceneView.scene = SCNScene()

sceneView.pointOfView?.position.z = 20
sceneView.allowsCameraControl = true
sceneView.showsStatistics = true
sceneView.backgroundColor = NSColor.black
sceneView.autoenablesDefaultLighting = true

let box = SCNBox(width: 1.0,
height: 1.0,
length: 1.0,
chamferRadius: 0.0)

box.firstMaterial?.diffuse.contents = NSColor.systemTeal

let boxNode = SCNNode(geometry: box)
boxNode.position = SCNVector3(0,0,-10)
sceneView.scene!.rootNode.addChildNode(boxNode)
}

@IBAction func createSnapshot(_ sender: NSButton) {

let image = sceneView.snapshot()
imageView.image = image
}
}
enter image description here

关于swift - SceneKit Cocoa快照声明失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41987827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com