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); }
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); }
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); } } }
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); } } }
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; }
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 * (innerArcs.Count - 1)]; 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(); int colliderIndex = 0; 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++, colliderIndex++) { 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[colliderIndex] = new GameObject("PanelCollider" + i + "-" + k); mf = colliders[colliderIndex].AddComponent <MeshFilter>(); //mr = colliders[i].AddComponent<MeshRenderer>(); mc = colliders[colliderIndex].AddComponent <MeshCollider>(); mf.mesh = colliderMesh; //mr.enabled = true; mc.sharedMesh = colliderMesh; mc.enabled = mc.convex = true; } } return(colliders); }
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()); }
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(); }
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; }
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; }
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(); }
//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(); }