Как загрузить вертикальную плоскость между двумя выбранными точками SCNVector3? - PullRequest
0 голосов
/ 28 июня 2019

Я выбрал две точки 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)

}

enter image description here

1 Ответ

1 голос
/ 28 июня 2019

Попробуйте этот код.

Я использовал SCNPlane, а не SCNBox, это было бы легко, поскольку у него нет глубины

В wallMaterial() вы можете просто создать двусторонний SCNMatirial object

В вашем случае вам нужно повернуть его так, чтобы оба ребра совпали с и две точки, что делает 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
}
...