Exemplo n.º 1
0
    public override void OnInspectorGUI()
    {
        CubePoint temp = (CubePoint)target;

        if (temp != null)
        {
            GUILayout.BeginVertical();
            GUILayout.BeginHorizontal();
            GUILayout.Space(10.0f);

            temp.cubePointName = EditorGUILayout.TextField("Cubemap Name:", temp.cubePointName);
            temp.name          = temp.cubePointName;


            if (aTexture != null)
            {
                GUI.DrawTexture(new Rect(60, 300, 149, 151), aTexture);
            }
            GUILayout.EndHorizontal();

            GUILayout.EndVertical();

            temp.tag = "Cubemap";
        }
    }
Exemplo n.º 2
0
    public static CubePoint RoundToNearestCube(ref CubePoint cube)
    {
        int rx = Mathf.RoundToInt(cube.x);
        int ry = Mathf.RoundToInt(cube.y);
        int rz = Mathf.RoundToInt(cube.z);

        float x_diff = Mathf.Abs((rx - cube.x));
        float y_diff = Mathf.Abs((ry - cube.y));
        float z_diff = Mathf.Abs((rz - cube.z));

        if (x_diff > y_diff && x_diff > z_diff)
        {
            rx = -ry - rz;
        }
        else if (y_diff > z_diff)
        {
            ry = -rx - rz;
        }
        else
        {
            rz = -rx - ry;
        }
        cube.x = rx;
        cube.y = ry;
        cube.z = rz;
        return(cube);
    }
Exemplo n.º 3
0
 public static CubePoint AxialToCubePoint(HexPoint hex, out CubePoint output)
 {
     output.x = hex.q;
     output.z = hex.r;
     output.y = -output.x - output.z;
     return(output);
 }
Exemplo n.º 4
0
    private static float HexDistance(HexPoint a, HexPoint b)
    {
        CubePoint ac = AxialToCubePoint(a, out ac);
        CubePoint bc = AxialToCubePoint(b, out ac);

        return(CubeDistance(ac, bc));
    }
Exemplo n.º 5
0
    public static HexPoint[] PointsBetweenHexPoints(HexPoint a, HexPoint b)
    {
        CubePoint ah = AxialToCubePoint(a, out ah);
        CubePoint bh = AxialToCubePoint(b, out bh);;

        List <HexPoint> points = new List <HexPoint>();

        int N = Mathf.RoundToInt(CubeDistance(ah, bh));

        for (int i = 0; i <= N; i++)
        {
            CubePoint cubePoint = CubeLerp(ah, bh, 1.0f / N * i);
            HexPoint  hexPoint  = CubeToAxialPoint(RoundToNearestCube(ref cubePoint), out hexPoint);
            if (!points.Contains(hexPoint))
            {
                if (PointIsOnMap(hexPoint))
                {
                    points.Add(hexPoint);
                }
            }
            else
            {
                cubePoint -= new CubePoint(1 * 0.2f, 2 * 0.2f, -3 * 0.2f);
                hexPoint   = CubeToAxialPoint(RoundToNearestCube(ref cubePoint), out hexPoint);
                if (PointIsOnMap(hexPoint))
                {
                    points.Add(hexPoint);
                }
            }
        }
        return(points.ToArray());
    }
Exemplo n.º 6
0
    IEnumerator CreateCubemap(CubePoint point)
    {
        GameObject camObject = new GameObject("CubemapCamera");

        camObject.transform.parent        = point.transform;
        camObject.transform.localPosition = Vector3.zero;
        Camera cam = (Camera)camObject.AddComponent(typeof(Camera));

        cam.fieldOfView   = 90;
        cam.depth         = 10;
        cam.nearClipPlane = 0.001f;
        cam.farClipPlane  = 1000.0f;

        Cubemap cubemap = new Cubemap(this.size, this.textureFormat, this.mipmap);

        //yield return new WaitForEndOfFrame();
        yield return(StartCoroutine(Snapshot(cubemap, CubemapFace.PositiveZ, cam)));

        yield return(StartCoroutine(Snapshot(cubemap, CubemapFace.PositiveX, cam)));

        yield return(StartCoroutine(Snapshot(cubemap, CubemapFace.NegativeX, cam)));

        yield return(StartCoroutine(Snapshot(cubemap, CubemapFace.NegativeZ, cam)));

        yield return(StartCoroutine(Snapshot(cubemap, CubemapFace.PositiveY, cam)));

        yield return(StartCoroutine(Snapshot(cubemap, CubemapFace.NegativeY, cam)));

        cubemap.Apply(mipmap);
        AssetDatabase.CreateAsset(cubemap, "Assets/AssetStore/" + path + "/" + point.cubePointName + ".cubemap");
        AssetDatabase.SaveAssets();

        Destroy(camObject);
    }
