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