EE-modders / CEM-tool

Tools for converting and modding Empire Earth 1 CEM files
GNU General Public License v3.0
12 stars 0 forks source link

Bug with UV maps and possible fix for the Blender plugin #3

Open Serpenthalis opened 1 month ago

Serpenthalis commented 1 month ago

Hi! :) I think I spotted a bug in the CEM-tool plugin for Blender in version 0.25 regarding UV maps and can even provide a fix for it: If you import Empire Earth cem-files into Blender using the CEM-tool plugin in version 0.25 you get mostly good results in regards to the UV maps, but not every time. Mostly cavalry units are prone to having messed up UVs after importing. I think I was able to track down the bug:

Somewhere after line 460 of the CEMimporter.py you have the following section:

### add UV coords
for p, polygon in enumerate(main_mesh.polygons):
    for i, index in enumerate(polygon.loop_indices):

        #print("FACE:", faces[p][i])
        #print("TEXTURE UV:", texture_uvs[faces[p][i]] )
        #print("INDEX:", index)

        main_mesh.uv_layers[0].data[index].uv = texture_uvs[mat_vertex_offset : mat_vertex_offset + mat_vertex_count][faces[p][i]]

I don't fully understand what the plugin does, but I will do my best to explain the bug. The bug seems to be in the last line when we access "faces[p][i]". I think the current code assumes that the content of "faces" (a list of coordinates basically) is equal to the list of vertices of the polygons in main_mesh. Which is mostly the case, but not quite in case of some units like cavalry! I tested it with the Sargon of Akkad hero unit and there are two entries missing in the "faces" list. This throws off the script as soon as it reaches the point when the lists diverge. Again, without understanding what the script actually does, my fix was to not use the "faces" list at all and simply access the vertices directly by saving them into their own list and accessing that list in the loop instead of "faces":

### add UV coords
vertex_list = [polygon.vertices for polygon in main_mesh.polygons]
for p, polygon in enumerate(main_mesh.polygons):
    for i, index in enumerate(polygon.loop_indices):    

        #print("FACE:", faces[p][i])
        #print("TEXTURE UV:", texture_uvs[faces[p][i]] )
        #print("INDEX:", index)

        main_mesh.uv_layers[0].data[index].uv = texture_uvs[mat_vertex_offset : mat_vertex_offset + mat_vertex_count][vertex_list[p][i]]

After implementing this little trick the UVs of Sargon of Akkad are no longer messed up and from what I've seen it should have fixed the issues for the other units as well. I know it's been like three years since the last commit to this, but still, I wanted to make you aware of the bug and provide a possible solution. Thanks for this tool, it has been really helpful!

zocker-160 commented 1 month ago

First of all thank you very much for this good report, I really appreciate it!

However I think your fix is caused by a small misunderstanding on how the faces layout works in CEM v2 and the result you are getting ends up with an incorrect import.

What should actually fix this issue is to disable the validate vertex option in the import settings: Screenshot_20240625_001304

The problem is that the original CEM files are using floats which Blender deems "invalid" and they get removed by the validator. That causes the vertex index array to get shifted which then messes everything up.

EDIT: yes you are right that there have not been updates in a long time, but this plugin is mostly "done" and working. Yes there could be some improvements made, which we will do in the future.

Also the code is not great, it is quite a hackjob.