- Home /
Mesh Obj export/import WITH SHADER REFERENCE
Hi everyone,
I've been playing around with the ObjExporter to export several meshes to an .obj file at once. In order to make Unity use existing materials when importing a previously exported mesh I simply put a folder called "Materials" with the original materials into the obj's folder before the import and changing the default import settings.
As I see it the ObjExporter doesn't properly export the materials (it only writes grey materials to the .mtl file). However, the .mtl file is still useful in the way that it tells unity which mesh uses which material and with the correct import setting it simply uses existing materials with the same name that is used in the .mtl file and referenced in the .obj file by usemtl
.
In addition to the material reference I really need a reference to shaders. In my case game objects in the scene may have the same material but I assign different shaders to them during runtime.
Is there anything like "`useshader`" that I can write to the .obj file so that unity uses the material specified by usemtl
with the specified shader (which preferably lies within a folder called "Shaders" next to the "Materials" folder ???
This is really important because the shaders are assigned dynamically to a lot of objects and I just can't assign them myself after import !!!
Thanks in advance for your help!
EDIT
Another idea that just came to me is to write the references to a file during the .obj export and write a custom import script that assigns the shaders afer the model was imported according to this file. How would I go about doing this ???
Answer by Schnodahipfe · Jul 03, 2013 at 09:13 AM
So in the end I wrote the references to a TextAsset. Then, in another project where I import the previously exported mesh, I had to attach a script to the GameObject (that was initialized from prefab, which is turn was created by unity from the .obj and .mtl files), which reads the TextAsset and assigns the shaders accordingly.
Not really a pretty solution :-/ . Does anyone have a better solution in mind?
Answer by OllyNicholson · Jun 27, 2013 at 10:35 AM
I believe the best way to do this would be to reference materials instead at runtime as that is primarily where you choose which shader to use (think of your unity project as the master material library) So in other words setup the appropriate library of materials in Unity first, then when you assign the material at runtime as discussed here for example:
http://answers.unity3d.com/questions/8993/change-material-of-an-object.html
It uses the appropriate shader you have setup for it in Unity.
I think I didn't really phrase my question well enough. I generate meshes at runtime and save these to an .obj file. I want someone else to be able to import these in another project and it's supposed to look the same as in my generated scene.
The problem is, that I assign different shaders (with different Offset
values) to make certain objects be displayed on top of others. However, the .mtl file only references "static" materials which have exactly one shader (the initial one I also use during the creation of the meshes).
What I want to do is tell unity "Use this material when importing, but don't use the shader that is already specified in this material, but use the one that I specified in my file".
I've done some more research and I think the only way to do this is to save the information which mesh object uses which shader in a file. Then in a custom AssetPostprocessor
I could read this file and the reassign the shaders accordingly.
Answer by InfiniteDesign · Oct 19, 2013 at 11:11 PM
sketchup(with plugin) exports .obj with a .mtl that unity recognizes and assigns correctly as long as there is only a single colour per sketchup group....maybe you can import into sketchup and export to something unity recognizes.....
nothings for sure here. goodluck
Your answer
Follow this Question
Related Questions
exporting custom point/vertex attribute from Houdini to unity 0 Answers
Create a hole in a mesh using a shader 0 Answers
How to combine several Materials into one? 0 Answers
Weird problem on the normals of an imported model. please help!! 4 Answers
How to light back side of quad mesh same way as front side? 1 Answer