Blender Importer: Multiple UV Maps

Hi all

I am new to the JME3 and during doing some prototype I’ve came to the conclusion that I’d like to use the Blender Importer. Unfortunately, it was only able to import one UVMap. But I’d like to use one for tiling texutres and one for baking ambient occlusion. Thus I have added some code to the blender importer which now imports all UVMaps and assigns them to TexCoord, TexCoord2 … TexCoord8. So SeparateTexCoord does actually work! XD
I’d be glad if my change could make it to the trunk.

Here is the patch-file: https://dl.dropboxusercontent.com/u/14799295/blender.patch

I don’t really know the blender file format nor JME3 very well so it would be nice of someone (@Kaelthas?) could actually review my changes. Here some details:

  • I am reading the ldata Custom Element Data attribute of the blender Mesh struct to get the UVMaps. As much as I can tell, this is only written if Blender is built with USE_BMESH_FORWARD_COMPAT. So I am not sure what that means for future blender versions
  • I haven't implemented any enhancements in MeshHelper.readTraditionalFaces only for readBMesh. Thus it won't work for blender files which were created before 2.63
  • I am sorting the UVMaps according to their "Custom Element Data", because I figure it correlates the order in the Blender UI, but that's pure heuristic. I didn't sort them by name, because those names seem to be automatically generated and it didn't correlate.
  • MeshBuilder.appendFace was kind of difficult, I am not sure I've got the vertexAlreadyUsed part right. Currently it check the usage on all UVMaps.
  • For the CombinedTexture.flatten(...) I simply use the TexCoord (first UV Map)

I’d be happy about some feedback.

7 Likes

That’s a nice idea, I can’t comment on the code (@Kaelthas is the person for that) but +1 for contributing :slight_smile:

1 Like

Hey, man! We awaited it for a long time!
According to this topic: http://hub.jmonkeyengine.org/forum/topic/blender-loader-texcoord2-support-request/

It seems you save our souls! :slight_smile:

@nehon , sorry for bothering. But will this code be implemented into JME? This is very important for LightMaps.
@Kaelthas was here about more than 1 month. And no activity from him. :frowning:

Well, Kaelthas will get back eventually, he always does. He’s to one able to review this code because the blender importer is his area of expertise.

Hey everyone,

I am finally back. Sorry for not responding for that long.
I will take a look on that when I get back home :wink:

3 Likes

Cool. Thank you for looking at it. I was about to bump this topic. :slight_smile:

Oh… how did i miss this post :slight_smile: This will in fact make much things like static lighting, AO or even more strange stuff* very easy :slight_smile: AO

*Like using a additional map that the artist creates in black-white for damage scales and is read on a hit the nearest triangels mid point to scale the weapon damage according to the hit position.

1 Like

OK
I have already copied the code but I will need to work with it and test it.
I have already fixed some NPE’s that the code generated when model had no uvs, but I still see some differences between the models I load now and
when I loaded them before.

I was away for some time so I am not sure if this is due to jme3 changes or because of the changes I applied from this patch.

@arpagaus could you send me your test model ?? :slight_smile:

I hope to get through the code as soon as possible.

3 Likes

Well, sorry for the NPEs. I am afraid the code might need some polish. As for the model, I’ve put together a little Suzanne with two textures: 1) UV grid texture and 2) Baked ambient occlusion

Uploaded it here: https://dl.dropboxusercontent.com/u/14799295/suzanne.zip

I belive that this model is incomplete.
Indeed it has two UV mappings but suzanne has no material and no textures.
So blender importer (and even blender itself) will never render ambient shading.

I have added the material so that the model will react to light and I also added two textures.
One of them maps the color and the other one ‘ambient shading’.

I can see the color mapping texture but I cannot see the ambient in blender’s rendered image.

If you could give me the complete model or tell me what am I doing wrong I would be really grateful :wink:

