예제 #1
0
 public TriangleInfo(Triangle3d triangle, XYZ normal, ColorWithTransparency color, XYZ _offset)
 {
     this.Triangle         = triangle;
     Normal                = normal;
     ColorWithTransparency = color;
     this.Offset           = _offset;
 }
예제 #2
0
        public void AddFaceToBuffer
            (Face _face,
            XYZ _normal,
            ColorWithTransparency _cwt,
            XYZ _offset,
            bool _transparent)
        {
            Mesh     mesh               = _face.Triangulate();
            var      transFaceBuffer    = this.TransFaceBuffer;
            var      nonTransFaceBuffer = this.NonTransFaceBuffer;
            MeshInfo meshInfo           = new MeshInfo(mesh, _normal, _cwt, _offset);

            if (_transparent)
            {
                transFaceBuffer.Meshes.Add(meshInfo);
                transFaceBuffer.VertexBufferCount += mesh.Vertices.Count;
                transFaceBuffer.PrimitiveCount    += mesh.NumTriangles;
            }
            else
            {
                nonTransFaceBuffer.Meshes.Add(meshInfo);
                nonTransFaceBuffer.VertexBufferCount += mesh.Vertices.Count;
                nonTransFaceBuffer.PrimitiveCount    += mesh.NumTriangles;
            }
        }
예제 #3
0
 public MeshInfo(Mesh mesh, XYZ normal, ColorWithTransparency color, XYZ _offset)
 {
     Mesh   = mesh;
     Normal = normal;
     ColorWithTransparency = color;
     this.Offset           = _offset;
 }
예제 #4
0
        public void AddCurveToBuffer
            (Curve _curve,
            ColorWithTransparency _color,
            XYZ _offset)
        {
            var xyzs = _curve.Tessellate();

            AddTessellatedCurveToBuffer(xyzs, _color, _offset);
        }
예제 #5
0
        public void AddPointToBuffer
            (XYZ _point,
            ColorWithTransparency _color,
            XYZ _offset)
        {
            var pointinfo = new PointInfo(_point, _color, _offset);
            var buffer    = this.PointBuffer;

            buffer.Points.Add(pointinfo);
            buffer.VertexBufferCount++;
            buffer.PrimitiveCount++;
        }
예제 #6
0
 public void AddSolidToBuffer
     (Solid _solid,
     ColorWithTransparency _cwt,
     XYZ _offset,
     bool _transparent)
 {
     foreach (Face face in _solid.Faces)
     {
         var normal = face.Evaluate(new UV(0, 0));
         AddFaceToBuffer(face, normal, _cwt, _offset, _transparent);
     }
 }
예제 #7
0
        public void AddEdgeToBuffer
            (Edge _edge,
            ColorWithTransparency _color,
            XYZ _offset)
        {
            var xyzs       = _edge.Tessellate();
            var edgeInfo   = new EdgeInfo(xyzs, _color, _offset);
            var edgeBuffer = this.EdgeBuffer;

            edgeBuffer.Edges.Add(edgeInfo);
            edgeBuffer.VertexBufferCount += xyzs.Count;
            edgeBuffer.PrimitiveCount    += xyzs.Count - 1;
        }
예제 #8
0
        public void AddTessellatedCurveToBuffer
            (IList <XYZ> _vertices,
            ColorWithTransparency _color,
            XYZ _offset)
        {
            var xyzs       = _vertices;
            var edgeInfo   = new EdgeInfo(xyzs, _color, _offset);
            var edgeBuffer = this.EdgeBuffer;

            edgeBuffer.Edges.Add(edgeInfo);
            edgeBuffer.VertexBufferCount += xyzs.Count;
            edgeBuffer.PrimitiveCount    += xyzs.Count - 1;
        }
