简体   繁体   中英

Modify the Bounding Box Size of 3D model

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM