3D 对象的重复模式(栅栏)
Repeating pattern of 3D objects (a fence)
我有一个栅栏的 3D 模型。该模型包含 1 个起点 post 和一个终点 post,中间有一个连接玻璃板。假设我希望能够指定任意长度的栅栏,并将其放置在虚拟世界中。如果可能的话,我将如何处理原始 3D 模型以将其更改为这个长度以及额外的围栏 posts?
例如:假设我想放置一个总长度为 L 的 4 posts 的栅栏,其中栅栏的高度与原始模型相同,但连接窗格的宽度可以改变取决于长度 L.
|-|-|-|
据我所知,现在只有几种可能的方法:
1) 通过制作某种自定义几何体来操纵原始对象。我真的不知道从哪里开始,只是我需要扩展或继承 SCNGeometry property,然后以某种有意义的方式更改几何体的顶点。
2) 根据想要的 posts 的数量和相对于总长度 L 缩放原始模型中 posts 和窗格的宽度(从而保持正确的尺寸posts),然后将栅栏的多个实例并排放置在 posts 重叠的位置。
由于可能会出现渲染问题,这可能不是理想的方法,但这可能是唯一的方法。
任何其他关于我应该寻找什么的建议或提示将不胜感激。
这是一个测试代码(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
}
}
}
P.S. 在 iOS 项目中使用 UIViewController
、UIImage
和 UIColor
.
这是一个平面纹理,用于检查距离是否真实:
测量工具实施...
If you'd like to know how to implement a measure tool
go here.
我有一个栅栏的 3D 模型。该模型包含 1 个起点 post 和一个终点 post,中间有一个连接玻璃板。假设我希望能够指定任意长度的栅栏,并将其放置在虚拟世界中。如果可能的话,我将如何处理原始 3D 模型以将其更改为这个长度以及额外的围栏 posts?
例如:假设我想放置一个总长度为 L 的 4 posts 的栅栏,其中栅栏的高度与原始模型相同,但连接窗格的宽度可以改变取决于长度 L.
|-|-|-|
据我所知,现在只有几种可能的方法:
1) 通过制作某种自定义几何体来操纵原始对象。我真的不知道从哪里开始,只是我需要扩展或继承 SCNGeometry property,然后以某种有意义的方式更改几何体的顶点。
2) 根据想要的 posts 的数量和相对于总长度 L 缩放原始模型中 posts 和窗格的宽度(从而保持正确的尺寸posts),然后将栅栏的多个实例并排放置在 posts 重叠的位置。 由于可能会出现渲染问题,这可能不是理想的方法,但这可能是唯一的方法。
任何其他关于我应该寻找什么的建议或提示将不胜感激。
这是一个测试代码(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
}
}
}
P.S. 在 iOS 项目中使用 UIViewController
、UIImage
和 UIColor
.
这是一个平面纹理,用于检查距离是否真实:
测量工具实施...
If you'd like to know how to implement a
measure tool
go here.