我试图基于放置在水平面上的点创建一个2D填充形状。我查看了其他问题,并整理了以下内容。
这是此过程中使用的全局数组
var rectNodes: Array<SCNVector3> = []
此功能允许用户在会话发现后将小的红色球形节点放置在水平面上。它被称为点击按钮
func addBasePointFunc() {
//set up hit testing for plane
let hitTest = sceneView.hitTest(screenCenter, types: .existingPlaneUsingExtent)
guard let worldTransformColumn3 = hitTest.first?.worldTransform.columns.3 else {return}
//setup spehere node
let sphere = SCNSphere(radius: 0.02)
sphere.firstMaterial?.diffuse.contents = UIColor.red
sphere.firstMaterial?.lightingModel = .constant
sphere.name = "sphere"
//create sphere node
let sphereNode = SCNNode(geometry: sphere)
sphereNode.worldPosition = SCNVector3(worldTransformColumn3.x, worldTransformColumn3.y, worldTransformColumn3.z)
sphereNode.name = "spherenode"
//add node to scene
sceneView.scene.rootNode.addChildNode(sphereNode)
//add node to array
rectNodes.append(sphereNode.position)
}
一旦放置了所有节点并完成用户操作,便会在单击按钮时调用以下功能。
func drawShape(nodeArray: Array<SCNVector3>){
let strokeBezierPath = UIBezierPath()
strokeBezierPath.lineWidth = 0.2
strokeBezierPath.fill()
//loop over nodes to create shape
for index in 0..<nodeArray.count {
let node = nodeArray[index]
if (index == 0) {
let point = CGPoint(x: CGFloat(node.x), y: CGFloat(node.z))
strokeBezierPath.move(to: point)
} else {
let point = CGPoint(x: CGFloat(node.x), y: CGFloat(node.z))
strokeBezierPath.addLine(to: point)
}
}
//close stroke after loop
strokeBezierPath.close()
let cgPath = strokeBezierPath.cgPath.copy(
strokingWithWidth: strokeBezierPath.lineWidth,
lineCap: strokeBezierPath.lineCapStyle,
lineJoin: strokeBezierPath.lineJoinStyle,
miterLimit: strokeBezierPath.miterLimit)
//create path based on cgPath
let bezierPath = UIBezierPath(cgPath: cgPath)
//Does nothing?
bezierPath.fill()
//create shape from points in array
let shape = SCNShape(path: bezierPath, extrusionDepth: 0.03)
//create color for shape .. only does outline?
shape.firstMaterial?.diffuse.contents = UIColor.purple
let node = SCNNode(geometry: shape)
//set name for other user functions
node.name = "base"
// set z position? -- Issue
node.position.z = shape.boundingBox.max.x
//set y position -- Issue
node.position.y = -0.5
//set pivot -- Issue
node.pivot = SCNMatrix4MakeTranslation(shape.boundingBox.min.x * 0.5, shape.boundingBox.min.x * 0.5, 0)
//rotation x angle to be flat.
node.eulerAngles.x = GLKMathDegreesToRadians(90)
//add to scene
sceneView.scene.rootNode.addChildNode(node)
//remove all sphere nodes from array
rectNodes.removeAll()
//remove all sphere nodes from scene
sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
if node.name == "spherenode" {
node.removeFromParentNode()
}
}
}
发生的事情是,它将在节点的形状中创建紫色轮廓形状,但是将处于不希望的位置并且未被填充。
我的问题是
我乐于接受其他方式,但是被困住了。如果您需要其他联系方式,图片或信息,请告诉我。 提前致谢。