private static NormalBuffers GetNormalBuffers(ModelMesh mesh, ModelMeshPart meshPart, GraphicsDevice graphicsDevice, float normalLengthPercentage) { if (meshPart.VertexBuffer == null) { return(null); } Vector3[] positions = VertexElementExtractor.GetVertexElement(meshPart, VertexElementUsage.Position); if (positions == null) { return(null); } Vector3[] normals = VertexElementExtractor.GetVertexElement(meshPart, VertexElementUsage.Normal); if (normals == null) { return(null); } NormalBuffers normalBuffers = new NormalBuffers(); normalBuffers.PrimitiveCount = normals.Length; normalBuffers.VertexCount = normals.Length * 2; float size = mesh.BoundingSphere.Radius * normalLengthPercentage; VertexBuffer vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), normalBuffers.VertexCount, BufferUsage.WriteOnly); VertexPositionColor[] vertices = new VertexPositionColor[normalBuffers.VertexCount]; int counter = 0; for (int i = 0; i < normals.Length; ++i) { Vector3 normalColorTemp = Vector3.Normalize(normals[i]); normalColorTemp += Vector3.One; normalColorTemp *= 0.5f; Color normalColor = new Color(normalColorTemp); vertices[counter++] = new VertexPositionColor(positions[i], normalColor); vertices[counter++] = new VertexPositionColor(positions[i] + (normals[i] * size), normalColor); } vertexBuffer.SetData(vertices); normalBuffers.Vertices = vertexBuffer; IndexBuffer indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, normalBuffers.VertexCount, BufferUsage.WriteOnly); indexBuffer.SetData(Enumerable.Range(0, normalBuffers.VertexCount).Select(i => (short)i).ToArray()); normalBuffers.Indices = indexBuffer; return(normalBuffers); }
private static BoundingBox?GetBoundingBox(ModelMeshPart meshPart) { if (meshPart.VertexBuffer == null) { return(null); } Vector3[] positions = VertexElementExtractor.GetVertexElement(meshPart, VertexElementUsage.Position); if (positions == null) { return(null); } return(BoundingBox.CreateFromPoints(positions)); }
protected virtual void RebuildBB() { if (EntityModel == null) { return; } if (verts == null) { verts = VertexElementExtractor.GetVertexData(EntityModel); } _world = Matrix.CreateScale(_scale) * Matrix.CreateWorld(_position, _forward, _up); bounds = VertexElementExtractor.UpdateBoundingBoxFromVertexList(EntityModel, _world, verts); }
/* Returns a new boudning box based on model mesh vertices. */ private static BoundingBox?GetMeshBoundingBox(ModelMeshPart meshPart, Matrix transform) { if (meshPart.VertexBuffer == null) { return(null); } Vector3[] positions = VertexElementExtractor.GetVertexElement(meshPart, VertexElementUsage.Position); if (positions == null) { return(null); } Vector3[] transformedPositions = new Vector3[positions.Length]; Vector3.Transform(positions, ref transform, transformedPositions); return(BoundingBox.CreateFromPoints(transformedPositions)); }