예제 #9
0
        public void AddTriangleToBuffer
            (g3.Triangle3d _triangle,
            XYZ _normal,
            ColorWithTransparency _cwt,
            XYZ _offset,
            bool _transparent)
        {
            var          transBuffer    = this.TransTriangleBuffer;
            var          nonTransBuffer = this.NonTransTriangleBuffer;
            TriangleInfo triangleInfo   = new TriangleInfo(_triangle, _normal, _cwt, _offset);

            if (_transparent)
            {
                transBuffer.Triangles.Add(triangleInfo);
                transBuffer.VertexBufferCount += 3;
                transBuffer.PrimitiveCount    += 1;
            }
            else
            {
                nonTransBuffer.Triangles.Add(triangleInfo);
                nonTransBuffer.VertexBufferCount += 3;
                nonTransBuffer.PrimitiveCount    += 1;
            }
        }
예제 #10
0
        private static RenderData ProcessPrimitive(ILogger logger, glTFLoader.Schema.MeshPrimitive primitive, Gltf gltf, byte[][] buffers, DisplayStyle displayStyle, Outline outline, Transform transform)
        {
            if (primitive.Mode != MeshPrimitive.ModeEnum.TRIANGLES)
            {
                logger.Debug("The selected primitive mode is not supported.");
                return(null);
            }

            var indexAccessor    = gltf.Accessors[(int)primitive.Indices];
            var positionAccessor = gltf.Accessors[primitive.Attributes["POSITION"]];
            var normalAccessor   = gltf.Accessors[primitive.Attributes["NORMAL"]];
            var hasColor         = primitive.Attributes.ContainsKey("COLOR_0");

            var indexBufferView    = gltf.BufferViews[(int)indexAccessor.BufferView];
            var positionBufferView = gltf.BufferViews[(int)positionAccessor.BufferView];
            var normalBufferView   = gltf.BufferViews[(int)normalAccessor.BufferView];

            var indices = new List <int>();

            for (var i = indexBufferView.ByteOffset; i < indexBufferView.ByteOffset + indexBufferView.ByteLength; i += indexBufferView.ByteStride ?? sizeof(ushort))
            {
                var index = BitConverter.ToUInt16(buffers[indexBufferView.Buffer], i);
                indices.Add(index);
            }

            var floatSize = sizeof(float);
            var positions = new List <XYZ>();

            for (var i = positionBufferView.ByteOffset; i < positionBufferView.ByteOffset + positionBufferView.ByteLength; i += positionBufferView.ByteStride ?? (floatSize * 3))
            {
                // Read x, y, z values
                var x  = BitConverter.ToSingle(buffers[positionBufferView.Buffer], i);
                var y  = BitConverter.ToSingle(buffers[positionBufferView.Buffer], i + floatSize);
                var z  = BitConverter.ToSingle(buffers[positionBufferView.Buffer], i + floatSize * 2);
                var pt = new XYZ(Elements.Units.MetersToFeet(x), Elements.Units.MetersToFeet(y), Elements.Units.MetersToFeet(z));
                if (transform != null)
                {
                    pt = transform.OfPoint(pt);
                }
                outline.AddPoint(pt);
                positions.Add(pt);
            }

            var normals = new List <XYZ>();

            for (var i = normalBufferView.ByteOffset; i < normalBufferView.ByteOffset + normalBufferView.ByteLength; i += normalBufferView.ByteStride ?? (floatSize * 3))
            {
                // Read x, y, z values
                var x = BitConverter.ToSingle(buffers[normalBufferView.Buffer], i);
                var y = BitConverter.ToSingle(buffers[normalBufferView.Buffer], i + floatSize);
                var z = BitConverter.ToSingle(buffers[normalBufferView.Buffer], i + floatSize * 2);
                var n = new XYZ(x, y, z);
                if (transform != null)
                {
                    n = transform.OfVector(n);
                }
                normals.Add(n);
            }

            var colors = new List <ColorWithTransparency>();

            if (hasColor)
            {
                var colorAccessor   = gltf.Accessors[primitive.Attributes["COLOR_0"]];
                var colorBufferView = gltf.BufferViews[(int)colorAccessor.BufferView];
                for (var i = colorBufferView.ByteOffset; i < colorBufferView.ByteOffset + colorBufferView.ByteLength; i += colorBufferView.ByteStride ?? (floatSize * 3))
                {
                    // Read x, y, z values
                    var r = BitConverter.ToSingle(buffers[colorBufferView.Buffer], i);
                    var g = BitConverter.ToSingle(buffers[colorBufferView.Buffer], i + floatSize);
                    var b = BitConverter.ToSingle(buffers[colorBufferView.Buffer], i + floatSize * 2);
                    colors.Add(displayStyle == DisplayStyle.HLR ? new ColorWithTransparency(255, 255, 255, 0) : new ColorWithTransparency((uint)(r * 255), (uint)(g * 255), (uint)(b * 255), 0));
                }
            }

            // The number of vertices will be the same as the length of the indices
            // because we'll duplicate vertices at every position.
            var numVertices   = indices.Count;
            var pType         = PrimitiveType.TriangleList;
            var numPrimitives = indices.Count / 3;
            var numIndices    = GetPrimitiveSize(pType) * numPrimitives;

            VertexFormatBits vertexFormatBits;

            switch (displayStyle)
            {
            case DisplayStyle.HLR:
            case DisplayStyle.FlatColors:
                vertexFormatBits = VertexFormatBits.PositionColored;
                break;

            default:
                vertexFormatBits = VertexFormatBits.PositionNormalColored;
                break;
            }
            var vertexFormat = new VertexFormat(vertexFormatBits);

            var vBuffer = new VertexBuffer(GetVertexSize(vertexFormatBits) * numVertices);
            var iBuffer = new IndexBuffer(numIndices);

            vBuffer.Map(GetVertexSize(vertexFormatBits) * numVertices);
            iBuffer.Map(numIndices);

            var verticesFlat = new List <VertexPositionColored>();
            var vertices     = new List <VertexPositionNormalColored>();
            var triangles    = new List <IndexTriangle>();

            ColorWithTransparency color = null;

            if (displayStyle == DisplayStyle.HLR)
            {
                color = new ColorWithTransparency(255, 255, 255, 0);
            }
            else if (primitive.Material != null)
            {
                var material = gltf.Materials[(int)primitive.Material];
                var r        = (uint)(material.PbrMetallicRoughness.BaseColorFactor[0] * 255);
                var g        = (uint)(material.PbrMetallicRoughness.BaseColorFactor[1] * 255);
                var b        = (uint)(material.PbrMetallicRoughness.BaseColorFactor[2] * 255);
                var a        = (uint)(material.PbrMetallicRoughness.BaseColorFactor[3] * 255);
                color = new ColorWithTransparency(r, g, b, a);
            }

            for (var i = 0; i < indices.Count; i += 3)
            {
                var ia = indices[i];
                var ib = indices[i + 1];
                var ic = indices[i + 2];

                var a = positions[ia];
                var b = positions[ib];
                var c = positions[ic];

                var na = normals[ia];
                var nb = normals[ib];
                var nc = normals[ic];

                switch (vertexFormatBits)
                {
                case VertexFormatBits.PositionColored:
                    if (hasColor)
                    {
                        color = colors[ia];
                    }
                    verticesFlat.Add(new VertexPositionColored(a, hasColor ? colors[ia] : color));
                    verticesFlat.Add(new VertexPositionColored(b, hasColor ? colors[ib] : color));
                    verticesFlat.Add(new VertexPositionColored(c, hasColor ? colors[ic] : color));
                    break;

                default:
                    if (hasColor)
                    {
                        color = colors[ia];
                    }
                    vertices.Add(new VertexPositionNormalColored(a, na, hasColor ? colors[ia] : color));
                    vertices.Add(new VertexPositionNormalColored(b, nb, hasColor ? colors[ib] : color));
                    vertices.Add(new VertexPositionNormalColored(c, nc, hasColor ? colors[ic] : color));
                    break;
                }

                triangles.Add(new IndexTriangle(i, i + 1, i + 2));
            }

            switch (displayStyle)
            {
            case DisplayStyle.HLR:
            case DisplayStyle.FlatColors:
                var pc = vBuffer.GetVertexStreamPositionColored();
                pc.AddVertices(verticesFlat);
                break;

            default:
                var pnc = vBuffer.GetVertexStreamPositionNormalColored();
                pnc.AddVertices(vertices);
                break;
            }

            var iPos = iBuffer.GetIndexStreamTriangle();

            iPos.AddTriangles(triangles);

            vBuffer.Unmap();
            iBuffer.Unmap();

            var effect = new EffectInstance(vertexFormatBits);

            var renderData = new RenderData()
            {
                VertexBuffer   = vBuffer,
                VertexCount    = numVertices,
                IndexBuffer    = iBuffer,
                IndexCount     = numIndices,
                VertexFormat   = vertexFormat,
                Effect         = effect,
                PrimitiveType  = pType,
                PrimitiveCount = numPrimitives
            };

            if (displayStyle != DisplayStyle.Wireframe && numPrimitives > 0)
            {
                DrawContext.FlushBuffer(vBuffer, numVertices, iBuffer, numIndices, vertexFormat, effect, pType, 0, numPrimitives);
            }

            return(renderData);
        }
