Generic mesh-building class; input each vert and triangle as created, it will take care of creating the actual mesh and calculating tangents
Esempio n. 1
0
 public List<Vertex> generateVertices(MeshBuilder builder, Vector3 offset, UVArea area, float v, float yCos, float ySin)
 {
     List<Vertex> vertices = builder.generateRadialVerticesFlatUVs(offset, faces, radius, height, startAngle, endAngle, v, area, yCos, ySin);
     startVector = vertices[0].vertex;
     endVector = vertices[vertices.Count - 1].vertex;
     return vertices;
 }
 private void enableRendering()
 {
     if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight)
     {
         Transform modelBase = part.transform.FindRecursive("model");
         DragCube cube;
         GameObject go;
         int len = part.DragCubes.Cubes.Count;
         Vector3 size;
         Vector3 center;
         Mesh mesh;
         MeshFilter filter;
         MeshRenderer render;
         for (int i = 0; i < len; i++)
         {
             cube = part.DragCubes.Cubes[i];
             go = new GameObject("DragCubeRenderer");
             MeshBuilder mb = new MeshBuilder();
             size = cube.Size;
             center = cube.Center;
             mb.generateCuboid(size, center, Vector2.zero, Vector2.one);
             mesh = mb.buildMesh();
             filter = go.AddComponent<MeshFilter>();
             filter.mesh = mesh;
             render = go.AddComponent<MeshRenderer>();
             render.enabled = true;
             go.transform.NestToParent(modelBase);
         }
     }
 }
Esempio n. 3
0
        public Mesh generateMesh()
        {
            MeshBuilder builder = new MeshBuilder();
            Arc bottomOuterArc = new Arc(bottomRadius, 0, 0, 360, faces);
            Arc topOuterArc = new Arc(topRadius, height, 0, 360, faces);
            List<Vertex> verts1 = bottomOuterArc.generateVertices(builder, offset, outsideUV, outsideUV.v1, 1, 0);
            List<Vertex> verts2 = topOuterArc.generateVertices(builder, offset, outsideUV, outsideUV.v2, 1, 0);
            builder.generateQuads(verts1, verts2, false);
            if (bottomInnerRadius != 0 || topInnerRadius != 0)
            {
                Arc bottomInnerArc = new Arc(bottomInnerRadius, 0, 0, 360, faces);
                Arc topInnerArc = new Arc(topInnerRadius, height, 0, 360, faces);
                float heightDiff = topInnerArc.height - bottomInnerArc.height;
                float radiusDiff = bottomInnerArc.radius - topInnerArc.radius;
                float sideRadians = Mathf.Atan2(heightDiff, radiusDiff) - 90 * Mathf.Deg2Rad;
                float yCos = Mathf.Cos(sideRadians);
                float ySin = Mathf.Sin(sideRadians);
                verts1 = bottomInnerArc.generateVertices(builder, offset, insideUV, insideUV.v1, -yCos, ySin);
                verts2 = topInnerArc.generateVertices(builder, offset, insideUV, insideUV.v2, -yCos, ySin);
                builder.generateQuads(verts1, verts2, true);
            }
            builder.generateCylinderCap(offset, faces, bottomRadius, bottomInnerRadius, 0, 0, 360, bottomUV, true);
            builder.generateCylinderCap(offset, faces, topRadius, topInnerRadius, height, 0, 360, topUV, false);

            Mesh mesh = builder.buildMesh();
            mesh.name = "ProceduralCylinderMesh";
            return mesh;
        }
