protected override bool CreateMesh()
        {
            base.CreateMesh();

            #region ProgressBar
            const float     MaxProgressCount   = 6f;
            float           ProgressCount      = 0;
            Action <string> DisplayProgressBar = (info) =>
            {
                if (voxelData.voxels.Length > 10000)
                {
                    EditorUtility.DisplayProgressBar("Create Mesh...", string.Format("{0} / {1}", ProgressCount, MaxProgressCount), (ProgressCount++ / MaxProgressCount));
                }
            };
            #endregion

            DisplayProgressBar("");

            #region Disable
            {
                if (voxelObject.disableData == null)
                {
                    voxelObject.disableData = new DisableData();
                }
                #region Erase
                {
                    List <IntVector3> removeList = new List <IntVector3>();
                    voxelObject.disableData.AllAction((pos, face) =>
                    {
                        if (voxelData.VoxelTableContains(pos) < 0)
                        {
                            removeList.Add(pos);
                        }
                    });
                    for (int j = 0; j < removeList.Count; j++)
                    {
                        voxelObject.disableData.RemoveDisable(removeList[j]);
                    }
                }
                #endregion
            }
            #endregion

            DisplayProgressBar("");

            #region Material
            {
                if (voxelObject.materialData == null)
                {
                    voxelObject.materialData = new List <MaterialData>();
                }
                if (voxelObject.materialData.Count == 0)
                {
                    voxelObject.materialData.Add(null);
                }
                for (int i = 0; i < voxelObject.materialData.Count; i++)
                {
                    if (voxelObject.materialData[i] == null)
                    {
                        voxelObject.materialData[i] = new MaterialData();
                    }
                }
                #region Erase
                for (int i = 0; i < voxelObject.materialData.Count; i++)
                {
                    List <IntVector3> removeList = new List <IntVector3>();
                    voxelObject.materialData[i].AllAction((pos) =>
                    {
                        if (voxelData.VoxelTableContains(pos) < 0)
                        {
                            removeList.Add(pos);
                        }
                    });
                    for (int j = 0; j < removeList.Count; j++)
                    {
                        voxelObject.materialData[i].RemoveMaterial(removeList[j]);
                    }
                }
                #endregion
                if (voxelObject.materials == null)
                {
                    voxelObject.materials = new List <Material>();
                }
                if (voxelObject.materials.Count < voxelObject.materialData.Count)
                {
                    for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
                    {
                        voxelObject.materials.Add(null);
                    }
                }
                else if (voxelObject.materials.Count > voxelObject.materialData.Count)
                {
                    voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
                }
            }
            voxelObject.CreateMaterialIndexTable();
            #endregion

            CalcDataCreate(voxelData.voxels);

            #region CreateFaceAreaTable
            {
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    var voxels = new VoxelData.Voxel[chunkDataList[i].voxels.Count];
                    for (int j = 0; j < chunkDataList[i].voxels.Count; j++)
                    {
                        voxels[j] = voxelData.voxels[chunkDataList[i].voxels[j]];
                    }
                    chunkDataList[i].faceAreaTable = CreateFaceArea(voxels);
                    //
                    if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Individual)
                    {
                        var paletteTable = new int[voxelData.palettes.Length];
                        for (int j = 0; j < voxelData.palettes.Length; j++)
                        {
                            int newIndex = -1;
                            for (int k = 0; k < chunkDataList[i].palettes.Length; k++)
                            {
                                if (chunkDataList[i].palettes[k] == voxelData.palettes[j])
                                {
                                    newIndex = k;
                                    break;
                                }
                            }
                            paletteTable[j] = newIndex;
                        }
                        chunkDataList[i].faceAreaTable.ReplacePalette(paletteTable);
                    }
                }
            }
            #endregion

            #region CreateTexture
            if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Combine)
            {
                #region Combine
                var tmpFaceAreaTable = new VoxelData.FaceAreaTable();
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    tmpFaceAreaTable.Merge(chunkDataList[i].faceAreaTable);
                }
                {
                    var atlasTextureTmp = voxelObject.atlasTexture;
                    if (chunkDataList != null && chunkDataList.Count > 0)
                    {
                        if (!CreateTexture(tmpFaceAreaTable, voxelData.palettes, ref chunkDataList[0].atlasRectTable, ref atlasTextureTmp, ref chunkDataList[0].atlasRects))
                        {
                            EditorUtility.ClearProgressBar();
                            return(false);
                        }
                    }
                    else
                    {
                        AtlasRectTable atlasRectTable = null;
                        Rect[]         atlasRects     = null;
                        if (!CreateTexture(tmpFaceAreaTable, voxelData.palettes, ref atlasRectTable, ref atlasTextureTmp, ref atlasRects))
                        {
                            EditorUtility.ClearProgressBar();
                            return(false);
                        }
                    }
                    voxelObject.atlasTexture = atlasTextureTmp;
                    if (!AssetDatabase.Contains(voxelObject.atlasTexture))
                    {
                        AddObjectToPrefabAsset(voxelObject.atlasTexture, "tex");
                    }
                    for (int i = 0; i < chunkDataList.Count; i++)
                    {
                        chunkDataList[i].atlasRects               = chunkDataList[0].atlasRects;
                        chunkDataList[i].atlasRectTable           = chunkDataList[0].atlasRectTable;
                        chunkDataList[i].chunkObject.materials    = null;
                        chunkDataList[i].chunkObject.atlasTexture = null;
                    }
                }
                #endregion
            }
            else if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Individual)
            {
                #region Individual
                if (voxelObject.materialData == null)
                {
                    voxelObject.materialData = new List <MaterialData>();
                }
                if (voxelObject.materialData.Count == 0)
                {
                    voxelObject.materialData.Add(null);
                }
                for (int i = 0; i < voxelObject.materialData.Count; i++)
                {
                    if (voxelObject.materialData[i] == null)
                    {
                        voxelObject.materialData[i] = new MaterialData();
                    }
                }
                voxelObject.materials    = null;
                voxelObject.atlasTexture = null;
                for (int c = 0; c < chunkDataList.Count; c++)
                {
                    var atlasTextureTmp = chunkDataList[c].chunkObject.atlasTexture;
                    if (!CreateTexture(chunkDataList[c].faceAreaTable, chunkDataList[c].palettes, ref chunkDataList[c].atlasRectTable, ref atlasTextureTmp, ref chunkDataList[c].atlasRects))
                    {
                        EditorUtility.ClearProgressBar();
                        return(false);
                    }
                    chunkDataList[c].chunkObject.atlasTexture = atlasTextureTmp;
                    if (!AssetDatabase.Contains(chunkDataList[c].chunkObject.atlasTexture))
                    {
                        AddObjectToPrefabAsset(chunkDataList[c].chunkObject.atlasTexture, string.Format("{0}_tex", chunkDataList[c].chunkObject.name));
                    }
                }
                #endregion
            }
            else
            {
                Assert.IsTrue(false);
            }
            #endregion

            #region CreateMesh
            DisplayProgressBar("");
            {
                if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Combine)
                {
                    #region Combine
                    if (voxelObject.importMode == VoxelBase.ImportMode.LowPoly)
                    {
                        int forward = 0;
                        int up      = 0;
                        int right   = 0;
                        int left    = 0;
                        int down    = 0;
                        int back    = 0;
                        for (int i = 0; i < chunkDataList.Count; i++)
                        {
                            AtlasRectTable atlasRectTable = new AtlasRectTable();
                            {
                                atlasRectTable.forward = chunkDataList[i].atlasRectTable.forward.GetRange(forward, chunkDataList[i].faceAreaTable.forward.Count);
                                forward          += chunkDataList[i].faceAreaTable.forward.Count;
                                atlasRectTable.up = chunkDataList[i].atlasRectTable.up.GetRange(up, chunkDataList[i].faceAreaTable.up.Count);
                                up += chunkDataList[i].faceAreaTable.up.Count;
                                atlasRectTable.right = chunkDataList[i].atlasRectTable.right.GetRange(right, chunkDataList[i].faceAreaTable.right.Count);
                                right += chunkDataList[i].faceAreaTable.right.Count;
                                atlasRectTable.left = chunkDataList[i].atlasRectTable.left.GetRange(left, chunkDataList[i].faceAreaTable.left.Count);
                                left += chunkDataList[i].faceAreaTable.left.Count;
                                atlasRectTable.down = chunkDataList[i].atlasRectTable.down.GetRange(down, chunkDataList[i].faceAreaTable.down.Count);
                                down += chunkDataList[i].faceAreaTable.down.Count;
                                atlasRectTable.back = chunkDataList[i].atlasRectTable.back.GetRange(back, chunkDataList[i].faceAreaTable.back.Count);
                                back += chunkDataList[i].faceAreaTable.back.Count;
                            }
                            chunkDataList[i].chunkObject.mesh = CreateMeshOnly(chunkDataList[i].chunkObject.mesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, chunkDataList[i].atlasRects, atlasRectTable, -(voxelObject.localOffset + chunkDataList[i].area.centerf), out chunkDataList[i].chunkObject.materialIndexes);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < chunkDataList.Count; i++)
                        {
                            chunkDataList[i].chunkObject.mesh = CreateMeshOnly(chunkDataList[i].chunkObject.mesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, chunkDataList[i].atlasRects, chunkDataList[i].atlasRectTable, -(voxelObject.localOffset + chunkDataList[i].area.centerf), out chunkDataList[i].chunkObject.materialIndexes);
                        }
                    }
                    {
                        HashSet <int> combineMaterialIndexes = new HashSet <int>();
                        for (int c = 0; c < chunkDataList.Count; c++)
                        {
                            foreach (var index in chunkDataList[c].chunkObject.materialIndexes)
                            {
                                combineMaterialIndexes.Add(index);
                            }
                        }
                        voxelObject.materialIndexes = combineMaterialIndexes.ToList();
                    }
                    #endregion
                }
                else if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Individual)
                {
                    #region Individual
                    for (int i = 0; i < chunkDataList.Count; i++)
                    {
                        chunkDataList[i].chunkObject.mesh = CreateMeshOnly(chunkDataList[i].chunkObject.mesh, chunkDataList[i].faceAreaTable, chunkDataList[i].chunkObject.atlasTexture, chunkDataList[i].atlasRects, chunkDataList[i].atlasRectTable, -(voxelObject.localOffset + chunkDataList[i].area.centerf), out chunkDataList[i].chunkObject.materialIndexes);
                    }
                    voxelObject.materialIndexes = new List <int>();
                    #endregion
                }
                else
                {
                    Assert.IsTrue(false);
                }
            }
            #endregion

            #region CreateMaterial
            {
                if (voxelObject.materialData == null)
                {
                    voxelObject.materialData = new List <MaterialData>();
                }
                if (voxelObject.materialData.Count == 0)
                {
                    voxelObject.materialData.Add(null);
                }
                for (int i = 0; i < voxelObject.materialData.Count; i++)
                {
                    if (voxelObject.materialData[i] == null)
                    {
                        voxelObject.materialData[i] = new MaterialData();
                    }
                }
                if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Combine)
                {
                    #region Combine
                    if (voxelObject.materials == null)
                    {
                        voxelObject.materials = new List <Material>();
                    }
                    if (voxelObject.materials.Count < voxelObject.materialData.Count)
                    {
                        for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
                        {
                            voxelObject.materials.Add(null);
                        }
                    }
                    else if (voxelObject.materials.Count > voxelObject.materialData.Count)
                    {
                        voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
                    }

                    for (int i = 0; i < voxelObject.materials.Count; i++)
                    {
                        if (!voxelObject.materialIndexes.Contains(i))
                        {
                            if (voxelObject.materials[i] != null)
                            {
                                voxelObject.materials[i] = null;
                                DestroyUnusedObjectInPrefabObject();
                            }
                            continue;
                        }
                        if (voxelObject.materials[i] == null)
                        {
                            voxelObject.materials[i] = EditorCommon.CreateStandardMaterial();
                        }
                        if (!AssetDatabase.Contains(voxelObject.materials[i]))
                        {
                            AddObjectToPrefabAsset(voxelObject.materials[i], "mat", i);
                        }
                    }
                    #endregion
                }
                else if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Individual)
                {
                    #region Individual
                    for (int c = 0; c < chunkDataList.Count; c++)
                    {
                        if (chunkDataList[c].chunkObject.materials == null)
                        {
                            chunkDataList[c].chunkObject.materials = new List <Material>();
                        }
                        if (chunkDataList[c].chunkObject.materials.Count < voxelObject.materialData.Count)
                        {
                            for (int i = chunkDataList[c].chunkObject.materials.Count; i < voxelObject.materialData.Count; i++)
                            {
                                chunkDataList[c].chunkObject.materials.Add(null);
                            }
                        }
                        else if (chunkDataList[c].chunkObject.materials.Count > voxelObject.materialData.Count)
                        {
                            chunkDataList[c].chunkObject.materials.RemoveRange(voxelObject.materialData.Count, chunkDataList[c].chunkObject.materials.Count - voxelObject.materialData.Count);
                        }
                        for (int i = 0; i < chunkDataList[c].chunkObject.materials.Count; i++)
                        {
                            if (!chunkDataList[c].chunkObject.materialIndexes.Contains(i))
                            {
                                if (chunkDataList[c].chunkObject.materials[i] != null)
                                {
                                    chunkDataList[c].chunkObject.materials[i] = null;
                                    DestroyUnusedObjectInPrefabObject();
                                }
                                continue;
                            }
                            if (chunkDataList[c].chunkObject.materials[i] == null)
                            {
                                chunkDataList[c].chunkObject.materials[i] = EditorCommon.CreateStandardMaterial();
                            }
                            if (!AssetDatabase.Contains(chunkDataList[c].chunkObject.materials[i]))
                            {
                                AddObjectToPrefabAsset(chunkDataList[c].chunkObject.materials[i], string.Format("{0}_mat", chunkDataList[c].chunkObject.name), i);
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    Assert.IsTrue(false);
                }
            }
            #endregion

            DisplayProgressBar("");
            {
                if (voxelBase.generateLightmapUVs)
                {
                    var param = voxelBase.GetLightmapParam();
                    for (int i = 0; i < chunkDataList.Count; i++)
                    {
                        if (chunkDataList[i].chunkObject.mesh.uv.Length > 0)
                        {
                            Unwrapping.GenerateSecondaryUVSet(chunkDataList[i].chunkObject.mesh, param);
                        }
                    }
                }
                if (voxelBase.generateTangents)
                {
                    for (int i = 0; i < chunkDataList.Count; i++)
                    {
                        chunkDataList[i].chunkObject.mesh.RecalculateTangents();
                    }
                }
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    if (!AssetDatabase.Contains(chunkDataList[i].chunkObject.mesh))
                    {
                        AddObjectToPrefabAsset(chunkDataList[i].chunkObject.mesh, string.Format("{0}_mesh", chunkDataList[i].chunkObject.name));
                    }
                }
            }

            DisplayProgressBar("");

            SetRendererCompornent();

            RefreshCheckerSave();

            EditorUtility.ClearProgressBar();

            return(true);
        }
        protected override bool CreateMesh()
        {
            #region ProgressBar
            const float     MaxProgressCount   = 5f;
            float           ProgressCount      = 0;
            Action <string> DisplayProgressBar = (info) =>
            {
                if (!EditorApplication.isPlaying && voxelBase.voxelData.voxels.Length > 10000)
                {
                    EditorUtility.DisplayProgressBar("Create Mesh...", string.Format("{0} / {1}", ProgressCount, MaxProgressCount), (ProgressCount++ / MaxProgressCount));
                }
            };
            #endregion

            DisplayProgressBar("");

            #region Material
            {
                if (voxelObject.materialData == null)
                {
                    voxelObject.materialData = new List <MaterialData>();
                }
                if (voxelObject.materialData.Count == 0)
                {
                    voxelObject.materialData.Add(null);
                }
                for (int i = 0; i < voxelObject.materialData.Count; i++)
                {
                    if (voxelObject.materialData[i] == null)
                    {
                        voxelObject.materialData[i] = new MaterialData();
                    }
                }
                if (voxelObject.materials == null)
                {
                    voxelObject.materials = new List <Material>();
                }
                if (voxelObject.materials.Count < voxelObject.materialData.Count)
                {
                    for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
                    {
                        voxelObject.materials.Add(null);
                    }
                }
                else if (voxelObject.materials.Count > voxelObject.materialData.Count)
                {
                    voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
                }
                #region Erase
                for (int i = 0; i < voxelObject.materialData.Count; i++)
                {
                    List <IntVector3> removeList = new List <IntVector3>();
                    voxelObject.materialData[i].AllAction((pos) =>
                    {
                        if (voxelObject.voxelData.VoxelTableContains(pos) < 0)
                        {
                            removeList.Add(pos);
                        }
                    });
                    for (int j = 0; j < removeList.Count; j++)
                    {
                        voxelObject.materialData[i].RemoveMaterial(removeList[j]);
                    }
                }
                #endregion
            }
            #endregion

            CalcDataCreate(voxelBase.voxelData.voxels);

            #region RemoveChunk
            var chunkObjects = FindChunkComponents();
            {
                bool   chunkObjectsUpdate = false;
                bool[] enableTale         = new bool[chunkObjects.Length];
                if (!removeAllChunk)
                {
                    for (int i = 0; i < chunkDataList.Count; i++)
                    {
                        for (int j = 0; j < chunkObjects.Length; j++)
                        {
                            if (chunkDataList[i].position == chunkObjects[j].position)
                            {
                                enableTale[j] = true;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    removeAllChunk = false;
                }
                for (int i = 0; i < enableTale.Length; i++)
                {
                    if (!enableTale[i])
                    {
                        var go = chunkObjects[i].gameObject;
                        while (go.transform.childCount > 0)
                        {
                            Undo.SetTransformParent(go.transform.GetChild(0), voxelObject.transform, "Remove Chunk");
                        }
                        Undo.DestroyObjectImmediate(go);
                        chunkObjectsUpdate = true;
                    }
                }
                if (chunkObjectsUpdate)
                {
                    chunkObjects = FindChunkComponents();
                }
            }
            #endregion

            #region AddChunk
            int chunkCount = 0;
            {
                bool chunkObjectsUpdate = false;
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    GameObject chunkObject = null;
                    for (int j = 0; j < chunkObjects.Length; j++)
                    {
                        if (chunkDataList[i].position == chunkObjects[j].position)
                        {
                            chunkObject = chunkObjects[j].gameObject;
                            break;
                        }
                    }
                    if (chunkObject == null)
                    {
                        chunkObject = new GameObject(chunkDataList[i].name);
                        Undo.RegisterCreatedObjectUndo(chunkObject, "Create Chunk");
                        Undo.SetTransformParent(chunkObject.transform, voxelObject.transform, "Create Chunk");
                        GameObjectUtility.SetStaticEditorFlags(chunkObject, GameObjectUtility.GetStaticEditorFlags(voxelObject.gameObject));
                        chunkObject.transform.localPosition = Vector3.Scale(voxelObject.localOffset + chunkDataList[i].area.centerf, voxelObject.importScale);
                        chunkObject.transform.localRotation = Quaternion.identity;
                        chunkObject.transform.localScale    = Vector3.one;
                        chunkObject.layer  = voxelObject.gameObject.layer;
                        chunkObject.tag    = voxelObject.gameObject.tag;
                        chunkObjectsUpdate = true;
                    }
                    VoxelChunksObjectChunk controller = chunkObject.GetComponent <VoxelChunksObjectChunk>();
                    if (controller == null)
                    {
                        controller = Undo.AddComponent <VoxelChunksObjectChunk>(chunkObject);
                    }
                    controller.position  = chunkDataList[i].position;
                    controller.chunkName = chunkDataList[i].name;
                    chunkCount++;
                }
                if (chunkObjectsUpdate)
                {
                    chunkObjects = FindChunkComponents();
                }
            }
            #endregion

            #region SortChunk
            {
                List <Transform> objList = new List <Transform>();
                var childCount           = voxelObject.transform.childCount;
                for (int i = 0; i < childCount; i++)
                {
                    objList.Add(voxelObject.transform.GetChild(i));
                }
                objList.Sort((obj1, obj2) => string.Compare(obj1.name, obj2.name));
                for (int i = 0; i < objList.Count; i++)
                {
                    objList[i].SetSiblingIndex(childCount - 1);
                }
                chunkObjects = FindChunkComponents();
            }
            #endregion

            #region UpdateChunk
            for (int i = 0; i < chunkObjects.Length; i++)
            {
                for (int j = 0; j < chunkDataList.Count; j++)
                {
                    if (chunkObjects[i].position == chunkDataList[j].position)
                    {
                        chunkDataList[j].chunkObject = chunkObjects[i];
                        break;
                    }
                }
            }
            #endregion

            #region UpdateVoxelVisibleFlag
            {
                for (int i = 0; i < voxelBase.voxelData.voxels.Length; i++)
                {
                    int            index;
                    VoxelBase.Face faceFlags = (VoxelBase.Face) 0;
                    index = voxelBase.voxelData.VoxelTableContains(voxelBase.voxelData.voxels[i].x, voxelBase.voxelData.voxels[i].y, voxelBase.voxelData.voxels[i].z + 1);
                    if (index < 0)
                    {
                        faceFlags |= VoxelBase.Face.forward;
                    }
                    index = voxelBase.voxelData.VoxelTableContains(voxelBase.voxelData.voxels[i].x, voxelBase.voxelData.voxels[i].y + 1, voxelBase.voxelData.voxels[i].z);
                    if (index < 0)
                    {
                        faceFlags |= VoxelBase.Face.up;
                    }
                    index = voxelBase.voxelData.VoxelTableContains(voxelBase.voxelData.voxels[i].x + 1, voxelBase.voxelData.voxels[i].y, voxelBase.voxelData.voxels[i].z);
                    if (index < 0)
                    {
                        faceFlags |= VoxelBase.Face.right;
                    }
                    index = voxelBase.voxelData.VoxelTableContains(voxelBase.voxelData.voxels[i].x - 1, voxelBase.voxelData.voxels[i].y, voxelBase.voxelData.voxels[i].z);
                    if (index < 0)
                    {
                        faceFlags |= VoxelBase.Face.left;
                    }
                    index = voxelBase.voxelData.VoxelTableContains(voxelBase.voxelData.voxels[i].x, voxelBase.voxelData.voxels[i].y - 1, voxelBase.voxelData.voxels[i].z);
                    if (index < 0)
                    {
                        faceFlags |= VoxelBase.Face.down;
                    }
                    index = voxelBase.voxelData.VoxelTableContains(voxelBase.voxelData.voxels[i].x, voxelBase.voxelData.voxels[i].y, voxelBase.voxelData.voxels[i].z - 1);
                    if (index < 0)
                    {
                        faceFlags |= VoxelBase.Face.back;
                    }
                    voxelBase.voxelData.voxels[i].visible = faceFlags;
                }
            }
            #endregion

            #region CreateFaceAreaTable
            {
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    var voxels = new VoxelData.Voxel[chunkDataList[i].voxels.Count];
                    for (int j = 0; j < chunkDataList[i].voxels.Count; j++)
                    {
                        voxels[j] = voxelBase.voxelData.voxels[chunkDataList[i].voxels[j]];
                    }
                    chunkDataList[i].faceAreaTable = CreateFaceArea(voxels);
                    //
                    if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Individual)
                    {
                        var paletteTable = new int[voxelBase.voxelData.palettes.Length];
                        for (int j = 0; j < voxelBase.voxelData.palettes.Length; j++)
                        {
                            int newIndex = -1;
                            for (int k = 0; k < chunkDataList[i].palettes.Length; k++)
                            {
                                if (chunkDataList[i].palettes[k] == voxelBase.voxelData.palettes[j])
                                {
                                    newIndex = k;
                                    break;
                                }
                            }
                            paletteTable[j] = newIndex;
                        }
                        chunkDataList[i].faceAreaTable.ReplacePalette(paletteTable);
                    }
                }
            }
            #endregion

            #region CreateTexture
            if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Combine)
            {
                #region Combine
                var tmpFaceAreaTable = new VoxelData.FaceAreaTable();
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    tmpFaceAreaTable.Merge(chunkDataList[i].faceAreaTable);
                }
                {
                    var atlasTextureTmp = voxelObject.atlasTexture;
                    if (!CreateTexture(tmpFaceAreaTable, voxelBase.voxelData.palettes, ref chunkDataList[0].atlasRectTable, ref atlasTextureTmp, ref chunkDataList[0].atlasRects))
                    {
                        EditorUtility.ClearProgressBar();
                        return(false);
                    }
                    voxelObject.atlasTexture = atlasTextureTmp;
                    {
                        if (voxelObject.materialData == null)
                        {
                            voxelObject.materialData = new List <MaterialData>();
                        }
                        if (voxelObject.materialData.Count == 0)
                        {
                            voxelObject.materialData.Add(null);
                        }
                        for (int i = 0; i < voxelObject.materialData.Count; i++)
                        {
                            if (voxelObject.materialData[i] == null)
                            {
                                voxelObject.materialData[i] = new MaterialData();
                            }
                        }
                        if (voxelObject.materials == null)
                        {
                            voxelObject.materials = new List <Material>();
                        }
                        if (voxelObject.materials.Count < voxelObject.materialData.Count)
                        {
                            for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
                            {
                                voxelObject.materials.Add(null);
                            }
                        }
                        else if (voxelObject.materials.Count > voxelObject.materialData.Count)
                        {
                            voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
                        }
                        for (int i = 0; i < voxelObject.materials.Count; i++)
                        {
                            if (voxelObject.materials[i] == null)
                            {
                                voxelObject.materials[i] = new Material(Shader.Find("Standard"));
                            }
                            voxelObject.materials[i].mainTexture = voxelObject.atlasTexture;
                        }
                    }
                    for (int i = 0; i < chunkDataList.Count; i++)
                    {
                        chunkDataList[i].atlasRects               = chunkDataList[0].atlasRects;
                        chunkDataList[i].atlasRectTable           = chunkDataList[0].atlasRectTable;
                        chunkDataList[i].chunkObject.materials    = null;
                        chunkDataList[i].chunkObject.atlasTexture = null;
                    }
                }
                #endregion
            }
            else if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Individual)
            {
                #region Individual
                if (voxelObject.materialData == null)
                {
                    voxelObject.materialData = new List <MaterialData>();
                }
                if (voxelObject.materialData.Count == 0)
                {
                    voxelObject.materialData.Add(null);
                }
                for (int i = 0; i < voxelObject.materialData.Count; i++)
                {
                    if (voxelObject.materialData[i] == null)
                    {
                        voxelObject.materialData[i] = new MaterialData();
                    }
                }
                voxelObject.materials    = null;
                voxelObject.atlasTexture = null;
                for (int c = 0; c < chunkDataList.Count; c++)
                {
                    var atlasTextureTmp = chunkDataList[c].chunkObject.atlasTexture;
                    if (!CreateTexture(chunkDataList[c].faceAreaTable, chunkDataList[c].palettes, ref chunkDataList[c].atlasRectTable, ref atlasTextureTmp, ref chunkDataList[c].atlasRects))
                    {
                        EditorUtility.ClearProgressBar();
                        return(false);
                    }
                    chunkDataList[c].chunkObject.atlasTexture = atlasTextureTmp;
                    {
                        if (chunkDataList[c].chunkObject.materials == null)
                        {
                            chunkDataList[c].chunkObject.materials = new List <Material>();
                        }
                        if (chunkDataList[c].chunkObject.materials.Count < voxelObject.materialData.Count)
                        {
                            for (int i = chunkDataList[c].chunkObject.materials.Count; i < voxelObject.materialData.Count; i++)
                            {
                                chunkDataList[c].chunkObject.materials.Add(null);
                            }
                        }
                        else if (chunkDataList[c].chunkObject.materials.Count > voxelObject.materialData.Count)
                        {
                            chunkDataList[c].chunkObject.materials.RemoveRange(voxelObject.materialData.Count, chunkDataList[c].chunkObject.materials.Count - voxelObject.materialData.Count);
                        }
                        for (int i = 0; i < chunkDataList[c].chunkObject.materials.Count; i++)
                        {
                            if (chunkDataList[c].chunkObject.materials[i] == null)
                            {
                                chunkDataList[c].chunkObject.materials[i] = new Material(Shader.Find("Standard"));
                            }
                            chunkDataList[c].chunkObject.materials[i].mainTexture = chunkDataList[c].chunkObject.atlasTexture;
                        }
                    }
                }
                #endregion
            }
            else
            {
                Assert.IsTrue(false);
            }
            #endregion

            #region CreateMesh
            DisplayProgressBar("");
            {
                if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Combine)
                {
                    #region Combine
                    if (voxelObject.importMode == VoxelBase.ImportMode.LowPoly)
                    {
                        int forward = 0;
                        int up      = 0;
                        int right   = 0;
                        int left    = 0;
                        int down    = 0;
                        int back    = 0;
                        for (int i = 0; i < chunkDataList.Count; i++)
                        {
                            AtlasRectTable atlasRectTable = new AtlasRectTable();
                            {
                                atlasRectTable.forward = chunkDataList[i].atlasRectTable.forward.GetRange(forward, chunkDataList[i].faceAreaTable.forward.Count);
                                forward          += chunkDataList[i].faceAreaTable.forward.Count;
                                atlasRectTable.up = chunkDataList[i].atlasRectTable.up.GetRange(up, chunkDataList[i].faceAreaTable.up.Count);
                                up += chunkDataList[i].faceAreaTable.up.Count;
                                atlasRectTable.right = chunkDataList[i].atlasRectTable.right.GetRange(right, chunkDataList[i].faceAreaTable.right.Count);
                                right += chunkDataList[i].faceAreaTable.right.Count;
                                atlasRectTable.left = chunkDataList[i].atlasRectTable.left.GetRange(left, chunkDataList[i].faceAreaTable.left.Count);
                                left += chunkDataList[i].faceAreaTable.left.Count;
                                atlasRectTable.down = chunkDataList[i].atlasRectTable.down.GetRange(down, chunkDataList[i].faceAreaTable.down.Count);
                                down += chunkDataList[i].faceAreaTable.down.Count;
                                atlasRectTable.back = chunkDataList[i].atlasRectTable.back.GetRange(back, chunkDataList[i].faceAreaTable.back.Count);
                                back += chunkDataList[i].faceAreaTable.back.Count;
                            }
                            chunkDataList[i].chunkObject.mesh = CreateMeshOnly(chunkDataList[i].chunkObject.mesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, chunkDataList[i].atlasRects, atlasRectTable, -(voxelObject.localOffset + chunkDataList[i].area.centerf), out chunkDataList[i].chunkObject.materialIndexes);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < chunkDataList.Count; i++)
                        {
                            var srcMesh = (chunkDataList[i].chunkObject.mesh != null && AssetDatabase.Contains(chunkDataList[i].chunkObject.mesh)) ? chunkDataList[i].chunkObject.mesh : null;
                            chunkDataList[i].chunkObject.mesh = CreateMeshOnly(srcMesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, chunkDataList[i].atlasRects, chunkDataList[i].atlasRectTable, -(voxelObject.localOffset + chunkDataList[i].area.centerf), out chunkDataList[i].chunkObject.materialIndexes);
                        }
                    }
                    #endregion
                }
                else if (voxelObject.materialMode == VoxelChunksObject.MaterialMode.Individual)
                {
                    #region Individual
                    for (int i = 0; i < chunkDataList.Count; i++)
                    {
                        chunkDataList[i].chunkObject.mesh = CreateMeshOnly(chunkDataList[i].chunkObject.mesh, chunkDataList[i].faceAreaTable, chunkDataList[i].chunkObject.atlasTexture, chunkDataList[i].atlasRects, chunkDataList[i].atlasRectTable, -(voxelObject.localOffset + chunkDataList[i].area.centerf), out chunkDataList[i].chunkObject.materialIndexes);
                    }
                    #endregion
                }
                else
                {
                    Assert.IsTrue(false);
                }
            }
            #endregion

            DisplayProgressBar("");
            if (voxelBase.generateLightmapUVs)
            {
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    if (chunkDataList[i].chunkObject.mesh.uv.Length > 0)
                    {
                        Unwrapping.GenerateSecondaryUVSet(chunkDataList[i].chunkObject.mesh);
                    }
                }
            }

            DisplayProgressBar("");

            SetRendererCompornent();

            CreateMeshAfterFree();

            RefreshCheckerSave();

            EditorUtility.ClearProgressBar();

            return(true);
        }
Exemple #3
0
        protected override bool CreateMesh()
        {
            base.CreateMesh();

            #region ProgressBar
            const float     MaxProgressCount   = 14f;
            float           ProgressCount      = 0;
            Action <string> DisplayProgressBar = (info) =>
            {
                if (voxelData.voxels.Length > 10000)
                {
                    EditorUtility.DisplayProgressBar("Create Mesh...", string.Format("{0} / {1}", ProgressCount, MaxProgressCount), (ProgressCount++ / MaxProgressCount));
                }
            };
            #endregion

            DisplayProgressBar("");

            {
                bool reloadVoxelData = false;
                foreach (var frame in voxelObject.frames)
                {
                    if (frame.disableData != null &&
                        frame.disableData.Count > 0)
                    {
                        reloadVoxelData = true;
                        break;
                    }
                    if (frame.materialData != null)
                    {
                        foreach (var material in frame.materialData)
                        {
                            if (material != null && material.Count > 0)
                            {
                                reloadVoxelData = true;
                                break;
                            }
                        }
                        if (reloadVoxelData)
                        {
                            break;
                        }
                    }
                }
                if (reloadVoxelData)
                {
                    ReadyIndividualVoxelData();
                }
            };

            DisplayProgressBar("");

            #region Combine VoxelData
            {
                voxelBase.voxelData            = new VoxelData();
                voxelBase.voxelData.chunkTable = new DataTable3 <IntVector3>(voxelBase.voxelData.voxelSize.x, voxelBase.voxelData.voxelSize.y, voxelBase.voxelData.voxelSize.z);

                chunkDataList = new List <ChunkData>(voxelObject.frames.Count);
                int totalVoxelCount = 0;
                {
                    for (int i = 0; i < voxelObject.frames.Count; i++)
                    {
                        totalVoxelCount += voxelObject.frames[i].voxelData.voxels.Length;
                    }
                }
                var        voxels    = new VoxelData.Voxel[totalVoxelCount];
                IntVector3 voxelSize = IntVector3.zero;
                Dictionary <Color, int> paletteTable = new Dictionary <Color, int>();
                {
                    int offset = 0;
                    int index  = 0;
                    for (int i = 0; i < voxelObject.frames.Count; i++)
                    {
                        var voxelData = voxelObject.frames[i].voxelData;
                        chunkDataList.Add(new ChunkData());
                        chunkDataList[i].voxelBegin = index;
                        for (int j = 0; j < voxelData.voxels.Length; j++)
                        {
                            var voxel = voxelData.voxels[j];
                            var color = voxelData.palettes[voxel.palette];
                            if (!paletteTable.ContainsKey(color))
                            {
                                paletteTable.Add(color, paletteTable.Count);
                            }
                            voxel.palette   = paletteTable[color];
                            voxel.z        += offset;
                            voxels[index++] = voxel;
                            voxelBase.voxelData.chunkTable.Set(voxel.position, new IntVector3(i, 0, 0));
                        }
                        chunkDataList[i].voxelEnd = index;
                        chunkDataList[i].area     = new VoxelData.ChunkArea()
                        {
                            min = new IntVector3(0, 0, offset), max = new IntVector3(voxelData.voxelSize.x, voxelData.voxelSize.y, offset + voxelData.voxelSize.z)
                        };
                        voxelSize = IntVector3.Max(voxelSize, new IntVector3(voxelData.voxelSize.x, voxelData.voxelSize.y, offset + voxelData.voxelSize.z));
                        offset   += voxelData.voxelSize.z + 1;
                    }
                }
                #region Create
                voxelBase.localOffset = Vector3.zero;

                voxelBase.fileType = VoxelBase.FileType.vox;

                voxelBase.voxelData.voxels   = voxels;
                voxelBase.voxelData.palettes = new Color[paletteTable.Count];
                foreach (var pair in paletteTable)
                {
                    voxelBase.voxelData.palettes[pair.Value] = pair.Key;
                }
                voxelBase.voxelData.voxelSize = voxelSize;

                base.CreateVoxelTable();
                base.UpdateVisibleFlags();
                #endregion
            }
            #endregion

            DisplayProgressBar("");

            #region Combine DisableData
            {
                #region Erase
                for (int i = 0; i < voxelObject.frames.Count; i++)
                {
                    if (voxelObject.frames[i].disableData == null)
                    {
                        continue;
                    }

                    List <IntVector3> removeList = new List <IntVector3>();
                    voxelObject.frames[i].disableData.AllAction((pos, face) =>
                    {
                        if (voxelObject.frames[i].voxelData.VoxelTableContains(pos) < 0)
                        {
                            removeList.Add(pos);
                        }
                    });
                    for (int k = 0; k < removeList.Count; k++)
                    {
                        voxelObject.frames[i].disableData.RemoveDisable(removeList[k]);
                    }
                }
                #endregion
                for (int i = 0; i < voxelObject.frames.Count; i++)
                {
                    if (voxelObject.frames[i].disableData == null)
                    {
                        voxelObject.frames[i].disableData = new DisableData();
                    }
                }
                {
                    voxelObject.disableData = new DisableData();
                    for (int j = 0; j < voxelObject.frames.Count; j++)
                    {
                        if (voxelObject.frames[j].disableData == null)
                        {
                            continue;
                        }
                        voxelObject.frames[j].disableData.AllAction((pos, face) =>
                        {
                            voxelObject.disableData.SetDisable(chunkDataList[j].area.min + pos, face);
                        });
                    }
                }
            }
            #endregion

            DisplayProgressBar("");

            #region Combine MaterialData
            {
                #region Erase
                for (int i = 0; i < voxelObject.frames.Count; i++)
                {
                    if (voxelObject.frames[i].materialData == null)
                    {
                        continue;
                    }
                    for (int j = 0; j < voxelObject.frames[i].materialData.Count; j++)
                    {
                        List <IntVector3> removeList = new List <IntVector3>();
                        voxelObject.frames[i].materialData[j].AllAction((pos) =>
                        {
                            if (voxelObject.frames[i].voxelData.VoxelTableContains(pos) < 0)
                            {
                                removeList.Add(pos);
                            }
                        });
                        for (int k = 0; k < removeList.Count; k++)
                        {
                            voxelObject.frames[i].materialData[j].RemoveMaterial(removeList[k]);
                        }
                    }
                }
                #endregion
                voxelObject.materialData = new List <MaterialData>();
                int materialCount = 1;
                for (int i = 0; i < voxelObject.frames.Count; i++)
                {
                    if (voxelObject.frames[i].materialData != null)
                    {
                        materialCount = Math.Max(materialCount, voxelObject.frames[i].materialData.Count);
                    }
                }
                for (int i = 0; i < voxelObject.frames.Count; i++)
                {
                    if (voxelObject.frames[i].materialData == null)
                    {
                        voxelObject.frames[i].materialData = new List <MaterialData>();
                    }
                    for (int j = voxelObject.frames[i].materialData.Count; j < materialCount; j++)
                    {
                        voxelObject.frames[i].materialData.Add(new MaterialData());
                    }
                }
                for (int i = 0; i < materialCount; i++)
                {
                    voxelObject.materialData.Add(new MaterialData());
                    voxelObject.materialData[i].name        = voxelObject.frames[0].materialData[i].name;
                    voxelObject.materialData[i].transparent = voxelObject.frames[0].materialData[i].transparent;
                    voxelObject.materialData[i].material    = voxelObject.frames[0].materialData[i].material;
                    for (int j = 0; j < voxelObject.frames.Count; j++)
                    {
                        if (voxelObject.frames[j].materialData[i] == null)
                        {
                            continue;
                        }
                        voxelObject.frames[j].materialData[i].AllAction((pos) =>
                        {
                            voxelObject.materialData[i].SetMaterial(chunkDataList[j].area.min + pos);
                        });
                    }
                }
            }
            #endregion

            DisplayProgressBar("");

            #region Material
            {
                if (voxelBase.materialData == null)
                {
                    voxelBase.materialData = new List <MaterialData>();
                }
                if (voxelBase.materialData.Count == 0)
                {
                    voxelBase.materialData.Add(null);
                }
                for (int i = 0; i < voxelBase.materialData.Count; i++)
                {
                    if (voxelBase.materialData[i] == null)
                    {
                        voxelBase.materialData[i] = new MaterialData();
                    }
                }
                if (voxelObject.materials == null)
                {
                    voxelObject.materials = new List <Material>();
                }
                if (voxelObject.materials.Count < voxelObject.materialData.Count)
                {
                    for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
                    {
                        voxelObject.materials.Add(null);
                    }
                }
                else if (voxelObject.materials.Count > voxelObject.materialData.Count)
                {
                    voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
                }
            }
            voxelBase.CreateMaterialIndexTable();
            #endregion

            DisplayProgressBar("");

            CalcDataCreate(voxelBase.voxelData.voxels);

            DisplayProgressBar("");

            #region CreateFaceAreaTable
            {
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    VoxelData.Voxel[] voxels = new VoxelData.Voxel[chunkDataList[i].voxelEnd - chunkDataList[i].voxelBegin];
                    Array.Copy(voxelBase.voxelData.voxels, chunkDataList[i].voxelBegin, voxels, 0, voxels.Length);
                    chunkDataList[i].faceAreaTable = CreateFaceArea(voxels);
                }
            }
            #endregion

            DisplayProgressBar("");

            #region CreateTexture
            {
                var tmpFaceAreaTable = new VoxelData.FaceAreaTable();
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    tmpFaceAreaTable.Merge(chunkDataList[i].faceAreaTable);
                }
                {
                    var atlasTextureTmp = voxelObject.atlasTexture;
                    if (!CreateTexture(tmpFaceAreaTable, voxelBase.voxelData.palettes, ref atlasRectTable, ref atlasTextureTmp, ref atlasRects))
                    {
                        EditorUtility.ClearProgressBar();
                        return(false);
                    }
                    voxelObject.atlasTexture = atlasTextureTmp;
                    if (!AssetDatabase.IsMainAsset(voxelObject.atlasTexture))
                    {
                        if (voxelBase.legacyAssetNaming)
                        {
                            voxelObject.atlasTexture.name = "tex";
                        }
                        else
                        {
                            voxelObject.atlasTexture.name = Path.GetFileNameWithoutExtension(voxelBase.voxelFilePath);
                        }
                    }
                    if (!AssetDatabase.Contains(voxelObject.atlasTexture))
                    {
                        AddObjectToPrefabAsset(voxelObject.atlasTexture);
                    }
                }
            }
            #endregion

            DisplayProgressBar("");

            #region CreateMesh
            DisplayProgressBar("");
            if (voxelObject.importMode == VoxelBase.ImportMode.LowPoly)
            {
                int forward = 0;
                int up      = 0;
                int right   = 0;
                int left    = 0;
                int down    = 0;
                int back    = 0;
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    AtlasRectTable atlasRectTableTmp = new AtlasRectTable();
                    {
                        atlasRectTableTmp.forward = atlasRectTable.forward.GetRange(forward, chunkDataList[i].faceAreaTable.forward.Count);
                        forward += chunkDataList[i].faceAreaTable.forward.Count;
                        atlasRectTableTmp.up = atlasRectTable.up.GetRange(up, chunkDataList[i].faceAreaTable.up.Count);
                        up += chunkDataList[i].faceAreaTable.up.Count;
                        atlasRectTableTmp.right = atlasRectTable.right.GetRange(right, chunkDataList[i].faceAreaTable.right.Count);
                        right += chunkDataList[i].faceAreaTable.right.Count;
                        atlasRectTableTmp.left = atlasRectTable.left.GetRange(left, chunkDataList[i].faceAreaTable.left.Count);
                        left += chunkDataList[i].faceAreaTable.left.Count;
                        atlasRectTableTmp.down = atlasRectTable.down.GetRange(down, chunkDataList[i].faceAreaTable.down.Count);
                        down += chunkDataList[i].faceAreaTable.down.Count;
                        atlasRectTableTmp.back = atlasRectTable.back.GetRange(back, chunkDataList[i].faceAreaTable.back.Count);
                        back += chunkDataList[i].faceAreaTable.back.Count;
                    }
                    var extraOffset = new Vector3(0, 0f, -chunkDataList[i].area.min.z);
                    voxelBase.localOffset      = voxelObject.frames[i].localOffset;
                    voxelObject.frames[i].mesh = CreateMeshOnly(voxelObject.frames[i].mesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, atlasRects, atlasRectTableTmp, extraOffset, out voxelObject.frames[i].materialIndexes);
                }
            }
            else
            {
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    var extraOffset = new Vector3(0, 0f, -chunkDataList[i].area.min.z);
                    voxelBase.localOffset      = voxelObject.frames[i].localOffset;
                    voxelObject.frames[i].mesh = CreateMeshOnly(voxelObject.frames[i].mesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, atlasRects, atlasRectTable, extraOffset, out voxelObject.frames[i].materialIndexes);
                }
            }
            {
                HashSet <int> combineMaterialIndexes = new HashSet <int>();
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    foreach (var index in voxelObject.frames[i].materialIndexes)
                    {
                        combineMaterialIndexes.Add(index);
                    }
                }
                voxelObject.materialIndexes = combineMaterialIndexes.ToList();
            }
            #endregion

            DisplayProgressBar("");

            #region CreateMaterial
            {
                if (voxelObject.materialData == null)
                {
                    voxelObject.materialData = new List <MaterialData>();
                }
                if (voxelObject.materialData.Count == 0)
                {
                    voxelObject.materialData.Add(null);
                }
                for (int i = 0; i < voxelObject.materialData.Count; i++)
                {
                    if (voxelObject.materialData[i] == null)
                    {
                        voxelObject.materialData[i] = new MaterialData();
                    }
                }
                if (voxelObject.materials == null)
                {
                    voxelObject.materials = new List <Material>();
                }
                if (voxelObject.materials.Count < voxelObject.materialData.Count)
                {
                    for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
                    {
                        voxelObject.materials.Add(null);
                    }
                }
                else if (voxelObject.materials.Count > voxelObject.materialData.Count)
                {
                    voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
                }

                for (int i = 0; i < voxelObject.materials.Count; i++)
                {
                    if (!voxelObject.materialIndexes.Contains(i))
                    {
                        if (voxelObject.materials[i] != null)
                        {
                            voxelObject.materials[i] = null;
                            DestroyUnusedObjectInPrefabObject();
                        }
                        continue;
                    }
                    if (voxelObject.materials[i] == null)
                    {
                        voxelObject.materials[i] = EditorCommon.CreateStandardMaterial();
                    }
                    if (!AssetDatabase.IsMainAsset(voxelObject.materials[i]))
                    {
                        if (voxelBase.legacyAssetNaming)
                        {
                            voxelObject.materials[i].name = string.Format("mat{0}", i);
                        }
                        else
                        {
                            voxelObject.materials[i].name = Path.GetFileNameWithoutExtension(voxelBase.voxelFilePath) + string.Format("_{0}", i);
                        }
                    }
                    if (!AssetDatabase.Contains(voxelObject.materials[i]))
                    {
                        AddObjectToPrefabAsset(voxelObject.materials[i]);
                    }
                }
            }
            #endregion

            DisplayProgressBar("");
            {
                if (voxelBase.generateLightmapUVs)
                {
                    var param = voxelBase.GetLightmapParam();
                    for (int i = 0; i < chunkDataList.Count; i++)
                    {
                        if (voxelObject.frames[i].mesh.uv.Length > 0)
                        {
                            Unwrapping.GenerateSecondaryUVSet(voxelObject.frames[i].mesh, param);
                        }
                    }
                }
                if (voxelBase.generateTangents)
                {
                    for (int i = 0; i < chunkDataList.Count; i++)
                    {
                        voxelObject.frames[i].mesh.RecalculateTangents();
                    }
                }
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    if (!AssetDatabase.IsMainAsset(voxelObject.frames[i].mesh))
                    {
                        if (voxelBase.legacyAssetNaming)
                        {
                            voxelObject.frames[i].mesh.name = string.Format("mesh_{0}", voxelObject.frames[i].name);
                        }
                        else
                        {
                            voxelObject.frames[i].mesh.name = Path.GetFileNameWithoutExtension(voxelBase.voxelFilePath) + string.Format("_{0}", voxelObject.frames[i].name);
                        }
                    }
                    if (!AssetDatabase.Contains(voxelObject.frames[i].mesh))
                    {
                        AddObjectToPrefabAsset(voxelObject.frames[i].mesh);
                    }
                }
            }

            DisplayProgressBar("");

            SetRendererCompornent();

            EditorUtility.ClearProgressBar();

            voxelObject.Edit_SetFrameCurrentVoxelOtherData();

            return(true);
        }
Exemple #4
0
        protected override bool CreateMesh()
        {
            base.CreateMesh();

            #region ProgressBar
            const float     MaxProgressCount   = 7f;
            float           ProgressCount      = 0;
            Action <string> DisplayProgressBar = (info) =>
            {
                if (voxelData.voxels.Length > 10000)
                {
                    EditorUtility.DisplayProgressBar("Create Mesh...", string.Format("{0} / {1}", ProgressCount, MaxProgressCount), (ProgressCount++ / MaxProgressCount));
                }
            };
            #endregion

            DisplayProgressBar("");

            #region Combine VoxelData
            {
                voxelBase.voxelData            = new VoxelData();
                voxelBase.voxelData.chunkTable = new DataTable3 <IntVector3>(voxelBase.voxelData.voxelSize.x, voxelBase.voxelData.voxelSize.y, voxelBase.voxelData.voxelSize.z);

                chunkDataList = new List <ChunkData>(voxelObject.frames.Count);
                List <VoxelData.Voxel>  voxels       = new List <VoxelData.Voxel>();
                IntVector3              voxelSize    = IntVector3.zero;
                Dictionary <Color, int> paletteTable = new Dictionary <Color, int>();
                int offset = 0;
                for (int i = 0; i < voxelObject.frames.Count; i++)
                {
                    chunkDataList.Add(new ChunkData());
                    chunkDataList[i].voxelBegin = voxels.Count;
                    for (int j = 0; j < voxelObject.frames[i].voxelData.voxels.Length; j++)
                    {
                        var voxel = voxelObject.frames[i].voxelData.voxels[j];
                        var color = voxelObject.frames[i].voxelData.palettes[voxel.palette];
                        if (!paletteTable.ContainsKey(color))
                        {
                            paletteTable.Add(color, paletteTable.Count);
                        }
                        voxel.palette = paletteTable[color];
                        voxel.z      += offset;
                        voxels.Add(voxel);
                        voxelBase.voxelData.chunkTable.Set(voxel.position, new IntVector3(i, 0, 0));
                    }
                    chunkDataList[i].voxelEnd = voxels.Count;
                    chunkDataList[i].area     = new VoxelData.ChunkArea()
                    {
                        min = new IntVector3(0, 0, offset), max = new IntVector3(voxelObject.frames[i].voxelData.voxelSize.x, voxelObject.frames[i].voxelData.voxelSize.y, offset + voxelObject.frames[i].voxelData.voxelSize.z)
                    };
                    voxelSize = IntVector3.Max(voxelSize, new IntVector3(voxelObject.frames[i].voxelData.voxelSize.x, voxelObject.frames[i].voxelData.voxelSize.y, offset + voxelObject.frames[i].voxelData.voxelSize.z));
                    offset   += voxelObject.frames[i].voxelData.voxelSize.z + 1;
                }
                #region Create
                voxelBase.localOffset = Vector3.zero;

                voxelBase.fileType = VoxelBase.FileType.vox;

                voxelBase.voxelData.voxels   = voxels.ToArray();
                voxelBase.voxelData.palettes = new Color[paletteTable.Count];
                foreach (var pair in paletteTable)
                {
                    voxelBase.voxelData.palettes[pair.Value] = pair.Key;
                }
                voxelBase.voxelData.voxelSize = voxelSize;

                voxelBase.voxelData.CreateVoxelTable();
                UpdateVisibleFlags();
                #endregion
            }
            #endregion

            DisplayProgressBar("");

            #region Combine MaterialData
            {
                #region Erase
                for (int i = 0; i < voxelObject.frames.Count; i++)
                {
                    if (voxelObject.frames[i].materialData == null)
                    {
                        continue;
                    }
                    for (int j = 0; j < voxelObject.frames[i].materialData.Count; j++)
                    {
                        List <IntVector3> removeList = new List <IntVector3>();
                        voxelObject.frames[i].materialData[j].AllAction((pos) =>
                        {
                            if (voxelObject.frames[i].voxelData.VoxelTableContains(pos) < 0)
                            {
                                removeList.Add(pos);
                            }
                        });
                        for (int k = 0; k < removeList.Count; k++)
                        {
                            voxelObject.frames[i].materialData[j].RemoveMaterial(removeList[k]);
                        }
                    }
                }
                #endregion
                voxelObject.materialData = new List <MaterialData>();
                int materialCount = 1;
                for (int i = 0; i < voxelObject.frames.Count; i++)
                {
                    if (voxelObject.frames[i].materialData != null)
                    {
                        materialCount = Math.Max(materialCount, voxelObject.frames[i].materialData.Count);
                    }
                }
                for (int i = 0; i < voxelObject.frames.Count; i++)
                {
                    if (voxelObject.frames[i].materialData == null)
                    {
                        voxelObject.frames[i].materialData = new List <MaterialData>();
                    }
                    for (int j = voxelObject.frames[i].materialData.Count; j < materialCount; j++)
                    {
                        voxelObject.frames[i].materialData.Add(new MaterialData());
                    }
                }
                for (int i = 0; i < materialCount; i++)
                {
                    voxelObject.materialData.Add(new MaterialData());
                    voxelObject.materialData[i].name        = voxelObject.frames[0].materialData[i].name;
                    voxelObject.materialData[i].transparent = voxelObject.frames[0].materialData[i].transparent;
                    voxelObject.materialData[i].material    = voxelObject.frames[0].materialData[i].material;
                    for (int j = 0; j < voxelObject.frames.Count; j++)
                    {
                        if (voxelObject.frames[j].materialData[i] == null)
                        {
                            continue;
                        }
                        voxelObject.frames[j].materialData[i].AllAction((pos) =>
                        {
                            voxelObject.materialData[i].SetMaterial(chunkDataList[j].area.min + pos);
                        });
                    }
                }
            }
            #endregion

            DisplayProgressBar("");

            #region Material
            {
                if (voxelBase.materialData == null)
                {
                    voxelBase.materialData = new List <MaterialData>();
                }
                if (voxelBase.materialData.Count == 0)
                {
                    voxelBase.materialData.Add(null);
                }
                for (int i = 0; i < voxelBase.materialData.Count; i++)
                {
                    if (voxelBase.materialData[i] == null)
                    {
                        voxelBase.materialData[i] = new MaterialData();
                    }
                }
                if (voxelObject.materials == null)
                {
                    voxelObject.materials = new List <Material>();
                }
                if (voxelObject.materials.Count < voxelObject.materialData.Count)
                {
                    for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
                    {
                        voxelObject.materials.Add(null);
                    }
                }
                else if (voxelObject.materials.Count > voxelObject.materialData.Count)
                {
                    voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
                }
            }
            voxelBase.CreateMaterialIndexTable();
            #endregion

            CalcDataCreate(voxelBase.voxelData.voxels);

            #region CreateFaceAreaTable
            {
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    VoxelData.Voxel[] voxels = new VoxelData.Voxel[chunkDataList[i].voxelEnd - chunkDataList[i].voxelBegin];
                    Array.Copy(voxelBase.voxelData.voxels, chunkDataList[i].voxelBegin, voxels, 0, voxels.Length);
                    chunkDataList[i].faceAreaTable = CreateFaceArea(voxels);
                }
            }
            #endregion

            #region CreateTexture
            {
                var tmpFaceAreaTable = new VoxelData.FaceAreaTable();
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    tmpFaceAreaTable.Merge(chunkDataList[i].faceAreaTable);
                }
                {
                    var atlasTextureTmp = voxelObject.atlasTexture;
                    if (!CreateTexture(tmpFaceAreaTable, voxelBase.voxelData.palettes, ref atlasRectTable, ref atlasTextureTmp, ref atlasRects))
                    {
                        EditorUtility.ClearProgressBar();
                        return(false);
                    }
                    voxelObject.atlasTexture = atlasTextureTmp;
                    {
                        if (voxelObject.materialData == null)
                        {
                            voxelObject.materialData = new List <MaterialData>();
                        }
                        if (voxelObject.materialData.Count == 0)
                        {
                            voxelObject.materialData.Add(null);
                        }
                        for (int i = 0; i < voxelObject.materialData.Count; i++)
                        {
                            if (voxelObject.materialData[i] == null)
                            {
                                voxelObject.materialData[i] = new MaterialData();
                            }
                        }
                        if (voxelObject.materials == null)
                        {
                            voxelObject.materials = new List <Material>();
                        }
                        if (voxelObject.materials.Count < voxelObject.materialData.Count)
                        {
                            for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
                            {
                                voxelObject.materials.Add(null);
                            }
                        }
                        else if (voxelObject.materials.Count > voxelObject.materialData.Count)
                        {
                            voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
                        }
                        for (int i = 0; i < voxelObject.materials.Count; i++)
                        {
                            if (voxelObject.materials[i] == null)
                            {
                                voxelObject.materials[i] = new Material(Shader.Find("Standard"));
                            }
                            if (!AssetDatabase.Contains(voxelObject.materials[i]))
                            {
                                AddObjectToPrefabAsset(voxelObject.materials[i], "mat", i);
                            }
                        }
                        if (!AssetDatabase.Contains(voxelObject.atlasTexture))
                        {
                            AddObjectToPrefabAsset(voxelObject.atlasTexture, "tex");
                        }
                    }
                }
            }
            #endregion

            #region CreateMesh
            DisplayProgressBar("");
            if (voxelObject.importMode == VoxelBase.ImportMode.LowPoly)
            {
                int forward = 0;
                int up      = 0;
                int right   = 0;
                int left    = 0;
                int down    = 0;
                int back    = 0;
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    AtlasRectTable atlasRectTableTmp = new AtlasRectTable();
                    {
                        atlasRectTableTmp.forward = atlasRectTable.forward.GetRange(forward, chunkDataList[i].faceAreaTable.forward.Count);
                        forward += chunkDataList[i].faceAreaTable.forward.Count;
                        atlasRectTableTmp.up = atlasRectTable.up.GetRange(up, chunkDataList[i].faceAreaTable.up.Count);
                        up += chunkDataList[i].faceAreaTable.up.Count;
                        atlasRectTableTmp.right = atlasRectTable.right.GetRange(right, chunkDataList[i].faceAreaTable.right.Count);
                        right += chunkDataList[i].faceAreaTable.right.Count;
                        atlasRectTableTmp.left = atlasRectTable.left.GetRange(left, chunkDataList[i].faceAreaTable.left.Count);
                        left += chunkDataList[i].faceAreaTable.left.Count;
                        atlasRectTableTmp.down = atlasRectTable.down.GetRange(down, chunkDataList[i].faceAreaTable.down.Count);
                        down += chunkDataList[i].faceAreaTable.down.Count;
                        atlasRectTableTmp.back = atlasRectTable.back.GetRange(back, chunkDataList[i].faceAreaTable.back.Count);
                        back += chunkDataList[i].faceAreaTable.back.Count;
                    }
                    var extraOffset = new Vector3(0, 0f, -chunkDataList[i].area.min.z);
                    voxelBase.localOffset      = voxelObject.frames[i].localOffset;
                    voxelObject.frames[i].mesh = CreateMeshOnly(voxelObject.frames[i].mesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, atlasRects, atlasRectTableTmp, extraOffset, out voxelObject.frames[i].materialIndexes);
                    if (!AssetDatabase.Contains(voxelObject.frames[i].mesh))
                    {
                        var name = voxelObject.frames[i].voxelFileObject != null ? voxelObject.frames[i].voxelFileObject.name : Path.GetFileNameWithoutExtension(voxelObject.frames[i].voxelFilePath);
                        AddObjectToPrefabAsset(voxelObject.frames[i].mesh, string.Format("mesh_{0}", name));
                    }
                }
            }
            else
            {
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    var extraOffset = new Vector3(0, 0f, -chunkDataList[i].area.min.z);
                    voxelBase.localOffset      = voxelObject.frames[i].localOffset;
                    voxelObject.frames[i].mesh = CreateMeshOnly(voxelObject.frames[i].mesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, atlasRects, atlasRectTable, extraOffset, out voxelObject.frames[i].materialIndexes);
                    if (!AssetDatabase.Contains(voxelObject.frames[i].mesh))
                    {
                        var name = voxelObject.frames[i].voxelFileObject != null ? voxelObject.frames[i].voxelFileObject.name : Path.GetFileNameWithoutExtension(voxelObject.frames[i].voxelFilePath);
                        AddObjectToPrefabAsset(voxelObject.frames[i].mesh, string.Format("mesh_{0}", name));
                    }
                }
            }
            #endregion

            DisplayProgressBar("");
            if (voxelBase.generateLightmapUVs)
            {
                for (int i = 0; i < chunkDataList.Count; i++)
                {
                    if (voxelObject.frames[i].mesh.uv.Length > 0)
                    {
                        Unwrapping.GenerateSecondaryUVSet(voxelObject.frames[i].mesh);
                    }
                }
            }

            DisplayProgressBar("");

            SetRendererCompornent();

            RefreshCheckerSave();

            EditorUtility.ClearProgressBar();

            voxelObject.Edit_SetFrameCurrentVoxelMaterialData();

            return(true);
        }