예제 #11
0
 public PointInfo(XYZ _vertex, ColorWithTransparency _color, XYZ _offset)
 {
     this.Vertex = _vertex;
     this.Color  = _color;
     this.Offset = _offset;
 }
예제 #12
0
 public EdgeInfo(IList <XYZ> _vertices, ColorWithTransparency _color, XYZ _offset)
 {
     this.Vertices = _vertices;
     this.Color    = _color;
     this.Offset   = _offset;
 }
        protected static int ToPointsBuffer
        (
            Rhino.Geometry.PointCloud pointCloud,
            Primitive.Part part,
            out VertexFormatBits vertexFormatBits,
            out VertexBuffer vb, out int vertexCount,
            out 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 = VertexFormatBits.PositionNormalColored;
                        vb = new VertexBuffer(pointsCount * VertexPositionNormalColored.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionNormalColored.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionNormalColored())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                var c     = new ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A);
                                vstream.AddVertex(new VertexPositionNormalColored(RawEncoder.ToHost(point.Location), RawEncoder.ToHost(point.Normal), c));
                            }
                        }

                        vb.Unmap();
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormal;
                        vb = new VertexBuffer(pointsCount * VertexPositionNormal.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionNormal.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionNormal())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                vstream.AddVertex(new VertexPositionNormal(RawEncoder.ToHost(point.Location), RawEncoder.ToHost(point.Normal)));
                            }
                        }

                        vb.Unmap();
                    }
                }
                else
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionColored;
                        vb = new VertexBuffer(pointsCount * VertexPositionColored.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionColored.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionColored())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                var c     = new ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A);
                                vstream.AddVertex(new VertexPositionColored(RawEncoder.ToHost(point.Location), c));
                            }
                        }

                        vb.Unmap();
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.Position;
                        vb = new VertexBuffer(pointsCount * VertexPosition.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPosition.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPosition())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                vstream.AddVertex(new VertexPosition(RawEncoder.ToHost(point.Location)));
                            }
                        }

                        vb.Unmap();
                    }
                }

                ib = IndexPointsBuffer(pointsCount);
            }
            else
            {
                vertexFormatBits = 0;
                vb = null;
                ib = null;
            }

            vertexCount = pointsCount;
            return(pointsCount);
        }
