Ejemplo n.º 1
0
    public void EditorTessEnd()
    {
        if (mTessellationJob == null || LOD == null)
        {
            return;
        }
        //save data
        MTQuadTreeHeader header = new MTQuadTreeHeader(DataName);

        header.QuadTreeDepth = QuadTreeDepth;
        header.BoundMin      = VolumnBound.min;
        header.BoundMax      = VolumnBound.max;
        header.LOD           = LOD.Length;
        foreach (var m in mTessellationJob.mesh)
        {
            MTMeshHeader mh = new MTMeshHeader(m.meshId, m.center);
            header.Meshes.Add(m.meshId, mh);
            MTFileUtils.SaveMesh(DataName, m);
        }
        MTLog.Log("mesh saved!");
        MTFileUtils.SaveQuadTreeHeader(DataName, header, Terrain.activeTerrain.terrainData.alphamapTextureCount);
        MTLog.Log("header saved!");
        string matPath = "Assets/MightyTerrainMesh/Resources";

        MTMatUtils.SaveMaterials(matPath, DataName, Terrain.activeTerrain);
        MTLog.Log("material saved!");
    }
Ejemplo n.º 2
0
    //
    private void OnGUI()
    {
        Terrain curentTarget = EditorGUILayout.ObjectField("Convert Target", terrainTarget, typeof(Terrain), true) as Terrain;

        if (curentTarget != terrainTarget)
        {
            ClearAll();
            terrainTarget = curentTarget;
        }
        int curSliceCount = Mathf.FloorToInt(Mathf.Pow(2, QuadTreeDepth));
        int sliceCount    = EditorGUILayout.IntField("Slice Count(NxN)", curSliceCount);

        if (sliceCount != curSliceCount)
        {
            curSliceCount = Mathf.NextPowerOfTwo(sliceCount);
            QuadTreeDepth = Mathf.FloorToInt(Mathf.Log(curSliceCount, 2));
        }

        int lodCount = EditorGUILayout.IntField("LOD Count", LODSettings.Length);

        if (lodCount != LODSettings.Length)
        {
            MTLODSetting[] old = LODSettings;
            LODSettings = new MTLODSetting[lodCount];
            for (int i = 0; i < Mathf.Min(lodCount, old.Length); ++i)
            {
                LODSettings[i] = old[i];
            }
            for (int i = Mathf.Min(lodCount, old.Length); i < Mathf.Max(lodCount, old.Length); ++i)
            {
                LODSettings[i] = new MTLODSetting();
            }
        }
        if (LODSettings.Length > 0)
        {
            for (int i = 0; i < LODSettings.Length; ++i)
            {
                LODSettings[i].OnGUIDraw(i);
            }
        }
        BakeMaterial = EditorGUILayout.ToggleLeft("Bake Material", BakeMaterial);
        if (BakeMaterial)
        {
            BakeTexRes = EditorGUILayout.IntField("Bake Texture Resolution", BakeTexRes);
            BakeTexRes = Mathf.Min(2048, Mathf.NextPowerOfTwo(BakeTexRes));
        }
        if (GUILayout.Button("Generate"))
        {
            if (LODSettings == null || LODSettings.Length == 0)
            {
                MTLog.LogError("no lod setting");
                return;
            }
            if (terrainTarget == null)
            {
                MTLog.LogError("no target terrain");
                return;
            }
            string[] lodFolder = new string[LODSettings.Length];
            for (int i = 0; i < LODSettings.Length; ++i)
            {
                string guid = AssetDatabase.CreateFolder("Assets", string.Format("{0}_LOD{1}", terrainTarget.name, i));
                lodFolder[i] = AssetDatabase.GUIDToAssetPath(guid);
            }
            int gridMax = 1 << QuadTreeDepth;
            dataCreateJob = new CreateDataJob(terrainTarget.terrainData.bounds, gridMax, gridMax, LODSettings);
            for (int i = 0; i < int.MaxValue; ++i)
            {
                dataCreateJob.Update();
                EditorUtility.DisplayProgressBar("creating data", "scaning volumn", dataCreateJob.progress);
                if (dataCreateJob.IsDone)
                {
                    break;
                }
            }
            dataCreateJob.EndProcess();
            tessellationJob = new TessellationJob(dataCreateJob.LODs);
            for (int i = 0; i < int.MaxValue; ++i)
            {
                tessellationJob.Update();
                EditorUtility.DisplayProgressBar("creating data", "tessellation", tessellationJob.progress);
                if (tessellationJob.IsDone)
                {
                    break;
                }
            }
            for (int i = 0; i < tessellationJob.mesh.Length; ++i)
            {
                EditorUtility.DisplayProgressBar("saving data", "processing", (float)i / tessellationJob.mesh.Length);
                MTMeshData data = tessellationJob.mesh[i];
                for (int lod = 0; lod < data.lods.Length; ++lod)
                {
                    SaveMesh(lodFolder[lod], data.meshId, data.lods[lod], sliceCount);
                    if (!BakeMaterial)
                    {
                        MTMatUtils.SaveMaterials(lodFolder[lod], terrainTarget.name, terrainTarget);
                    }
                }
            }
            if (BakeMaterial)
            {
                string guid = AssetDatabase.CreateFolder("Assets", string.Format("{0}_BakedMats", terrainTarget.name));
                AssetDatabase.Refresh();
                RunBakeMaterial(AssetDatabase.GUIDToAssetPath(guid), terrainTarget, BakeTexRes);
            }
            EditorUtility.ClearProgressBar();
            AssetDatabase.Refresh();
        }
    }