Esempio n. 1
0
        public override Bounds RebuildMesh(ProBuilderMesh mesh, Vector3 size, Quaternion rotation)
        {
            mesh.Clear();

            Vector3[] points = new Vector3[k_CubeTriangles.Length];

            for (int i = 0; i < k_CubeTriangles.Length; i++)
            {
                points[i] = rotation * Vector3.Scale(k_CubeVertices[k_CubeTriangles[i]], Math.Abs(size));
            }

            mesh.GeometryWithPoints(points);

            return(mesh.mesh.bounds);
        }
Esempio n. 2
0
        public override Bounds RebuildMesh(ProBuilderMesh mesh, Vector3 size, Quaternion rotation)
        {
            int w = m_WidthSegments + 1;
            int h = m_HeightSegments + 1;

            Vector2[] p = new Vector2[(w * h) * 4];
            Vector3[] v = new Vector3[(w * h) * 4];

            float width = 1f, height = 1f;
            int   i = 0;

            {
                for (int y = 0; y < h; y++)
                {
                    for (int x = 0; x < w; x++)
                    {
                        float x0 = x * (width / w) - (width / 2f);
                        float x1 = (x + 1) * (width / w) - (width / 2f);

                        float y0 = y * (height / h) - (height / 2f);
                        float y1 = (y + 1) * (height / h) - (height / 2f);

                        p[i + 0] = new Vector2(x0, y0);
                        p[i + 1] = new Vector2(x1, y0);
                        p[i + 2] = new Vector2(x0, y1);
                        p[i + 3] = new Vector2(x1, y1);

                        i += 4;
                    }
                }
            }

            for (i = 0; i < v.Length; i++)
            {
                v[i] = new Vector3(p[i].y, 0f, p[i].x);
            }

            mesh.GeometryWithPoints(v);

            return(mesh.mesh.bounds);
        }
Esempio n. 3
0
        public override Bounds RebuildMesh(ProBuilderMesh mesh, Vector3 size, Quaternion rotation)
        {
            var upDir      = Vector3.Scale(rotation * Vector3.up, size);
            var rightDir   = Vector3.Scale(rotation * Vector3.right, size);
            var forwardDir = Vector3.Scale(rotation * Vector3.forward, size);

            var height  = upDir.magnitude;
            var xRadius = rightDir.magnitude / 2f;
            var zRadius = forwardDir.magnitude / 2f;

            // template is outer ring - radius refers to outer ring always
            Vector2[] templateOut = new Vector2[m_NumberOfSides];
            Vector2[] templateIn  = new Vector2[m_NumberOfSides];

            Vector2 tangent;

            for (int i = 0; i < m_NumberOfSides; i++)
            {
                float angle = i * (360f / m_NumberOfSides);
                templateOut[i] = Math.PointInEllipseCircumference(xRadius, zRadius, angle, Vector2.zero, out tangent);

                Vector2 tangentOrtho = new Vector2(-tangent.y, tangent.x);
                templateIn[i] = templateOut[i] + (m_Thickness * tangentOrtho);
            }

            List <Vector3> v     = new List <Vector3>();
            var            baseY = height / 2f;
            // build out sides
            Vector2 tmp, tmp2, tmp3, tmp4;
            var     heightSegments = m_HeightCuts + 1;

            for (int i = 0; i < heightSegments; i++)
            {
                // height subdivisions
                float y  = i * (height / heightSegments) - baseY;
                float y2 = (i + 1) * (height / heightSegments) - baseY;

                for (int n = 0; n < m_NumberOfSides; n++)
                {
                    tmp  = templateOut[n];
                    tmp2 = n < (m_NumberOfSides - 1) ? templateOut[n + 1] : templateOut[0];

                    // outside quads
                    Vector3[] qvo = new Vector3[4]
                    {
                        new Vector3(tmp2.x, y, tmp2.y),
                        new Vector3(tmp.x, y, tmp.y),
                        new Vector3(tmp2.x, y2, tmp2.y),
                        new Vector3(tmp.x, y2, tmp.y)
                    };

                    // inside quad
                    tmp  = templateIn[n];
                    tmp2 = n < (m_NumberOfSides - 1) ? templateIn[n + 1] : templateIn[0];
                    Vector3[] qvi = new Vector3[4]
                    {
                        new Vector3(tmp.x, y, tmp.y),
                        new Vector3(tmp2.x, y, tmp2.y),
                        new Vector3(tmp.x, y2, tmp.y),
                        new Vector3(tmp2.x, y2, tmp2.y)
                    };

                    v.AddRange(qvo);
                    v.AddRange(qvi);
                }
            }

            // build top and bottom
            for (int i = 0; i < m_NumberOfSides; i++)
            {
                tmp  = templateOut[i];
                tmp2 = (i < m_NumberOfSides - 1) ? templateOut[i + 1] : templateOut[0];
                tmp3 = templateIn[i];
                tmp4 = (i < m_NumberOfSides - 1) ? templateIn[i + 1] : templateIn[0];

                // top
                Vector3[] tpt = new Vector3[4]
                {
                    new Vector3(tmp2.x, height - baseY, tmp2.y),
                    new Vector3(tmp.x, height - baseY, tmp.y),
                    new Vector3(tmp4.x, height - baseY, tmp4.y),
                    new Vector3(tmp3.x, height - baseY, tmp3.y)
                };

                // bottom
                Vector3[] tpb = new Vector3[4]
                {
                    new Vector3(tmp.x, -baseY, tmp.y),
                    new Vector3(tmp2.x, -baseY, tmp2.y),
                    new Vector3(tmp3.x, -baseY, tmp3.y),
                    new Vector3(tmp4.x, -baseY, tmp4.y),
                };

                v.AddRange(tpb);
                v.AddRange(tpt);
            }

            for (int i = 0; i < v.Count; i++)
            {
                v[i] = rotation * v[i];
            }

            mesh.GeometryWithPoints(v.ToArray());

            //Smooth internal and external faces
            if (m_Smooth)
            {
                int smoothCount = 2 * heightSegments * m_NumberOfSides;
                for (int i = 0; i < smoothCount; i++)
                {
                    mesh.facesInternal[i].smoothingGroup = 1;
                }
            }

            return(UpdateBounds(mesh, size, rotation, new Bounds()));
        }
