Ejemplo n.º 1
0
        IEnumerator SetMeshAsync()
        {
            if (this.data == null)
            {
                this.data = PlanetUtility.Read(this.PlanetName, iPos, jPos, kPos, this.planetSide.side);
                yield return(null);
            }

            Mesh mesh;
            Mesh meshCollider;

            this.BuildMeshAlt(out mesh, out meshCollider);

            this.GetComponent <MeshFilter>().sharedMesh = mesh;

            this.GetComponent <MeshRenderer>().materials = this.planetSide.Materials;
            yield return(null);

            if (this.GetComponent <MeshCollider>())
            {
                this.GetComponent <MeshCollider>().sharedMesh = meshCollider;
            }

            this.meshSet = true;
            PlanetChunckManager.Instance.workingLock = false;
        }
Ejemplo n.º 2
0
        public void SetMesh(bool async = true)
        {
            PlanetChunckManager.Instance.workingLock = true;
            if (async)
            {
                StartCoroutine(SetMeshAsync());
            }
            else
            {
                if (this.data == null)
                {
                    this.data = PlanetUtility.Read(this.PlanetName, iPos, jPos, kPos, this.planetSide.side);
                }

                Mesh mesh;
                Mesh meshCollider;
                this.BuildMeshAlt(out mesh, out meshCollider);

                this.GetComponent <MeshFilter>().sharedMesh = mesh;

                this.GetComponent <MeshRenderer>().materials = this.planetSide.Materials;

                if (this.GetComponent <MeshCollider>())
                {
                    this.GetComponent <MeshCollider>().sharedMesh = meshCollider;
                }

                this.meshSet = true;
            }
        }
