如何将 SKScene 添加到 SCNNode 平面?
How do I Add a SKScene to a SCNNode Plane?
我已经完成了 raywenderlich.com
教程使用 ARKit
构建博物馆应用程序 2. 我尝试修改此应用程序以在锚图像上放置 SKScene
而不是 AVPlayer
.
我真正想做的就是在图像上放一个 SKShapeNode
,但经过研究后我发现我必须创建一个 SKScene
,将 SKShapeNode
添加到SKScene
然后将 SKScene
添加到 SCNPlane
.
You can set a value for this property using any of the following types:
A color (NSColor/UIColor or CGColor), specifying a uniform color for the material’s surface
A number (NSNumber), specifying a uniform scalar value for the material's surface (useful for physically based properties such as metalness)
An image (NSImage/UIImage or CGImage), specifying a texture to be mapped across the material’s surface
An NSString or NSURL object specifying the location of an image file
A video player (AVPlayer) or live video capture preview (AVCaptureDevice, in iOS only)
A Core Animation layer (CALayer)
A texture (SKTexture, MDLTexture, MTLTexture, or GLKTextureInfo)
A SpriteKit scene (SKScene)
A specially formatted image or array of six images, specifying the faces of a cube map
这是我修改后的代码:
private func handleFoundImage(_ imageAnchor: ARImageAnchor, _ node: SCNNode) {
let name = imageAnchor.referenceImage.name!
print("you found a \(name) image")
let size = imageAnchor.referenceImage.physicalSize
let skScene = SKScene(size: size)
print("SKSCENE FRAME: \(skScene.frame)")
skScene.backgroundColor = .white
let skShape = SKShapeNode(rectOf: size, cornerRadius: 5.0)
skShape.fillColor = .blue
skShape.strokeColor = .red
skShape.lineWidth = 1.5
skScene.addChild(skShape)
let plane = SCNPlane(width: size.width, height: size.height)
let material = SCNMaterial()
material.lightingModel = SCNMaterial.LightingModel.constant
material.isDoubleSided = true
material.diffuse.contents = skScene
plane.materials = [material]
let rectNode = SCNNode(geometry: plane)
rectNode.eulerAngles.x = -.pi / 2
node.addChildNode(rectNode)
}
当我在我的 iPhone 上 运行 并将其指向图像时,应用程序崩溃,我得到以下堆栈跟踪。
2019-04-15 22:32:28.650432-0600 ImageTracker[3807:1577260] [DYMTLInitPlatform] platform initialization successful
2019-04-15 22:32:29.238671-0600 ImageTracker[3807:1577061] [Accessibility] ****************** Loading GAX Client Bundle ****************
2019-04-15 22:32:29.362407-0600 ImageTracker[3807:1577061] Metal GPU Frame Capture Enabled
2019-04-15 22:32:29.364368-0600 ImageTracker[3807:1577061] Metal API Validation Enabled
2019-04-15 22:32:29.499256-0600 ImageTracker[3807:1577061] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2019-04-15 22:32:29.500912-0600 ImageTracker[3807:1577061] [MC] Reading from public effective user settings.
Point the camera at a dinsoaur.
you found a stegosaurus image
SKSCENE FRAME: (-0.0, -0.0, 0.10541000217199326, 0.06538714197231457)
<SKScene> name:'(null)' frame:{{-0, -0}, {0.10541000217199326, 0.065387141972314572}} anchor:{0, 0}
2019-04-15 22:32:37.189388-0600 ImageTracker[3807:1577292] validateTextureDimensions, line 1071: error 'MTLTextureDescriptor has width of zero.'
validateTextureDimensions:1071: failed assertion `MTLTextureDescriptor has width of zero.'
我不太确定 MTLTextureDescriptor
是什么或如何使宽度成为非零值。
谁能帮我弄清楚我做错了什么,或者这是正确的方法吗?同样,我只想找到一张图片,然后用 SKShapeNode
覆盖它。如果我必须使用 SKScene
来执行此操作,那很好,我只需要帮助弄清楚如何执行此操作。如果这不是最好的方法,有人可以指点我可以找到仪表解决方案的文档吗?
谢谢!
那是因为size
这里let size = imageAnchor.referenceImage.physicalSize
的宽度和高度都是零。
您获得的尺寸将是您为参考图片设置的 physicalSize
,肯定小于 1 米。因此,当您将其用作 skScene
的大小时,您创建的高度和宽度都为零。
例如:如果您的参考图片的物理尺寸为 10x15 厘米,则您的 size
的宽度为 0.1,高度为 0.15。这将更改为零。
我推荐:
let physicalSize = imageAnchor.referenceImage.physicalSize
let size = CGSize(width: physicalSize.width * 1000, height: physicalSize.height * 1000)
我已经完成了 raywenderlich.com
教程使用 ARKit
构建博物馆应用程序 2. 我尝试修改此应用程序以在锚图像上放置 SKScene
而不是 AVPlayer
.
我真正想做的就是在图像上放一个 SKShapeNode
,但经过研究后我发现我必须创建一个 SKScene
,将 SKShapeNode
添加到SKScene
然后将 SKScene
添加到 SCNPlane
.
You can set a value for this property using any of the following types:
A color (NSColor/UIColor or CGColor), specifying a uniform color for the material’s surface
A number (NSNumber), specifying a uniform scalar value for the material's surface (useful for physically based properties such as metalness)
An image (NSImage/UIImage or CGImage), specifying a texture to be mapped across the material’s surface
An NSString or NSURL object specifying the location of an image file
A video player (AVPlayer) or live video capture preview (AVCaptureDevice, in iOS only)
A Core Animation layer (CALayer)
A texture (SKTexture, MDLTexture, MTLTexture, or GLKTextureInfo)
A SpriteKit scene (SKScene)
A specially formatted image or array of six images, specifying the faces of a cube map
这是我修改后的代码:
private func handleFoundImage(_ imageAnchor: ARImageAnchor, _ node: SCNNode) {
let name = imageAnchor.referenceImage.name!
print("you found a \(name) image")
let size = imageAnchor.referenceImage.physicalSize
let skScene = SKScene(size: size)
print("SKSCENE FRAME: \(skScene.frame)")
skScene.backgroundColor = .white
let skShape = SKShapeNode(rectOf: size, cornerRadius: 5.0)
skShape.fillColor = .blue
skShape.strokeColor = .red
skShape.lineWidth = 1.5
skScene.addChild(skShape)
let plane = SCNPlane(width: size.width, height: size.height)
let material = SCNMaterial()
material.lightingModel = SCNMaterial.LightingModel.constant
material.isDoubleSided = true
material.diffuse.contents = skScene
plane.materials = [material]
let rectNode = SCNNode(geometry: plane)
rectNode.eulerAngles.x = -.pi / 2
node.addChildNode(rectNode)
}
当我在我的 iPhone 上 运行 并将其指向图像时,应用程序崩溃,我得到以下堆栈跟踪。
2019-04-15 22:32:28.650432-0600 ImageTracker[3807:1577260] [DYMTLInitPlatform] platform initialization successful
2019-04-15 22:32:29.238671-0600 ImageTracker[3807:1577061] [Accessibility] ****************** Loading GAX Client Bundle ****************
2019-04-15 22:32:29.362407-0600 ImageTracker[3807:1577061] Metal GPU Frame Capture Enabled
2019-04-15 22:32:29.364368-0600 ImageTracker[3807:1577061] Metal API Validation Enabled
2019-04-15 22:32:29.499256-0600 ImageTracker[3807:1577061] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2019-04-15 22:32:29.500912-0600 ImageTracker[3807:1577061] [MC] Reading from public effective user settings.
Point the camera at a dinsoaur.
you found a stegosaurus image
SKSCENE FRAME: (-0.0, -0.0, 0.10541000217199326, 0.06538714197231457)
<SKScene> name:'(null)' frame:{{-0, -0}, {0.10541000217199326, 0.065387141972314572}} anchor:{0, 0}
2019-04-15 22:32:37.189388-0600 ImageTracker[3807:1577292] validateTextureDimensions, line 1071: error 'MTLTextureDescriptor has width of zero.'
validateTextureDimensions:1071: failed assertion `MTLTextureDescriptor has width of zero.'
我不太确定 MTLTextureDescriptor
是什么或如何使宽度成为非零值。
谁能帮我弄清楚我做错了什么,或者这是正确的方法吗?同样,我只想找到一张图片,然后用 SKShapeNode
覆盖它。如果我必须使用 SKScene
来执行此操作,那很好,我只需要帮助弄清楚如何执行此操作。如果这不是最好的方法,有人可以指点我可以找到仪表解决方案的文档吗?
谢谢!
那是因为size
这里let size = imageAnchor.referenceImage.physicalSize
的宽度和高度都是零。
您获得的尺寸将是您为参考图片设置的 physicalSize
,肯定小于 1 米。因此,当您将其用作 skScene
的大小时,您创建的高度和宽度都为零。
例如:如果您的参考图片的物理尺寸为 10x15 厘米,则您的 size
的宽度为 0.1,高度为 0.15。这将更改为零。
我推荐:
let physicalSize = imageAnchor.referenceImage.physicalSize
let size = CGSize(width: physicalSize.width * 1000, height: physicalSize.height * 1000)