3D对象(栅栏)的重复图案

时间:2019-05-02 16:11:11

标签: swift scenekit arkit

我有一个栅栏的3D模型。该模型包含1个起点柱和一个终点柱,两者之间有一块连接的玻璃板。考虑一下,我希望能够指定要在虚拟世界中放置的围栏的任意长度。如果有可能,我将如何处理原始3D模型以将其更改为该长度以及附加的围栏柱?

例如:假设我要放置一个栅栏,栅栏的长度为L,长度为4,栅栏的长度为4,栅栏的高度与原始模型相同,但是连接窗格的宽度可能会随长度L而变化。 / p>

|-|-|-|

正如我现在看到的,只有几种可行的方法可以解决:

1)通过制作某种自定义几何来操纵原始对象。我真的不知道从哪里开始,只是我需要扩展或继承SCNGeometry property,然后以某种有意义的方式更改几何的顶点。

2)根据所需的柱子数量和相对于总长度L(从而保持柱子的正确尺寸)缩放原始模型中柱子和窗格的宽度,然后沿其放置栅栏的多个实例职位重叠的地方彼此相对。 由于可能会出现渲染问题,因此这可能不理想,但这可能是唯一的方法。

对于我应该寻找的任何其他建议或提示,将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是一个测试代码(macOS版本),您可以将其用作起点。尝试一下以了解for-in循环的工作原理。另外,您需要实现一种方法来计算栅栏的距离(这是一个distance值)。

import SceneKit

class GameViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = SCNScene()
        let scnView = self.view as! SCNView
        scnView.scene = scene
        scnView.allowsCameraControl = true
        scnView.backgroundColor = NSColor.black


        // A PLANE FOR CHECKING A SIZE OF 3D STRUCTURE
        let plane = SCNNode(geometry: SCNPlane(width: 15, height: 1))
        plane.geometry?.firstMaterial?.diffuse.contents = NSImage(named: NSImage.Name("UVmap.jpg"))
        plane.position = SCNVector3(7.5, -1.5, 0.5)
        plane.rotation = SCNVector4(1, 0, 0, -CGFloat.pi/2)
        scene.rootNode.addChildNode(plane)


        // ALL YOU NEED TO DO HERE – IS TO CHANGE THIS VALUE
        let distance: CGFloat = 12.7     // comes from tape-measure tool

        let nth_part: CGFloat = (distance - CGFloat(Int(distance))) / CGFloat(Int(distance))
        var section: CGFloat = 0

        print(distance)                  // floating point distance
        print(Int(distance))             // rounded to lower integer
        print(nth_part)                  // 0.7 / 12 = 0.058

        for _ in 1...Int(distance) {     // loop for building 12 sections

            let postNode = SCNNode(geometry: SCNBox(width: 0.11, height: 3, length: 0.11, chamferRadius: 0))
            postNode.position = SCNVector3(0, 0, 0)
            scene.rootNode.addChildNode(postNode)

            // width of each pane is 1m + 0.058m
            let paneNode = SCNNode(geometry: SCNBox(width: (1 + nth_part), height: 2.9, length: 0.1, chamferRadius: 0))
            paneNode.geometry?.firstMaterial?.diffuse.contents = NSColor.blue

            // offset for next pane -– 0.558m, 1.116m, 2.174m, etc
            paneNode.position = SCNVector3(((section + 0.5) + (nth_part * section)), 0, 0)
            scene.rootNode.addChildNode(paneNode)

            // offset for next post -– 1.058m, 2.116m, 3.174m, etc
            let lastPostostNode = SCNNode(geometry: SCNBox(width: 0.11, height: 3, length: 0.11, chamferRadius: 0))
            lastPostostNode.position = SCNVector3(((section + 1) + (nth_part * section)), 0, 0)
            scene.rootNode.addChildNode(lastPostostNode)

            section += 1
        }
    }
}

PS 。在iOS项目中,使用UIViewControllerUIImageUIColor

enter image description here

以下是飞机的纹理,用于检查距离是否为真:

enter image description here

测量工具正在实施...

  

如果您想知道如何实现 measure tool go here