protected static int ToPointsBuffer ( Point point, out DB3D.VertexFormatBits vertexFormatBits, out DB3D.VertexBuffer vb, out int vertexCount, out DB3D.IndexBuffer ib ) { int pointsCount = 0; if (point.Location.IsValid) { pointsCount = 1; vertexCount = 1; vertexFormatBits = DB3D.VertexFormatBits.Position; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPosition.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPosition.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPosition()) { vstream.AddVertex(new DB3D.VertexPosition(RawEncoder.AsXYZ(point.Location))); } vb.Unmap(); ib = IndexPointsBuffer(pointsCount); } else { vertexFormatBits = 0; vb = null; vertexCount = 0; ib = null; } return(pointsCount); }
public virtual bool Regen() { if (geometry != null) { if (!BeginRegen()) { return(false); } if (geometry.IsValid) { if (geometry is Mesh mesh) { vertexBuffer = ToVertexBuffer(mesh, part, out vertexFormatBits); vertexCount = part.VertexCount; triangleBuffer = ToTrianglesBuffer(mesh, part, out triangleCount); linesBuffer = ToEdgeBuffer(mesh, part, out linesCount); } else if (geometry is Curve curve) { using (var polyline = curve.ToPolyline(Revit.ShortCurveTolerance * Revit.ModelUnits, Revit.AngleTolerance * 100.0, Revit.ShortCurveTolerance * Revit.ModelUnits, 0.0)) { var pline = polyline.ToPolyline(); // Reduce too complex polylines. { var tol = Revit.VertexTolerance * Revit.ModelUnits; while (pline.Count > 0x4000) { tol *= 2.0; if (pline.ReduceSegments(tol) == 0) { break; } } } linesCount = ToPolylineBuffer(pline, out vertexFormatBits, out vertexBuffer, out vertexCount, out linesBuffer); } } else if (geometry is Point point) { linesCount = -ToPointsBuffer(point, out vertexFormatBits, out vertexBuffer, out vertexCount, out linesBuffer); } else if (geometry is PointCloud pointCloud) { linesCount = -ToPointsBuffer(pointCloud, part, out vertexFormatBits, out vertexBuffer, out vertexCount, out linesBuffer); } vertexFormat = new DB3D.VertexFormat(vertexFormatBits); } geometry = null; EndRegen(); } return(true); }
void IDisposable.Dispose() { effectInstance?.Dispose(); effectInstance = null; if (linesBuffer != indexLinesBuffer && linesBuffer != indexPointsBuffer) { linesBuffer?.Dispose(); } linesBuffer = null; linesCount = 0; triangleBuffer?.Dispose(); triangleBuffer = null; triangleCount = 0; vertexFormat?.Dispose(); vertexFormat = null; vertexBuffer?.Dispose(); vertexBuffer = null; vertexCount = 0; }
protected static int ToPolylineBuffer ( Polyline polyline, out DB3D.VertexFormatBits vertexFormatBits, out DB3D.VertexBuffer vb, out int vertexCount, out DB3D.IndexBuffer ib ) { int linesCount = 0; if (polyline.SegmentCount > 0) { linesCount = polyline.SegmentCount; vertexCount = polyline.Count; vertexFormatBits = DB3D.VertexFormatBits.Position; vb = new DB3D.VertexBuffer(vertexCount * DB3D.VertexPosition.GetSizeInFloats()); vb.Map(vertexCount * DB3D.VertexPosition.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPosition()) { foreach (var v in polyline) { vstream.AddVertex(new DB3D.VertexPosition(RawEncoder.AsXYZ(v))); } } vb.Unmap(); ib = IndexLinesBuffer(vertexCount); } else { vertexFormatBits = 0; vb = null; vertexCount = 0; ib = null; } return(linesCount); }
protected static int ToPointsBuffer ( PointCloud pointCloud, Primitive.Part part, out DB3D.VertexFormatBits vertexFormatBits, out DB3D.VertexBuffer vb, out int vertexCount, out DB3D.IndexBuffer ib ) { int pointsCount = part.VertexCount; int normalCount = pointCloud.ContainsNormals ? pointsCount : 0; int colorsCount = pointCloud.ContainsColors ? pointsCount : 0; bool hasPoints = pointsCount > 0; bool hasNormals = normalCount == pointsCount; bool hasColors = colorsCount == pointsCount; if (hasPoints) { if (hasNormals) { if (hasColors) { vertexFormatBits = DB3D.VertexFormatBits.PositionNormalColored; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPositionNormalColored.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPositionNormalColored.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPositionNormalColored()) { for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p) { var point = pointCloud[p]; var c = new DB.ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A); vstream.AddVertex(new DB3D.VertexPositionNormalColored(RawEncoder.AsXYZ(point.Location), RawEncoder.AsXYZ(point.Normal), c)); } } vb.Unmap(); } else { vertexFormatBits = DB3D.VertexFormatBits.PositionNormal; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPositionNormal.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPositionNormal.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPositionNormal()) { for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p) { var point = pointCloud[p]; vstream.AddVertex(new DB3D.VertexPositionNormal(RawEncoder.AsXYZ(point.Location), RawEncoder.AsXYZ(point.Normal))); } } vb.Unmap(); } } else { if (hasColors) { vertexFormatBits = DB3D.VertexFormatBits.PositionColored; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPositionColored.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPositionColored.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPositionColored()) { for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p) { var point = pointCloud[p]; var c = new DB.ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A); vstream.AddVertex(new DB3D.VertexPositionColored(RawEncoder.AsXYZ(point.Location), c)); } } vb.Unmap(); } else { vertexFormatBits = DB3D.VertexFormatBits.Position; vb = new DB3D.VertexBuffer(pointsCount * DB3D.VertexPosition.GetSizeInFloats()); vb.Map(pointsCount * DB3D.VertexPosition.GetSizeInFloats()); using (var vstream = vb.GetVertexStreamPosition()) { for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p) { var point = pointCloud[p]; vstream.AddVertex(new DB3D.VertexPosition(RawEncoder.AsXYZ(point.Location))); } } vb.Unmap(); } } ib = IndexPointsBuffer(pointsCount); } else { vertexFormatBits = 0; vb = null; ib = null; } vertexCount = pointsCount; return(pointsCount); }
protected static DB3D.VertexBuffer ToVertexBuffer ( Mesh mesh, Primitive.Part part, out DB3D.VertexFormatBits vertexFormatBits, System.Drawing.Color color = default ) { int verticesCount = part.EndVertexIndex - part.StartVertexIndex; int normalCount = mesh.Normals.Count == mesh.Vertices.Count ? verticesCount : 0; int colorsCount = color.IsEmpty ? (mesh.VertexColors.Count == mesh.Vertices.Count ? verticesCount : 0) : verticesCount; bool hasVertices = verticesCount > 0; bool hasNormals = normalCount > 0; bool hasColors = colorsCount > 0; if (hasVertices) { var vertices = mesh.Vertices; if (hasNormals) { var normals = mesh.Normals; if (hasColors) { vertexFormatBits = DB3D.VertexFormatBits.PositionNormalColored; var colors = mesh.VertexColors; var vb = new DB3D.VertexBuffer(verticesCount * DB3D.VertexPositionNormalColored.GetSizeInFloats()); vb.Map(verticesCount * DB3D.VertexPositionNormalColored.GetSizeInFloats()); using (var stream = vb.GetVertexStreamPositionNormalColored()) { for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v) { var c = !color.IsEmpty ? color : colors[v]; uint T = Math.Max(1, 255u - c.A); stream.AddVertex(new DB3D.VertexPositionNormalColored(RawEncoder.AsXYZ(vertices[v]), RawEncoder.AsXYZ(normals[v]), new DB.ColorWithTransparency(c.R, c.G, c.B, T))); } } vb.Unmap(); return(vb); } else { vertexFormatBits = DB3D.VertexFormatBits.PositionNormal; var sizeInFloats = DB3D.VertexPositionNormal.GetSizeInFloats(); var vb = new DB3D.VertexBuffer(verticesCount * sizeInFloats); vb.Map(verticesCount * sizeInFloats); using (var stream = vb.GetVertexStreamPositionNormal()) { for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v) { stream.AddVertex(new DB3D.VertexPositionNormal(RawEncoder.AsXYZ(vertices[v]), RawEncoder.AsXYZ(normals[v]))); } } vb.Unmap(); return(vb); } } else { if (hasColors) { vertexFormatBits = DB3D.VertexFormatBits.PositionColored; var colors = mesh.VertexColors; var vb = new DB3D.VertexBuffer(verticesCount * DB3D.VertexPositionColored.GetSizeInFloats()); vb.Map(verticesCount * DB3D.VertexPositionColored.GetSizeInFloats()); using (var stream = vb.GetVertexStreamPositionColored()) { for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v) { var c = !color.IsEmpty ? color : colors[v]; uint T = Math.Max(1, 255u - c.A); stream.AddVertex(new DB3D.VertexPositionColored(RawEncoder.AsXYZ(vertices[v]), new DB.ColorWithTransparency(c.R, c.G, c.B, T))); } } vb.Unmap(); return(vb); } else { vertexFormatBits = DB3D.VertexFormatBits.Position; var vb = new DB3D.VertexBuffer(verticesCount * DB3D.VertexPosition.GetSizeInFloats()); vb.Map(verticesCount * DB3D.VertexPosition.GetSizeInFloats()); using (var stream = vb.GetVertexStreamPosition()) { for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v) { stream.AddVertex(new DB3D.VertexPosition(RawEncoder.AsXYZ(vertices[v]))); } } vb.Unmap(); return(vb); } } } vertexFormatBits = 0; return(null); }