Esempio n. 4
0
        private void generateSidewalls(MeshBuilder builder, UVArea caps)
        {
            int len = outerArcs.Count;
            float[] distances = new float[len];
            float[] us = new float[len];
            float totalLength = 0;
            float length;
            Vector3 a, b;
            for (int i = 0; i < len - 1; i++)
            {
                a = outerArcs[i].startVector;
                b = outerArcs[i + 1].startVector;
                length = Vector3.Distance(a, b);
                totalLength += length;
                distances[i + 1] = length;
            }

            length = 0;
            float percent;
            float uDelta = caps.u2 - caps.u1;
            for (int i = 0; i < len; i++)
            {
                length += distances[i];
                percent = length / totalLength;
                us[i] = caps.u1 + percent * uDelta;
            }

            Vector3 leftNorm = new Vector3();
            leftNorm.x = outerArcs[0].startVector.z;
            leftNorm.z = -outerArcs[0].startVector.x;
            leftNorm.Normalize();
            Vector3 rightNorm = new Vector3();
            rightNorm.x = outerArcs[0].endVector.z;
            rightNorm.z = -outerArcs[0].endVector.x;
            rightNorm.Normalize();
            rightNorm = -rightNorm;//as faces get inverted for this side, so norms get inverted as well

            //left
            List<Vertex> outerStartVerts = new List<Vertex>();
            List<Vertex> innerStartVerts = new List<Vertex>();

            //right
            List<Vertex> outerEndVerts = new List<Vertex>();
            List<Vertex> innerEndVerts = new List<Vertex>();

            len = outerArcs.Count;
            Vector3 outer, inner;
            Vector2 uv;
            for (int i = 0; i < len; i++)
            {
                outer = outerArcs[i].startVector;
                uv = new Vector2(us[i], caps.v1);
                outerStartVerts.Add(builder.addVertex(outer, leftNorm, uv));
                inner = innerArcs[i].startVector;
                uv = new Vector2(us[i], caps.v2);
                innerStartVerts.Add(builder.addVertex(inner, leftNorm, uv));

                outer = outerArcs[i].endVector;
                uv = new Vector2(us[i], caps.v1);
                outerEndVerts.Add(builder.addVertex(outer, rightNorm, uv));
                inner = innerArcs[i].endVector;
                uv = new Vector2(us[i], caps.v2);
                innerEndVerts.Add(builder.addVertex(inner, rightNorm, uv));
            }
            builder.generateQuads(outerStartVerts, innerStartVerts, false);
            builder.generateQuads(outerEndVerts, innerEndVerts, true);
        }
Esempio n. 5
0
 private void generatePanelSegment(MeshBuilder builder, Vector3 pos, Arc arcA, Arc arcB, UVArea area, bool invertFaces, bool invertNormalY, bool invertNormalXZ)
 {
     float heightDiff = arcB.height - arcA.height;
     float offset = arcA.radius - arcB.radius;
     float sideRadians = Mathf.Atan2(heightDiff, offset) - 90 * Mathf.Deg2Rad;
     float yCos = Mathf.Cos(sideRadians);
     float ySin = Mathf.Sin(sideRadians);
     if (invertNormalY) { ySin *= -1; }
     if (invertNormalXZ) { yCos *= -1; }
     List<Vertex> verts1 = new List<Vertex>();
     List<Vertex> verts2 = new List<Vertex>();
     verts1.AddRange(arcA.generateVertices(builder, pos, area, area.v1, yCos, ySin));
     verts2.AddRange(arcB.generateVertices(builder, pos, area, area.v2, yCos, ySin));
     builder.generateQuads(verts1, verts2, invertFaces);
 }
Esempio n. 6
0
        public Mesh generatePanels(Vector3 pos, UVArea outer, UVArea inner, UVArea caps)
        {
            int len = outerArcs.Count;
            MeshBuilder builder = new MeshBuilder();

            for (int i = 0; i < len - 1; i++)
            {
                generatePanelSegment(builder, pos, outerArcs[i], outerArcs[i + 1], outer, false, true, false);//outside panels
                generatePanelSegment(builder, pos, innerArcs[i], innerArcs[i + 1], inner, true, false, true);//inside panels
            }
            generatePanelSegment(builder, pos, outerArcs[0], innerArcs[0], caps, true, false, false);//bottom cap
            generatePanelSegment(builder, pos, outerArcs[outerArcs.Count - 1], innerArcs[innerArcs.Count - 1], caps, false, false, false);//top cap

            if (shouldGenerateSidewalls)
            {
                generateSidewalls(builder, caps);
            }

            return builder.buildMesh();
        }