Exemplo n.º 7
0
    private static CubePoint CubeLerp(CubePoint a, CubePoint b, float step)
    {
        float x = a.x + (b.x - a.x) * step;
        float y = a.y + (b.y - a.y) * step;
        float z = a.z + (b.z - a.z) * step;

        return(new CubePoint(x, y, z));
    }
Exemplo n.º 8
0
    private CubePoint[] GetPoints(World world, int x, int y, int z, CubePoint[] points)
    {
        for (int i = 0; i < 8; i++)
        {
            CubePoint point = points[x + CubePointsX[i] + (world.chunkSize + 1) * (y + CubePointsY[i] + (z + CubePointsZ[i]) * (world.chunkSize + 1))];
            _initPoints[i] = point;
        }

        return(_initPoints);
    }
Exemplo n.º 9
0
    private static int GetAdjacentCubesCount(Cube cube, Dictionary <Point, Cube> cubes, Point[] directions)
    {
        int count = 0;

        foreach (Point dir in directions)
        {
            Point p = cube.Point switch {
                HyperPoint hp when dir is HyperPoint d => new HyperPoint(hp.W + d.W, hp.X + d.X, hp.Y + d.Y, hp.Z + d.Z),
                CubePoint cp when dir is CubePoint d => new CubePoint(cp.X + d.X, cp.Y + d.Y, cp.Z + d.Z),
                _ => new Point(cube.Point.X + dir.X, cube.Point.Y + dir.Y)
            };
            if (cubes.GetValueOrDefault(p)?.State == ON_STATE)
            {
                count++;
            }
        }
        return(count);
    }
Exemplo n.º 10
0
    private Vector3[] GenerateVertexList(CubePoint[] points, int edgeIndex, ref Color[] colours2)
    {
        for (int i = 0; i < 12; i++)
        {
            if ((edgeIndex & (1 << i)) != 0)
            {
                int[] edgePair = LookupTables.EdgeIndexTable[i];
                int   edge1    = edgePair[0];
                int   edge2    = edgePair[1];

                CubePoint point1 = points[edge1];
                CubePoint point2 = points[edge2];

                _vertexList[i] = VertexInterpolate(point1.localPosition, point2.localPosition, point1.density, point2.density);
                colours2[i]    = new Color(Mathf.Lerp(point1.colour.r, point2.colour.r, 0.5f), Mathf.Lerp(point1.colour.g, point2.colour.g, 0.5f), Mathf.Lerp(point1.colour.b, point1.colour.b, 0.5f), Mathf.Lerp(point1.colour.a, point1.colour.a, 0.5f));
            }
        }

        return(_vertexList);
    }
Exemplo n.º 11
0
 private static Vector3 Interpolate(float isoLevel, CubePoint p1, CubePoint p2)
 {
     if (Mathf.Abs(isoLevel - p1.value) < 0.00001)
     {
         return(p1.vertex);
     }
     else if (Mathf.Abs(isoLevel - p2.value) < 0.00001)
     {
         return(p2.vertex);
     }
     else if (Mathf.Abs(p1.value - p2.value) < 0.00001)
     {
         return(p1.vertex);
     }
     else
     {
         float mu = (isoLevel - p1.value) / (p2.value - p1.value);
         float x  = p1.vertex.x + mu * (p2.vertex.x - p1.vertex.x);
         float y  = p1.vertex.y + mu * (p2.vertex.y - p1.vertex.y);
         float z  = p1.vertex.z + mu * (p2.vertex.z - p1.vertex.z);
         return(new Vector3(x, y, z));
     }
 }
Exemplo n.º 12
0
    public static HexPoint RoundToNearestHex(HexPoint point)
    {
        CubePoint cb = AxialToCubePoint(point, out cb);

        return(CubeToAxialPoint(RoundToNearestCube(ref cb), out point));
    }
Exemplo n.º 13
0
 public static HexPoint CubeToAxialPoint(CubePoint cube, out HexPoint output)
 {
     output.q = cube.x;
     output.r = cube.z;
     return(output);
 }
