gpt4 book ai didi

ios - 如何将结构或类对象附加到 SCNNode

转载 作者:行者123 更新时间:2023-11-28 05:41:04 24 4
gpt4 key购买 nike

我正在尝试创建一个场景,让不同的用户可以展示不同的汽车。当用户首次登录到应用程序时,他们会获得 uid、用户名等。当他们选择汽车时,汽车模型具有汽车类型和标识他们的用户对象。

我可以创建一辆汽车并实现 hitTest 来查找哪辆汽车被点击了。我想不通的是如何为被窃听的汽车分配数据模型,以便我知道谁是谁?

到目前为止我在节点上找到的唯一属性是 node.name ,我可以将 userId 附加到它,然后通过一系列用户来找出谁是谁,但如果我有很多用户,那似乎不切实际。删除节点时我还需要该属性:

sceneView.session.pause()
sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
// this would actually remove all the ferraries on the scene but this is just an example
if node.name == "ferrari" {
node.removeFromParentNode()
}
}

如何将数据模型附加到 SCNNode?

class User {
var userId: String?
var username: String?
}

class RaceCar {
var user: User?
var type: String?
}

// user creates an account and picks a ferrari

let raceCar = RaceCar()
let type = raceCar.type!

switch type {

case "ferrari":

createFerrariAndAddItToScene(raceCar)

case "bmw":
...etc
}

func createFerrariAndAddItToScene(_ raceCar: RaceCar) {

let image = art.scnassests/"\(raceCar.typ!e).jpg"

let material = SCNMaterial()
material.diffuse.contents = image

let plane = SCNPlane(width: 0.1, height: 0.1)
plane.materials = [material]

let node = SCNNode()
node.name = raceCar.type!
node.geometry = plane
node.position = SCNVector(0.0, 0.0, -0.2)

sceneView.scene.rootNode.addChildNode(node)
}

@objc func nodeWasTapped(_ sender: UITapGestureRecognizer) {

guard let sceneView = sender.view as? ARSCNView else { return }
let touchLocation = sender.location(in: sceneView)

let hitResults = sceneView.hitTest(touchLocation, options: [:])

if !hitResults.isEmpty {

guard let hitResult = hitResults.first else { return }
let node = hitResult.node

print(node.name) // I need to get some info from the dataModel here like node.raceCar.user.username or node.raceCar.user.userId
}
}

最佳答案

您可以子类化 SCNNode 并向其添加您需要的任何数据模型:

class CarNode: SCNNode {
var raceCar: RaceCar?

init(raceCar: RaceCar, geometry: SCNGeometry) {
super.init()

self.geometry = geometry

self.raceCar = raceCar
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

要创建新节点,只需使用这个新类:

func createFerrariAndAddItToScene(_ raceCar: RaceCar) {
...
let node = CarNode(raceCar: raceCar, geometry: plane)
...
}

要访问您的数据模型,您可以这样做:

@objc func nodeWasTapped(_ sender: UITapGestureRecognizer) {
...
if let node = hitResult.node as? CarNode {
print(node.raceCar.user.username)
}
}

关于ios - 如何将结构或类对象附加到 SCNNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56721340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com