Esempio n. 7
0
 public GameObject[] generateColliders(Vector3 center, int facesPerCollider)
 {
     float totalAngle = endAngle - startAngle;
     float anglePerFace = 360f / (float)faces;
     int localFaces = (int)Math.Round(totalAngle / anglePerFace);
     localFaces /= facesPerCollider;
     GameObject[] colliders = new GameObject[localFaces];
     float localStart, localEnd, startY, height, topRadius, bottomRadius, thickness;
     Mesh colliderMesh;
     MeshFilter mf;
     //MeshRenderer mr;
     MeshCollider mc;
     thickness = outerArcs[0].radius - innerArcs[0].radius;
     MeshBuilder builder = new MeshBuilder();
     for (int i = 0; i < localFaces; i++)
     {
         localStart = startAngle + (float)i * anglePerFace;
         localEnd = localStart + (anglePerFace * facesPerCollider);
         for (int k = 0; k < innerArcs.Count - 1; k++)
         {
             startY = innerArcs[k].height;
             height = innerArcs[k+1].height - startY;
             bottomRadius = outerArcs[k].radius;
             topRadius = outerArcs[k+1].radius;
             builder.generatePanelCollider(center, localStart, localEnd, startY, height, bottomRadius, topRadius, thickness);
             colliderMesh = builder.buildMesh();
             builder.clear();
             colliders[i] = new GameObject("PanelCollider"+i+"-"+k);
             mf = colliders[i].AddComponent<MeshFilter>();
             //mr = colliders[i].AddComponent<MeshRenderer>();
             mc = colliders[i].AddComponent<MeshCollider>();
             mf.mesh = colliderMesh;
             //mr.enabled = true;
             mc.sharedMesh = colliderMesh;
             mc.enabled = mc.convex = true;
         }
     }
     return colliders;
 }
Esempio n. 8
0
        public GameObject[] generateColliders()
        {
            GameObject[] colliders = new GameObject[faces];

            float anglePerFace = 360f / (float)faces;
            float start = 0;
            float end = start + anglePerFace;
            float thickness = topRadius - topInnerRadius;

            Mesh mesh;
            MeshFilter mf;
            MeshCollider mc;
            MeshBuilder builder = new MeshBuilder();

            GameObject collider;
            for (int i = 0; i < faces; i++)
            {
                collider = new GameObject("ProceduralCylinderCollider-" + i);
                colliders[i] = collider;
                start = (float)i * anglePerFace;
                end = start + anglePerFace;
                builder.generatePanelCollider(offset, start, end, 0, height, bottomRadius, topRadius, thickness);
                mesh = builder.buildMesh();
                mf = collider.AddComponent<MeshFilter>();
                mf.mesh = mesh;
                mc = collider.AddComponent<MeshCollider>();
                mc.sharedMesh = mesh;
                mc.convex = true;
                builder.clear();
            }
            return colliders;
        }