예제 #14
0
 public MeshInfo(Mesh mesh, XYZ normal, ColorWithTransparency color)
 {
     Mesh   = mesh;
     Normal = normal;
     ColorWithTransparency = color;
 }
예제 #15
0
        // Create and populate a pair of vertex and index buffers. Also update parameters associated with the format of the vertices.
        private void ProcessFaces(RenderingPassBufferStorage bufferStorage)
        {
            List <MeshInfo> meshes = bufferStorage.Meshes;

            if (meshes.Count == 0)
            {
                return;
            }
            List <int> numVerticesInMeshesBefore = new List <int>();

            bool useNormals = this.Inputs.EnableFaceNormal;

            // Vertex attributes are stored sequentially in vertex buffers. The attributes can include position, normal vector, and color.
            // All vertices within a vertex buffer must have the same format. Possible formats are enumerated by VertexFormatBits.
            // Vertex format also determines the type of rendering effect that can be used with the vertex buffer. In this sample,
            // the color is always encoded in the vertex attributes.

            bufferStorage.FormatBits = useNormals ? VertexFormatBits.PositionNormalColored : VertexFormatBits.PositionColored;

            // The format of the vertices determines the size of the vertex buffer.
            int vertexBufferSizeInFloats = (useNormals ? VertexPositionNormalColored.GetSizeInFloats() : VertexPositionColored.GetSizeInFloats()) *
                                           bufferStorage.VertexBufferCount;

            numVerticesInMeshesBefore.Add(0);

            bufferStorage.VertexBuffer = new VertexBuffer(vertexBufferSizeInFloats);
            bufferStorage.VertexBuffer.Map(vertexBufferSizeInFloats);

            int numMeshes = meshes.Count;

            if (useNormals)
            {
                // A VertexStream is used to write data into a VertexBuffer.
                VertexStreamPositionNormalColored vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionNormalColored();
                for (int i = 0; i < numMeshes; i++)
                {
                    var  meshInfo = meshes[i];
                    Mesh mesh     = meshInfo.Mesh;
                    foreach (XYZ vertex in mesh.Vertices)
                    {
                        vertexStream.AddVertex(new VertexPositionNormalColored(vertex + meshInfo.Offset, meshInfo.Normal, meshInfo.ColorWithTransparency));
                    }

                    numVerticesInMeshesBefore.Add(numVerticesInMeshesBefore.Last() + mesh.Vertices.Count);
                }
            }
            else
            {
                // A VertexStream is used to write data into a VertexBuffer.
                VertexStreamPositionColored vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionColored();
                for (int i = 0; i < numMeshes; i++)
                {
                    var  meshInfo = meshes[i];
                    Mesh mesh     = meshInfo.Mesh;
                    // make the color of all faces white in HLR
                    ColorWithTransparency color = meshInfo.ColorWithTransparency;
                    foreach (XYZ vertex in mesh.Vertices)
                    {
                        vertexStream.AddVertex(new VertexPositionColored(vertex + meshInfo.Offset, color));
                    }

                    numVerticesInMeshesBefore.Add(numVerticesInMeshesBefore.Last() + mesh.Vertices.Count);
                }
            }

            bufferStorage.VertexBuffer.Unmap();

            // Primitives are specified using a pair of vertex and index buffers. An index buffer contains a sequence of indices into
            // the associated vertex buffer, each index referencing a particular vertex.

            int meshNumber = 0;

            bufferStorage.IndexBufferCount = bufferStorage.PrimitiveCount * IndexTriangle.GetSizeInShortInts();
            int indexBufferSizeInShortInts = 1 * bufferStorage.IndexBufferCount;

            bufferStorage.IndexBuffer = new IndexBuffer(indexBufferSizeInShortInts);
            bufferStorage.IndexBuffer.Map(indexBufferSizeInShortInts);
            {
                // An IndexStream is used to write data into an IndexBuffer.
                IndexStreamTriangle indexStream = bufferStorage.IndexBuffer.GetIndexStreamTriangle();
                foreach (MeshInfo meshInfo in meshes)
                {
                    Mesh mesh       = meshInfo.Mesh;
                    int  startIndex = numVerticesInMeshesBefore[meshNumber];
                    for (int i = 0; i < mesh.NumTriangles; i++)
                    {
                        MeshTriangle mt = mesh.get_Triangle(i);
                        // Add three indices that define a triangle.
                        indexStream.AddTriangle(new IndexTriangle((int)(startIndex + mt.get_Index(0)),
                                                                  (int)(startIndex + mt.get_Index(1)),
                                                                  (int)(startIndex + mt.get_Index(2))));
                    }
                    meshNumber++;
                }
            }
            bufferStorage.IndexBuffer.Unmap();

            // VertexFormat is a specification of the data that is associated with a vertex (e.g., position).
            bufferStorage.VertexFormat = new VertexFormat(bufferStorage.FormatBits);
            // Effect instance is a specification of the appearance of geometry. For example, it may be used to specify color, if there is no color information provided with the vertices.
            bufferStorage.EffectInstance = new EffectInstance(bufferStorage.FormatBits);
        }
