0

Two nodes, The text and board are separated. I want the board to be the background for the text.

Is something wrong?

I referred to this site!

let housenode = SCNNode()

Method to create a text node

//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
}

Method to create a board node

//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
}

Method to place a node

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)

}
Andronicus
  • 25,419
  • 17
  • 47
  • 88
WkWk
  • 5
  • 1

1 Answers1

0

You're adding the panelNode as a child node of your text here:

sphereNode.addChildNode(panelNode)

This means that the position you set inside your addPanelToTheWorld() function is relative to the text node.

A quick fix to put the board at the right position would be to change that relative position. For instance, inside addPanelToTheWorld() change

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

to

box.position = SCNVector3(w/2, h, 0)
alpaca0114
  • 31
  • 3
  • Thank you very much! I wrote the source according to the advice and it worked! I was so in trouble that I was saved! – WkWk Aug 18 '19 at 07:14