Exemplo n.º 1
0
        static public Byte[][][] Read(string planet, int iPos, int jPos, int kPos, Planet.Side side)
        {
            string       directoryPath = Application.dataPath + "/../PlanetData/" + planet + "/" + side;
            string       saveFilePath  = directoryPath + "/data_" + (int)side + "_" + iPos + "_" + jPos + "_" + kPos + ".pdat";
            FileStream   saveFile      = new FileStream(saveFilePath, FileMode.Open);
            BinaryReader dataStream    = new BinaryReader(saveFile);

            Byte[][][] chunckDatas = new Byte[PlanetUtility.ChunckSize][][];
            for (int i = 0; i < PlanetUtility.ChunckSize; i++)
            {
                chunckDatas[i] = new Byte[PlanetUtility.ChunckSize][];
                for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                {
                    chunckDatas[i][j] = new Byte[PlanetUtility.ChunckSize];
                    for (int k = 0; k < PlanetUtility.ChunckSize; k++)
                    {
                        chunckDatas[i][j][k] = dataStream.ReadByte();
                    }
                }
            }
            dataStream.Close();
            saveFile.Close();

            return(chunckDatas);
        }
Exemplo n.º 2
0
        static public Quaternion LocalRotationFromSide(Planet.Side side)
        {
            Quaternion sideLocalRotation = Quaternion.identity;

            switch (side)
            {
            case Planet.Side.Top:
                sideLocalRotation = Quaternion.Euler(0f, 0f, 90f);
                break;

            case Planet.Side.Bottom:
                sideLocalRotation = Quaternion.Euler(0f, 0f, -90f);
                break;

            case Planet.Side.Right:
                sideLocalRotation = Quaternion.Euler(0f, 0f, 0f);
                break;

            case Planet.Side.Left:
                sideLocalRotation = Quaternion.Euler(0f, 180f, 0f);
                break;

            case Planet.Side.Front:
                sideLocalRotation = Quaternion.Euler(0f, -90f, 0f);
                break;

            case Planet.Side.Back:
                sideLocalRotation = Quaternion.Euler(0f, 90f, 0f);
                break;
            }
            return(sideLocalRotation);
        }
Exemplo n.º 3
0
        private void GenerateSide(Planet.Side side)
        {
            PlanetSide planetSide = Target.planetSides[side];

            planetSide.Initialize(Target);

            for (int i = 0; i < planetSide.nbChunks; i++)
            {
                for (int j = 0; j < planetSide.nbChunks; j++)
                {
                    for (int k = 0; k < planetSide.nbChunks / 2; k++)
                    {
                        chuncksToInstantiate.Add(new ChunckDataToInstantiate(i, j, k, planetSide));
                    }
                }
            }
            EditorApplication.update -= InstantiatePlanetChunck;
            EditorApplication.update += InstantiatePlanetChunck;
        }
Exemplo n.º 4
0
        public PlanetSide WorldPositionToPlanetSide(Vector3 worldPos)
        {
            Vector3 localPos = this.transform.worldToLocalMatrix * worldPos;

            float[] angles = new float[6];
            angles[(int)Planet.Side.Right]  = Vector3.Angle(localPos, Vector3.right);
            angles[(int)Planet.Side.Left]   = Vector3.Angle(localPos, -Vector3.right);
            angles[(int)Planet.Side.Front]  = Vector3.Angle(localPos, Vector3.forward);
            angles[(int)Planet.Side.Back]   = Vector3.Angle(localPos, -Vector3.forward);
            angles[(int)Planet.Side.Top]    = Vector3.Angle(localPos, Vector3.up);
            angles[(int)Planet.Side.Bottom] = Vector3.Angle(localPos, -Vector3.up);

            Planet.Side smallest = Planet.Side.Top;
            foreach (Planet.Side side in Enum.GetValues(typeof(Planet.Side)))
            {
                if (angles[(int)side] < angles[(int)smallest])
                {
                    smallest = side;
                }
            }

            return(this.planetSides[smallest]);
        }
Exemplo n.º 5
0
        private Byte[][][] GetByteFor(
            int iPos,
            int jPos,
            int kPos,
            Planet.Side side,
            LargeMap map, LargeMap
            holes0Map,
            LargeMap holes0DepthMap,
            LargeMap holes1Map,
            LargeMap holes1DepthMap,
            LargeMap holes2Map,
            LargeMap holes2DepthMap
            )
        {
            Byte[][][] chunckData = new Byte[PlanetUtility.ChunckSize][][];
            int        size       = PlanetUtility.DegreeToSize(PlanetUtility.KPosToDegree(kPos));
            int        mapSize    = map.heightMap.Length;
            int        sizeRatio  = mapSize / size;

            float t2 = Time.realtimeSinceStartup;

            for (int i = 0; i < PlanetUtility.ChunckSize; i++)
            {
                chunckData[i] = new Byte[PlanetUtility.ChunckSize][];
                for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                {
                    chunckData[i][j] = new Byte[PlanetUtility.ChunckSize];
                    for (int k = 0; k < PlanetUtility.ChunckSize; k++)
                    {
                        int iGlobal         = iPos * PlanetUtility.ChunckSize + i;
                        int jGlobal         = jPos * PlanetUtility.ChunckSize + j;
                        int kGlobal         = kPos * PlanetUtility.ChunckSize + k;
                        int heightThreshold = Mathf.FloorToInt(map.heightMap[iGlobal * sizeRatio][jGlobal * sizeRatio] * lowToHighPoint + lowestPoint);
                        int h0Alt           = Mathf.FloorToInt(holes0Map.heightMap[iGlobal * sizeRatio][jGlobal * sizeRatio] * lowToHighPoint + lowestPoint - lowToHighPoint / 2);
                        int h0Depth         = Mathf.FloorToInt(holes0DepthMap.heightMap[iGlobal * sizeRatio][jGlobal * sizeRatio] * lowToHighPoint / 2);
                        //h0Depth = 0;
                        int h1Alt   = Mathf.FloorToInt(holes1Map.heightMap[iGlobal * sizeRatio][jGlobal * sizeRatio] * lowToHighPoint + lowestPoint - lowToHighPoint);
                        int h1Depth = Mathf.FloorToInt(holes1DepthMap.heightMap[iGlobal * sizeRatio][jGlobal * sizeRatio] * lowToHighPoint / 2);
                        //h1Depth = 0;
                        int h2Alt   = Mathf.FloorToInt(holes2Map.heightMap[iGlobal * sizeRatio][jGlobal * sizeRatio] * lowToHighPoint + lowestPoint - 3 * lowToHighPoint / 2);
                        int h2Depth = Mathf.FloorToInt(holes2DepthMap.heightMap[iGlobal * sizeRatio][jGlobal * sizeRatio] * lowToHighPoint / 2);

                        BlockData data = BlockData.Empty;

                        // if k is under main heightMap threshold
                        if (kGlobal <= heightThreshold)
                        {
                            // if k is not in generated hole0
                            if ((kGlobal <= h0Alt) || (kGlobal >= h0Alt + h0Depth))
                            {
                                // if k is not in generated hole1
                                if ((kGlobal <= h1Alt) || (kGlobal >= h1Alt + h1Depth))
                                {
                                    // if k is not in generated hole2
                                    if ((kGlobal <= h2Alt) || (kGlobal >= h2Alt + h2Depth))
                                    {
                                        // then data is not empty, set rock as default
                                        data = BlockData.Rock;
                                        // compute depth of soil block (blocks that are not rocks)
                                        int soilDepth = Mathf.FloorToInt(Mathf.Abs(Mathf.Cos(i * 53 + j * 41 + k * 29) * 5) + 1);
                                        // if k is close to the surface of the map or of any hole
                                        if (
                                            ((kGlobal <= heightThreshold) && (heightThreshold - kGlobal < soilDepth)) ||
                                            ((kGlobal <= h0Alt) && (h0Alt - kGlobal < soilDepth)) ||
                                            ((kGlobal <= h1Alt) && (h1Alt - kGlobal < soilDepth)) ||
                                            ((kGlobal <= h2Alt) && (h2Alt - kGlobal < soilDepth)))
                                        {
                                            // then data is soil block
                                            data = GetSoilBlock(map.latMap [iGlobal * sizeRatio] [jGlobal * sizeRatio], iGlobal, jGlobal, kGlobal);
                                            // if data is Dirt
                                            if (data == BlockData.Dirt)
                                            {
                                                // and if data is strictly at surface of any hole
                                                if (
                                                    (kGlobal == heightThreshold) ||
                                                    (kGlobal == h0Alt))
                                                {
                                                    data = BlockData.Grass;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        /*
                         * if (kGlobal <= heightThreshold) {
                         *      // then data is not empty, set rock as default
                         *      data = BlockData.Rock;
                         *      // compute depth of soil block (blocks that are not rocks)
                         *      int soilDepth = Mathf.FloorToInt (Mathf.Abs (Mathf.Cos (i * 53 + j * 41 + k * 29) * 5) + 1);
                         *      if (heightThreshold - kGlobal < soilDepth) {
                         *              // then data is soil block
                         *              data = GetSoilBlock (map.latMap [iGlobal * sizeRatio] [jGlobal * sizeRatio], iGlobal, jGlobal, kGlobal);
                         *              // if data is Dirt
                         *              if (data == BlockData.Dirt) {
                         *                      // and if data is strictly at surface of any hole
                         *                      if (kGlobal == heightThreshold) {
                         *                              data = BlockData.Grass;
                         *                      }
                         *              }
                         *      }
                         * }
                         */
                        chunckData [i] [j] [k] = (byte)data;
                    }
                }
            }
            float t3 = Time.realtimeSinceStartup;

            tSetBytes += t3 - t2;

            return(chunckData);
        }
Exemplo n.º 6
0
        private LargeMap GetLargeMapFor(RandomSeed seed, Planet.Side side, bool withLatMap = false)
        {
            float t0 = Time.realtimeSinceStartup;

            int      x, y, z;
            int      size     = PlanetUtility.DegreeToSize(PlanetUtility.KPosToDegree(this.kPosMax));
            string   mapName  = side + "-" + degreeAtKPosMax + "-" + seed.seed;
            bool     evaluate = false;
            LargeMap map      = Load(mapName, size);

            if (map == null)
            {
                map      = new LargeMap(size);
                evaluate = true;
            }

            if (side == Planet.Side.Top)
            {
                y = size;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        x = size - j;
                        z = i;
                        if (evaluate)
                        {
                            map.heightMap[i][j] = EvaluateTriCubic(x, y, z, seed);
                        }
                        if (withLatMap)
                        {
                            map.latMap[i][j] = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                        }
                    }
                }
            }
            else if (side == Planet.Side.Bottom)
            {
                y = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        x = j;
                        z = i;
                        if (evaluate)
                        {
                            map.heightMap[i][j] = EvaluateTriCubic(x, y, z, seed);
                        }
                        if (withLatMap)
                        {
                            map.latMap[i][j] = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                        }
                    }
                }
            }
            else if (side == Planet.Side.Right)
            {
                x = size;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        y = j;
                        z = i;
                        if (evaluate)
                        {
                            map.heightMap[i][j] = EvaluateTriCubic(x, y, z, seed);
                        }
                        if (withLatMap)
                        {
                            map.latMap[i][j] = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                        }
                    }
                }
            }
            else if (side == Planet.Side.Left)
            {
                x = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        y = j;
                        z = size - i;
                        if (evaluate)
                        {
                            map.heightMap[i][j] = EvaluateTriCubic(x, y, z, seed);
                        }
                        if (withLatMap)
                        {
                            map.latMap[i][j] = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                        }
                    }
                }
            }
            else if (side == Planet.Side.Front)
            {
                z = size;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        y = j;
                        x = size - i;
                        if (evaluate)
                        {
                            map.heightMap[i][j] = EvaluateTriCubic(x, y, z, seed);
                        }
                        if (withLatMap)
                        {
                            map.latMap[i][j] = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                        }
                    }
                }
            }
            else if (side == Planet.Side.Back)
            {
                z = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        y = j;
                        x = i;
                        if (evaluate)
                        {
                            map.heightMap[i][j] = EvaluateTriCubic(x, y, z, seed);
                        }
                        if (withLatMap)
                        {
                            map.latMap[i][j] = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                        }
                    }
                }
            }
            Save(mapName, map);
            float t1 = Time.realtimeSinceStartup;

            tBuildHeightMap += t1 - t0;

            return(map);
        }
Exemplo n.º 7
0
        private Byte[][][] GetByteFor(int iPos, int jPos, int kPos, Planet.Side side, Map map)
        {
            Byte[][][] chunckData = new Byte[PlanetUtility.ChunckSize][][];
            int        maxHeight  = this.chunckCount / 2 * PlanetUtility.ChunckSize / 2;

            float t2 = Time.realtimeSinceStartup;

            for (int i = 0; i < PlanetUtility.ChunckSize; i++)
            {
                chunckData[i] = new Byte[PlanetUtility.ChunckSize][];
                for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                {
                    chunckData[i][j] = new Byte[PlanetUtility.ChunckSize];
                    for (int k = 0; k < PlanetUtility.ChunckSize; k++)
                    {
                        int h = kPos * PlanetUtility.ChunckSize + k;
                        if (h == 0)
                        {
                            chunckData[i][j][k] = 131;
                        }
                        else if (h <= map.heightMap[i][j])
                        {
                            int[] localThresholds = GetLocalThresholds(maxHeight, map.latMap[i][j]);
                            if (h >= localThresholds[0])
                            {
                                chunckData[i][j][k] = 132;
                            }
                            else if (h >= localThresholds[1])
                            {
                                chunckData[i][j][k] = 130;
                            }
                            else if (h >= localThresholds[2])
                            {
                                if (h == map.heightMap[i][j])
                                {
                                    chunckData[i][j][k] = 128;
                                }
                                else
                                {
                                    chunckData[i][j][k] = 129;
                                }
                            }
                            else if (h >= localThresholds[3])
                            {
                                chunckData[i][j][k] = 133;
                            }
                            else
                            {
                                chunckData[i][j][k] = 131;
                            }
                        }
                        else
                        {
                            chunckData[i][j][k] = 0;
                        }
                    }
                }
            }
            float t3 = Time.realtimeSinceStartup;

            tSetBytes += t3 - t2;

            return(chunckData);
        }
Exemplo n.º 8
0
        private Map GetHeightMapFor(RandomSeed seed, int iPos, int jPos, Planet.Side side)
        {
            int maxHeight = this.chunckCount / 2 * PlanetUtility.ChunckSize / 2;
            int x, y, z;
            Map map = new Map();

            float t0 = Time.realtimeSinceStartup;

            if (side == Planet.Side.Top)
            {
                y = size;
                for (int i = 0; i < PlanetUtility.ChunckSize; i++)
                {
                    for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                    {
                        x = size - (j + jPos * PlanetUtility.ChunckSize);
                        z = i + iPos * PlanetUtility.ChunckSize;
                        map.heightMap[i][j] = Mathf.FloorToInt(EvaluateTriCubic(x, y, z, seed) * maxHeight + maxHeight / 2);
                        map.latMap[i][j]    = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                    }
                }
            }
            else if (side == Planet.Side.Bottom)
            {
                y = 0;
                for (int i = 0; i < PlanetUtility.ChunckSize; i++)
                {
                    for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                    {
                        x = j + jPos * PlanetUtility.ChunckSize;
                        z = i + iPos * PlanetUtility.ChunckSize;
                        map.heightMap[i][j] = Mathf.FloorToInt(EvaluateTriCubic(x, y, z, seed) * maxHeight + maxHeight / 2);
                        map.latMap[i][j]    = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                    }
                }
            }
            else if (side == Planet.Side.Right)
            {
                x = size;
                for (int i = 0; i < PlanetUtility.ChunckSize; i++)
                {
                    for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                    {
                        y = j + jPos * PlanetUtility.ChunckSize;
                        z = i + iPos * PlanetUtility.ChunckSize;
                        map.heightMap[i][j] = Mathf.FloorToInt(EvaluateTriCubic(x, y, z, seed) * maxHeight + maxHeight / 2);
                        map.latMap[i][j]    = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                    }
                }
            }
            else if (side == Planet.Side.Left)
            {
                x = 0;
                for (int i = 0; i < PlanetUtility.ChunckSize; i++)
                {
                    for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                    {
                        y = j + jPos * PlanetUtility.ChunckSize;
                        z = size - (i + iPos * PlanetUtility.ChunckSize);
                        map.heightMap[i][j] = Mathf.FloorToInt(EvaluateTriCubic(x, y, z, seed) * maxHeight + maxHeight / 2);
                        map.latMap[i][j]    = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                    }
                }
            }
            else if (side == Planet.Side.Front)
            {
                z = size;
                for (int i = 0; i < PlanetUtility.ChunckSize; i++)
                {
                    for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                    {
                        y = j + jPos * PlanetUtility.ChunckSize;
                        x = size - (i + iPos * PlanetUtility.ChunckSize);
                        map.heightMap[i][j] = Mathf.FloorToInt(EvaluateTriCubic(x, y, z, seed) * maxHeight + maxHeight / 2);
                        map.latMap[i][j]    = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                    }
                }
            }
            else if (side == Planet.Side.Back)
            {
                z = 0;
                for (int i = 0; i < PlanetUtility.ChunckSize; i++)
                {
                    for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                    {
                        y = j + jPos * PlanetUtility.ChunckSize;
                        x = i + iPos * PlanetUtility.ChunckSize;
                        map.heightMap[i][j] = Mathf.FloorToInt(EvaluateTriCubic(x, y, z, seed) * maxHeight + maxHeight / 2);
                        map.latMap[i][j]    = Mathf.Abs(Vector3.Angle(PlanetUtility.EvaluateVertex(size, x, y, z), Vector3.up) - 90f);
                    }
                }
            }
            float t1 = Time.realtimeSinceStartup;

            tBuildHeightMap += t1 - t0;

            return(map);
        }
Exemplo n.º 9
0
        static public int SaveForBabylonJSVersion(string planet, Byte[][][] chunckDatas, int iPos, int jPos, int kPos, Planet.Side side)
        {
            string directoryPath = Application.dataPath + "/../PlanetDataBabylonJS/" + planet + "/" + side + "/" + iPos + "/" + jPos + "/" + kPos + "/";

            Directory.CreateDirectory(directoryPath);
            string       saveFilePath = directoryPath + "data.txt";
            FileStream   saveFile     = new FileStream(saveFilePath, FileMode.Create, FileAccess.Write);
            StreamWriter dataStream   = new StreamWriter(saveFile);

            for (int i = 0; i < PlanetUtility.ChunckSize; i++)
            {
                for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                {
                    for (int k = 0; k < PlanetUtility.ChunckSize; k++)
                    {
                        dataStream.Write(chunckDatas[i][j][k].ToString("X2"));
                    }
                }
            }
            dataStream.Close();
            saveFile.Close();
            return(1);
        }
Exemplo n.º 10
0
        static public int Save(string planet, Byte[][][] chunckDatas, int iPos, int jPos, int kPos, Planet.Side side)
        {
            string directoryPath = Application.dataPath + "/../PlanetData/" + planet + "/" + side;

            Directory.CreateDirectory(directoryPath);
            string       saveFilePath = directoryPath + "/data_" + (int)side + "_" + iPos + "_" + jPos + "_" + kPos + ".pdat";
            FileStream   saveFile     = new FileStream(saveFilePath, FileMode.Create, FileAccess.Write);
            BinaryWriter dataStream   = new BinaryWriter(saveFile);

            for (int i = 0; i < PlanetUtility.ChunckSize; i++)
            {
                for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                {
                    for (int k = 0; k < PlanetUtility.ChunckSize; k++)
                    {
                        dataStream.Write(chunckDatas[i][j][k]);
                    }
                }
            }
            dataStream.Close();
            saveFile.Close();

            return(1);
        }
Exemplo n.º 11
0
 private void ClearSide(Planet.Side side)
 {
     Target.planetSides[side].Clear();
 }