예제 #1
0
        /// <summary>
        /// <para>Draw a sphere from center to radius</para>
        /// </summary>
        static public void Sphere(SurfaceMap surfaceMap, Vector3Int center, int radius, int material, bool contour)
        {
            if (center.x < 0 || center.y < 0 || center.z < 0)
            {
                throw new Exception("PointBelowZero");
            }
            if (center.x >= surfaceMap.resolution || center.y >= surfaceMap.resolution || center.z >= surfaceMap.resolution)
            {
                throw new Exception("PointAboweResolution");
            }

            int radius2 = radius * radius;

            for (int z = center.z - radius; z <= center.z + radius && z < surfaceMap.resolution; z++)
            {
                if (z < 0)
                {
                    z = 0;
                }
                for (int y = center.y - radius; y <= center.y + radius && y < surfaceMap.resolution; y++)
                {
                    if (y < 0)
                    {
                        y = 0;
                    }
                    for (int x = center.x - radius; x <= center.x + radius && x < surfaceMap.resolution; x++)
                    {
                        if (x < 0)
                        {
                            x = 0;
                        }
                        int pointOnSphere = (int)(Mathf.Pow(x - center.x, 2) + Mathf.Pow(y - center.y, 2) + Mathf.Pow(z - center.z, 2));
                        if (pointOnSphere <= radius2)
                        {
                            surfaceMap.SetBiome(x, y, z, 1);
                            surfaceMap.SetMaterial(x, y, z, material);

                            if (contour && (
                                    (int)(Mathf.Pow(x - center.x + 1, 2) + Mathf.Pow(y - center.y, 2) + Mathf.Pow(z - center.z, 2)) > radius2 ||
                                    (int)(Mathf.Pow(x - center.x, 2) + Mathf.Pow(y - center.y + 1, 2) + Mathf.Pow(z - center.z, 2)) > radius2 ||
                                    (int)(Mathf.Pow(x - center.x, 2) + Mathf.Pow(y - center.y, 2) + Mathf.Pow(z - center.z + 1, 2)) > radius2 ||
                                    (int)(Mathf.Pow(x - center.x - 1, 2) + Mathf.Pow(y - center.y, 2) + Mathf.Pow(z - center.z, 2)) > radius2 ||
                                    (int)(Mathf.Pow(x - center.x, 2) + Mathf.Pow(y - center.y - 1, 2) + Mathf.Pow(z - center.z, 2)) > radius2 ||
                                    (int)(Mathf.Pow(x - center.x, 2) + Mathf.Pow(y - center.y, 2) + Mathf.Pow(z - center.z - 1, 2)) > radius2 ||
                                    x == 0 || x == surfaceMap.resolution - 1 ||
                                    y == 0 || y == surfaceMap.resolution - 1 ||
                                    z == 0 || y == surfaceMap.resolution - 1)
                                )
                            {
                                surfaceMap.SetContour(x, y, z);
                            }
                        }
                        //else surfaceMap.SetMaterial(x, y, z, 0);
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// <para>Draw a straight line between p1 & p2</para>
        /// <para>Mode: 0 - overlay, 1 - add, 2 - inverse</para>
        /// </summary>
        static public void Line(SurfaceMap surfaceMap, Vector3Int p1, Vector3Int p2, int width, int material, bool contour, int mode)
        {
            if (p1.x < 0 || p1.x >= surfaceMap.resolution || p1.y < 0 || p1.y >= surfaceMap.resolution || p1.z < 0 || p1.z >= surfaceMap.resolution)
            {
                throw new Exception("p1OutOfBounds");
            }
            if (p2.x < 0 || p2.x >= surfaceMap.resolution || p2.y < 0 || p2.y >= surfaceMap.resolution || p2.z < 0 || p2.z >= surfaceMap.resolution)
            {
                throw new Exception("p2OutOfBounds");
            }
            if (mode < 0 || mode > 2)
            {
                throw new Exception("InvalidMode");
            }

            if (Vector3Int.Distance(Vector3Int.zero, p1) > Vector3Int.Distance(Vector3Int.zero, p2))
            {
                Vector3Int tmp = p2;
                p2 = p1;
                p1 = tmp;
            }

            Vector3Int direction = p2 - p1;
            int        x, y, z;

            for (float t = 0.0f; t <= 1.0f; t = t + (1.0f / (2 * Vector3.Distance(p1, p2))))
            {
                int offset    = 0;
                int widthCopy = width;
                while (widthCopy > 0)
                {
                    if (width % 2 == 1)
                    {
                        x = p1.x + offset + (int)(direction.x * t);
                        y = p1.y + offset + (int)(direction.y * t);
                        z = p1.z + offset + (int)(direction.z * t);
                    }
                    else
                    {
                        x = p1.x - offset + (int)(direction.x * t);
                        y = p1.y - offset + (int)(direction.y * t);
                        z = p1.z - offset + (int)(direction.z * t);
                    }
                    offset++;
                    widthCopy--;
                    surfaceMap.SetMaterial(x, y, z, material);
                    if (contour)
                    {
                        surfaceMap.SetContour(x, y, z);
                    }
                }
            }
        }