private ModelMeshContent ProcessMesh(MeshContent mesh, ModelBoneContent parent, ContentProcessorContext context)
        {
            var bounds       = new BoundingSphere();
            var parts        = new List <ModelMeshPartContent>();
            var vertexBuffer = new VertexBufferContent();
            var indexBuffer  = new IndexCollection();

            var startVertex = 0;

            foreach (var geometry in mesh.Geometry)
            {
                var vertices    = geometry.Vertices;
                var vertexCount = vertices.VertexCount;
                var geomBuffer  = geometry.Vertices.CreateVertexBuffer();
                vertexBuffer.Write(vertexBuffer.VertexData.Length, 1, geomBuffer.VertexData);

                var startIndex = indexBuffer.Count;
                indexBuffer.AddRange(geometry.Indices);

                var partContent = new ModelMeshPartContent(vertexBuffer, indexBuffer, startVertex, vertexCount, startIndex, geometry.Indices.Count / 3);
                partContent.Material = geometry.Material;
                parts.Add(partContent);

                // Update mesh bounding box
                bounds = BoundingSphere.CreateMerged(bounds, BoundingSphere.CreateFromPoints(geometry.Vertices.Positions));

                // Geoms are supposed to all have the same decl, so just steal one of these
                vertexBuffer.VertexDeclaration = geomBuffer.VertexDeclaration;

                startVertex += vertexCount;
            }

            return(new ModelMeshContent(mesh.Name, mesh, parent, bounds, parts));
        }
Exemple #2
0
        private ModelMeshContent ProcessMesh(MeshContent mesh, ModelBoneContent parent, ContentProcessorContext context)
        {
            var parts        = new List <ModelMeshPartContent>();
            var vertexBuffer = new VertexBufferContent();
            var indexBuffer  = new IndexCollection();

            if (GenerateTangentFrames)
            {
                context.Logger.LogMessage("Generating tangent frames.");
                foreach (GeometryContent geom in mesh.Geometry)
                {
                    if (!geom.Vertices.Channels.Contains(VertexChannelNames.Normal(0)))
                    {
                        MeshHelper.CalculateNormals(geom, true);
                    }

                    if (!geom.Vertices.Channels.Contains(VertexChannelNames.Tangent(0)) ||
                        !geom.Vertices.Channels.Contains(VertexChannelNames.Binormal(0)))
                    {
                        MeshHelper.CalculateTangentFrames(geom, VertexChannelNames.TextureCoordinate(0), VertexChannelNames.Tangent(0),
                                                          VertexChannelNames.Binormal(0));
                    }
                }
            }

            var startVertex = 0;

            foreach (var geometry in mesh.Geometry)
            {
                var vertices    = geometry.Vertices;
                var vertexCount = vertices.VertexCount;
                ModelMeshPartContent partContent;
                if (vertexCount == 0)
                {
                    partContent = new ModelMeshPartContent();
                }
                else
                {
                    var geomBuffer = geometry.Vertices.CreateVertexBuffer();
                    vertexBuffer.Write(vertexBuffer.VertexData.Length, 1, geomBuffer.VertexData);

                    var startIndex = indexBuffer.Count;
                    indexBuffer.AddRange(geometry.Indices);

                    partContent = new ModelMeshPartContent(vertexBuffer, indexBuffer, startVertex, vertexCount, startIndex, geometry.Indices.Count / 3);

                    // Geoms are supposed to all have the same decl, so just steal one of these
                    vertexBuffer.VertexDeclaration = geomBuffer.VertexDeclaration;

                    startVertex += vertexCount;
                }

                partContent.Material = geometry.Material;
                parts.Add(partContent);
            }

            var bounds = new BoundingSphere();

            if (mesh.Positions.Count > 0)
            {
                bounds = BoundingSphere.CreateFromPoints(mesh.Positions);
            }

            return(new ModelMeshContent(mesh.Name, mesh, parent, bounds, parts));
        }
 /// <summary>
 /// Called when a basic effect is encountered and potentially replaced by
 /// BasicPaletteEffect (if not overridden).  This is called afer effects have been processed.
 /// </summary>
 /// <param name="skinningType">The the skinning type of the meshpart.</param>
 /// <param name="meshPart">The MeshPart that contains the BasicMaterialContent.</param>
 protected virtual void ReplaceBasicEffect(SkinningType skinningType,
     ModelMeshPartContent meshPart)
 {
     BasicMaterialContent basic = meshPart.Material as BasicMaterialContent;
     if (basic != null)
     {
         // Create a new PaletteSourceCode object and set its palette size
         // based on the platform since xbox has fewer registers.
         PaletteSourceCode source;
         if (context.TargetPlatform != TargetPlatform.Xbox360)
         {
             source = new PaletteSourceCode(56);
         }
         else
         {
             source = new PaletteSourceCode(40);
         }
         // Process the material and set the meshPart material to the new
         // material.
         PaletteInfoProcessor processor = new PaletteInfoProcessor();
         meshPart.Material = processor.Process(
             new PaletteInfo(source.SourceCode4BonesPerVertex,
             source.PALETTE_SIZE, basic), context);
     }
 }