public void CreatePrefab(VoxelData data, string pfName)
        {
            VoxelData dd = new VoxelData(data);

            VerifyShaders();

            GameObject obj = new GameObject(pfName);

            obj.isStatic = importStatic;

            OptimizedVoxelMesh assetDifuse = null;
            Material           diffuseMat  = new Material(importDiffuseShader);

            diffuseMat.name             = "Diffuse";
            diffuseMat.enableInstancing = true;

            if (dd.VoxelCount() > 0)
            {
                assetDifuse              = new OptimizedVoxelMesh(dd, importPivot, importTextureScale, importPadding, importScale);
                assetDifuse.mesh.name    = "Diffuse";
                assetDifuse.texture.name = "Diffuse";

                MeshFilter mf = obj.AddComponent <MeshFilter>();
                mf.mesh = assetDifuse.mesh;

                diffuseMat.mainTexture = assetDifuse.texture;
                diffuseMat.SetFloat("_Metallic", 0);
                diffuseMat.SetFloat("_Glossiness", 0);

                MeshRenderer mr = obj.AddComponent <MeshRenderer>();
                if (import2Sided)
                {
                    mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided;
                }

                mr.material = diffuseMat;

                AddCollider(obj);
                if (importRigidbody)
                {
                    obj.AddComponent <Rigidbody>();
                }
            }



            //create prefab

            if (assetDifuse != null)
            {
                string assetPath  = importAssetPath + pfName + ".asset";
                string prefabPath = importAssetPath + pfName + ".prefab";

                AssetDatabase.CreateAsset(new Mesh(), assetPath);

                if (assetDifuse != null)
                {
                    AssetDatabase.AddObjectToAsset(assetDifuse.mesh, assetPath);
                    AssetDatabase.AddObjectToAsset(assetDifuse.texture, assetPath);
                    AssetDatabase.AddObjectToAsset(diffuseMat, assetPath);
                }

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();

                PrefabUtility.CreatePrefab(prefabPath, obj);
                //GameObject prefab = PrefabUtility.CreatePrefab(prefabPath, obj);
                //Selection.activeObject = prefab;
            }

            GameObject.DestroyImmediate(obj);
        }