Esempio n. 4
0
        public override Bounds RebuildMesh(ProBuilderMesh mesh, Vector3 size, Quaternion rotation)
        {
            var upDir      = Vector3.Scale(rotation * Vector3.up, size);
            var rightDir   = Vector3.Scale(rotation * Vector3.right, size);
            var forwardDir = Vector3.Scale(rotation * Vector3.forward, size);

            var xRadius = rightDir.magnitude / 2f;
            var yRadius = upDir.magnitude;
            var depth   = forwardDir.magnitude / 2f;

            var radialCuts  = m_NumberOfSides + 1;
            var angle       = m_ArchDegrees;
            var templateOut = new Vector2[radialCuts];
            var templateIn  = new Vector2[radialCuts];

            if (angle < 90f)
            {
                xRadius *= 2f;
            }
            else if (angle < 180f)
            {
                xRadius *= 1f + Mathf.Lerp(1f, 0f, Mathf.Abs(Mathf.Cos(angle * Mathf.Deg2Rad)));
            }
            else if (angle > 180f)
            {
                yRadius /= 1f + Mathf.Lerp(0f, 1f, (angle - 180f) / 90f);
            }

            for (int i = 0; i < radialCuts; i++)
            {
                var     currentAngle = i * (angle / (radialCuts - 1));
                Vector2 tangent;
                templateOut[i] = Math.PointInEllipseCircumference(xRadius, yRadius, currentAngle, Vector2.zero, out tangent);
                templateIn[i]  = Math.PointInEllipseCircumference(xRadius - m_Thickness, yRadius - m_Thickness, currentAngle, Vector2.zero, out tangent);
            }

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

            Vector2 tmp, tmp2, tmp3, tmp4;

            float y = -depth;
            int   smoothedFaceCount = 0;

            for (int n = 0; n < radialCuts - 1; n++)
            {
                // outside faces
                tmp  = templateOut[n];
                tmp2 = n < (radialCuts - 1) ? templateOut[n + 1] : templateOut[n];

                Vector3[] qvo = GetFace(tmp, tmp2, -depth);

                // inside faces
                tmp  = templateIn[n];
                tmp2 = n < (radialCuts - 1) ? templateIn[n + 1] : templateIn[n];

                Vector3[] qvi = GetFace(tmp2, tmp, -depth);

                // left side bottom face
                if (angle < 360f && m_EndCaps)
                {
                    if (n == 0)
                    {
                        v.AddRange(GetFace(templateOut[n], templateIn[n], depth));
                    }
                }

                v.AddRange(qvo);
                v.AddRange(qvi);
                smoothedFaceCount += 2;

                if (angle < 360f && m_EndCaps)
                {
                    // right side bottom face
                    if (n == radialCuts - 2)
                    {
                        v.AddRange(GetFace(templateIn[n + 1], templateOut[n + 1], depth));
                    }
                }
            }

            // build front and back faces
            for (int i = 0; i < radialCuts - 1; i++)
            {
                tmp  = templateOut[i];
                tmp2 = (i < radialCuts - 1) ? templateOut[i + 1] : templateOut[i];
                tmp3 = templateIn[i];
                tmp4 = (i < radialCuts - 1) ? templateIn[i + 1] : templateIn[i];

                // front
                Vector3[] tpb = new Vector3[4]
                {
                    new Vector3(tmp.x, tmp.y, depth),
                    new Vector3(tmp2.x, tmp2.y, depth),
                    new Vector3(tmp3.x, tmp3.y, depth),
                    new Vector3(tmp4.x, tmp4.y, depth),
                };

                // back
                Vector3[] tpt = new Vector3[4]
                {
                    new Vector3(tmp2.x, tmp2.y, y),
                    new Vector3(tmp.x, tmp.y, y),
                    new Vector3(tmp4.x, tmp4.y, y),
                    new Vector3(tmp3.x, tmp3.y, y)
                };

                v.AddRange(tpb);
                v.AddRange(tpt);
            }

            var sizeSigns = Math.Sign(size);

            for (int i = 0; i < v.Count; i++)
            {
                v[i] = Vector3.Scale(rotation * v[i], sizeSigns);
            }

            mesh.GeometryWithPoints(v.ToArray());

            if (m_Smooth)
            {
                for (int i = (angle < 360f && m_EndCaps) ? 1 : 0; i < smoothedFaceCount; i++)
                {
                    mesh.facesInternal[i].smoothingGroup = 1;
                }
            }

            var sizeSign = sizeSigns.x * sizeSigns.y * sizeSigns.z;

            if (sizeSign < 0)
            {
                var faces = mesh.facesInternal;
                foreach (var face in faces)
                {
                    face.Reverse();
                }
            }

            mesh.TranslateVerticesInWorldSpace(mesh.mesh.triangles, mesh.transform.TransformDirection(-mesh.mesh.bounds.center));
            mesh.Refresh();

            return(UpdateBounds(mesh, size, rotation, new Bounds()));
        }
