Modify the Bounding Box Size of 3D model

社会主义新天地 提交于 2019-12-10 21:24:44

问题


This function is supposed to add a 3D model to screen in a confined space:

func addCharacter(gridPosition: SCNVector3, levelNode: SCNNode){
    let carScene = SCNScene(named: "assets.scnassets/Models/cube.dae")

    // Create a material using the model_texture.tga image
    let carMaterial = SCNMaterial()
    carMaterial.diffuse.contents = UIImage(named: "assets.scnassets/Textures/model_texture.tga")
    carMaterial.locksAmbientWithDiffuse = false

    // Create a clone of the Car node of the carScene - you need a clone because you need to add many cars
    let carNode = carScene!.rootNode.childNodeWithName("Cube", recursively: false)!.clone() as SCNNode

    carNode.name = "Cube"

    carNode.position = gridPosition

    // Set the material
    carNode.geometry!.firstMaterial = carMaterial

    // Create a physicsbody for collision detection
    let carPhysicsBodyShape = SCNPhysicsShape(geometry: SCNBox(width: 0.30, height: 0.20, length: 0.16, chamferRadius: 0.0), options: nil)

    carNode.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Kinematic, shape: carPhysicsBodyShape)
    carNode.physicsBody!.categoryBitMask = PhysicsCategory.Car
    carNode.physicsBody!.collisionBitMask = PhysicsCategory.Player

    levelNode.addChildNode(carNode)
}

The code works if I change the dimensions of the dae file before adding it to Xcode, and by dimensions I mean the Bounding Box Size property of Xcode. I wanted to know if It was possible to change this bounding box size directly from Xcode. It does not seem possible from the UI. Is it possible with code? Or, even better, scale down the object maintaining proportions to a size for the XYZ between the range 0.3 -> 0.7? At the moment my objects show a box size of over 45 for XYZ. Furthermore, If I was to use a .scn file instead of a .dae in the code above, would that still work?

EDIT: If I change the size via code, would it have an impact on efficiency? I notice that for larger .dae models the fps drops from 60 to 30 and the game slows down.


回答1:


Changing carNode's scale property will reduce the apparent size of the car.

However, I think you'll be ahead to load your .DAE into the Xcode scene editor. That will allow you to scale it down ahead of time (in the Node Inspector, option-command-3). You can also, if you want, add your texture. Then save it as a .SCN file, which is compressed and should load faster.

Changing the scaling, either in code or in the Xcode scene editor, won't affect efficiency. Reducing the complexity of the car (the number of polygons/vertices) will speed things up, though.

Consider creating SCNLevelOfDetail instances for your car node. That will cause the renderer to use substitute, lower resolution geometry when the node is far from the camera. The WWDC 2014 slide deck demonstrates this, slide 38, AAPLSlideLOD.m.



来源:https://stackoverflow.com/questions/34569551/modify-the-bounding-box-size-of-3d-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!