예제 #16
0
        // Create and populate a pair of vertex and index buffers. Also update parameters associated with the format of the vertices.
        private void ProcessTriangles(RenderingPassBufferStorage bufferStorage)
        {
            List <TriangleInfo> triangles = bufferStorage.Triangles;

            if (triangles.Count == 0)
            {
                return;
            }

            bool useNormals = this.Inputs.EnableFaceNormal;

            // Vertex attributes are stored sequentially in vertex buffers. The attributes can include position, normal vector, and color.
            // All vertices within a vertex buffer must have the same format. Possible formats are enumerated by VertexFormatBits.
            // Vertex format also determines the type of rendering effect that can be used with the vertex buffer. In this sample,
            // the color is always encoded in the vertex attributes.

            bufferStorage.FormatBits = useNormals ? VertexFormatBits.PositionNormalColored : VertexFormatBits.PositionColored;

            // The format of the vertices determines the size of the vertex buffer.
            int vertexBufferSizeInFloats = (useNormals ? VertexPositionNormalColored.GetSizeInFloats() : VertexPositionColored.GetSizeInFloats()) *
                                           bufferStorage.VertexBufferCount;

            bufferStorage.VertexBuffer = new VertexBuffer(vertexBufferSizeInFloats);
            bufferStorage.VertexBuffer.Map(vertexBufferSizeInFloats);

            int numTriangles = triangles.Count;

            if (useNormals)
            {
                // A VertexStream is used to write data into a VertexBuffer.
                VertexStreamPositionNormalColored vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionNormalColored();
                for (int i = 0; i < numTriangles; i++)
                {
                    var           triangleInfo = triangles[i];
                    g3.Triangle3d triangle     = triangleInfo.Triangle;
                    vertexStream.AddVertex(new VertexPositionNormalColored(triangle.V0.ToXYZ() + triangleInfo.Offset, triangleInfo.Normal, triangleInfo.ColorWithTransparency));
                    vertexStream.AddVertex(new VertexPositionNormalColored(triangle.V1.ToXYZ() + triangleInfo.Offset, triangleInfo.Normal, triangleInfo.ColorWithTransparency));
                    vertexStream.AddVertex(new VertexPositionNormalColored(triangle.V2.ToXYZ() + triangleInfo.Offset, triangleInfo.Normal, triangleInfo.ColorWithTransparency));
                }
            }
            else
            {
                // A VertexStream is used to write data into a VertexBuffer.
                VertexStreamPositionColored vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionColored();
                for (int i = 0; i < numTriangles; i++)
                {
                    var           triangleInfo = triangles[i];
                    g3.Triangle3d triangle     = triangleInfo.Triangle;
                    // make the color of all faces white in HLR
                    ColorWithTransparency color = triangleInfo.ColorWithTransparency;
                    vertexStream.AddVertex(new VertexPositionColored(triangle.V0.ToXYZ() + triangleInfo.Offset, color));
                    vertexStream.AddVertex(new VertexPositionColored(triangle.V1.ToXYZ() + triangleInfo.Offset, color));
                    vertexStream.AddVertex(new VertexPositionColored(triangle.V2.ToXYZ() + triangleInfo.Offset, color));
                }
            }

            bufferStorage.VertexBuffer.Unmap();

            // Primitives are specified using a pair of vertex and index buffers. An index buffer contains a sequence of indices into
            // the associated vertex buffer, each index referencing a particular vertex.

            bufferStorage.IndexBufferCount = bufferStorage.PrimitiveCount * IndexTriangle.GetSizeInShortInts();
            int indexBufferSizeInShortInts = 1 * bufferStorage.IndexBufferCount;

            bufferStorage.IndexBuffer = new IndexBuffer(indexBufferSizeInShortInts);
            bufferStorage.IndexBuffer.Map(indexBufferSizeInShortInts);
            // An IndexStream is used to write data into an IndexBuffer.
            IndexStreamTriangle indexStream = bufferStorage.IndexBuffer.GetIndexStreamTriangle();
            int currIndex = 0;

            for (int i = 0; i < numTriangles; i++)
            {
                // Add three indices that define a triangle.
                indexStream.AddTriangle(new IndexTriangle(currIndex + 0, currIndex + 1, currIndex + 2));
                currIndex += 3;
            }
            bufferStorage.IndexBuffer.Unmap();

            // VertexFormat is a specification of the data that is associated with a vertex (e.g., position).
            bufferStorage.VertexFormat = new VertexFormat(bufferStorage.FormatBits);
            // Effect instance is a specification of the appearance of geometry. For example, it may be used to specify color, if there is no color information provided with the vertices.
            bufferStorage.EffectInstance = new EffectInstance(bufferStorage.FormatBits);
        }
