Combining static geometry + lightmaps
링크 : http://forum.unity3d.com/threads/159693-GenerateSecondaryUVSet-destroying-mesh
Answering to my own thread here.
It seems like the MeshMerger script from the wiki itself was causing this trouble. On fourth thought and investigation it seems like copying of triangles and vertices is kind of buggy in the script, so finally I also saw some lonesome vertices flowing in the world after combining. Therefore it might not be the best idea to use that script at all.
Nevertheless, the CombineChildren script from the Standard Assets turned out to be a much better basis to work with. I've rewritten parts of it now to suit my needs, which basically is generating the combined static geometry in the editor and being able to generate lightmaps for this as well. Works like a charm!
I've seen this topic multiple times in the community without a real solution, so it might be a good idea to have a script ready for this. No big adjustment, but I hope it helps some people out there. Here it is, use it as you wish.
CombineChildrenAdvanced.cs
Usage: Place this anywhere in your project and attach it to the parent gameObject containing the other meshes to be combined.
Basically it delivers the very same feature as the standard CombineChildren script, yet it needs to be executed in the editor.
By default it'll automatically generate the lightmapping UVs for the combined geometry. If you don't want this to happen, either use the CombineChildren.cs from the Standard Assets, or uncheck the corresponding checkbox.
If you want to split geometry up again, just use the corresponding button. It'll reset everything accordingly.
using UnityEngine; using System.Collections; /* Attach this script as a parent to some game objects. Then by using the corresponding controls you may generate combined meshes (with lightmapping UVs). This is useful as a performance optimization since it is faster to render one big mesh than many small meshes. See the docs on graphics performance optimization for more info. Different materials will cause multiple meshes to be created, thus it is useful to share as many textures/material as you can. NOTE: Using multiple materials may break things due to lightmapping generation. So lightmapping UVs as well as splitting the combined geometry is not supported. Workaround: Create hierarchies in which the children only have exactly one material. */ [AddComponentMenu("Mad Vulture Games/Combine Children 1.5")] /// Usually rendering with triangle strips is faster. /// However when combining objects with very low triangle counts, it can be faster to use triangles. /// Best is to try out which value is faster in practice. public bool generateTriangleStrips = true; public bool generateLightmappingUVs = true; public bool isCombined = false; return; } Matrix4x4 myTransform = transform.worldToLocalMatrix; MeshCombineUtility.MeshInstance instance = new MeshCombineUtility.MeshInstance (); instance.mesh = filter.sharedMesh; } else { } } curRenderer.enabled = false; } } foreach (DictionaryEntry de in materialToMesh) { ArrayList elements = (ArrayList)de.Value; MeshCombineUtility.MeshInstance[] instances = (MeshCombineUtility.MeshInstance[])elements.ToArray(typeof(MeshCombineUtility.MeshInstance)); // We have a maximum of one material, so just attach the mesh to our own game object { // Make sure we have a mesh filter & renderer renderer.enabled = true; } // We have multiple materials to take care of, build one mesh / gameobject for each material // and parent it to this object else { go.transform.parent = transform; } } isCombined = true; } }
CombineChildrenAdvancedEditor.cs
Usage: Place this in your Assets/Editor folder, it actually contains the main part of it.
using UnityEngine; using UnityEditor; using System.Collections; [CustomEditor(typeof(CombineChildrenAdvanced))] CombineChildrenAdvanced _target; } _target.generateTriangleStrips = EditorGUILayout.Toggle("Generate Triangle Strips", _target.generateTriangleStrips); _target.generateLightmappingUVs = EditorGUILayout.Toggle ("Generate Lightmapping UVs", _target.generateLightmappingUVs); if (!_target.isCombined) { if (_target.generateLightmappingUVs) { GenerateLightmappingUVs(); } } } if (_target.isCombined) { GenerateLightmappingUVs(); } mr.enabled = true; } _target.isCombined = false; } } } void GenerateLightmappingUVs() { // make null check because if not enough meshes are present no combined mesh would have been created! if (mf != null) { } } }
Note: While the script still gives you the opportunity to use it with meshes with multiple materials (which will effectively generate multiple new gameObjects), this is not supported in this version, you might get errors. If you need that, feel free to build that yourself or let me know, shouldn't be hard to add.
'Unity3D > Tips' 카테고리의 다른 글
Unity 4.x 에서 바뀐 점 정리.. (0) | 2013.02.05 |
---|---|
유니티 종료 시, 호출되는 함수(OnApplicationQuit) (0) | 2013.01.29 |
Unity3D 모바일 디바이스에서 파일생성 및 읽고 쓰기 (12) | 2013.01.22 |
Unity3D에서 오브젝트가 카메라 뷰포트에 들어오는지 초간단 체크 (0) | 2013.01.21 |
[뻘TIP] 유니티 멤버 변수 컨벤션 관련.. (0) | 2013.01.16 |