Exemplo n.º 14
0
 private static float CubeDistance(CubePoint a, CubePoint b)
 {
     return((float)(Mathf.Max(Mathf.Abs((float)(a.x - b.x)), Mathf.Abs((float)(a.y - b.y)), Mathf.Abs((float)(a.z - b.z)))));
 }
    public static void SmoothTerrain(World world, Vector3 point, Vector3 up)
    {
        List <Chunk> chunksToUpdate = new List <Chunk>();

        float[,,] densities     = new float[Mathf.CeilToInt(world.range) * 2 + 2, 3, Mathf.CeilToInt(world.range) * 2 + 2];
        List <Chunk>[,,] chunks = new List <Chunk> [Mathf.CeilToInt(world.range) * 2 + 2, 3, Mathf.CeilToInt(world.range) * 2 + 2];
        CubePoint[,,] points    = new CubePoint[Mathf.CeilToInt(world.range) * 2 + 2, 3, Mathf.CeilToInt(world.range) * 2 + 2];

        Vector3 left    = new Vector3(-up.z, up.y, up.x).normalized;
        Vector3 forward = new Vector3(left.x, left.y, -left.z).normalized;

        int start = Mathf.FloorToInt(-world.range);

        if (world.range < 1)
        {
            start = 0;
        }

        // Add densities
        for (int x = start; x <= world.range; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                for (int z = start; z <= world.range; z++)
                {
                    List <Chunk> worldChunks = world.GetChunks(((new Vector3(point.x, point.y, point.z) + x * left + y * up + z * forward) - world.transform.position).RoundToNearestX(world.transform.lossyScale.x) + world.transform.position);

                    if (worldChunks.Count != 0)
                    {
                        Vector3 localPosition = (new Vector3(point.x, point.y, point.z) + x * left + y * up + z * forward - worldChunks[0].transform.position) / world.transform.lossyScale.x;
                        densities[x + Mathf.CeilToInt(world.range) + 1, y + 1, z + Mathf.CeilToInt(world.range) + 1] = worldChunks[0].GetPoint(world, localPosition).density;
                        chunks[x + Mathf.CeilToInt(world.range) + 1, y + 1, z + Mathf.CeilToInt(world.range) + 1]    = new List <Chunk>();
                        points[x + Mathf.CeilToInt(world.range) + 1, y + 1, z + Mathf.CeilToInt(world.range) + 1]    = worldChunks[0].GetPoint(world, localPosition);

                        for (int i = 0; i < worldChunks.Count; i++)
                        {
                            if (!chunksToUpdate.Contains(worldChunks[i]))
                            {
                                chunksToUpdate.Add(worldChunks[i]);
                            }

                            chunks[x + Mathf.CeilToInt(world.range) + 1, y + 1, z + Mathf.CeilToInt(world.range) + 1].Add(worldChunks[i]);
                        }
                    }
                    else
                    {
                        densities[x + Mathf.CeilToInt(world.range) + 1, y + 1, z + Mathf.CeilToInt(world.range) + 1] = float.MaxValue;
                    }
                }
            }
        }

        // Calculate smoothed value
        for (int x = 1; x < densities.GetLength(0) - 1; x++)
        {
            for (int z = 1; z < densities.GetLength(1) - 1; z++)
            {
                if (chunks[x, 1, z] != null)
                {
                    float totalDensity      = 0;
                    int   amountOfDensities = 0;
                    AddNeighboringPoints(ref totalDensity, ref amountOfDensities, densities, x, 0, z);
                    AddNeighboringPoints(ref totalDensity, ref amountOfDensities, densities, x, 1, z);
                    AddNeighboringPoints(ref totalDensity, ref amountOfDensities, densities, x, 2, z);

                    float averageDensity = totalDensity / (amountOfDensities / 3);
                    for (int i = 0; i < chunks[x, 1, z].Count; i++)
                    {
                        chunks[x, 1, z][i].SetDensity(world, Mathf.Lerp(points[x, 1, z].density, averageDensity, world.force), points[x, 1, z].localPosition);
                    }
                }
            }
        }

        for (int i = 0; i < chunksToUpdate.Count; i++)
        {
            chunksToUpdate[i].Generate(world);
        }
    }
Exemplo n.º 16
0
    IEnumerator CreateCubemap(CubePoint point)
    {
        GameObject camObject = new GameObject("CubemapCamera");
        camObject.transform.parent = point.transform;
        camObject.transform.localPosition = Vector3.zero;
        Camera cam = (Camera)camObject.AddComponent(typeof(Camera));

        cam.fieldOfView = 90;
        cam.depth = 10;
        cam.nearClipPlane = 0.001f;
        cam.farClipPlane = 1000.0f;

        Cubemap cubemap = new Cubemap(this.size, this.textureFormat, this.mipmap);
		//yield return new WaitForEndOfFrame();
       yield return StartCoroutine(Snapshot(cubemap, CubemapFace.PositiveZ, cam));
       yield return StartCoroutine(Snapshot(cubemap, CubemapFace.PositiveX, cam));
        yield return StartCoroutine(Snapshot(cubemap, CubemapFace.NegativeX, cam));
       yield return StartCoroutine(Snapshot(cubemap, CubemapFace.NegativeZ, cam));
       yield return StartCoroutine(Snapshot(cubemap, CubemapFace.PositiveY, cam));
       yield return StartCoroutine(Snapshot(cubemap, CubemapFace.NegativeY, cam));

        cubemap.Apply(mipmap);
        AssetDatabase.CreateAsset(cubemap, "Assets/" + path + "/" + point.cubePointName + ".cubemap");
        AssetDatabase.SaveAssets();

        Destroy(camObject);
    }