예제 #17
0
        // Initialize and populate buffers that hold graphics primitives, set up related parameters that are needed for drawing.
        private void CreateBufferStorageForElement(GeometryElement geomElem, DisplayStyle displayStyle)
        {
            List <Solid> allSolids = new List <Solid>();

            foreach (GeometryObject geomObj in geomElem)
            {
                if (geomObj is Solid)
                {
                    Solid solid = (Solid)geomObj;
                    if (solid.Volume > 1e-06)
                    {
                        allSolids.Add(solid);
                    }
                }
            }

            m_nonTransparentFaceBufferStorage = new RenderingPassBufferStorage(displayStyle);
            m_transparentFaceBufferStorage    = new RenderingPassBufferStorage(displayStyle);
            m_edgeBufferStorage = new RenderingPassBufferStorage(displayStyle);

            // Collect primitives (and associated rendering parameters, such as colors) from faces and edges.
            foreach (Solid solid in allSolids)
            {
                foreach (Face face in solid.Faces)
                {
                    if (face.Area > 1e-06)
                    {
                        Mesh mesh = face.Triangulate();

                        ElementId             materialId    = face.MaterialElementId;
                        bool                  isTransparent = false;
                        ColorWithTransparency cwt           = new ColorWithTransparency(127, 127, 127, 0);
                        if (materialId != ElementId.InvalidElementId)
                        {
                            Material material = m_element.Document.GetElement(materialId) as Material;

                            Color color = material.Color;
                            int   transparency0To100 = material.Transparency;
                            uint  transparency0To255 = (uint)((float)transparency0To100 / 100f * 255f);

                            cwt = new ColorWithTransparency(color.Red, color.Green, color.Blue, transparency0To255);
                            if (transparency0To255 > 0)
                            {
                                isTransparent = true;
                            }
                        }

                        BoundingBoxUV env    = face.GetBoundingBox();
                        UV            center = 0.5 * (env.Min + env.Max);
                        XYZ           normal = face.ComputeNormal(center);

                        MeshInfo meshInfo = new MeshInfo(mesh, normal, cwt);

                        if (isTransparent)
                        {
                            m_transparentFaceBufferStorage.Meshes.Add(meshInfo);
                            m_transparentFaceBufferStorage.VertexBufferCount += mesh.Vertices.Count;
                            m_transparentFaceBufferStorage.PrimitiveCount    += mesh.NumTriangles;
                        }
                        else
                        {
                            m_nonTransparentFaceBufferStorage.Meshes.Add(meshInfo);
                            m_nonTransparentFaceBufferStorage.VertexBufferCount += mesh.Vertices.Count;
                            m_nonTransparentFaceBufferStorage.PrimitiveCount    += mesh.NumTriangles;
                        }
                    }
                }

                foreach (Edge edge in solid.Edges)
                {
                    // if (edge.Length > 1e-06)
                    {
                        IList <XYZ> xyzs = edge.Tessellate();

                        m_edgeBufferStorage.VertexBufferCount += xyzs.Count;
                        m_edgeBufferStorage.PrimitiveCount    += xyzs.Count - 1;
                        m_edgeBufferStorage.EdgeXYZs.Add(xyzs);
                    }
                }
            }

            // Fill out buffers with primitives based on the intermediate information about faces and edges.
            ProcessFaces(m_nonTransparentFaceBufferStorage);
            ProcessFaces(m_transparentFaceBufferStorage);
            ProcessEdges(m_edgeBufferStorage);
        }