Esempio n. 9
0
        private Mesh generatePanelCollider(Vector3 center, float startAngle, float endAngle, float height, float bottomRadius, float topRadius, float thickness)
        {
            MeshBuilder builder = new MeshBuilder();

            float bottomInnerRadius = bottomRadius - thickness;
            float topInnerRadius = topRadius - thickness;
            float startRads = Mathf.Deg2Rad * startAngle;
            float endRads = Mathf.Deg2Rad * endAngle;
            float startXSin = -Mathf.Sin(startRads);
            float startZCos = Mathf.Cos(startRads);
            float endXSin = -Mathf.Sin(endRads);
            float endZCos = Mathf.Cos(endRads);

            Vector3 frontBottomLeft = new Vector3(center.x + bottomRadius * startXSin, center.y, center.z + bottomRadius * startZCos);
            Vector3 frontBottomRight = new Vector3(center.x + bottomRadius * endXSin, center.y, center.z + bottomRadius * endZCos);
            Vector3 frontTopLeft = new Vector3(center.x + topRadius * startXSin, center.y + height, center.z + topRadius * startZCos);
            Vector3 frontTopRight = new Vector3(center.x + topRadius * endXSin, center.y + height, center.z + topRadius * endZCos);

            Vector3 rearBottomLeft = new Vector3(center.x + bottomInnerRadius * startXSin, center.y, center.z + bottomInnerRadius * startZCos);
            Vector3 rearBottomRight = new Vector3(center.x + bottomInnerRadius * endXSin, center.y, center.z + bottomInnerRadius * endZCos);
            Vector3 rearTopLeft = new Vector3(center.x + topInnerRadius * startXSin, center.y + height, center.z + topInnerRadius * startZCos);
            Vector3 rearTopRight = new Vector3(center.x + topInnerRadius * endXSin, center.y + height, center.z + topInnerRadius * endZCos);

            Vector3 normFront = Vector3.forward;
            Vector3 normRear = Vector3.back;
            Vector3 normLeft = Vector3.left;
            Vector3 normRight = Vector3.right;
            Vector3 normUp = Vector3.up;
            Vector3 normDown = Vector3.down;

            Vector2 uv1 = new Vector2(0, 0);
            Vector2 uv2 = new Vector2(1, 0);
            Vector2 uv3 = new Vector2(0, 1);
            Vector2 uv4 = new Vector2(1, 1);

            List<Vertex> v1 = new List<Vertex>();
            List<Vertex> v2 = new List<Vertex>();

            //generate front face
            v1.Add(builder.addVertex(frontBottomLeft, normFront, uv1));
            v1.Add(builder.addVertex(frontBottomRight, normFront, uv2));
            v2.Add(builder.addVertex(frontTopLeft, normFront, uv3));
            v2.Add(builder.addVertex(frontTopRight, normFront, uv4));
            builder.generateQuads(v1, v2, false);
            v1.Clear();
            v2.Clear();

            //generate rear face
            v1.Add(builder.addVertex(rearBottomLeft, normRear, uv2));
            v1.Add(builder.addVertex(rearBottomRight, normRear, uv1));
            v2.Add(builder.addVertex(rearTopLeft, normRear, uv4));
            v2.Add(builder.addVertex(rearTopRight, normRear, uv3));
            builder.generateQuads(v1, v2, true);
            v1.Clear();
            v2.Clear();

            //generate left face
            v1.Add(builder.addVertex(frontBottomLeft, normLeft, uv2));
            v1.Add(builder.addVertex(rearBottomLeft, normLeft, uv1));
            v2.Add(builder.addVertex(frontTopLeft, normLeft, uv4));
            v2.Add(builder.addVertex(rearTopLeft, normLeft, uv3));
            builder.generateQuads(v1, v2, true);
            v1.Clear();
            v2.Clear();

            //generate right face
            v1.Add(builder.addVertex(frontBottomRight, normRight, uv1));
            v1.Add(builder.addVertex(rearBottomRight, normRight, uv2));
            v2.Add(builder.addVertex(frontTopRight, normRight, uv3));
            v2.Add(builder.addVertex(rearTopRight, normRight, uv4));
            builder.generateQuads(v1, v2, false);
            v1.Clear();
            v2.Clear();

            //generate top face
            v1.Add(builder.addVertex(frontTopRight, normUp, uv2));
            v1.Add(builder.addVertex(frontTopLeft, normUp, uv1));
            v2.Add(builder.addVertex(rearTopRight, normUp, uv4));
            v2.Add(builder.addVertex(rearTopLeft, normUp, uv3));
            builder.generateQuads(v1, v2, true);
            v1.Clear();
            v2.Clear();

            //generate bottom face
            v1.Add(builder.addVertex(frontBottomRight, normUp, uv1));
            v1.Add(builder.addVertex(frontBottomLeft, normUp, uv2));
            v2.Add(builder.addVertex(rearBottomRight, normUp, uv3));
            v2.Add(builder.addVertex(rearBottomLeft, normUp, uv4));
            builder.generateQuads(v1, v2, false);
            v1.Clear();
            v2.Clear();

            return builder.buildMesh();
        }
Esempio n. 10
0
        //TODO
        private Mesh generatePanelCollider(Vector3 pos, float start, float end, float bottomRad, float topRad, float y1, float y2)
        {
            MeshBuilder builder = new MeshBuilder();
            List<Vertex> verts1 = new List<Vertex>();
            List<Vertex> verts2 = new List<Vertex>();
            Vector3 outerBottomStart;


            throw new NotImplementedException();
            builder.generateQuads(verts1, verts2, false);
            return builder.buildMesh();
        }