static BrushMesh.Polygon[] CreateBoxPolygons(SurfaceLayers layers, SurfaceFlags surfaceFlags)
        {
            return(new[]
            {
                // left/right
                new BrushMesh.Polygon {
                    surfaceID = 0, firstEdge = 0, edgeCount = 4, description = new SurfaceDescription {
                        UV0 = UVMatrix.centered, surfaceFlags = surfaceFlags, smoothingGroup = 0
                    }, layers = layers
                },
                new BrushMesh.Polygon {
                    surfaceID = 1, firstEdge = 4, edgeCount = 4, description = new SurfaceDescription {
                        UV0 = UVMatrix.centered, surfaceFlags = surfaceFlags, smoothingGroup = 0
                    }, layers = layers
                },

                // front/back
                new BrushMesh.Polygon {
                    surfaceID = 2, firstEdge = 8, edgeCount = 4, description = new SurfaceDescription {
                        UV0 = UVMatrix.centered, surfaceFlags = surfaceFlags, smoothingGroup = 0
                    }, layers = layers
                },
                new BrushMesh.Polygon {
                    surfaceID = 3, firstEdge = 12, edgeCount = 4, description = new SurfaceDescription {
                        UV0 = UVMatrix.centered, surfaceFlags = surfaceFlags, smoothingGroup = 0
                    }, layers = layers
                },

                // top/down
                new BrushMesh.Polygon {
                    surfaceID = 4, firstEdge = 16, edgeCount = 4, description = new SurfaceDescription {
                        UV0 = UVMatrix.centered, surfaceFlags = surfaceFlags, smoothingGroup = 0
                    }, layers = layers
                },
                new BrushMesh.Polygon {
                    surfaceID = 5, firstEdge = 20, edgeCount = 4, description = new SurfaceDescription {
                        UV0 = UVMatrix.centered, surfaceFlags = surfaceFlags, smoothingGroup = 0
                    }, layers = layers
                }
            });
        }
        public static BrushMesh CreateBox(UnityEngine.Vector3 min, UnityEngine.Vector3 max, SurfaceLayers layers, SurfaceFlags surfaceFlags = SurfaceFlags.None)
        {
            if (!BoundsExtensions.IsValid(min, max))
            {
                return(null);
            }

            if (min.x > max.x)
            {
                float x = min.x; min.x = max.x; max.x = x;
            }
            if (min.y > max.y)
            {
                float y = min.y; min.y = max.y; max.y = y;
            }
            if (min.z > max.z)
            {
                float z = min.z; min.z = max.z; max.z = z;
            }

            return(new BrushMesh
            {
                polygons = CreateBoxPolygons(layers, surfaceFlags),
                halfEdges = boxHalfEdges.ToArray(),
                vertices = CreateBoxVertices(min, max)
            });
        }
        /// <summary>
        /// Creates a box <see cref="Chisel.Core.BrushMesh"/> with <paramref name="size"/> and optional <paramref name="material"/>
        /// </summary>
        /// <param name="size">The size of the box</param>
        /// <param name="material">The [UnityEngine.Material](https://docs.unity3d.com/ScriptReference/Material.html) that will be set to all surfaces of the box (optional)</param>
        /// <returns>A <see cref="Chisel.Core.BrushMesh"/> on success, null on failure</returns>
        public static BrushMesh CreateBox(UnityEngine.Vector3 size, SurfaceLayers layers, SurfaceFlags surfaceFlags = SurfaceFlags.None)
        {
            var halfSize = size * 0.5f;

            return(CreateBox(-halfSize, halfSize, layers, surfaceFlags));
        }
