- Home /
How to set up UV for lightmaps for generated meshes?
Hello!
How to I set up the UV coordinates (I suspect it is the second UV set) for a in-editor generated mesh?
I create the mesh in the editor (not in the game) because it is part of my custom editor.
The information is needed, because the Unity Lightmapper only gives complete black lightmaps when I am trying to generate one. (Yes, I set the object to be static)
Thanks and best regards,
Marian Frische
Answer by Kuba · Aug 07, 2012 at 01:24 PM
To calculate UVs suitable for lightmapping for your in-editor-created mesh please use: Unwrapping.GenerateSecondaryUVSet().
Answer by maxest · Aug 07, 2012 at 11:23 AM
Ehhh... I've got exactly the same problem, posted here http://forum.unity3d.com/threads/145880-Lightmapping-of-custom-meshes?p=997959#post997959. I also wrote to Unity Support and am waiting for an answer. If I've got something I'm gonna share it
Oh, I did not find that thread when I searched.
However, thank you - hopefully we can get this fixed/answered!
Answer by maxest2 · Aug 07, 2012 at 02:15 PM
I've just tried this:
Mesh mesh = new Mesh();
Unwrapping.GenerateSecondaryUVSet(mesh);
mesh.vertices = vertices;
mesh.normals = normals;
mesh.tangents = tangents;
mesh.colors = colors;
mesh.uv = texCoords;
mesh.triangles = triangles;
but it doesn't work. Also, I tried moving the call to GenerateSecondaryUVSet in various places, interlacing assignment of other attributes but nothing works. The result is the same all the time, that is, I see my object blinking with various parts of the lightmap, depending on where my camera is.
Add the Unwrapping.GenerateSecondaryUVSet as the last step. You probably also want to recalculate the bounds and normals before.
Answer by maxest2 · Aug 08, 2012 at 08:09 AM
When I do this:
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.uv = texCoords;
mesh.normals = normals;
mesh.tangents = tangents;
mesh.colors = colors;
Unwrapping.GenerateSecondaryUVSet(mesh);
mesh.triangles = triangles;
I get logs in console like this:
Shader wants normals, but the mesh doesn't have them
Shader wants texture coordinates, but the mesh doesn't have them
Also, my meshes are solid colored, indicating that base UVs are screwed.
When I tried:
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.uv = texCoords;
mesh.normals = normals;
mesh.tangents = tangents;
mesh.colors = colors;
mesh.triangles = triangles;
Unwrapping.GenerateSecondaryUVSet(mesh);
Still problems as I described. The lightmap is blinking on the mesh depending on where my camera is.
The triangle indices have to be set before Unwrapping.GenerateSecondaryUVSet(mesh); is called. Your second code snippet looks good and should work. If it doesn't -- please report a bug with the repro project and post the case number here. Thanks!
Ahh... never $$anonymous$$d. Got it working finally. The function indeed needs to be called after the assignment of triangles. I suppose it does more than just actually generate secondary UVs, as some vertices might need to be split in the unwrapping process.
The reason it didn't work for me at the beginning is that either my meshes were not marked static, or the game objects holding those meshes were disabled.
Anyway, it all works now. Thanks for the help!
Your answer
Follow this Question
Related Questions
Realtime lighting with generated mesh causes light bleed at seams / edges (minecraft clone) 2 Answers
Why does Frame Selected toggle between position and bounds for procedural and imported meshes? 1 Answer
stretched UV map in generated mesh 0 Answers
Moving Mesh Instance UVs in Editor? (code provided) 3 Answers
Mesh visible in editor but not build 1 Answer