Ejemplo n.º 3
0
        public void Initialize(Planet planet)
        {
            this.planet = planet;

            this.chuncks = new PlanetChunck[7][][];
            for (int kPos = 0; kPos <= 6; kPos++)
            {
                int chuncksCount = PlanetUtility.DegreeToChuncksCount(PlanetUtility.KPosToDegree(kPos));
                this.chuncks [kPos] = new PlanetChunck[chuncksCount][];
                for (int iPos = 0; iPos < chuncksCount; iPos++)
                {
                    this.chuncks[kPos][iPos] = new PlanetChunck[chuncksCount];
                    for (int jPos = 0; jPos < chuncksCount; jPos++)
                    {
                        Transform planetChunkChild = this.transform.Find(PlanetUtility.ChunckName(iPos, jPos, kPos));
                        if (planetChunkChild != null)
                        {
                            this.chuncks[kPos][iPos][jPos] = planetChunkChild.GetComponent <PlanetChunck>();
                        }
                        if (this.chuncks[kPos][iPos][jPos] == null)
                        {
                            this.chuncks[kPos][iPos][jPos] = PlanetUtility.InstantiatePlanetChunck(iPos, jPos, kPos, this);
                        }
                        this.chuncks[kPos][iPos][jPos].Initialize();
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void Initialize(Planet planet)
        {
            this.planet   = planet;
            this.nbChunks = ((int)Mathf.Pow(2, this.Degree)) / PlanetUtility.ChunckSize;

            this.chuncks = new PlanetChunck[this.nbChunks][][];
            for (int iPos = 0; iPos < this.nbChunks; iPos++)
            {
                this.chuncks[iPos] = new PlanetChunck[this.nbChunks][];
                for (int jPos = 0; jPos < this.nbChunks; jPos++)
                {
                    this.chuncks[iPos][jPos] = new PlanetChunck[this.nbChunks / 2];
                    for (int kPos = 0; kPos < this.nbChunks / 2; kPos++)
                    {
                        Transform planetChunkChild = this.transform.Find(PlanetUtility.ChunckName(iPos, jPos, kPos));
                        if (planetChunkChild != null)
                        {
                            this.chuncks[iPos][jPos][kPos] = planetChunkChild.GetComponent <PlanetChunck>();
                        }
                        if (this.chuncks[iPos][jPos][kPos] == null)
                        {
                            this.chuncks[iPos][jPos][kPos] = PlanetUtility.InstantiatePlanetChunck(iPos, jPos, kPos, this);
                        }
                        this.chuncks[iPos][jPos][kPos].Initialize();
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public Vector3 GetLocalPosBlockCenter(int i, int j, int k)
        {
            Vector3 localPos = Vector3.zero;

            Vector3 [][] baseMesh = PlanetUtility.GetBaseMesh(this.subDegree).vertices;

            localPos  = baseMesh [i] [j] + baseMesh [i] [j + 1] + baseMesh [i + 1] [j] + baseMesh [i + 1] [j + 1];
            localPos  = localPos / 4f;
            localPos += localPos.normalized * k;

            return(localPos);
        }
Ejemplo n.º 6
0
        public void Initialize()
        {
            PlanetChunckManager.Instances.Add(this);

            this.transform.parent        = this.planetSide.transform;
            this.name                    = "Chunck_" + this.iPos + "|" + this.jPos + "|" + this.kPos;
            this.transform.localPosition = Vector3.zero;
            this.transform.localRotation = Quaternion.identity;
            this.transform.localScale    = Vector3.one;

            int y = PlanetUtility.ChunckSize / 2 + this.jPos * PlanetUtility.ChunckSize;
            int z = PlanetUtility.ChunckSize / 2 + this.iPos * PlanetUtility.ChunckSize;

            this.localUp            = PlanetUtility.EvaluateVertex(this.planetSide.Size, y, z).normalized;
            this.AngleToReferential = Vector3.Angle(PlanetChunckManager.Instance.Referential.position - this.transform.position, this.transform.TransformVector(this.LocalUp));
        }
Ejemplo n.º 7
0
        public PlanetChunck SetDataAtIJKPos(PlanetSide planetSide, int iPos, int jPos, int kPos, Byte data, bool ifZero = false, bool rebuild = true, bool save = true)
        {
            PlanetChunck planetChunck = planetSide.chuncks[iPos / PlanetUtility.ChunckSize][jPos / PlanetUtility.ChunckSize][kPos / PlanetUtility.ChunckSize];

            if ((ifZero && planetChunck.data[iPos % PlanetUtility.ChunckSize][jPos % PlanetUtility.ChunckSize][kPos % PlanetUtility.ChunckSize] == 0) || !ifZero)
            {
                planetChunck.SetData(data, iPos % PlanetUtility.ChunckSize, jPos % PlanetUtility.ChunckSize, kPos % PlanetUtility.ChunckSize);
            }
            if (rebuild)
            {
                planetChunck.SetMesh();
            }
            if (save)
            {
                PlanetUtility.Save(planetChunck.PlanetName, planetChunck.data, planetChunck.iPos, planetChunck.jPos, planetChunck.kPos, planetChunck.planetSide.side);
            }
            return(planetChunck);
        }
Ejemplo n.º 8
0
        static public Vector3 GetLocalPosBlockCenter()
        {
            int i = (int)posInPlanetSide.x;
            int j = (int)posInPlanetSide.y;
            int k = (int)posInPlanetSide.z;

            Vector3[][] baseMesh = PlanetUtility.GetBaseMesh(cursorPlanetSide.subDegree).vertices;

            Vector3 center = Vector3.zero;

            center += baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized;
            center += baseMesh[i][j] + (k) * baseMesh[i][j].normalized;
            center += baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized;
            center += baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized;

            center = center / 4f;

            return(center);
        }
Ejemplo n.º 9
0
        public void Initialize()
        {
            Debug.Log("Planet Initialize");
            PlanetSide[] planetSidesInChildren = this.GetComponentsInChildren <PlanetSide>();

            this.ReadPlanetInfoFile();
            this.planetSides = new Dictionary <Planet.Side, PlanetSide>();
            this.size        = Mathf.FloorToInt(Mathf.Pow(2f, this.degree));
            this.rMin        = Mathf.FloorToInt((2 / Mathf.PI - 1 / 8f) * size);

            // Search for PlanetSide in Children. If any cannot be found, it is instantiated.
            foreach (Side side in Enum.GetValues(typeof(Planet.Side)))
            {
                bool       instantiatePlanetSide = true;
                PlanetSide newPlanetSide         = null;

                // Search for an already existing planetSide.
                foreach (PlanetSide planetSide in planetSidesInChildren)
                {
                    if (planetSide.side == side)
                    {
                        newPlanetSide         = planetSide;
                        instantiatePlanetSide = false;
                    }
                }

                // If no existing PlanetSide has been found, create one.
                if (instantiatePlanetSide)
                {
                    GameObject newSideGameObject = new GameObject();
                    newSideGameObject.name                    = side.ToString();
                    newSideGameObject.transform.parent        = this.transform;
                    newSideGameObject.transform.localPosition = Vector3.zero;
                    newSideGameObject.transform.localRotation = PlanetUtility.LocalRotationFromSide(side);

                    newPlanetSide      = newSideGameObject.AddComponent <PlanetSide>();
                    newPlanetSide.side = side;
                }

                this.planetSides.Add(side, newPlanetSide);
            }
        }
Ejemplo n.º 10
0
        public PlanetChunck SetDataAtWorldPos(Vector3 worldPos, Byte data, bool ifZero = false, bool rebuild = true, bool save = true)
        {
            PlanetChunck planetChunck;
            int          i, j, k;

            this.WorldPositionToIJK(worldPos, out planetChunck, out i, out j, out k);
            if ((ifZero && planetChunck.data[i][j][k] == 0) || !ifZero)
            {
                planetChunck.SetData(data, i, j, k);
            }
            else
            {
                return(null);
            }
            if (rebuild)
            {
                planetChunck.SetMesh();
            }
            if (save)
            {
                PlanetUtility.Save(planetChunck.PlanetName, planetChunck.data, planetChunck.iPos, planetChunck.jPos, planetChunck.kPos, planetChunck.planetSide.side);
            }
            return(planetChunck);
        }
Ejemplo n.º 11
0
        static void BuildCursor(int blockType, int i, int j, int k, PlanetSide planetSide)
        {
            Mesh cursorMesh = new Mesh();

            List <Vector3> cursorVertices  = new List <Vector3> ();
            List <Vector3> cursorNormals   = new List <Vector3> ();
            List <Vector2> cursorUV        = new List <Vector2> ();
            List <int>     cursorTriangles = new List <int> ();

            Vector3[][] baseMesh = PlanetUtility.GetBaseMesh(cursorPlanetSide.subDegree).vertices;

            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;

            float blockHeight = 1f;

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add((baseMesh[i][j + 1] - baseMesh[i + 1][j + 1]).normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k + blockHeight) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add((baseMesh[i][j + 1] - baseMesh[i + 1][j + 1]).normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k + blockHeight) * baseMesh[i][j].normalized);
            cursorNormals.Add((baseMesh[i][j] - baseMesh[i + 1][j]).normalized);
            cursorUV.Add(new Vector2(1, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k) * baseMesh[i][j].normalized);
            cursorNormals.Add((baseMesh[i][j] - baseMesh[i + 1][j]).normalized);
            cursorUV.Add(new Vector2(1, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add((baseMesh[i + 1][j] - baseMesh[i][j]).normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k + blockHeight) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add((baseMesh[i + 1][j] - baseMesh[i][j]).normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k + blockHeight) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add((baseMesh[i + 1][j + 1] - baseMesh[i][j + 1]).normalized);
            cursorUV.Add(new Vector2(1, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add((baseMesh[i + 1][j + 1] - baseMesh[i][j + 1]).normalized);
            cursorUV.Add(new Vector2(1, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k) * baseMesh[i][j].normalized);
            cursorNormals.Add((baseMesh[i][j] - baseMesh[i][j + 1]).normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k + blockHeight) * baseMesh[i][j].normalized);
            cursorNormals.Add((baseMesh[i][j] - baseMesh[i][j + 1]).normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k + blockHeight) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add((baseMesh[i + 1][j] - baseMesh[i + 1][j + 1]).normalized);
            cursorUV.Add(new Vector2(1, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add((baseMesh[i + 1][j] - baseMesh[i + 1][j + 1]).normalized);
            cursorUV.Add(new Vector2(1, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add((baseMesh[i + 1][j + 1] - baseMesh[i + 1][j]).normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k + blockHeight) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add((baseMesh[i + 1][j + 1] - baseMesh[i + 1][j]).normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k + blockHeight) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add((baseMesh[i][j + 1] - baseMesh[i][j]).normalized);
            cursorUV.Add(new Vector2(1, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add((baseMesh[i][j + 1] - baseMesh[i][j]).normalized);
            cursorUV.Add(new Vector2(1, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add(-baseMesh[i][j + 1].normalized);
            cursorUV.Add(new Vector2(0, 0));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k) * baseMesh[i][j].normalized);
            cursorNormals.Add(-baseMesh[i][j].normalized);
            cursorUV.Add(new Vector2(0, 0.5f));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add(-baseMesh[i + 1][j].normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add(-baseMesh[i + 1][j + 1].normalized);
            cursorUV.Add(new Vector2(0.5f, 0));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k + blockHeight) * baseMesh[i][j].normalized);
            cursorNormals.Add(baseMesh[i][j].normalized);
            cursorUV.Add(new Vector2(0, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k + blockHeight) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add(baseMesh[i][j + 1].normalized);
            cursorUV.Add(new Vector2(0, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k + blockHeight) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add(baseMesh[i + 1][j + 1].normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k + blockHeight) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add(baseMesh[i + 1][j].normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            if (blockType == 0)
            {
                Vector3 barycenter = Vector3.zero;
                foreach (Vector3 v in cursorVertices)
                {
                    barycenter += v;
                }

                barycenter /= ((float)cursorVertices.Count);

                for (int v = 0; v < cursorVertices.Count; v++)
                {
                    cursorVertices [v] += (cursorVertices [v] - barycenter).normalized * 0.1f;
                }
            }

            cursorMesh.vertices  = cursorVertices.ToArray();
            cursorMesh.triangles = cursorTriangles.ToArray();
            cursorMesh.normals   = cursorNormals.ToArray();
            cursorMesh.uv        = cursorUV.ToArray();

            PlanetCursor.CursorMeshFilter.sharedMesh   = cursorMesh;
            PlanetCursor.posInPlanetSide               = new Vector3(i, j, k);
            PlanetCursor.CursorTransform.parent        = planetSide.transform;
            PlanetCursor.CursorTransform.localPosition = Vector3.zero;
            PlanetCursor.CursorTransform.localRotation = Quaternion.identity;
            PlanetCursor.CursorMeshRenderer.enabled    = true;
        }
Ejemplo n.º 12
0
        public Vector3 GetIJKFor(Vector3 planetPos)
        {
            Vector3 localPos = Quaternion.Inverse(this.transform.localRotation) * planetPos;

            return(PlanetUtility.GetBaseMesh(this.subDegree).GetIJK(localPos));
        }
Ejemplo n.º 13
0
        public void Initialize(Planet planet, Vector3 orientation, int subDegree)
        {
            this.planet        = planet;
            this.materials     = planet.planetMaterials;
            this.subDegree     = subDegree;
            this.orientation   = orientation;
            this.nbChunks      = ((int)Mathf.Pow(2, subDegree)) / PlanetUtility.ChunckSize;
            this.heightChuncks = 1;

            this.chuncks = new PlanetChunck[this.nbChunks * this.nbChunks * this.heightChuncks];
            for (int i = 0; i < this.nbChunks; i++)
            {
                for (int j = 0; j < this.nbChunks; j++)
                {
                    for (int k = 0; k < this.heightChuncks; k++)
                    {
                        this.chuncks[i + j * this.nbChunks + k * this.nbChunks * this.heightChuncks] = PlanetUtility.InstantiatePlanetChunck(this.orientation, new Vector3(i, j, k), this);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        public Mesh BuildMesh()
        {
            Mesh mesh = new Mesh();

            mesh.subMeshCount = 2;

            this.meshCollider = new Mesh();
            this.meshCollider.subMeshCount = 1;

            List <Vector3> vertices = new List <Vector3> ();

            List <int>[] triangles = new List <int> [2];
            for (int i = 0; i < 2; i++)
            {
                triangles[i] = new List <int> ();
            }
            List <Vector3> normals = new List <Vector3> ();
            List <Vector2> uv      = new List <Vector2> ();

            int iOff = (int)this.posInChunck.x * PlanetUtility.ChunckSize;
            int jOff = (int)this.posInChunck.y * PlanetUtility.ChunckSize;
            int kOff = (int)this.posInChunck.z * PlanetUtility.ChunckSize;

            Vector3[][] baseMesh = PlanetUtility.GetBaseMesh(this.planetSide.subDegree).vertices;

            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;

            for (int i = 0; i < PlanetUtility.ChunckSize; i++)
            {
                for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                {
                    for (int k = 0; k < 32; k++)
                    {
                        if (this.blocks [i + j * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] == 0)
                        {
                            if (k < this.planetSide.planet.waterLevel)
                            {
                                this.blocks [i + j * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] = 2;
                            }
                        }

                        if (this.blocks [i + j * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] != 0)
                        {
                            int blockType = this.blocks [i + j * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize];

                            bool isOnTop = false;

                            if (k + 1 < 32)
                            {
                                if (this.blocks [i + j * PlanetUtility.ChunckSize + (k + 1) * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] == 0)
                                {
                                    isOnTop = true;
                                }
                                else if (blockType == 2)
                                {
                                    if (this.blocks [i + j * PlanetUtility.ChunckSize + (k + 1) * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] != 2)
                                    {
                                        isOnTop = true;
                                    }
                                }
                            }
                            else if (k + 1 == 32)
                            {
                                isOnTop = true;
                            }

                            float blockHeight = 1f;
                            if ((blockType == 2) && (isOnTop))
                            {
                                blockHeight = 0.8f;
                            }

                            int subMesh = 0;
                            if (blockType == 2)
                            {
                                subMesh = 1;
                            }

                            bool buildIMinus = false;

                            if (blockType != 2)
                            {
                                int iMinusBlock;
                                if (i > 0)
                                {
                                    iMinusBlock = this.blocks [i - 1 + j * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize];
                                }
                                else if (i == 0)
                                {
                                    iMinusBlock = this.planetSide.GetBlockFor(iOff + i - 1, jOff + j, kOff + k);
                                }
                                else
                                {
                                    iMinusBlock = -1;
                                }
                                if (((iMinusBlock == 0) || (iMinusBlock == 2)) || (iMinusBlock == -1))
                                {
                                    buildIMinus = true;
                                }
                            }

                            if (buildIMinus)
                            {
                                a = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j + 1] + (kOff + k) * baseMesh[iOff + i][jOff + j + 1].normalized);
                                normals.Add((baseMesh[iOff + i][jOff + j + 1] - baseMesh[iOff + i + 1][jOff + j + 1]).normalized);

                                b = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j + 1] + (kOff + k + blockHeight) * baseMesh[iOff + i][jOff + j + 1].normalized);
                                normals.Add((baseMesh[iOff + i][jOff + j + 1] - baseMesh[iOff + i + 1][jOff + j + 1]).normalized);

                                c = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j] + (kOff + k + blockHeight) * baseMesh[iOff + i][jOff + j].normalized);
                                normals.Add((baseMesh[iOff + i][jOff + j] - baseMesh[iOff + i + 1][jOff + j]).normalized);

                                d = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j] + (kOff + k) * baseMesh[iOff + i][jOff + j].normalized);
                                normals.Add((baseMesh[iOff + i][jOff + j] - baseMesh[iOff + i + 1][jOff + j]).normalized);

                                this.AddUV(uv, blockType, CubeFace.Side, isOnTop);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(b);
                                triangles[subMesh].Add(c);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(c);
                                triangles[subMesh].Add(d);
                            }

                            bool buildIPlus = false;

                            if (blockType != 2)
                            {
                                int iPlusBlock;
                                if (i + 1 < PlanetUtility.ChunckSize)
                                {
                                    iPlusBlock = this.blocks [i + 1 + j * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize];
                                }
                                else if (i + 1 == PlanetUtility.ChunckSize)
                                {
                                    iPlusBlock = this.planetSide.GetBlockFor(iOff + i + 1, jOff + j, kOff + k);
                                }
                                else
                                {
                                    iPlusBlock = -1;
                                }
                                if (((iPlusBlock == 0) || (iPlusBlock == 2)) || (iPlusBlock == -1))
                                {
                                    buildIPlus = true;
                                }
                            }

                            if (buildIPlus)
                            {
                                a = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j] + (kOff + k) * baseMesh[iOff + i + 1][jOff + j].normalized);
                                normals.Add((baseMesh[iOff + i + 1][jOff + j] - baseMesh[iOff + i][jOff + j]).normalized);

                                b = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j] + (kOff + k + blockHeight) * baseMesh[iOff + i + 1][jOff + j].normalized);
                                normals.Add((baseMesh[iOff + i + 1][jOff + j] - baseMesh[iOff + i][jOff + j]).normalized);

                                c = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j + 1] + (kOff + k + blockHeight) * baseMesh[iOff + i + 1][jOff + j + 1].normalized);
                                normals.Add((baseMesh[iOff + i + 1][jOff + j + 1] - baseMesh[iOff + i][jOff + j + 1]).normalized);

                                d = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j + 1] + (kOff + k) * baseMesh[iOff + i + 1][jOff + j + 1].normalized);
                                normals.Add((baseMesh[iOff + i + 1][jOff + j + 1] - baseMesh[iOff + i][jOff + j + 1]).normalized);

                                this.AddUV(uv, blockType, CubeFace.Side, isOnTop);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(b);
                                triangles[subMesh].Add(c);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(c);
                                triangles[subMesh].Add(d);
                            }

                            bool buildJMinus = false;

                            if (blockType != 2)
                            {
                                int jMinusBlock;
                                if (j > 0)
                                {
                                    jMinusBlock = this.blocks [i + (j - 1) * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize];
                                }
                                else if (j == 0)
                                {
                                    jMinusBlock = this.planetSide.GetBlockFor(iOff + i, jOff + j - 1, kOff + k);
                                }
                                else
                                {
                                    jMinusBlock = -1;
                                }
                                if (((jMinusBlock == 0) || (jMinusBlock == 2)) || (jMinusBlock == -1))
                                {
                                    buildJMinus = true;
                                }
                            }

                            if (buildJMinus)
                            {
                                a = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j] + (kOff + k) * baseMesh[iOff + i][jOff + j].normalized);
                                normals.Add((baseMesh[iOff + i][jOff + j] - baseMesh[iOff + i][jOff + j + 1]).normalized);

                                b = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j] + (kOff + k + blockHeight) * baseMesh[iOff + i][jOff + j].normalized);
                                normals.Add((baseMesh[iOff + i][jOff + j] - baseMesh[iOff + i][jOff + j + 1]).normalized);

                                c = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j] + (kOff + k + blockHeight) * baseMesh[iOff + i + 1][jOff + j].normalized);
                                normals.Add((baseMesh[iOff + i + 1][jOff + j] - baseMesh[iOff + i + 1][jOff + j + 1]).normalized);

                                d = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j] + (kOff + k) * baseMesh[iOff + i + 1][jOff + j].normalized);
                                normals.Add((baseMesh[iOff + i + 1][jOff + j] - baseMesh[iOff + i + 1][jOff + j + 1]).normalized);

                                this.AddUV(uv, blockType, CubeFace.Side, isOnTop);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(b);
                                triangles[subMesh].Add(c);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(c);
                                triangles[subMesh].Add(d);
                            }

                            bool buildJPlus = false;

                            if (blockType != 2)
                            {
                                int jPlusBlock;
                                if (j + 1 < PlanetUtility.ChunckSize)
                                {
                                    jPlusBlock = this.blocks [i + (j + 1) * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize];
                                }
                                else if (j + 1 == PlanetUtility.ChunckSize)
                                {
                                    jPlusBlock = this.planetSide.GetBlockFor(iOff + i, jOff + j + 1, kOff + k);
                                }
                                else
                                {
                                    jPlusBlock = -1;
                                }
                                if (((jPlusBlock == 0) || (jPlusBlock == 2)) || (jPlusBlock == -1))
                                {
                                    buildJPlus = true;
                                }
                            }

                            if (buildJPlus)
                            {
                                a = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j + 1] + (kOff + k) * baseMesh[iOff + i + 1][jOff + j + 1].normalized);
                                normals.Add((baseMesh[iOff + i + 1][jOff + j + 1] - baseMesh[iOff + i + 1][jOff + j]).normalized);

                                b = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j + 1] + (kOff + k + blockHeight) * baseMesh[iOff + i + 1][jOff + j + 1].normalized);
                                normals.Add((baseMesh[iOff + i + 1][jOff + j + 1] - baseMesh[iOff + i + 1][jOff + j]).normalized);

                                c = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j + 1] + (kOff + k + blockHeight) * baseMesh[iOff + i][jOff + j + 1].normalized);
                                normals.Add((baseMesh[iOff + i][jOff + j + 1] - baseMesh[iOff + i][jOff + j]).normalized);

                                d = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j + 1] + (kOff + k) * baseMesh[iOff + i][jOff + j + 1].normalized);
                                normals.Add((baseMesh[iOff + i][jOff + j + 1] - baseMesh[iOff + i][jOff + j]).normalized);

                                this.AddUV(uv, blockType, CubeFace.Side, isOnTop);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(b);
                                triangles[subMesh].Add(c);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(c);
                                triangles[subMesh].Add(d);
                            }

                            bool buildKMinus = false;

                            if (blockType != 2)
                            {
                                if (k > 0)
                                {
                                    if ((this.blocks [i + j * PlanetUtility.ChunckSize + (k - 1) * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] == 0) || (this.blocks [i + j * PlanetUtility.ChunckSize + (k - 1) * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] == 2))
                                    {
                                        buildKMinus = true;
                                    }
                                }
                                else if (k == 0)
                                {
                                    buildKMinus = false;
                                }
                            }

                            if (buildKMinus)
                            {
                                a = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j + 1] + (kOff + k) * baseMesh[iOff + i][jOff + j + 1].normalized);
                                normals.Add(-baseMesh[iOff + i][jOff + j + 1].normalized);

                                b = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j] + (kOff + k) * baseMesh[iOff + i][jOff + j].normalized);
                                normals.Add(-baseMesh[iOff + i][jOff + j].normalized);

                                c = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j] + (kOff + k) * baseMesh[iOff + i + 1][jOff + j].normalized);
                                normals.Add(-baseMesh[iOff + i + 1][jOff + j].normalized);

                                d = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j + 1] + (kOff + k) * baseMesh[iOff + i + 1][jOff + j + 1].normalized);
                                normals.Add(-baseMesh[iOff + i + 1][jOff + j + 1].normalized);

                                this.AddUV(uv, blockType, CubeFace.Bottom, isOnTop);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(b);
                                triangles[subMesh].Add(c);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(c);
                                triangles[subMesh].Add(d);
                            }

                            bool buildKPlus = false;

                            if (blockType != 2)
                            {
                                if (k + 1 < 32)
                                {
                                    if ((this.blocks [i + j * PlanetUtility.ChunckSize + (k + 1) * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] == 0) || (this.blocks [i + j * PlanetUtility.ChunckSize + (k + 1) * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] == 2))
                                    {
                                        buildKPlus = true;
                                    }
                                }
                                else if (k + 1 == 32)
                                {
                                    buildKPlus = true;
                                }
                            }
                            else if ((blockType == 2) && isOnTop)
                            {
                                buildKPlus = true;
                            }

                            if (buildKPlus)
                            {
                                a = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j] + (kOff + k + blockHeight) * baseMesh[iOff + i][jOff + j].normalized);
                                normals.Add(baseMesh[iOff + i][jOff + j].normalized);

                                b = vertices.Count;
                                vertices.Add(baseMesh[iOff + i][jOff + j + 1] + (kOff + k + blockHeight) * baseMesh[iOff + i][jOff + j + 1].normalized);
                                normals.Add(baseMesh[iOff + i][jOff + j + 1].normalized);

                                c = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j + 1] + (kOff + k + blockHeight) * baseMesh[iOff + i + 1][jOff + j + 1].normalized);
                                normals.Add(baseMesh[iOff + i + 1][jOff + j + 1].normalized);

                                d = vertices.Count;
                                vertices.Add(baseMesh[iOff + i + 1][jOff + j] + (kOff + k + blockHeight) * baseMesh[iOff + i + 1][jOff + j].normalized);
                                normals.Add(baseMesh[iOff + i + 1][jOff + j].normalized);

                                this.AddUV(uv, blockType, CubeFace.Top, isOnTop);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(b);
                                triangles[subMesh].Add(c);

                                triangles[subMesh].Add(a);
                                triangles[subMesh].Add(c);
                                triangles[subMesh].Add(d);
                            }
                        }
                    }
                }
            }

            mesh.vertices = vertices.ToArray();
            this.meshCollider.vertices = vertices.ToArray();

            mesh.SetTriangles(triangles [0].ToArray(), 0);
            mesh.SetTriangles(triangles [1].ToArray(), 1);
            this.meshCollider.SetTriangles(triangles [0].ToArray(), 0);

            mesh.normals = normals.ToArray();
            mesh.uv      = uv.ToArray();

            return(mesh);
        }