The model actually doesn’t need to be rendered in Blender. I just used it for the geometry and baking AO. In the code sample of SuzanneApplication I replace the material with a custom unshaded one, I am not actually using the material from the Blender importer.


		Spatial model = assetManager.loadModel("Models/suzanne.blend");

		Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		material.setTexture("ColorMap", assetManager.loadTexture("Models/SuzanneGrid.png"));
		material.setTexture("LightMap", assetManager.loadTexture("Models/SuzanneLightMap.png"));
		material.setBoolean("SeparateTexCoord", true);
		model.setMaterial(material);

When a I start the application I get the following results.
With lightmap:

Without lightmap:

Does that make sens or did I miss something? :slight_smile:

I use my own j3m materials and my own shaders too… i guess this makes sense a lot.

1 Like

@Kaelthas , here is my blend model with 2 UV Coordinates:
https://code.google.com/p/jmonkeyplatform-contributions/source/browse/#svn%2Ftrunk%2Fshaderblowlib%2FShaderBlow%2Ftest-data%2FTestModels%2FLightBlow%2Flightmap
lightmap.blend - model
lightmap.mesh.xml - the model works in JME correctly with both UV coordinates.
lightmap.png - texture for UVTexCoord2.

You can use any other texture for UVTexCoord1.

Screenshot: http://i.imgur.com/0asKc2r.jpg

1 Like

OK it looks like I managed to load light map directly from blender. It required additional code.

Now @arpagaus I will try to combine your code with mine and we’ll see what will happen :slight_smile:
Hopefully I will make a commit within this week after I test it :wink:

2 Likes

Thanks a lot. Indeed, I haven’t done anything for the material / texture part. Because I didn’t actually use those, I didn’t cross my mind to implement anything there.

Looking forward to omit my local changes then :wink:

@Kaelthas said: OK it looks like I managed to load light map directly from blender. It required additional code.

Now @arpagaus I will try to combine your code with mine and we’ll see what will happen :slight_smile:
Hopefully I will make a commit within this week after I test it :wink:


Just to be clear, because I feel there is a bit of confusion about this.
The second uv set can be use for many things other than light maps.
The blender loader should be able to load all uv sets (at least as much as JME supports : 8 ) regardless of what they are used for.

<cite>@nehon said:</cite> Just to be clear, because I feel there is a bit of confusion about this. The second uv set can be use for many things other than light maps. The blender loader should be able to load all uv sets (at least as much as JME supports : 8 ) regardless of what they are used for.

That’s my understanding as well. All it does, is loading the available UVMaps from the Blender model and assigning them to TexCoord1, TexCoord2 … TexCoord8 in the JME geometry.
What they are used for, is completely up to the developer. I just happened to use them for light maps, but there is no restriction at all.

@nehon no worry. That is exactly what I had in mind :slight_smile: I was speaking of light map but the importer will load UV sets regardless of what they are used for.

Although I belive there are some problems with @arpagaus implementation because now UV’s (even when only one UV map is used) in traditional triangles or quads seem a little messed up.
But I am currently working on that so I hope to commit code without any known errors :wink:

3 Likes

OK it looks like I came accross a huge problem.

In Blender one vertex can have two, three or more UV coordinates for one mesh vertex in one UV set. In JME one vertex can have assigned only one UV coord.
So far, when I came accross such problem, I simply created two different vertices and each had its own UV.

But now when I want to load several UV sets for one mesh, how am I supposed to deal with the situation when in one set a vertex has one UV and in the other one it has two or more ?

Is there a way in JME to assign two different UV’s to one vertex ?

There are several things I could do:

  1. I could only load those sets that have the same amount of UV’s as the basic one and log a warning.
  2. I could throw an exception so that the user will be forced to fix the problem in the blender file.
  3. I can also load each vertex separately, but that will multiply the verts and increase the model size in memory (generating a warning could be good in such situation).

The loading works good when the model is not smooth so I am pretty sure that the third point should do the trick.

Let me know what you think about it or tell me where I made an error :wink:

1 Like