COLLADA textures not appearing in Scene Kit - ios

I'm trying to view a file called test.dae using Scene Kit. When I click on the file in the editor, the object preview in the editor window is modeled properly, including texture. However, when I create a scene with test.dae, the model shows up, but without texture.
Here's my code:
var scene = SCNScene(named: "art.scnassets/test.dae")
mySCNView.scene = scene
mySCNView.allowsCameraControl = true
mySCNView.autoenablesDefaultLighting = true;
mySCNView.backgroundColor = UIColor.darkGrayColor()
I can't figure out why the texture would load in the preview, but not in the actual SCNView. Any help would be much appreciated.

Figured it out. When exporting from .obj to .dae in MeshLab, checking the Color and Normal options did the trick.

Related

3D model not loading correctly in Scenekit

So I purchased a 3D model of a car to display in my ARKIT app.The folder consisted of different model formats and a single image.
I dragged and dropped the .dae file to my project. This is the file structure.
However when adding to the scene the axis would be completely off and not load correctly. It did not load the complete model with textures, and would load above me.
See example:
Then I decided to load the .Blender file in Blender and export again as a colada .dae file.
This was the result when adding to my project.
When loading it in the real world a half car would appear where I stood at a ridiculously large scale.
I have tried the project with a separate model and it worked fine. Does anyone have any ideas how I can work with the 3D Model to load correctly in my app ?
I had some problems scaling models I have purchased. You aren't supposed to post links, but it's too big to paste in - I followed this guide to scale down the models and rotate them into the proper position in blender before exporting.
https://jibransyed.wordpress.com/2014/06/05/how-to-correct-scale-and-rotation-of-static-blender-models-for-unity/
On most models I use this to load:
func loadCollada(sceneName: String, objName: String) -> SCNNode
{
let vScene = SCNScene(named: sceneName)!
let gObject = vScene.rootNode.childNode(withName: objName,
recursively: true)!
return gObject
}
On some larger models, I had to do it this way:
func collada2SCNNode(filepath: String) -> SCNNode
{
let returnNode = SCNNode()
let scene = SCNScene(named: filepath)
for vNode in (scene?.rootNode.childNodes)!
{
returnNode.addChildNode(vNode)
}
return returnNode
}
Then set Y UP:
scntool --convert fighter0.dae --format c3d --output out.dae --force-y-up --force-interleaved --look-for-pvrtc-image

Loading SketchUp Models in SceneKit for ARKit App

I have a scene.scn file which contains a car.dae model. Here is the image:
I have the following code to load the scene.
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
// Create a new scene
let scene = SCNScene(named: "art.scnassets/scene.scn")!
// Set the scene to the view
sceneView.scene = scene
}
I do not see the car being loading in the scene. Any ideas?
The following workflow produces repeatable results for me when exporting a 3D model from SketchUp Make 2017 (I use the Simple Template - Meters for drawing) to Xcode 9.0:
In Xcode, create a SceneKit asset catalog: Create an assets catalog and change the extension to .scnassets before saving.
In Xcode, in the asset catalog Settings, ensure that Always use the Y-up axis is not checked. This setting is buggy; while it works during runtime, it isn't taken into account by the Scene Editor.
In SketchUp, export the model using Export 3D Model... and select the file format COLLADA File (*.dae). Export it to the .scnassets folder of the asset catalog (or a subfolder thereof).
Hint: If you export somewhere else, use the Finder to copy or move the files. Don't drag-and-drop the exported model to Xcode, as it would only create a reference to the model and you would lose the textures on the way, as they are referenced in the .dae file by a relative path.
In Xcode, create a SceneKit Scene File: First, select the project and create a New file with the template SceneKit Scene File there; Save it in the .scnassets folder; Then delete the file (Remove reference) from your project, so that it is referenced only in the .scnassets folder.
Hint: You can't create a .scn file directly in the asset catalog, as the New file feature creates an empty file that the Scene Editor won't recognize, even if you rename it with the .scn extension.
Embed the .dae file in the scene and fix the axes:
a. Open the .scn file.
b. Drag-and-drop the .dae file to the scene. This creates a reference to the model. Ensure that the reference to the model is selected.
c. In the Node Inspector, under Transforms, select the Editing space: Local.
d. Set the local Position to x: 0, y: 0, z: 0 (or whatever position is needed).
e. Set the local Euler angles to x: -90, y: 0, z: 0. Now, the object is oriented as expected, e.g. its front is displayed when the point of view Front is selected in the toolbar at the bottom of the scene editor.
f. Set the local Scale to x: 0.0254, y: 0.0254, z: 0.0254. I assume that the .dae file import expects sizes in inches.
g. Still in the Node Inspector, under Transforms, select the Editing space: World. Check the size of the Bounding Box. I assume that meters are used as measurement unit here. Save the changes.
There's a flag in Xcode's scnassets inspector to set which axis is "up". In SceneKit, Y is up. IIRC SketchUp uses Z up by default.
Tip: I found for all the collada (dae) models created by SketchUp you need to adjust the Euler x-axis to -90 in the scene editor
In order to get the model the right way up when you load it.

