/// <summary> /// Builds a cube of 4 vertices and a defined height. /// </summary> public static BuiltVerticesRange BuildCube(this GeometrySurface target, Vector3 topA, Vector3 topB, Vector3 topC, Vector3 topD, float height) { var result = new BuiltVerticesRange(target.Owner) { StartVertex = target.Owner.CountVertices }; var startTriangleIndex = target.CountTriangles; // Calculate texture coordinates var size = new Vector3( (topB - topA).Length(), Math.Abs(height), (topC - topB).Length()); var texX = 1f; var texY = 1f; var texZ = 1f; if (target.IsTextureTileModeEnabled(out var tileSize)) { texX = size.X / tileSize.X; texY = size.Y / tileSize.Y; texZ = size.Z / tileSize.X; } // Calculate bottom vectors var bottomA = new Vector3(topA.X, topA.Y - height, topA.Z); var bottomB = new Vector3(topB.X, topB.Y - height, topB.Z); var bottomC = new Vector3(topC.X, topC.Y - height, topC.Z); var bottomD = new Vector3(topD.X, topD.Y - height, topD.Z); // Build Top side var vertex = new VertexBasic(topA, new Vector2(texX, 0f), new Vector3(0f, 1f, 0f)); var a = target.Owner.AddVertex(vertex); var b = target.Owner.AddVertex(vertex.Copy(topB, new Vector2(texX, texY))); var c = target.Owner.AddVertex(vertex.Copy(topC, new Vector2(0f, texY))); var d = target.Owner.AddVertex(vertex.Copy(topD, new Vector2(0f, 0f))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); // Build Bottom side vertex = new VertexBasic(topA, new Vector2(0f, 0f), new Vector3(0f, -1f, 0f)); a = target.Owner.AddVertex(vertex); b = target.Owner.AddVertex(vertex.Copy(topD, new Vector2(texX, 0f))); c = target.Owner.AddVertex(vertex.Copy(topC, new Vector2(texX, texY))); d = target.Owner.AddVertex(vertex.Copy(topB, new Vector2(0f, texY))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); // Build Front side vertex = new VertexBasic(topA, new Vector2(0f, texY), new Vector3(0f, 0f, -1f)); a = target.Owner.AddVertex(vertex); b = target.Owner.AddVertex(vertex.Copy(topB, new Vector2(texX, texY))); c = target.Owner.AddVertex(vertex.Copy(bottomB, new Vector2(texX, 0f))); d = target.Owner.AddVertex(vertex.Copy(bottomA, new Vector2(0f, 0f))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); // Build Right side a = target.Owner.AddVertex(vertex.Copy(topB, new Vector3(1f, 0f, 0f), new Vector2(0f, texY))); b = target.Owner.AddVertex(vertex.Copy(topC, new Vector3(1f, 0f, 0f), new Vector2(texZ, texY))); c = target.Owner.AddVertex(vertex.Copy(bottomC, new Vector3(1f, 0f, 0f), new Vector2(texZ, 0f))); d = target.Owner.AddVertex(vertex.Copy(bottomB, new Vector3(1f, 0f, 0f), new Vector2(0f, 0f))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); // Build Back side a = target.Owner.AddVertex(vertex.Copy(topC, new Vector3(0f, 0f, 1f), new Vector2(0f, texY))); b = target.Owner.AddVertex(vertex.Copy(topD, new Vector3(0f, 0f, 1f), new Vector2(texX, texY))); c = target.Owner.AddVertex(vertex.Copy(bottomD, new Vector3(0f, 0f, 1f), new Vector2(texX, 0f))); d = target.Owner.AddVertex(vertex.Copy(bottomC, new Vector3(0f, 0f, 1f), new Vector2(0f, 0f))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); // Build Left side a = target.Owner.AddVertex(vertex.Copy(topD, new Vector3(-1f, 0f, 0f), new Vector2(0f, texY))); b = target.Owner.AddVertex(vertex.Copy(topA, new Vector3(-1f, 0f, 0f), new Vector2(texZ, texY))); c = target.Owner.AddVertex(vertex.Copy(bottomA, new Vector3(-1f, 0f, 0f), new Vector2(texZ, 0f))); d = target.Owner.AddVertex(vertex.Copy(bottomD, new Vector3(-1f, 0f, 0f), new Vector2(0f, 0f))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); // Calculate normals finally target.CalculateNormalsFlat(startTriangleIndex, target.CountTriangles - startTriangleIndex); result.VertexCount = target.Owner.CountVertices - result.StartVertex; return(result); }
/// <summary> /// Builds a cone into the geometry with correct texture coordinates and normals. /// </summary> /// <param name="target">Target <see cref="GeometrySurface"/>.</param> /// <param name="radius">The radius of the cone.</param> /// <param name="height">The height of the cone.</param> /// <param name="countOfSegments">Total count of segments to generate.</param> public static BuiltVerticesRange BuildCone(this GeometrySurface target, float radius, float height, int countOfSegments) { var startVertex = target.Owner.CountVertices; radius = Math.Max(EngineMath.TOLERANCE_FLOAT_POSITIVE, radius); height = Math.Max(EngineMath.TOLERANCE_FLOAT_POSITIVE, height); countOfSegments = Math.Max(3, countOfSegments); var diameter = radius * 2f; // Get texture offsets var texX = 1f; var texY = 1f; if (target.IsTextureTileModeEnabled(out var tileSize)) { texX = diameter / tileSize.X; texY = diameter / tileSize.Y; } // Specify bottom and top middle coordinates var bottomCoordinate = new Vector3(0f, -(height / 2f), 0f); var topCoordinate = new Vector3(bottomCoordinate.X, bottomCoordinate.Y + height, bottomCoordinate.Z); // Create bottom and top vertices var bottomVertex = new VertexBasic(bottomCoordinate, new Vector2(texX / 2f, texY / 2f), new Vector3(0f, -1f, 0f)); // AddObject bottom and top vertices to the geometry var bottomVertexIndex = target.Owner.AddVertex(bottomVertex); // Generate all segments var countOfSegmentsF = (float)countOfSegments; for (var loop = 0; loop < countOfSegments; loop++) { // Calculate rotation values for each segment border var startRadian = EngineMath.RAD_360DEG * (loop / countOfSegmentsF); var targetRadian = EngineMath.RAD_360DEG * ((loop + 1) / countOfSegmentsF); var normalRadian = startRadian + (targetRadian - startRadian) / 2f; // Generate all normals var sideNormal = Vector3Ex.NormalFromHVRotation(normalRadian, 0f); var sideLeftNormal = Vector3Ex.NormalFromHVRotation(startRadian, 0f); var sideRightNormal = Vector3Ex.NormalFromHVRotation(targetRadian, 0f); //Generate all points var sideLeftBottomCoord = bottomCoordinate + sideLeftNormal * radius; var sideRightBottomCoord = bottomCoordinate + sideRightNormal * radius; var sideMiddleBottomCoord = bottomCoordinate + sideNormal * radius; var sideLeftTexCoord = new Vector2( texX / (diameter / (sideLeftBottomCoord.X + radius)), texY / (diameter / (sideLeftBottomCoord.Z + radius))); var sideRightTexCoord = new Vector2( texX / (diameter / (sideRightBottomCoord.X + radius)), texY / (diameter / (sideRightBottomCoord.Z + radius))); //AddObject segment bottom triangle var segmentBottomLeft = bottomVertex.Copy(sideLeftBottomCoord, sideLeftTexCoord); var segmentBottomRight = bottomVertex.Copy(sideRightBottomCoord, sideRightTexCoord); target.AddTriangle( bottomVertexIndex, target.Owner.AddVertex(segmentBottomLeft), target.Owner.AddVertex(segmentBottomRight)); //Generate side normal var vectorToTop = topCoordinate - sideMiddleBottomCoord; var vectorToTopRotation = Vector3Ex.ToHVRotation(vectorToTop); vectorToTopRotation.Y = vectorToTopRotation.Y + EngineMath.RAD_90DEG; var topSideNormal = Vector3Ex.NormalFromHVRotation(vectorToTopRotation); //AddObject segment top triangle var topVertex = new VertexBasic(topCoordinate, new Vector2(texX / 2f, texY / 2f), topSideNormal); var segmentTopLeft = topVertex.Copy(sideLeftBottomCoord, sideLeftTexCoord); var segmentTopRight = topVertex.Copy(sideRightBottomCoord, sideRightTexCoord); target.AddTriangle(target.Owner.AddVertex(topVertex), target.Owner.AddVertex(segmentTopRight), target.Owner.AddVertex(segmentTopLeft)); } return(new BuiltVerticesRange(target.Owner, startVertex, target.Owner.CountVertices - startVertex)); }
/// <summary> /// Builds cube sides into this Geometry (these sides are built up of 16 vertices, so texture coordinates and normals are set) /// </summary> /// <param name="target">Target <see cref="GeometrySurface"/>.</param> /// <param name="start">Start point of the cube</param> /// <param name="size">Size of the cube</param> public static BuiltVerticesRange BuildCubeSides(this GeometrySurface target, Vector3 start, Vector3 size) { var result = new BuiltVerticesRange(target.Owner) { StartVertex = target.Owner.CountVertices }; var dest = start + size; var texX = 1f; var texY = 1f; var texZ = 1f; if (target.IsTextureTileModeEnabled(out var tileSize)) { texX = size.X / tileSize.X; texY = size.Y / tileSize.Y; texZ = size.Z / tileSize.X; } //Front side var vertex = new VertexBasic(start, new Vector2(0f, texY), new Vector3(0f, 0f, -1f)); var a = target.Owner.AddVertex(vertex); var b = target.Owner.AddVertex(vertex.Copy(new Vector3(dest.X, start.Y, start.Z), new Vector2(texX, texY))); var c = target.Owner.AddVertex(vertex.Copy(new Vector3(dest.X, dest.Y, start.Z), new Vector2(texX, 0f))); var d = target.Owner.AddVertex(vertex.Copy(new Vector3(start.X, dest.Y, start.Z), new Vector2(0f, 0f))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); //Right side a = target.Owner.AddVertex(vertex.Copy(new Vector3(dest.X, start.Y, start.Z), new Vector3(1f, 0f, 0f), new Vector2(0f, texY))); b = target.Owner.AddVertex(vertex.Copy(new Vector3(dest.X, start.Y, dest.Z), new Vector3(1f, 0f, 0f), new Vector2(texZ, texY))); c = target.Owner.AddVertex(vertex.Copy(new Vector3(dest.X, dest.Y, dest.Z), new Vector3(1f, 0f, 0f), new Vector2(texZ, 0f))); d = target.Owner.AddVertex(vertex.Copy(new Vector3(dest.X, dest.Y, start.Z), new Vector3(1f, 0f, 0f), new Vector2(0f, 0f))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); //Back side a = target.Owner.AddVertex(vertex.Copy(new Vector3(dest.X, start.Y, dest.Z), new Vector3(0f, 0f, 1f), new Vector2(0f, texY))); b = target.Owner.AddVertex(vertex.Copy(new Vector3(start.X, start.Y, dest.Z), new Vector3(0f, 0f, 1f), new Vector2(texX, texY))); c = target.Owner.AddVertex(vertex.Copy(new Vector3(start.X, dest.Y, dest.Z), new Vector3(0f, 0f, 1f), new Vector2(texX, 0f))); d = target.Owner.AddVertex(vertex.Copy(new Vector3(dest.X, dest.Y, dest.Z), new Vector3(0f, 0f, 1f), new Vector2(0f, 0f))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); //Left side a = target.Owner.AddVertex(vertex.Copy(new Vector3(start.X, start.Y, dest.Z), new Vector3(-1f, 0f, 0f), new Vector2(0f, texY))); b = target.Owner.AddVertex(vertex.Copy(new Vector3(start.X, start.Y, start.Z), new Vector3(-1f, 0f, 0f), new Vector2(texZ, texY))); c = target.Owner.AddVertex(vertex.Copy(new Vector3(start.X, dest.Y, start.Z), new Vector3(-1f, 0f, 0f), new Vector2(texZ, 0f))); d = target.Owner.AddVertex(vertex.Copy(new Vector3(start.X, dest.Y, dest.Z), new Vector3(-1f, 0f, 0f), new Vector2(0f, 0f))); target.AddTriangle(a, c, b); target.AddTriangle(a, d, c); result.VertexCount = target.Owner.CountVertices - result.StartVertex; return(result); }
/// <summary> /// Builds a cylinder into the geometry with correct texture coordinates and normals. /// </summary> /// <param name="target">Target <see cref="GeometrySurface"/>.</param> /// <param name="bottomMiddle">Coordinate of bottom middle.</param> /// <param name="radius">The radius of the cylinder.</param> /// <param name="height">The height of the cylinder.</param> /// <param name="countOfSegments">Total count of segments to generate.</param> /// <param name="buildBottom">Build bottom of the cylinder.</param> /// <param name="buildSides">Build sides of the cylinder.</param> /// <param name="buildTop">Build top side of the cylinder.</param> public static BuiltVerticesRange BuildCylinder( this GeometrySurface target, Vector3 bottomMiddle, float radius, float height, int countOfSegments, bool buildSides, bool buildBottom, bool buildTop) { var startVertex = target.Owner.CountVertices; radius = Math.Max(EngineMath.TOLERANCE_FLOAT_POSITIVE, radius); height = Math.Max(EngineMath.TOLERANCE_FLOAT_POSITIVE, height); countOfSegments = Math.Max(3, countOfSegments); var diameter = radius * 2f; // Get texture offsets var texX = 1f; var texY = 1f; var texSegmentY = 1f; var texSegmentX = 1f; if (target.IsTextureTileModeEnabled(out var tileSize)) { texX = diameter / tileSize.X; texY = diameter / tileSize.Y; texSegmentY = height / tileSize.Y; texSegmentX = EngineMath.RAD_180DEG * diameter / tileSize.X; } // Specify bottom and top middle coordinates var bottomCoordinate = bottomMiddle; var topCoordinate = new Vector3(bottomMiddle.X, bottomMiddle.Y + height, bottomMiddle.Z); // Create bottom and top vertices var bottomVertex = new VertexBasic(bottomCoordinate, new Vector2(texX / 2f, texY / 2f), new Vector3(0f, -1f, 0f)); var topVertex = new VertexBasic(topCoordinate, new Vector2(texX / 2f, texY / 2f), new Vector3(0f, 1f, 0f)); // AddObject bottom and top vertices to the geometry var bottomVertexIndex = target.Owner.AddVertex(bottomVertex); var topVertexIndex = target.Owner.AddVertex(topVertex); // Generate all segments var fullRadian = EngineMath.RAD_360DEG; var countOfSegmentsF = (float)countOfSegments; for (var loop = 0; loop < countOfSegments; loop++) { // Calculate rotation values for each segment border var startRadian = fullRadian * (loop / countOfSegmentsF); var targetRadian = fullRadian * ((loop + 1) / countOfSegmentsF); var normalRadian = startRadian + (targetRadian - startRadian) / 2f; // Generate all normals var sideNormal = Vector3Ex.NormalFromHVRotation(normalRadian, 0f); var sideLeftNormal = Vector3Ex.NormalFromHVRotation(startRadian, 0f); var sideRightNormal = Vector3Ex.NormalFromHVRotation(targetRadian, 0f); // var sideLeftTexCoord = new Vector2(0.5f + sideLeftNormal.X * radius, 0.5f + sideLeftNormal.Z * radius); var sideRightTexCoord = new Vector2(0.5f + sideRightNormal.X * radius, 0.5f + sideRightNormal.Z * radius); // Generate all points var sideLeftBottomCoord = bottomCoordinate + sideLeftNormal * radius; var sideRightBottomCoord = bottomCoordinate + sideRightNormal * radius; var sideLeftTopCoord = new Vector3(sideLeftBottomCoord.X, sideLeftBottomCoord.Y + height, sideLeftBottomCoord.Z); var sideRightTopCoord = new Vector3(sideRightBottomCoord.X, sideRightBottomCoord.Y + height, sideRightBottomCoord.Z); // AddObject segment bottom triangle if (buildBottom) { var segmentBottomLeft = bottomVertex.Copy(sideLeftBottomCoord, sideLeftTexCoord); var segmentBottomRight = bottomVertex.Copy(sideRightBottomCoord, sideRightTexCoord); target.AddTriangle( bottomVertexIndex, target.Owner.AddVertex(segmentBottomLeft), target.Owner.AddVertex(segmentBottomRight)); } // AddObject segment top triangle if (buildTop) { var segmentTopLeft = topVertex.Copy(sideLeftTopCoord, sideLeftTexCoord); var segmentTopRight = topVertex.Copy(sideRightTopCoord, sideRightTexCoord); target.AddTriangle( topVertexIndex, target.Owner.AddVertex(segmentTopRight), target.Owner.AddVertex(segmentTopLeft)); } if (buildSides) { // Calculate texture coords for side segment var texCoordSegmentStart = new Vector2(texSegmentX * (loop / (float)countOfSegments), 0f); var texCoordSegmentTarget = new Vector2(texSegmentX * ((loop + 1) / (float)countOfSegments), texSegmentY); // AddObject segment side target.BuildRect(sideLeftBottomCoord, sideRightBottomCoord, sideRightTopCoord, sideLeftTopCoord, sideNormal, texCoordSegmentStart, texCoordSegmentTarget); } } return(new BuiltVerticesRange(target.Owner, startVertex, target.Owner.CountVertices - startVertex)); }