我想用 ARKit 显示两个相互重叠的节点
I want to display two nodes overlapping each other with ARKit
两个节点,
文本和板是分开的。
我想让白板做文字的背景
有什么问题吗?
我参考了this site!
let housenode = SCNNode()
创建文本节点的方法
//TextNode
private func addTextToTheWorld() -> SCNNode{
let text = SCNText(string: "Hello", extrusionDepth: 0.5)
let font = UIFont(name: "Futura", size: 2)
text.font = font
text.alignmentMode = CATextLayerAlignmentMode.center.rawValue
text.firstMaterial?.diffuse.contents = UIColor.red
text.firstMaterial?.specular.contents = UIColor.white
text.firstMaterial?.isDoubleSided = true
text.chamferRadius = 0.01
let (minBound, maxBound) = text.boundingBox
let textNode = SCNNode(geometry: text)
textNode.pivot = SCNMatrix4MakeTranslation( (maxBound.x - minBound.x)/2, minBound.y, 0.02/2)
textNode.scale = SCNVector3Make(0.1, 0.1, 0.1)
textNode.position = SCNVector3(0.1, 0.1, -0.1)
return textNode
}
创建看板节点的方法
//PanelNode
private func addPanelToTheWorld(node:SCNNode) -> SCNNode{
let (min, max) = (node.boundingBox)
let w = CGFloat(max.x - min.x)
let h = CGFloat(max.y - min.y)
let boxGeo = SCNBox(width: w * 1.1, height: h * 1.1, length: 0, chamferRadius: 0)
boxGeo.firstMaterial?.diffuse.contents = UIColor.blue
let box = SCNNode(geometry: boxGeo)
//box.scale = SCNVector3Make(0.1, 0.1, 0.1)
box.position = SCNVector3(0.1, 0.1, -0.1)
//scene.rootNode.addChildNode(box)
return box
}
放置节点的方法
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let sphereNode = addTextToTheWorld()
sphereNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red
let panelNode = addPanelToTheWorld(node:sphereNode)
sphereNode.addChildNode(panelNode)
housenode.addChildNode(sphereNode)
let infrontCamera = SCNVector3Make(0, 0, -0.3)
guard let cameraNode = sceneView.pointOfView else {
return
}
let pointInWorld = cameraNode.convertPosition(infrontCamera, to: nil)
var screenPosition = sceneView.projectPoint(pointInWorld)
guard let location : CGPoint = touches.first?.location(in: sceneView) else{
return
}
screenPosition.x = Float(location.x)
screenPosition.y = Float(location.y)
let finalPostion = sceneView.unprojectPoint(screenPosition)
housenode.eulerAngles = cameraNode.eulerAngles
housenode.position = finalPostion
self.sceneView.scene.rootNode.addChildNode(housenode)
}
您要将 panelNode 添加为此处文本的子节点:
sphereNode.addChildNode(panelNode)
这意味着您在 addPanelToTheWorld()
函数中设置的位置是相对于文本节点的。
将电路板放在正确位置的快速修复方法是更改该相对位置。例如,在 addPanelToTheWorld()
内更改
box.position = SCNVector3(0.1, 0.1, -0.1)
到
box.position = SCNVector3(w/2, h, 0)
两个节点, 文本和板是分开的。 我想让白板做文字的背景
有什么问题吗?
我参考了this site!
let housenode = SCNNode()
创建文本节点的方法
//TextNode
private func addTextToTheWorld() -> SCNNode{
let text = SCNText(string: "Hello", extrusionDepth: 0.5)
let font = UIFont(name: "Futura", size: 2)
text.font = font
text.alignmentMode = CATextLayerAlignmentMode.center.rawValue
text.firstMaterial?.diffuse.contents = UIColor.red
text.firstMaterial?.specular.contents = UIColor.white
text.firstMaterial?.isDoubleSided = true
text.chamferRadius = 0.01
let (minBound, maxBound) = text.boundingBox
let textNode = SCNNode(geometry: text)
textNode.pivot = SCNMatrix4MakeTranslation( (maxBound.x - minBound.x)/2, minBound.y, 0.02/2)
textNode.scale = SCNVector3Make(0.1, 0.1, 0.1)
textNode.position = SCNVector3(0.1, 0.1, -0.1)
return textNode
}
创建看板节点的方法
//PanelNode
private func addPanelToTheWorld(node:SCNNode) -> SCNNode{
let (min, max) = (node.boundingBox)
let w = CGFloat(max.x - min.x)
let h = CGFloat(max.y - min.y)
let boxGeo = SCNBox(width: w * 1.1, height: h * 1.1, length: 0, chamferRadius: 0)
boxGeo.firstMaterial?.diffuse.contents = UIColor.blue
let box = SCNNode(geometry: boxGeo)
//box.scale = SCNVector3Make(0.1, 0.1, 0.1)
box.position = SCNVector3(0.1, 0.1, -0.1)
//scene.rootNode.addChildNode(box)
return box
}
放置节点的方法
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let sphereNode = addTextToTheWorld()
sphereNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red
let panelNode = addPanelToTheWorld(node:sphereNode)
sphereNode.addChildNode(panelNode)
housenode.addChildNode(sphereNode)
let infrontCamera = SCNVector3Make(0, 0, -0.3)
guard let cameraNode = sceneView.pointOfView else {
return
}
let pointInWorld = cameraNode.convertPosition(infrontCamera, to: nil)
var screenPosition = sceneView.projectPoint(pointInWorld)
guard let location : CGPoint = touches.first?.location(in: sceneView) else{
return
}
screenPosition.x = Float(location.x)
screenPosition.y = Float(location.y)
let finalPostion = sceneView.unprojectPoint(screenPosition)
housenode.eulerAngles = cameraNode.eulerAngles
housenode.position = finalPostion
self.sceneView.scene.rootNode.addChildNode(housenode)
}
您要将 panelNode 添加为此处文本的子节点:
sphereNode.addChildNode(panelNode)
这意味着您在 addPanelToTheWorld()
函数中设置的位置是相对于文本节点的。
将电路板放在正确位置的快速修复方法是更改该相对位置。例如,在 addPanelToTheWorld()
内更改
box.position = SCNVector3(0.1, 0.1, -0.1)
到
box.position = SCNVector3(w/2, h, 0)