public static IndexBuffer IndexBufferBuild(ProceduralShape shape) { var testIndexBuffer = new IndexBuffer(SystemCore.GraphicsDevice, IndexElementSize.SixteenBits, shape.Indices.Count(), BufferUsage.None); testIndexBuffer.SetData(shape.Indices.ToArray()); return(testIndexBuffer); }
private ProceduralShape AddSkirt(ref VertexPositionColorTextureNormal[] vertices, ref List <int> topEdges, ProceduralShape spherePatch, bool insideOut) { ProceduralShapeBuilder builder = new ProceduralShapeBuilder(); float skirtSize = 10f; float offset = 0; for (int i = 0; i < topEdges.Count - 1; i++) { Vector3 point = vertices[topEdges[i]].Position; Vector3 toCenter = Vector3.Normalize(-point); Vector3 lowPoint = point + toCenter * skirtSize; point += toCenter * offset; lowPoint += toCenter * offset; Vector3 point2 = vertices[topEdges[i + 1]].Position; Vector3 toCenter2 = Vector3.Normalize(-point2); Vector3 lowPoint2 = point2 + toCenter2 * skirtSize; point2 += toCenter2 * offset; lowPoint2 += toCenter2 * offset; builder.AddFaceWithColor(vertices[topEdges[i]].Color, point, point2, lowPoint2, lowPoint); } var skirt = builder.BakeShape(); if (insideOut) { skirt.InsideOut(); } spherePatch = ProceduralShape.Combine(spherePatch, skirt); return(spherePatch); }
public static VertexBuffer VertexBufferBuild(ProceduralShape shape) { var testBuffer = new VertexBuffer(SystemCore.GraphicsDevice, VertexPositionColorTextureNormal.VertexDeclaration, shape.Vertices.Count(), BufferUsage.None); testBuffer.SetData(shape.Vertices.ToArray()); return(testBuffer); }
public static ProceduralShape Combine(params ProceduralShape[] shapes) { ProceduralShape newShape = new ProceduralShape(); newShape.Vertices = new VertexPositionColorTextureNormal[shapes.Sum(x => x.Vertices.Length)]; newShape.Indices = new short[shapes.Sum(x => x.Indices.Length)]; int VertexOffset = 0; int IndexOffset = 0; foreach (ProceduralShape s in shapes) { for (int v = 0; v < s.Vertices.Length; v++) { newShape.Vertices[VertexOffset + v] = s.Vertices[v]; } for (int i = 0; i < s.Indices.Length; i++) { newShape.Indices[IndexOffset + i] = (short)(s.Indices[i] + VertexOffset); } VertexOffset += s.Vertices.Length; IndexOffset += s.Indices.Length; } newShape.PrimitiveCount = shapes.Sum(x => x.PrimitiveCount); return(newShape); }
public void GenerateGeometry(Effect testEffect, IModule module) { this.effect = testEffect; this.module = module; vertices = new VertexPositionColorTextureNormal[heightMapSize * heightMapSize]; int vertIndex = 0; for (float i = 0; i < heightMapSize; i++) { for (float j = 0; j < heightMapSize; j++) { var vert = new VertexPositionColorTextureNormal(); vert.Position = CalculateVertexPosition(i, j); vert.Texture = new Vector2(i * 2f / heightMapSize, j * 2f / heightMapSize); vert.Normal = normal; vert.Color = NodeColor; vertices[vertIndex] = vert; vertIndex++; } } GenerateIndices(); if (normal == Vector3.Down || normal == Vector3.Backward || normal == Vector3.Right) { indices = indices.Reverse().ToArray(); } indices = indices.Reverse().ToArray(); Sphereify(sphereSize); GenerateNormals(ref vertices); short[] ind = indices; var p = vertices.Select(x => x.Position).ToList(); var s = BoundingSphere.CreateFromPoints(p); ProceduralShape spherePatch = new ProceduralShape(vertices, indices); spherePatch.Translate(positionOffset); gameObject = GameObjectFactory.CreateRenderableGameObjectFromShape(spherePatch, effect); SystemCore.GameObjectManager.AddAndInitialiseGameObject(gameObject); isLeaf = true; }
public ProceduralShape Clone() { ProceduralShape clone = new ProceduralShape(); clone.Indices = new short[Indices.Length]; clone.Vertices = new VertexPositionColorTextureNormal[Vertices.Length]; Indices.CopyTo(clone.Indices, 0); Vertices.CopyTo(clone.Vertices, 0); clone.PrimitiveCount = this.PrimitiveCount; return(clone); }
public ProceduralShape BakeShape() { ProceduralShape shape = new ProceduralShape(); shape.Vertices = vertices.ToArray(); shape.Indices = indices.ToArray(); shape.PrimitiveCount = primCount; shape.FlipNormals(); return(shape); }
public static ProceduralShape Mirror(ProceduralShape shapeToMirror, Plane mirrorAxis) { ProceduralShape newShape = shapeToMirror.Clone(); for (int i = 0; i < shapeToMirror.Vertices.Length; i++) { //newShape.Vertices[i].Normal = -shapeToMirror.Vertices[i].Normal; newShape.Vertices[i].Position = MonoMathHelper.MirrorInPlane(shapeToMirror.Vertices[i].Position, mirrorAxis); } newShape.ReverseIndices(); newShape.FlipNormals(); return(newShape); }
public GameObject.GameObject CreateTranslatedRenderableHeightMap(Color color, Effect effect, Vector3 translation) { var vertexArray = GenerateVertexArray(translation.X, translation.Y, translation.Z); var indexArray = GenerateIndices(); GameObject.GameObject heightMapObject = new GameObject.GameObject(); ProceduralShape shape = new ProceduralShape(vertexArray, indexArray); shape.SetColor(color); RenderGeometryComponent renderGeom = new RenderGeometryComponent(shape); EffectRenderComponent renderComponent = new EffectRenderComponent(effect); heightMapObject.AddComponent(renderGeom); heightMapObject.AddComponent(renderComponent); heightMapObject.AddComponent(new StaticMeshColliderComponent(heightMapObject, GetVertices(), GetIndices().ToArray(), Vector3.Zero)); return(heightMapObject); }
public static ProceduralShape ProceduralAsteroid() { var module = new FastRidgedMultifractal(); ((FastRidgedMultifractal)module).Frequency = 0.2f; ((FastRidgedMultifractal)module).NoiseQuality = NoiseQuality.Standard; ((FastRidgedMultifractal)module).Seed = RandomHelper.GetRandomInt(0, 5000); ((FastRidgedMultifractal)module).OctaveCount = 6; ((FastRidgedMultifractal)module).Lacunarity = 2; Sphere sphere = new Sphere(32, 32); VertexPositionColorTextureNormal[] verts = sphere.GenerateVertexArray(); short[] ind = sphere.GenerateIndices(); var p = verts.Select(x => x.Position).ToList(); var s = BoundingSphere.CreateFromPoints(p); ProceduralShape asteroid = new ProceduralShape(verts, ind); return(asteroid); }
/// <summary> /// Builds a composite shape composed of a cylinder and two spheres. /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <param name="width"></param> /// <returns></returns> public static ProceduralShape Capsule(Vector3 start, Vector3 end, float width) { ProceduralSphereTwo s = new ProceduralSphereTwo(20); ProceduralSphereTwo e = new ProceduralSphereTwo(20); s.Scale(width); e.Scale(width); Vector3 translation = Vector3.Zero - ((start + end) / 2); //untranslated cylinder will have a centroid at zero, a bottom point at (0,-length/2,0) and a top point at (0,length/2,0). // we want to find the transform that takes these points to our desired start + end. Vector3 length = start - end; float l = length.Length(); Vector3 oldBottom = new Vector3(0, -l / 2, 0); Vector3 oldTop = new Vector3(0, l / 2, 0); s.Translate(oldBottom); e.Translate(oldTop); Vector3 oldLength = oldBottom - oldTop; Vector3 crossAxis = Vector3.Cross(Vector3.Normalize(oldLength), Vector3.Normalize(length)); ProceduralCylinder cylinder = new ProceduralCylinder(width, width, l, 20, 2); var finalShape = ProceduralShape.Combine(cylinder, s, e); if (crossAxis != Vector3.Zero) { float angleOfRotation = (float)MonoMathHelper.GetSignedAngleBetween2DVectors(oldLength, length); Matrix rotation = Matrix.CreateFromAxisAngle(Vector3.Normalize(crossAxis), angleOfRotation); finalShape.Transform(rotation); } finalShape.Translate(-translation); return(finalShape); }
public static ProceduralShape CapsuleCube() { var cube = new ProceduralCube(); var cyl1 = Capsule(new Vector3(-0.5f, 0.5f, 0.5f), new Vector3(0.5f, 0.5f, 0.5f), 0.01f); var cyl2 = Capsule(new Vector3(-0.5f, 0.5f, -0.5f), new Vector3(0.5f, 0.5f, -0.5f), 0.01f); var cyl3 = Capsule(new Vector3(-0.5f, 0.5f, 0.5f), new Vector3(-0.5f, 0.5f, -0.5f), 0.01f); var cyl4 = Capsule(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(0.5f, 0.5f, -0.5f), 0.01f); var cyl5 = Capsule(new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(0.5f, -0.5f, 0.5f), 0.01f); var cyl6 = Capsule(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0.5f, -0.5f, -0.5f), 0.01f); var cyl7 = Capsule(new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(-0.5f, -0.5f, -0.5f), 0.01f); var cyl8 = Capsule(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(0.5f, -0.5f, -0.5f), 0.01f); var cyl9 = Capsule(new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(-0.5f, 0.5f, 0.5f), 0.01f); var cyl10 = Capsule(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(-0.5f, 0.5f, -0.5f), 0.01f); var cyl11 = Capsule(new Vector3(0.5f, -0.5f, -0.5f), new Vector3(0.5f, 0.5f, -0.5f), 0.01f); var cyl12 = Capsule(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(0.5f, 0.5f, 0.5f), 0.01f); var finalShape = ProceduralShape.Combine(cube, cyl1, cyl2, cyl3, cyl4, cyl5, cyl6, cyl7, cyl8, cyl9, cyl10, cyl11, cyl12); return(finalShape); }
public void BuildGeometry() { var vertices = new VertexPositionColorTextureNormal[(heightMapSize * heightMapSize)]; int vertIndex = 0; List <int> topEdges = new List <int>(); List <int> bottomEdges = new List <int>(); List <int> leftEdges = new List <int>(); List <int> rightEdges = new List <int>(); for (float i = 0; i < heightMapSize; i++) { for (float j = 0; j < heightMapSize; j++) { var vert = new VertexPositionColorTextureNormal(); vert.Position = CalculateVertexPosition(i, j); vert.Texture = new Vector2(i * 2f / heightMapSize, j * 2f / heightMapSize); vert.Normal = normal; vert.Color = NodeColor; vertices[vertIndex] = vert; if (i == 0) { rightEdges.Add(vertIndex); } if (i == heightMapSize - 1) { leftEdges.Add(vertIndex); } if (j == 0) { bottomEdges.Add(vertIndex); } if (j == heightMapSize - 1) { topEdges.Add(vertIndex); } vertIndex++; } } var indices = GenerateIndices(); if (normal == Vector3.Up || normal == Vector3.Forward || normal == Vector3.Left) { indices = indices.Reverse().ToArray(); } List <NeighbourTracker.Connection> connections = this.Planet.GetNeighbours(this); if (connections != null && SystemWarGlobalSettings.RepairSeams) { var northConn = connections.Find(x => x.direction == NeighbourTracker.ConnectionDirection.north); var southConn = connections.Find(x => x.direction == NeighbourTracker.ConnectionDirection.south); var westConn = connections.Find(x => x.direction == NeighbourTracker.ConnectionDirection.west); var eastConn = connections.Find(x => x.direction == NeighbourTracker.ConnectionDirection.east); Sphereify(sphereSize, ref vertices); if (northConn != null) { CalculateEdgeAdjustment(northConn, ref vertices, topEdges, NeighbourTracker.ConnectionDirection.north); } if (southConn != null) { CalculateEdgeAdjustment(southConn, ref vertices, bottomEdges, NeighbourTracker.ConnectionDirection.south); } if (westConn != null) { CalculateEdgeAdjustment(westConn, ref vertices, leftEdges, NeighbourTracker.ConnectionDirection.west); } if (eastConn != null) { CalculateEdgeAdjustment(eastConn, ref vertices, rightEdges, NeighbourTracker.ConnectionDirection.east); } } else { Sphereify(sphereSize, ref vertices); } GenerateNormals(ref vertices, ref indices); var p = vertices.Select(x => x.Position).ToList(); boundingSphere = BoundingSphere.CreateFromPoints(p); ProceduralShape spherePatch = new ProceduralShape(vertices, indices); this.AddComponent(new RenderGeometryComponent(spherePatch)); meshCollider = new MobileMeshColliderComponent(this, spherePatch.GetVertices(), spherePatch.GetIndicesAsInt().ToArray()); AddComponent(meshCollider); renderComponent = new EffectRenderComponent(effect); renderComponent.DrawOrder = Planet.DrawOrder; this.AddComponent(renderComponent); SetHighPrecisionPosition(this); built = true; }