纳金网

标题: 动态载入外部物体,Loading 3d models at runtime in Unity3d [打印本页]

作者: 会飞的鱼    时间: 2011-12-7 15:13
标题: 动态载入外部物体,Loading 3d models at runtime in Unity3d
很有用的教程,如何在运行时动态的载入外部物体。
This post is an addition to the last series I wrote about dynamic content loading in Unity3D.
When building a dynamic application, every now and then you might need to load some 3d models at runtime. Either because there's a database with models you want to use or just because you want someone to update the models without rebuilding the project. I actually had this situation a few weeks ago. Surprisingly, this option does not come with Unity out of the box.
Asset bundles and resource folders
But first thing first. I said there's no ready-to-use way to load 3d models at runtime, but it's not 100% true. In fact, there are two options: asset bundles and resource folders. These methods are discussed in depth here.
They can both be useful in some cases, but they both have one fundamental drawback: in order to create them, someone needs to open the project in Unity, import the 3d models, create the bundles or resource folders and then export the whole thing again. To create an AssetBundle you need to run a script in the editor, while the Resource Folders... honestly, I failed to even make them work!
In brief, it's all too complicated for what I was looking for.
A straightforward solution
What I needed and wanted was easy and simple: to load a 3d object (geometry + some materials and textures) at runtime, using just a URL as parameter. The only solution seemed to write my own importer.
Basically, I had the choice between Collada and Wavefornt OBJ. I would choose OBJ any time of the day because it's a simple, concise plain-text format, while Collada is bloated and is XML-based.
It's not that I hate XML (although I'm not a big fan either) but in order to parse XML you need to include a pretty weighty DLL in your *.unity3d file, around 850Kb, which in this case (and in many others) defeats the purpose. Still, it's good to know that it possible, and there are situations when it's ok to use it. If you want to learn more about Unity3D and XML there's a awesome article on this topic by Paul Tondeur.
It turned out that while it's not rocket science to write an OBJ importer, it's not exactly banal either. I spent a few days coding it so I thought I'll share this with everyone - maybe someone will make good use of it.
Here's a package with the source code, along with a simple scene demonstrating how it works. In fact it couldn't be simpler. All you have to do is to create an instance of the OBJ class and start a coroutine to load the contents, like this:
PLAIN TEXT C#:

1.string path = "http://www.everyday3d/unity3d/obj/monkey.obj";

2.OBJ obj = new OBJ();

3.StartCoroutine(obj.Load(path));

Supported features
■Vertex, normals and UV data

■Multiple objects per file

■Vertex groups and multiple materials per object

■MTL files, diffuse and specular materials

■Basic textures

And it's all at a cost of ~12Kb extra added to your final file!
Testing and the universality of OBJ format
In the 3D world the OBJ format is nearly universal - I think that every 3D editor in existence is able to export to this format. This also means that Wavefront files come in many different flavors.
I tested the code against models created with Blender and against common sense assumptions on how an OBJ file can be constructed. If it doesn't work with files exported from your editor send me the OBJ file, and if possible I'll update the code. It's released under the MIT license so feel free to use it in your projects, commercial or not.
That's it! I hope you'll find it useful!
转自互联网,版权归everyday3d所有!
作者: C.R.CAN    时间: 2012-2-8 23:31
精典,学习了!

作者: tc    时间: 2012-4-7 23:31
路过……

作者: tc    时间: 2012-6-20 23:25
无聊时可以刷屏幕 灌水 也可以试试 帖子的标题究竟可以写多长

作者: 菜刀吻电线    时间: 2012-8-31 01:12
谢谢楼主,真是太实用了

作者: 奇    时间: 2012-9-19 09:47
不错不错,收藏了

作者: tc    时间: 2012-10-6 23:27
真不错,全存下来了.

作者: 奇    时间: 2012-10-24 23:25
路过、路过、快到鸟,列位请继续...ing

作者: 菜刀吻电线    时间: 2013-1-25 12:57
心中有爱,爱咋咋地

作者: C.R.CAN    时间: 2013-2-25 23:19
响应天帅号召,顶





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5