0

I use the SceneKit library (Swift, iOS) to show the mesh (.obj file) of a building in my iPhone app. Mesh is, in fact, a 3D model of the entire building. Mesh is one big monolith file. So consider the mesh file contains all elements (walls, floor, rooms).

var myNode: SCNNode?

let myscene = try SCNScene(url: urlToMyOBJFile, options: nil)
if let mesh = myscene.rootNode.childNodes.first {
    self.myNode = mesh
    self.mainScene.rootNode.addChildNode(self.myNode!)
}


On the "hide" button tapped I run animation to hide the mesh:

 let action = SCNAction.fadeOut(duration: 3)
 self.myNode?.runAction(action)

During this animation opacity of the mesh changes from 1 to 0.
It creates an effect when, during the animation, walls became semi-transparent, and users can see through them.
I mean, during the animation all mesh became semi-transparent (for example opacity = 0.5 in the middle of the animation), so do walls.

And users can see the parts of the mesh which are located behind walls (aka next room).

How to make mesh fade out without the "x-ray eyes" effect?

screenshots:
1 - wall
2 - wall during SCNAction.fadeOut() animation became semi-transparent, you can see other rooms behind the wall.

enter image description here enter image description here



more code on how I compose the scene (answer for ZAY's comment):

let sceneView = SCNView()
let mainScene = SCNScene()

override func viewDidLoad() {
    super.viewDidLoad()
    
    sceneView.frame = self.view.frame
    self.view.addSubview(sceneView)

    sceneView.scene = mainScene
    let camera = SCNCamera()
    let cameraNode = SCNNode()
    cameraNode.camera = camera
    cameraNode.position = SCNVector3(x: 0.0, y: 0.0, z: 3.0)
    let light = SCNLight()
    light.type = SCNLightTypeOmni
    let lightNode = SCNNode()
    lightNode.light = light
    lightNode.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5)
    let cubeGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.0)

    scene.rootNode.addChildNode(lightNode)
    scene.rootNode.addChildNode(cameraNode)
}
Ihor Kram
  • 41
  • 5
  • can you provide more information on how you compose the scene? Can you add print-screens? – ZAY Jun 27 '23 at 11:09
  • How do you add the walls to the scene? Do you add them to "myNode" or are they added to the rootNode of the main scene? In order to help you better with your project you could share it (google drive or whatever). I'd like to have a look at it what's going on. – ZAY Jun 28 '23 at 09:11
  • @ZAY Thank you for your question. I already have edited the description. So the myNode is created from .obj file. And this file is a big monolith 3D model which contains every element of the building (walls, cellar, floor etc.). To answering for your question, I don't add walls or other room separetly. They are the part of the myNode. – Ihor Kram Jun 29 '23 at 08:32

1 Answers1

0

@Ihor Kram: based on your comment I beleave the node you wish to fade out is a childNode of your myNode object - at some specific index. Open the object file with the visual editor within Xcode. On the left side you will find the Scene Graph section. This should give you an overview on the different nodes/objects the .obj fle contains. Start counting from the top to the bottom. The element on the top has index 0. Just count until you identify the node you wish to fade out. Then you can run the SCNAction to fade out the specific SCNNode like this:

 let action = SCNAction.fadeOut(duration: 3.0)
 self.myNode?.childNodes[3].runAction(action)

(assuming the Node you wish to fade out is at index 3 - like in this example)

Hope it will work.

In addition you could convert the .obj to a .scn file. The .scn file loads a bit faster to memory compared to other formats like .obj or .dae. .scn also has the advantage that it stores the materials way better if you define them within the visual editor. i.Ex. for .physicallyBased PBR textures.

ZAY
  • 3,882
  • 2
  • 13
  • 21