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

标签 ios swift xcode scenekit arkit

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

注意: Material 未显示正确尺寸。 Material 浆糊且大。

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

最佳答案

试试这段代码。

我使用的是 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
}

关于ios - 如何在选定的两个 SCNVector3 点之间加载垂直平面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56802624/

相关文章:

xcode - 从 UITableViewCell 获取单元格的实际高度大小

ios - 当值为 nil 更新到 3.10 时,iOS 上的 Google Analytics 崩溃

ios - 我无法将按钮与操作相关联

ios - 在 tableview Objective-c 中对齐行中心的单元格

ios - 使用 IOS 5 发出 HTTP GET 请求的最简单方法是什么?

Swift 子级在嵌套或 ObserveSingleEvent 时返回 Null,在未嵌套时返回良好

Swift Firebase 如何停止进程?

ios - 如何修复iOS应用程序中的 "http not found in com.apple.developer.associated-domains entitlement"?

ios - Swift-Firebase 多个观察者

ios - 如何在IOS中对约束进行一一动画?