Esempio n. 2
0
        public PreviewVoxelMesh(VoxelData data)
        {
            List <Vector3> verts  = new List <Vector3>();
            List <int>     idxs   = new List <int>();
            List <Color32> colors = new List <Color32>();

            for (int x = 0; x < data.width; x++)
            {
                for (int y = 0; y < data.height; y++)
                {
                    for (int z = 0; z < data.depth; z++)
                    {
                        if (data.IsVisible(x, y, z))
                        {
                            int i = verts.Count;

                            Color32 col = data.VoxelColor(x, y, z);

                            float   c  = ((float)((data.GetVoxel(x, y, z) - 1))) / 256;
                            Vector2 uv = new Vector2(c, 0);

                            //Top...
                            verts.Add(new Vector3(x, y + 1, z));
                            verts.Add(new Vector3(x, y + 1, z + 1));
                            verts.Add(new Vector3(x + 1, y + 1, z + 1));
                            verts.Add(new Vector3(x + 1, y + 1, z));

                            colors.Add(col); colors.Add(col); colors.Add(col); colors.Add(col);

                            idxs.Add(i + 0);
                            idxs.Add(i + 1);
                            idxs.Add(i + 2);
                            idxs.Add(i + 0);
                            idxs.Add(i + 2);
                            idxs.Add(i + 3);
                            i += 4;

                            //Bottom...
                            verts.Add(new Vector3(x, y, z));
                            verts.Add(new Vector3(x, y, z + 1));
                            verts.Add(new Vector3(x + 1, y, z + 1));
                            verts.Add(new Vector3(x + 1, y, z));

                            colors.Add(col); colors.Add(col); colors.Add(col); colors.Add(col);

                            idxs.Add(i + 0);
                            idxs.Add(i + 2);
                            idxs.Add(i + 1);
                            idxs.Add(i + 0);
                            idxs.Add(i + 3);
                            idxs.Add(i + 2);
                            i += 4;

                            //Front...
                            verts.Add(new Vector3(x, y, z));
                            verts.Add(new Vector3(x, y + 1, z));
                            verts.Add(new Vector3(x + 1, y + 1, z));
                            verts.Add(new Vector3(x + 1, y, z));

                            colors.Add(col); colors.Add(col); colors.Add(col); colors.Add(col);

                            idxs.Add(i + 0);
                            idxs.Add(i + 1);
                            idxs.Add(i + 2);
                            idxs.Add(i + 0);
                            idxs.Add(i + 2);
                            idxs.Add(i + 3);
                            i += 4;

                            //Back...
                            verts.Add(new Vector3(x, y, z + 1));
                            verts.Add(new Vector3(x, y + 1, z + 1));
                            verts.Add(new Vector3(x + 1, y + 1, z + 1));
                            verts.Add(new Vector3(x + 1, y, z + 1));

                            colors.Add(col); colors.Add(col); colors.Add(col); colors.Add(col);

                            idxs.Add(i + 0);
                            idxs.Add(i + 2);
                            idxs.Add(i + 1);
                            idxs.Add(i + 0);
                            idxs.Add(i + 3);
                            idxs.Add(i + 2);
                            i += 4;

                            //Left...
                            verts.Add(new Vector3(x, y, z));
                            verts.Add(new Vector3(x, y + 1, z));
                            verts.Add(new Vector3(x, y + 1, z + 1));
                            verts.Add(new Vector3(x, y, z + 1));

                            colors.Add(col); colors.Add(col); colors.Add(col); colors.Add(col);

                            idxs.Add(i + 0);
                            idxs.Add(i + 3);
                            idxs.Add(i + 2);
                            idxs.Add(i + 0);
                            idxs.Add(i + 2);
                            idxs.Add(i + 1);
                            i += 4;

                            //Right...
                            verts.Add(new Vector3(x + 1, y, z));
                            verts.Add(new Vector3(x + 1, y + 1, z));
                            verts.Add(new Vector3(x + 1, y + 1, z + 1));
                            verts.Add(new Vector3(x + 1, y, z + 1));

                            colors.Add(col); colors.Add(col); colors.Add(col); colors.Add(col);

                            idxs.Add(i + 0);
                            idxs.Add(i + 1);
                            idxs.Add(i + 2);
                            idxs.Add(i + 0);
                            idxs.Add(i + 2);
                            idxs.Add(i + 3);
                            i += 4;
                        }
                    }
                }
            }

            //Debug.Log(verts.Count);
            //Debug.Log(uvs.Count);
            //Debug.Log(idxs.Count);

            mesh              = new Mesh();
            mesh.indexFormat  = UnityEngine.Rendering.IndexFormat.UInt32;
            mesh.subMeshCount = 1;

            mesh.vertices = verts.ToArray();
            mesh.SetIndices(idxs.ToArray(), MeshTopology.Triangles, 0);
            mesh.SetColors(colors);

            mesh.RecalculateNormals();
        }
        private VoxelData MergedModels()
        {
            Vector3Int min = new Vector3Int(4096, 4096, 4096);
            Vector3Int max = new Vector3Int(-4096, -4096, -4096);

            for (int i = 0; i < importer.models.Count; i++)
            {
                Vector3Int s = importer.Size(i);
                Vector3Int c = importer.modelOffsets[i];//centers

                int xmin = c.x - (s.x / 2);
                int xmax = c.x + (s.x / 2);
                if (min.x > xmin)
                {
                    min.x = xmin;
                }
                if (max.x < xmax)
                {
                    max.x = xmax;
                }

                int ymin = c.y - (s.y / 2);
                int ymax = c.y + (s.y / 2);
                if (min.y > ymin)
                {
                    min.y = ymin;
                }
                if (max.y < ymax)
                {
                    max.y = ymax;
                }

                int zmin = c.z - (s.z / 2);
                int zmax = c.z + (s.z / 2);
                if (min.z > zmin)
                {
                    min.z = zmin;
                }
                if (max.z < zmax)
                {
                    max.z = zmax;
                }
            }

            //Debug.Log(min.ToString());
            //Debug.Log(max.ToString());

            int w = Mathf.Abs(max.x - min.x) + 1;
            int h = Mathf.Abs(max.y - min.y) + 1;
            int d = Mathf.Abs(max.z - min.z) + 1;
            //Debug.Log(w + "," + h + "," + d);

            VoxelData data = new VoxelData(w, h, d, importer.palette, importer.materials);

            //Merge all models...
            for (int i = 0; i < importer.models.Count; i++)
            {
                Vector3Int s = importer.Size(i);
                Vector3Int o = importer.modelOffsets[i];
                o.x -= min.x;
                o.y -= min.y;
                o.z -= min.z;

                o.x -= s.x / 2;
                o.y -= s.y / 2;
                o.z -= s.z / 2;

                for (int x = 0; x < s.x; x++)
                {
                    for (int y = 0; y < s.y; y++)
                    {
                        for (int z = 0; z < s.z; z++)
                        {
                            if (importer.models[i][x, y, z] > 0)
                            {
                                data.SetVoxel(x + o.x, y + o.y, z + o.z, importer.models[i][x, y, z]);
                            }
                        }
                    }
                }
            }

            return(data);
        }