【发布时间】: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)
}
【问题讨论】: