예제 #1
0
        protected override void CreateChunkData()
        {
            if (voxelObject.splitMode != VoxelChunksObject.SplitMode.ChunkSize)
            {
                if (voxelData.chunkDataList == null)
                {
                    voxelObject.splitMode = VoxelChunksObject.SplitMode.ChunkSize;
                }
            }
            if (voxelObject.splitMode == VoxelChunksObject.SplitMode.QubicleMatrix || voxelObject.splitMode == VoxelChunksObject.SplitMode.WorldEditor)
            {
                voxelData.chunkTable = new DataTable3 <IntVector3>(voxelData.voxelSize.x, voxelData.voxelSize.y, voxelData.voxelSize.z);

                var chunkVoxels   = new List <int> [voxelData.chunkDataList.Count];
                var chunkPalettes = new HashSet <Color> [voxelData.chunkDataList.Count];
                for (int i = 0; i < voxelData.chunkDataList.Count; i++)
                {
                    chunkVoxels[i]   = new List <int>();
                    chunkPalettes[i] = new HashSet <Color>();
                }
                for (int i = 0; i < voxelData.voxels.Length; i++)
                {
                    var chunkIndex = voxelData.chunkIndexTable.Get(voxelData.voxels[i].position);
                    //voxel
                    chunkVoxels[chunkIndex].Add(i);
                    //palette
                    chunkPalettes[chunkIndex].Add(voxelData.palettes[voxelData.voxels[i].palette]);
                    //
                    voxelData.chunkTable.Set(voxelData.voxels[i].position, new IntVector3(chunkIndex, int.MinValue, int.MinValue));
                }
                {
                    chunkDataList = new List <ChunkData>(chunkVoxels.Length);
                    for (int i = 0; i < chunkVoxels.Length; i++)
                    {
                        chunkDataList.Add(new ChunkData()
                        {
                            position = new IntVector3(i, int.MinValue, int.MinValue),
                            name     = string.Format("Chunk({0})", voxelData.chunkDataList[i].name),
                            area     = voxelData.chunkDataList[i].area,
                            voxels   = chunkVoxels[i],
                            palettes = chunkPalettes[i].ToArray()
                        });
                    }
                    chunkDataList.Sort((a, b) => string.Compare(voxelData.chunkDataList[a.position.x].name, voxelData.chunkDataList[b.position.x].name));
                }
            }
            else
            {
                voxelData.chunkTable = new DataTable3 <IntVector3>(voxelData.voxelSize.x, voxelData.voxelSize.y, voxelData.voxelSize.z);

                var chunkVoxels   = new DataTable3 <List <int> >(voxelData.voxelSize.x, voxelData.voxelSize.y, voxelData.voxelSize.z);
                var chunkPalettes = new DataTable3 <HashSet <Color> >(voxelData.voxelSize.x, voxelData.voxelSize.y, voxelData.voxelSize.z);
                for (int i = 0; i < voxelData.voxels.Length; i++)
                {
                    var chunkPosition = GetChunkPosition(voxelData.voxels[i].position);
                    //voxel
                    if (!chunkVoxels.Contains(chunkPosition))
                    {
                        chunkVoxels.Set(chunkPosition, new List <int>());
                    }
                    chunkVoxels.Get(chunkPosition).Add(i);
                    //palette
                    if (!chunkPalettes.Contains(chunkPosition))
                    {
                        chunkPalettes.Set(chunkPosition, new HashSet <Color>());
                    }
                    chunkPalettes.Get(chunkPosition).Add(voxelData.palettes[voxelData.voxels[i].palette]);
                    //
                    voxelData.chunkTable.Set(voxelData.voxels[i].position, chunkPosition);
                }
                {
                    chunkDataList = new List <ChunkData>();
                    chunkVoxels.AllAction((x, y, z, list) =>
                    {
                        var pos = new IntVector3(x, y, z);
                        chunkDataList.Add(new ChunkData()
                        {
                            position = pos,
                            name     = string.Format("Chunk({0}, {1}, {2})", x, y, z),
                            area     = new VoxelData.ChunkArea()
                            {
                                min = pos * voxelObject.chunkSize, max = (pos + IntVector3.one) * voxelObject.chunkSize - IntVector3.one
                            },
                            voxels   = list,
                            palettes = chunkPalettes.Get(pos).ToArray()
                        });
                    });
                    chunkDataList.Sort((a, b) => a.position.x != b.position.x ? a.position.x - b.position.x : a.position.y != b.position.y ? a.position.y - b.position.y : a.position.z - b.position.z);
                }
            }
        }
예제 #2
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);
        }
예제 #3
0
 public void RevertEditorParam()
 {
     edit_importScale  = importScale;
     edit_importOffset = importOffset;
     edit_chunkSize    = chunkSize;
 }
예제 #4
0
 public void ApplyEditorParam()
 {
     importScale  = edit_importScale;
     importOffset = edit_importOffset;
     chunkSize    = edit_chunkSize;
 }
예제 #5
0
        public List <IntVector3> GetFillVoxel(IntVector3 pos)
        {
            if (objectTarget.voxelData == null)
            {
                return(null);
            }
            if (objectTarget.voxelData.VoxelTableContains(pos) < 0)
            {
                return(null);
            }

            CheckFillVoxelTableCheck();
            if (fillVoxelTable == null)
            {
                fillVoxelTable = new DataTable3 <List <IntVector3> >(objectTarget.voxelData.voxelSize.x, objectTarget.voxelData.voxelSize.y, objectTarget.voxelData.voxelSize.z);
            }
            if (!fillVoxelTable.Contains(pos))
            {
                List <IntVector3> searchList = new List <IntVector3>();
                for (int i = 0; i < objectTarget.voxelData.voxels.Length; i++)
                {
                    int posPalette = 0;
                    var doneTable  = new FlagTable3(objectTarget.voxelData.voxelSize.x, objectTarget.voxelData.voxelSize.y, objectTarget.voxelData.voxelSize.z);
                    {
                        var p = objectTarget.voxelData.voxels[i].position;
                        if (fillVoxelTable.Get(p) != null)
                        {
                            continue;
                        }
                        var index = objectTarget.voxelData.VoxelTableContains(p);
                        posPalette = objectTarget.voxelData.voxels[index].palette;
                        searchList.Clear();
                        searchList.Add(p);
                        doneTable.Set(p, true);
                    }
                    var result = new List <IntVector3>();
                    for (int j = 0; j < searchList.Count; j++)
                    {
                        var p     = searchList[j];
                        var index = objectTarget.voxelData.VoxelTableContains(p);
                        if (index < 0)
                        {
                            continue;
                        }
                        if (objectTarget.voxelData.voxels[index].palette == posPalette)
                        {
                            result.Add(p);
                            for (int x = p.x - 1; x <= p.x + 1; x++)
                            {
                                for (int y = p.y - 1; y <= p.y + 1; y++)
                                {
                                    for (int z = p.z - 1; z <= p.z + 1; z++)
                                    {
                                        if (x >= 0 && y >= 0 && z >= 0 &&
                                            x < objectTarget.voxelData.voxelSize.x && y < objectTarget.voxelData.voxelSize.y && z < objectTarget.voxelData.voxelSize.z &&
                                            !doneTable.Get(x, y, z))
                                        {
                                            doneTable.Set(x, y, z, true);
                                            var indexTmp = objectTarget.voxelData.VoxelTableContains(x, y, z);
                                            if (indexTmp >= 0 && objectTarget.voxelData.voxels[indexTmp].palette == posPalette)
                                            {
                                                searchList.Add(new IntVector3(x, y, z));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    for (int j = 0; j < result.Count; j++)
                    {
                        fillVoxelTable.Set(result[j], result);
                    }
                }
            }
            var fillVoxel = fillVoxelTable.Get(pos);

            return(fillVoxel);
        }