private Mesh GenerateFlatRightWall(float tableWidth, float tableHeight, RampVertex rv) { var invTableWidth = 1.0f / tableWidth; var invTableHeight = 1.0f / tableHeight; var numVertices = rv.VertexCount * 2; var numIndices = (rv.VertexCount - 1) * 6; var mesh = new Mesh("RightWall") { Vertices = new Vertex3DNoTex2[numVertices], Indices = new int[numIndices] }; for (var i = 0; i < rv.VertexCount; i++) { var rgv3d1 = new Vertex3DNoTex2(); var rgv3d2 = new Vertex3DNoTex2(); rgv3d1.X = rv.RgvLocal[i].X; rgv3d1.Y = rv.RgvLocal[i].Y; rgv3d1.Z = rv.PointHeights[i]; rgv3d2.X = rv.RgvLocal[i].X; rgv3d2.Y = rv.RgvLocal[i].Y; rgv3d2.Z = (rv.PointHeights[i] + _data.RightWallHeightVisible); if (_data.ImageAlignment == RampImageAlignment.ImageModeWorld) { rgv3d1.Tu = rgv3d1.X * invTableWidth; rgv3d1.Tv = rgv3d1.Y * invTableHeight; } else { rgv3d1.Tu = 0; rgv3d1.Tv = rv.PointRatios[i]; } rgv3d2.Tu = rgv3d1.Tu; rgv3d2.Tv = rgv3d1.Tv; mesh.Vertices[i * 2] = rgv3d1; mesh.Vertices[i * 2 + 1] = rgv3d2; if (i == rv.VertexCount - 1) { break; } mesh.Indices[i * 6] = i * 2; mesh.Indices[i * 6 + 1] = i * 2 + 1; mesh.Indices[i * 6 + 2] = i * 2 + 3; mesh.Indices[i * 6 + 3] = i * 2; mesh.Indices[i * 6 + 4] = i * 2 + 3; mesh.Indices[i * 6 + 5] = i * 2 + 2; } Mesh.ComputeNormals(mesh.Vertices, numVertices, mesh.Indices, (rv.VertexCount - 1) * 6); return(mesh); }
public RampVertex GetRampVertex(float tableHeight, float accuracy, bool incWidth) { var result = new RampVertex(); // vvertex are the 2D vertices forming the central curve of the ramp as seen from above var vertex = GetCentralCurve(accuracy); var numVertices = vertex.Length; result.VertexCount = numVertices; result.PointHeights = new float[numVertices]; result.Cross = new bool[numVertices]; result.PointRatios = new float[numVertices]; result.MiddlePoints = new Vertex2D[numVertices]; result.RgvLocal = new Vertex2D[_data.Type != RampType.RampTypeFlat ? (numVertices + 1) * 2 : numVertices * 2]; // Compute an approximation to the length of the central curve // by adding up the lengths of the line segments. var totalLength = 0f; var bottomHeight = _data.HeightBottom + tableHeight; var topHeight = _data.HeightTop + tableHeight; for (var i = 0; i < numVertices - 1; i++) { var v1 = vertex[i]; var v2 = vertex[i + 1]; var dx = v1.X - v2.X; var dy = v1.Y - v2.Y; var length = MathF.Sqrt(dx * dx + dy * dy); totalLength += length; } var currentLength = 0f; for (var i = 0; i < numVertices; i++) { // clamp next and prev as ramps do not loop var prev = vertex[i > 0 ? i - 1 : i]; var next = vertex[i < numVertices - 1 ? i + 1 : i]; var middle = vertex[i]; result.Cross[i] = middle.IsControlPoint; var normal = new Vertex2D(); // Get normal at this point // Notice that these values equal the ones in the line // equation and could probably be substituted by them. var v1Normal = new Vertex2D(prev.Y - middle.Y, middle.X - prev.X); // vector vmiddle-vprev rotated RIGHT var v2Normal = new Vertex2D(middle.Y - next.Y, next.X - middle.X); // vector vnext-vmiddle rotated RIGHT // special handling for beginning and end of the ramp, as ramps do not loop if (i == numVertices - 1) { v1Normal.Normalize(); normal = v1Normal; } else if (i == 0) { v2Normal.Normalize(); normal = v2Normal; } else { v1Normal.Normalize(); v2Normal.Normalize(); if (MathF.Abs(v1Normal.X - v2Normal.X) < 0.0001 && MathF.Abs(v1Normal.Y - v2Normal.Y) < 0.0001) { // Two parallel segments normal = v1Normal; } else { // Find intersection of the two edges meeting this points, but // shift those lines outwards along their normals // First line var a = prev.Y - middle.Y; var b = middle.X - prev.X; // Shift line along the normal var c = -(a * (prev.X - v1Normal.X) + b * (prev.Y - v1Normal.Y)); // Second line var d = next.Y - middle.Y; var e = middle.X - next.X; // Shift line along the normal var f = -(d * (next.X - v2Normal.X) + e * (next.Y - v2Normal.Y)); var det = a * e - b * d; var invDet = det != 0.0 ? 1.0f / det : 0.0f; var intersectX = (b * f - e * c) * invDet; var intersectY = (c * d - a * f) * invDet; normal.X = middle.X - intersectX; normal.Y = middle.Y - intersectY; } } // Update current length along the ramp. var dx = prev.X - middle.X; var dy = prev.Y - middle.Y; var length = MathF.Sqrt(dx * dx + dy * dy); currentLength += length; var percentage = currentLength / totalLength; var currentWidth = percentage * (_data.WidthTop - _data.WidthBottom) + _data.WidthBottom; result.PointHeights[i] = middle.Z + percentage * (topHeight - bottomHeight) + bottomHeight; AssignHeightToControlPoint(new Vertex2D(vertex[i].X, vertex[i].Y), middle.Z + percentage * (topHeight - bottomHeight) + bottomHeight); result.PointRatios[i] = 1.0f - percentage; // only change the width if we want to create vertices for rendering or for the editor // the collision engine uses flat type ramps if (IsHabitrail() && _data.Type != RampType.RampType1Wire) { currentWidth = _data.WireDistanceX; if (incWidth) { currentWidth += 20.0f; } } else if (_data.Type == RampType.RampType1Wire) { currentWidth = _data.WireDiameter; } result.MiddlePoints[i] = new Vertex2D(middle.X, middle.Y) + normal; result.RgvLocal[i] = new Vertex2D(middle.X, middle.Y) + currentWidth * 0.5f * normal; result.RgvLocal[numVertices * 2 - i - 1] = new Vertex2D(middle.X, middle.Y) - currentWidth * 0.5f * normal; } return(result); }
private Mesh GenerateFlatFloorMesh(Table.Table table, RampVertex rv) { var invTableWidth = 1.0f / table.Width; var invTableHeight = 1.0f / table.Height; var numVertices = rv.VertexCount * 2; var numIndices = (rv.VertexCount - 1) * 6; var mesh = new Mesh("Floor") { Vertices = new Vertex3DNoTex2[numVertices], Indices = new int[numIndices] }; for (var i = 0; i < rv.VertexCount; i++) { var rgv3d1 = new Vertex3DNoTex2(); var rgv3d2 = new Vertex3DNoTex2(); rgv3d1.X = rv.RgvLocal[i].X; rgv3d1.Y = rv.RgvLocal[i].Y; rgv3d1.Z = rv.PointHeights[i] * table.GetScaleZ(); rgv3d2.X = rv.RgvLocal[rv.VertexCount * 2 - i - 1].X; rgv3d2.Y = rv.RgvLocal[rv.VertexCount * 2 - i - 1].Y; rgv3d2.Z = rgv3d1.Z; if (_data.Image != null) { if (_data.ImageAlignment == RampImageAlignment.ImageModeWorld) { rgv3d1.Tu = rgv3d1.X * invTableWidth; rgv3d1.Tv = rgv3d1.Y * invTableHeight; rgv3d2.Tu = rgv3d2.X * invTableWidth; rgv3d2.Tv = rgv3d2.Y * invTableHeight; } else { rgv3d1.Tu = 1.0f; rgv3d1.Tv = rv.PointRatios[i]; rgv3d2.Tu = 0.0f; rgv3d2.Tv = rv.PointRatios[i]; } } else { rgv3d1.Tu = 0.0f; rgv3d1.Tv = 0.0f; rgv3d2.Tu = 0.0f; rgv3d2.Tv = 0.0f; } mesh.Vertices[i * 2] = rgv3d1; mesh.Vertices[i * 2 + 1] = rgv3d2; if (i == rv.VertexCount - 1) { break; } mesh.Indices[i * 6] = i * 2; mesh.Indices[i * 6 + 1] = i * 2 + 1; mesh.Indices[i * 6 + 2] = i * 2 + 3; mesh.Indices[i * 6 + 3] = i * 2; mesh.Indices[i * 6 + 4] = i * 2 + 3; mesh.Indices[i * 6 + 5] = i * 2 + 2; } Mesh.ComputeNormals(mesh.Vertices, numVertices, mesh.Indices, (rv.VertexCount - 1) * 6); return(mesh); }