Ejemplo n.º 15
0
        public void BuildMeshAlt(out Mesh mesh, out Mesh meshCollider)
        {
            int   size   = this.planetSide.Size;
            int   rMin   = this.planetSide.RMin;
            float rWater = WaterLevel - 0.2f;

            mesh = new Mesh();
            List <Vector3> vertices        = new List <Vector3>();
            List <Vector2> uvs             = new List <Vector2>();
            List <int>     trianglesTop    = new List <int>();
            List <int>     trianglesSide   = new List <int>();
            List <int>     trianglesBottom = new List <int>();
            List <int>     trianglesWater  = new List <int>();

            for (int i = 0; i < PlanetUtility.ChunckSize; i++)
            {
                for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                {
                    for (int k = 0; k < PlanetUtility.ChunckSize; k++)
                    {
                        int y = j + this.jPos * PlanetUtility.ChunckSize;
                        int z = i + this.iPos * PlanetUtility.ChunckSize;
                        if ((k + this.kPos * PlanetUtility.ChunckSize) == WaterLevel)
                        {
                            if (this.data[i][j][k] == 0)
                            {
                                int a = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z) * (rWater + rMin));
                                int b = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z) * (rWater + rMin));
                                int c = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z + 1) * (rWater + rMin));
                                int d = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z + 1) * (rWater + rMin));

                                trianglesWater.Add(a);
                                trianglesWater.Add(b);
                                trianglesWater.Add(c);

                                trianglesWater.Add(a);
                                trianglesWater.Add(c);
                                trianglesWater.Add(d);

                                AddUVWater(uvs);
                            }
                        }
                        Byte block = this.data[i][j][k];
                        if (block != 0)
                        {
                            if ((i - 1 < 0) || (this.data[i - 1][j][k] < 128 && this.data[i - 1][j][k] != block))
                            {
                                int a = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int b = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int c = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int d = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));

                                trianglesSide.Add(a);
                                trianglesSide.Add(b);
                                trianglesSide.Add(c);

                                trianglesSide.Add(a);
                                trianglesSide.Add(c);
                                trianglesSide.Add(d);

                                PlanetUtility.AddUV(uvs, block);
                            }
                            if ((j - 1 < 0) || (this.data[i][j - 1][k] < 128 && this.data[i][j - 1][k] != block))
                            {
                                int a = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int b = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int c = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z + 1) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int d = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z + 1) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));

                                trianglesSide.Add(a);
                                trianglesSide.Add(b);
                                trianglesSide.Add(c);

                                trianglesSide.Add(a);
                                trianglesSide.Add(c);
                                trianglesSide.Add(d);

                                PlanetUtility.AddUV(uvs, block);
                            }
                            if ((k - 1 < 0) || (this.data[i][j][k - 1] < 128 && this.data[i][j][k - 1] != block))
                            {
                                int a = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int b = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z + 1) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int c = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z + 1) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int d = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));

                                trianglesTop.Add(a);
                                trianglesTop.Add(b);
                                trianglesTop.Add(c);

                                trianglesTop.Add(a);
                                trianglesTop.Add(c);
                                trianglesTop.Add(d);

                                PlanetUtility.AddUV(uvs, block);
                            }
                            if ((i + 1 >= PlanetUtility.ChunckSize) || (this.data[i + 1][j][k] < 128 && this.data[i + 1][j][k] != block))
                            {
                                int a = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z + 1) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int b = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z + 1) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int c = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z + 1) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int d = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z + 1) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));

                                trianglesSide.Add(a);
                                trianglesSide.Add(b);
                                trianglesSide.Add(c);

                                trianglesSide.Add(a);
                                trianglesSide.Add(c);
                                trianglesSide.Add(d);

                                PlanetUtility.AddUV(uvs, block);
                            }
                            if ((j + 1 >= PlanetUtility.ChunckSize) || (this.data[i][j + 1][k] < 128 && this.data[i][j + 1][k] != block))
                            {
                                int a = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z + 1) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int b = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z + 1) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int c = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int d = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z) * (k + this.kPos * PlanetUtility.ChunckSize + rMin));

                                trianglesSide.Add(a);
                                trianglesSide.Add(b);
                                trianglesSide.Add(c);

                                trianglesSide.Add(a);
                                trianglesSide.Add(c);
                                trianglesSide.Add(d);

                                PlanetUtility.AddUV(uvs, block);
                            }
                            if ((k + 1 >= PlanetUtility.ChunckSize) || (this.data[i][j][k + 1] < 128 && this.data[i][j][k + 1] != block))
                            {
                                int a = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int b = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int c = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y + 1, z + 1) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));
                                int d = vertices.Count;
                                vertices.Add(PlanetUtility.EvaluateVertex(size, y, z + 1) * (k + 1 + this.kPos * PlanetUtility.ChunckSize + rMin));

                                trianglesTop.Add(a);
                                trianglesTop.Add(b);
                                trianglesTop.Add(c);

                                trianglesTop.Add(a);
                                trianglesTop.Add(c);
                                trianglesTop.Add(d);

                                PlanetUtility.AddUV(uvs, block);
                            }
                        }
                    }
                }
            }

            mesh.vertices     = vertices.ToArray();
            mesh.uv           = uvs.ToArray();
            mesh.subMeshCount = 4;
            mesh.SetTriangles(trianglesTop, 0);
            mesh.SetTriangles(trianglesSide, 1);
            mesh.SetTriangles(trianglesBottom, 2);
            mesh.SetTriangles(trianglesWater, 3);
            mesh.RecalculateNormals();

            meshCollider          = new Mesh();
            meshCollider.vertices = vertices.ToArray();
            List <int> meshColliderTriangles = new List <int>(trianglesTop);

            meshColliderTriangles.AddRange(trianglesSide);
            meshColliderTriangles.AddRange(trianglesBottom);
            meshCollider.triangles = meshColliderTriangles.ToArray();
        }
Ejemplo n.º 16
0
 static public int KGlobalToDegree(int k)
 {
     return(PlanetUtility.KPosToDegree(k / PlanetUtility.ChunckSize));
 }
Ejemplo n.º 17
0
 static public int DegreeToChuncksCount(int degree)
 {
     return(PlanetUtility.DegreeToSize(degree) / PlanetUtility.ChunckSize);
 }
Ejemplo n.º 18
0
        public Mesh WorldPositionToBlockMesh(int iPos, int jPos, int kPos, Byte block)
        {
            int size = this.size;
            int rMin = this.rMin;

            Mesh           m               = new Mesh();
            List <Vector3> vertices        = new List <Vector3>();
            List <Vector2> uvs             = new List <Vector2>();
            List <int>     trianglesTop    = new List <int>();
            List <int>     trianglesSide   = new List <int>();
            List <int>     trianglesBottom = new List <int>();

            int a = vertices.Count;

            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos) * (kPos + rMin));
            int b = vertices.Count;

            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos) * (kPos + 1 + rMin));
            int c = vertices.Count;

            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos) * (kPos + 1 + rMin));
            int d = vertices.Count;

            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos) * (kPos + rMin));

            trianglesSide.Add(a);
            trianglesSide.Add(b);
            trianglesSide.Add(c);

            trianglesSide.Add(a);
            trianglesSide.Add(c);
            trianglesSide.Add(d);

            PlanetUtility.AddUV(uvs, block);

            a = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos) * (kPos + rMin));
            b = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos) * (kPos + 1 + rMin));
            c = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos + 1) * (kPos + 1 + rMin));
            d = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos + 1) * (kPos + rMin));

            trianglesSide.Add(a);
            trianglesSide.Add(b);
            trianglesSide.Add(c);

            trianglesSide.Add(a);
            trianglesSide.Add(c);
            trianglesSide.Add(d);

            PlanetUtility.AddUV(uvs, block);

            a = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos) * (kPos + rMin));
            b = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos + 1) * (kPos + rMin));
            c = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos + 1) * (kPos + rMin));
            d = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos) * (kPos + rMin));

            trianglesTop.Add(a);
            trianglesTop.Add(b);
            trianglesTop.Add(c);

            trianglesTop.Add(a);
            trianglesTop.Add(c);
            trianglesTop.Add(d);

            PlanetUtility.AddUV(uvs, block);

            a = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos + 1) * (kPos + rMin));
            b = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos + 1) * (kPos + 1 + rMin));
            c = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos + 1) * (kPos + 1 + rMin));
            d = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos + 1) * (kPos + rMin));

            trianglesSide.Add(a);
            trianglesSide.Add(b);
            trianglesSide.Add(c);

            trianglesSide.Add(a);
            trianglesSide.Add(c);
            trianglesSide.Add(d);

            PlanetUtility.AddUV(uvs, block);

            a = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos + 1) * (kPos + rMin));
            b = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos + 1) * (kPos + 1 + rMin));
            c = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos) * (kPos + 1 + rMin));
            d = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos) * (kPos + rMin));

            trianglesSide.Add(a);
            trianglesSide.Add(b);
            trianglesSide.Add(c);

            trianglesSide.Add(a);
            trianglesSide.Add(c);
            trianglesSide.Add(d);

            PlanetUtility.AddUV(uvs, block);

            a = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos) * (kPos + 1 + rMin));
            b = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos) * (kPos + 1 + rMin));
            c = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos + 1, iPos + 1) * (kPos + 1 + rMin));
            d = vertices.Count;
            vertices.Add(PlanetUtility.EvaluateVertex(size, jPos, iPos + 1) * (kPos + 1 + rMin));

            trianglesTop.Add(a);
            trianglesTop.Add(b);
            trianglesTop.Add(c);

            trianglesTop.Add(a);
            trianglesTop.Add(c);
            trianglesTop.Add(d);

            PlanetUtility.AddUV(uvs, block);

            m.vertices     = vertices.ToArray();
            m.subMeshCount = 3;
            m.SetTriangles(trianglesTop, 0);
            m.SetTriangles(trianglesSide, 1);
            m.SetTriangles(trianglesBottom, 2);
            m.uv = uvs.ToArray();
            m.RecalculateNormals();

            if (block == 0)
            {
                Vector3[] eraserVertices = new Vector3[m.vertices.Length];
                for (int i = 0; i < m.vertices.Length; i++)
                {
                    eraserVertices[i] = m.vertices[i] + m.normals[i] * 0.05f;
                }
                m.vertices = eraserVertices;
            }

            return(m);
        }