SCNScene from collada file node.geometry is nil

The problem is in the title. I need to access the SCNGeometry of a SCNNode and then edit the SCNMaterial collection of the SCNGeometry but when I inspect the node almost all its properties are set to nil including the geometry property. The weird thing is that all the nodes have been loaded from a .dae file and are all rendered fine in the scene. Any insight would be much appreciated.
are you sure that you are retrieving the correct node? What API do you use to do that? What do you see in the SceneKit editor that's built into Xcode?

Show shadow for SCNScene from .dae file [iOS]

I've got a SCNView with a SCNScene from a .dae file. I can access the objects in the scene, move and modify them properly, but I don't find the way to show the shadow projected by the light over the object.
SCNView *myScene.scene = [SCNScene sceneNamed:#"scene.dae"];
The scene already includes lights and shadows, I can preview them, but the SCNView only shows the lights, not the projected shadow.
Do you know how to make the shadow visible? I know SpriteKit allows it, but I need to use SceneKit for my 3D scene.
Many thanks

How to use multiple cameras for the same scene in Scene Kit

I have 2 SCNViews next to each other and both should show the same scene but through different cameras.
It seems to me that Scene Kit uses that node with a camera that is highest in the node hierarchy so I tried something like that
leftSceneView.scene?.rootNode.addChildNode(scene.rootNode)
rightSceneView.scene?.rootNode.addChildNode(scene.rootNode)
leftSceneView.scene?.rootNode.addChildNode(cameraNodeLeft)
rightSceneView.scene?.rootNode.addChildNode(cameraNodeRight)
but I got the error message [SCNKit ERROR] removing the root node of a scene from its scene is not allowed and it did not work at all.
Has anybody a suggestion how I can achieve that?
Toby
This answer regards the issue (mentioned by #WolfLink) that having multiple SCNView objects with different cameras showing the same SCNScene causes the entire update sequence to occur multiple times.
To fix this, all you have to do is set the SCNSceneRendererDelegate to only one of the SCNView objects used. Assuming that the delegate is taking care of all of the nodes in the SCNScene and is updating them accordingly, the other SCNView objects that don't have an assigned delegate would still be able to see all of the changes occurring. This is because the changes are updated in the actual SCNScene that all of the SCNView objects are hooked up to.
So, using the original answer by #Toyos, the way to go about using 2 cameras without causing the entire update sequence to fire twice is:
// Set up sceneView 1
sceneView1.scene = scnScene
sceneView1.pointOfView = scnScene.camera1
sceneView1.delegate = scnScene
// Set up sceneView 2
sceneView2.scene = scnScene
sceneView2.pointOfView = scnScene.camera2
(Disclaimer: I was going to comment on the answer by #Toyos, but I currently don't have enough reputation for that since I'm still new to the StackOverflow community).
Set the point of view to render the scene with the "pointOfView" property of the SCNView.
scnView.pointOfView = cameraNodeLeft;
SCNTransaction.animationDuration = 1.0
scnView.defaultCameraController.frameNodes([node])
let newPositoin = SCNVector3(node.boundingSphere.center.x, 0, nodeShereRadius*2)
cameraNode.position = newPositoin
scnView.pointOfView = cameraNode
SCNTransaction.commit()

Resources