我选择了两个SCNVector3点,并加载相同宽度和高度的SCNBox,即两个SCNVector3点之间的距离和固定长度。但是不要在两点之间加载适当的东西。如何在选定的两个SCNVector3点之间正确加载垂直平面。

注意:材料显示的尺寸不正确。材料是淀粉状且大的。

let pos1 = SCNVector3(x: 0.26943, y: -0.022023335, z: 0.22480452)
let pos2 = SCNVector3(x: 0.3568532, y: -0.02038227, z: 0.30196056)

//Find distance between two points
let d = self.distanceBetweenPoints(A: pos1, B: pos2)
func distanceBetweenPoints(A: SCNVector3, B: SCNVector3) -> CGFloat {
    let l = sqrt(
        (A.x - B.x) * (A.x - B.x)
            +   (A.y - B.y) * (A.y - B.y)
            +   (A.z - B.z) * (A.z - B.z)
    )
    return CGFloat(l)
}

// width = distance between two points
//position = mid points of two points

func addwall(width:CGFloat, position : SCNVector3){

    let box = SCNBox(width: width, height: width, length: 0.001, chamferRadius: 0)

    let width = width
    let height = 2.3

    let material = SCNMaterial()
    //material.diffuse.contents = UIColor.red
    material.diffuse.contents = UIImage(named: "Titles.jpg")
    material.isDoubleSided = true

    material.diffuse.contentsTransform = SCNMatrix4MakeScale(1, -1, 1)
    material.diffuse.wrapS = .repeat
    material.diffuse.wrapT = .repeat
    box.materials = [material]

    var p = position
    let boxNode = SCNNode(geometry: box)
    let (minVec, maxVec) = boxNode.boundingBox
    let unScaledHeight = maxVec.y - minVec.y
    let unScaledWidth = maxVec.x - minVec.x
    let unScaleddep = maxVec.z - minVec.z
    p.y = (p.y) + (unScaledHeight/2)
    boxNode.position = p

    boxNode.name = "wall"
    self.sceneView.scene.rootNode.addChildNode(boxNode)

}

ios - 如何在选定的两个SCNVector3点之间加载垂直平面?-LMLPHP

最佳答案

试试这个代码。

我用过SCNPlane而不是SCNBox,这很容易,因为它没有深度

wallMaterial()中,您可以简单地创建双面SCNMatirial对象

在您的情况下,您需要旋转它,以便两条边都与from相匹配,并且node.eulerAngles所做的两点可能是这条唯一的线可以解决您的问题,而可能不会

func node(from:SCNVector3,
                to:SCNVector3,height:Float) -> SCNNode {
    let distance = MathHelper().getMeasurementBetween(vector1: from, and: to)

    let wall = SCNPlane(width: CGFloat(distance),
                        height: CGFloat(height))
    wall.firstMaterial = wallMaterial()
    let node = SCNNode(geometry: wall)

    node.renderingOrder = -10



    let normalizedTO = to.normalized()
    let normalizedFrom = from.normalized()
    let angleBetweenTwoVectors = normalizedTO.cross(normalizedFrom)

    var from = from
    var to = to



    node.position = SCNVector3(from.x + (to.x - from.x) * 0.5,
                               from.y + height * 0.5,
                               from.z + (to.z - from.z) * 0.5)

    // orientation of the wall is fairly simple. we only need to orient it around the y axis,
    // and the angle is calculated with 2d math.. now this calculation does not factor in the position of the
    // camera, and so if you move the cursor right relative to the starting position the
    // wall will be oriented away from the camera (in this app the wall material is set as double sided so you will not notice)
    // - obviously if you want to render something on the walls, this issue will need to be resolved.


    node.eulerAngles = SCNVector3(0, -atan2(to.x - node.position.x, from.z - node.position.z) - Float.pi * 0.5, 0)


    return node
}

10-06 02:55