【问题标题】:I want to display two nodes overlapping each other with ARKit我想用 ARKit 显示两个相互重叠的节点
【发布时间】:2019-08-18 11:25:11
【问题描述】:

两个节点, 文字和白板是分开的。 我希望白板成为文本的背景。

有什么问题吗?

我提到了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)

}

【问题讨论】:

    标签: swift arkit scnnode


    【解决方案1】:

    您在此处将 panelNode 添加为文本的子节点:

    sphereNode.addChildNode(panelNode)
    

    这意味着您在addPanelToTheWorld() 函数中设置的位置是相对于文本节点的。

    将电路板放置在正确位置的快速解决方法是更改​​该相对位置。比如在addPanelToTheWorld()里面改

    box.position = SCNVector3(0.1, 0.1, -0.1)
    

    box.position = SCNVector3(w/2, h, 0)
    

    【讨论】:

    • 非常感谢!我根据建议编写了源代码,并且成功了!我遇到了麻烦,所以我得救了!
    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2011-08-09
    • 2014-01-25
    • 2018-09-06
    • 1970-01-01
    相关资源
    最近更新 更多