Including textures when exporting from Blender to COLLADA/.dae format for use in SceneKit

╄→гoц情女王★ 提交于 2019-11-28 18:01:36
FlippinFun

Create a scnassets folder first (not required but useful)

  1. Create a folder on your desktop and give it an extension of ".scnassets"

  2. Put your collada(.dae) file along with any textures you will be using in it.

  3. Drag the folder into your project and save as copy like usual.

  4. Click on your scnassets folder and you will see a checked box (if not check it) for converting to a "y" up axis.

Assigning textures to your scene model

  1. Click on your collada(.dae) file inside your project in xcode.

  2. You should see your scene and on its left side a list of cameras, lights, materials, and such. This is your scene tree.

  3. Open the materials tab and click on one of your materials.

  4. On the right hand side in the inspection window click on the blue ball shaped icon(Material inspector) to view the diffuse, specularity, and such of that one material.

  5. Click on the diffuse tab and when it opens you should have an option of colors and your textures within your project. Select the texture you used on your model within 3d program. As long as you UV unwrapped them properly in your 3D program, they should apply instantly within your scene view.

What if I want to change my material after loading my scene? Glad you asked!

To do this we must use entryWithIdentifier method of SCNSceneSource class. I am going to use swift here because it is awesome! Here we go...

  1. Get the url of your scene(.dae) like so...

    let url = NSBundle.mainBundle().URLForResource("YourFolder.scnassets/yourScene", withExtension "dae")
    
  2. Now lets put that url to use...

    let source = SCNSceneSource(URL: url, options: nil)
    
  3. Click on your .dae and under Scene Graph is list of items one of which is your geometry. It will have a tee kettle just to the right of it signifying so. We are going to use its name here. Let say your geometry's name is Geo. Use it like so...

    let yourGeometry = source.entryWithIdentifier("Geo", withClass: SCNGeometry.self) as SCNGeometry
    
  4. Now we have a source attatched to a SCNNode called yourGeomerty. Lets create a new material with a UIColor like so...

    let newMaterial = SCNMaterial()
    newMaterial.diffuse.contents = UIColor.redColor()
    
  5. Finally we will switch out the old material with the newMaterial like so...

    yourGeometry.geometry.replaceMaterialAtIndex(0, withMaterial: newMaterial)
    

Play around with it and other indexes if you have more than one material. You can also use the UIImage class to use another texture instead of a color.

TIP If you want to add to or delete something from your scnassets folder, navigate to your project folder to do so and not xcode. This took me a while to figure out so thought I would save everyone the trouble.

Collada files don't embed textures, they only have references to them. Make sure that your textures are reachable from the collada file when you open it in Preview and make sure to include the textures in the app bundle when building an app.

3 things I had to do to make it work.

  1. Make sure images are packed in the .blend file (this is an option in UV editor)

  2. when you export, the file will not automatically include the uv's or the materials. there is a checkbox to include the UV's and Materials in the DAE. I missed this as well when doing it.

  3. This one you only need if your putting in a playground. The dae isn't quite applized, but i was able to use their scntool in the developer tools to export a dae that is. here is a sample command line that i used.

./scntool --convert ~/Documents/Art/BlenderArt/tableandappleUV.dae --format c3d --output ~/Documents/Table5.dae

notice the c3d format. use that and your playground can now also recognize the dae. (One other note: if you want to used the dae in the playground you need to put in resources folder of the playground)

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