Esempio n. 5
0
        public override Bounds RebuildMesh(ProBuilderMesh mesh, Vector3 size, Quaternion rotation)
        {
            var upDir      = Vector3.Scale(rotation * Vector3.up, size);
            var rightDir   = Vector3.Scale(rotation * Vector3.right, size);
            var forwardDir = Vector3.Scale(rotation * Vector3.forward, size);

            float totalWidth  = rightDir.magnitude;
            float totalHeight = upDir.magnitude;
            float depth       = forwardDir.magnitude;

            float xLegCoord   = totalWidth / 2f;
            var   legWidth    = xLegCoord - m_LegWidth > 0 ? xLegCoord - m_LegWidth : 0.001f;
            var   ledgeHeight = (totalHeight - m_DoorHeight * 2f) > 0 ? totalHeight - m_DoorHeight * 2f : 0.001f;

            var baseY = -totalHeight;
            var front = depth / 2f;

            // 8---9---10--11
            // |           |
            // 4   5---6   7
            // |   |   |   |
            // 0   1   2   3
            Vector3[] template = new Vector3[12]
            {
                new Vector3(-xLegCoord, baseY, front),        // 0
                new Vector3(-legWidth, baseY, front),         // 1
                new Vector3(legWidth, baseY, front),          // 2
                new Vector3(xLegCoord, baseY, front),         // 3
                new Vector3(-xLegCoord, ledgeHeight, front),  // 4
                new Vector3(-legWidth, ledgeHeight, front),   // 5
                new Vector3(legWidth, ledgeHeight, front),    // 6
                new Vector3(xLegCoord, ledgeHeight, front),   // 7
                new Vector3(-xLegCoord, totalHeight, front),  // 8
                new Vector3(-legWidth, totalHeight, front),   // 9
                new Vector3(legWidth, totalHeight, front),    // 10
                new Vector3(xLegCoord, totalHeight, front)    // 11
            };

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

            points.Add(template[4]);
            points.Add(template[0]);
            points.Add(template[5]);
            points.Add(template[1]);

            points.Add(template[2]);
            points.Add(template[3]);
            points.Add(template[6]);
            points.Add(template[7]);

            points.Add(template[4]);
            points.Add(template[5]);
            points.Add(template[8]);
            points.Add(template[9]);

            points.Add(template[10]);
            points.Add(template[6]);
            points.Add(template[11]);
            points.Add(template[7]);

            points.Add(template[5]);
            points.Add(template[6]);
            points.Add(template[9]);
            points.Add(template[10]);

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

            for (int i = 0; i < points.Count; i += 4)
            {
                reverse.Add(points[i + 0] - Vector3.forward * depth);
                reverse.Add(points[i + 2] - Vector3.forward * depth);
                reverse.Add(points[i + 1] - Vector3.forward * depth);
                reverse.Add(points[i + 3] - Vector3.forward * depth);
            }

            points.AddRange(reverse);

            points.Add(template[6]);
            points.Add(template[5]);
            points.Add(template[6] - Vector3.forward * depth);
            points.Add(template[5] - Vector3.forward * depth);

            points.Add(template[2] - Vector3.forward * depth);
            points.Add(template[2]);
            points.Add(template[6] - Vector3.forward * depth);
            points.Add(template[6]);

            points.Add(template[1]);
            points.Add(template[1] - Vector3.forward * depth);
            points.Add(template[5]);
            points.Add(template[5] - Vector3.forward * depth);

            var sizeSigns = Math.Sign(size);

            for (int i = 0; i < points.Count; i++)
            {
                points[i] = Vector3.Scale(rotation * points[i], sizeSigns);
            }

            mesh.GeometryWithPoints(points.ToArray());

            var sizeSign = sizeSigns.x * sizeSigns.y * sizeSigns.z;

            if (sizeSign < 0)
            {
                var faces = mesh.facesInternal;
                foreach (var face in faces)
                {
                    face.Reverse();
                }
            }

            return(mesh.mesh.bounds);
        }