Beispiel #4
0
        public static BrushMesh CreateSphere(Vector3 diameterXYZ, float offsetY, bool generateFromCenter, int horzSegments, int vertSegments, SurfaceLayers layers = default, SurfaceFlags surfaceFlags = SurfaceFlags.None)
        {
            var lastVertSegment = vertSegments - 1;

            var triangleCount = horzSegments + horzSegments;      // top & bottom
            var quadCount     = horzSegments * (vertSegments - 2);
            int polygonCount  = triangleCount + quadCount;
            int halfEdgeCount = (triangleCount * 3) + (quadCount * 4);

            Vector3[] vertices = null;
            CreateSphereVertices(diameterXYZ, offsetY, generateFromCenter, horzSegments, vertSegments, ref vertices);

            var polygons  = new Polygon[polygonCount];
            var halfEdges = new HalfEdge[halfEdgeCount];

            var edgeIndex    = 0;
            var polygonIndex = 0;
            var startVertex  = 2;

            for (int v = 0; v < vertSegments; v++)
            {
                var startEdge = edgeIndex;
                for (int h = 0, p = horzSegments - 1; h < horzSegments; p = h, h++)
                {
                    var n = (h + 1) % horzSegments;
                    int polygonEdgeCount;
                    if (v == 0) // top
                    {
                        //          0
                        //          *
                        //         ^ \
                        //     p1 /0 1\ n0
                        //       /  2  v
                        //		*<------*
                        //     2    t    1
                        polygonEdgeCount = 3;
                        var p1 = (p * 3) + 1;
                        var n0 = (n * 3) + 0;
                        var t  = ((vertSegments == 2) ? (startEdge + (horzSegments * 3) + (h * 3) + 1) : (startEdge + (horzSegments * 3) + (h * 4) + 1));
                        halfEdges[edgeIndex + 0] = new HalfEdge {
                            twinIndex = p1, vertexIndex = 0
                        };
                        halfEdges[edgeIndex + 1] = new HalfEdge {
                            twinIndex = n0, vertexIndex = startVertex + (horzSegments - 1) - h
                        };
                        halfEdges[edgeIndex + 2] = new HalfEdge {
                            twinIndex = t, vertexIndex = startVertex + (horzSegments - 1) - p
                        };
                    }
                    else
                    if (v == lastVertSegment)
                    {
                        //     0    t    1
                        //		*------>*
                        //       ^  1  /
                        //     p1 \0 2/ n0
                        //         \ v
                        //          *
                        //          2
                        polygonEdgeCount = 3;
                        var p2 = startEdge + (p * 3) + 2;
                        var n0 = startEdge + (n * 3) + 0;
                        var t  = ((vertSegments == 2) ? (startEdge - (horzSegments * 3) + (h * 3) + 2) : (startEdge - (horzSegments * 4) + (h * 4) + 3));
                        halfEdges[edgeIndex + 0] = new HalfEdge {
                            twinIndex = p2, vertexIndex = startVertex + (horzSegments - 1) - p
                        };
                        halfEdges[edgeIndex + 1] = new HalfEdge {
                            twinIndex = t, vertexIndex = startVertex + (horzSegments - 1) - h
                        };
                        halfEdges[edgeIndex + 2] = new HalfEdge {
                            twinIndex = n0, vertexIndex = 1
                        };
                    }
                    else
                    {
                        //     0    t3   1
                        //		*------>*
                        //      ^   1   |
                        //   p1 |0     2| n0
                        //      |   3   v
                        //		*<------*
                        //     3    t1   2
                        polygonEdgeCount = 4;
                        var p1 = startEdge + (p * 4) + 2;
                        var n0 = startEdge + (n * 4) + 0;
                        var t3 = ((v == 1) ? (startEdge - (horzSegments * 3) + (h * 3) + 2) : (startEdge - (horzSegments * 4) + (h * 4) + 3));
                        var t1 = ((v == lastVertSegment - 1) ? (startEdge + (horzSegments * 4) + (h * 3) + 1) : (startEdge + (horzSegments * 4) + (h * 4) + 1));
                        halfEdges[edgeIndex + 0] = new HalfEdge {
                            twinIndex = p1, vertexIndex = startVertex + (horzSegments - 1) - p
                        };
                        halfEdges[edgeIndex + 1] = new HalfEdge {
                            twinIndex = t3, vertexIndex = startVertex + (horzSegments - 1) - h
                        };
                        halfEdges[edgeIndex + 2] = new HalfEdge {
                            twinIndex = n0, vertexIndex = startVertex + (horzSegments - 1) - h + horzSegments
                        };
                        halfEdges[edgeIndex + 3] = new HalfEdge {
                            twinIndex = t1, vertexIndex = startVertex + (horzSegments - 1) - p + horzSegments
                        };
                    }

                    polygons[polygonIndex] = new Polygon
                    {
                        surfaceID   = polygonIndex,
                        firstEdge   = edgeIndex,
                        edgeCount   = polygonEdgeCount,
                        description = new SurfaceDescription {
                            UV0 = UVMatrix.centered, surfaceFlags = surfaceFlags, smoothingGroup = 0
                        },
                        layers = layers
                    };

                    edgeIndex += polygonEdgeCount;
                    polygonIndex++;
                }
                if (v > 0)
                {
                    startVertex += horzSegments;
                }
            }

            return(new BrushMesh
            {
                polygons = polygons,
                halfEdges = halfEdges,
                vertices = vertices
            });
        }