Exemplo n.º 1
0
        public void StartMesh(ModelMeshContent mmc)
        {
            v0 = new object[dataChannels.Count];
            v1 = new object[dataChannels.Count];
            v2 = new object[dataChannels.Count];

            for (int i = 0, n = dataChannels.Count; i != n; ++i)
            {
                switch (dataChannels[i].VertexElementFormat)
                {
                case VertexElementFormat.Color:
                    v0[i] = new Color(255, 255, 255, 255);
                    v1[i] = new Color(255, 255, 255, 255);
                    v2[i] = new Color(255, 255, 255, 255);
                    break;

                case VertexElementFormat.Vector2:
                    v0[i] = new Vector2(0, 0);
                    v1[i] = new Vector2(0, 0);
                    v2[i] = new Vector2(0, 0);
                    break;

                case VertexElementFormat.Vector3:
                    v0[i] = new Vector3(0, 0, 0);
                    v1[i] = new Vector3(0, 0, 0);
                    v2[i] = new Vector3(0, 0, 0);
                    break;
                }
            }

            vertexBufferData_ = mmc.VertexBuffer.VertexData;
            indexBufferData_  = mmc.IndexBuffer;
        }
Exemplo n.º 2
0
 // Go through the modelmeshes and replace all basic effects for skinned models
 // with BasicPaletteEffect.
 private void ReplaceBasicEffects(ModelMeshContent input)
 {
     /*foreach (ModelMeshPartContent part in input.MeshParts)
      * {
      *  SkinningType skinType = ContentUtil.GetSkinningType(part.VertexBuffer.VertexDeclaration);
      *  if (skinType != SkinningType.None)
      *  {
      *      ReplaceBasicEffect(skinType, part);
      *  }
      * }*/
 }
Exemplo n.º 3
0
 // Go through the modelmeshes and replace all basic effects for skinned models
 // with BasicPaletteEffect.
 private void ReplaceBasicEffects(ModelMeshContent input)
 {
     foreach (ModelMeshPartContent part in input.MeshParts)
     {
         SkinningType skinType = ContentUtil.GetSkinningType(part.GetVertexDeclaration());
         if (skinType != SkinningType.None)
         {
             ReplaceBasicEffect(skinType, part);
         }
     }
 }
        public DynamicModelMeshContent(ModelMeshContent source)
        {
            this.Source = source;

            //deep clone MeshParts
            MeshParts = new List <DynamicModelMeshPartContent>(source.MeshParts.Count);
            foreach (var mesh in source.MeshParts)
            {
                MeshParts.Add(new DynamicModelMeshPartContent(mesh));
            }
        }
Exemplo n.º 5
0
        private static Vector3 GetMeshTranslation(ModelMeshContent mesh)
        {
            var pos  = Vector3.Zero;
            var bone = mesh.ParentBone;

            while (bone != null)
            {
                pos += bone.Transform.Translation;
                bone = bone.Parent;
            }
            return(pos);
        }
Exemplo n.º 6
0
        // Go through the modelmeshes and replace all basic effects for skinned models
        // with BasicPaletteEffect.
        private void ReplaceBasicEffects(ModelMeshContent input)
        {
            foreach (ModelMeshPartContent part in input.MeshParts)
            {
#if XNA4
                SkinningType skinType = ContentUtil.GetSkinningType(part.VertexBuffer.VertexDeclaration.VertexElements);
#else
                SkinningType skinType = ContentUtil.GetSkinningType(part.GetVertexDeclaration());
#endif
                if (skinType != SkinningType.None)
                {
                    ReplaceBasicEffect(skinType, part);
                }
            }
        }
Exemplo n.º 7
0
        private static Vector3[] MeshToVertexArray(TargetPlatform platform, ModelMeshContent mesh)
        {
            MemoryStream ms;
            var          buffer = mesh.MeshParts[0].VertexBuffer;

//			if (platform == TargetPlatform.Xbox360)
//			{
//				ms = new MemoryStream(ReverseByteOrder(buffer.VertexData));
//			}
//			else
//			{
            ms = new MemoryStream(buffer.VertexData);
//			}
            BinaryReader reader = new BinaryReader(ms);

            var elems = buffer.VertexDeclaration.VertexElements;
            int count = mesh.MeshParts[0].NumVertices;

            ms.Seek(mesh.MeshParts[0].VertexOffset * buffer.VertexDeclaration.VertexStride.Value, SeekOrigin.Begin);

            var vertices = new Vector3[count];

            for (int i = 0; i < count; i++)
            {
                foreach (var elType in elems)
                {
                    if (elType.VertexElementUsage == VertexElementUsage.Position)
                    {
                        vertices[i].X = reader.ReadSingle();
                        vertices[i].Y = reader.ReadSingle();
                        vertices[i].Z = reader.ReadSingle();
                    }
                    else
                    {
                        switch (elType.VertexElementFormat)
                        {
                        case VertexElementFormat.Color:
                            reader.ReadUInt32();
                            break;

                        case VertexElementFormat.Vector2:
                            reader.ReadSingle();
                            reader.ReadSingle();
                            break;

                        case VertexElementFormat.Vector3:
                            reader.ReadSingle();
                            reader.ReadSingle();
                            reader.ReadSingle();
                            break;

                        default:
                            throw new InvalidContentException("Unrecognized element type in vertex buffer: " + elType.ToString());
                        }
                    }
                }
            }

            var transforms = new Stack <Matrix>();
            var bone       = mesh.ParentBone;

            while (bone != null)
            {
                transforms.Push(bone.Transform);
                bone = bone.Parent;
            }

            var transform = Matrix.Identity;

            while (transforms.Count > 0)
            {
                transform *= transforms.Pop();
            }

            Vector3.Transform(vertices, ref transform, vertices);
            return(vertices);
        }
Exemplo n.º 8
0
        public override ModelContent Process(SceneContent input, ContentProcessorContext context)
        {
            List <ModelMeshContent> meshes       = new List <ModelMeshContent>();
            VertexBufferContent     vertexBuffer = new VertexBufferContent(new VertexAttribute[] {
                new PositionAttribute(),
                new NormalAttribute(),
                new TextureCoordinateAttribute(),
            });

            if (Skin)
            {
                vertexBuffer.AddAttribute(new BlendWeightsAttribute());
                vertexBuffer.AddAttribute(new BlendIndicesAttribute());
            }

            BoneContent rootBone = BuildSkeleton(input.Scene.RootNode, null);

            MaterialContent[] materials = new MaterialContent[input.Scene.MaterialCount];
            for (int i = 0; i < input.Scene.MaterialCount; i++)
            {
                Dictionary <string, object> parameters = new Dictionary <string, object>();
                GetMaterialParameters(input.Scene.Materials[i], parameters, context);
                string materialFactory;
                if (!MaterialTypeFactoryReplacements.TryGetValue(i, out materialFactory))
                {
                    materialFactory = DefaultMaterialTypeFactory;
                }
                MaterialContent material = context.ContentManager.MaterialFactoryManager.GetMaterialFactory(materialFactory)(context, parameters);
                material.Name = input.Scene.Materials[i].HasName ? input.Scene.Materials[i].Name : string.Format("{0}_Material_{1}", input.Name, i);
                materials[i]  = material;
            }

            uint numVertices = 0;
            uint numIndices  = 0;

            for (int i = 0; i < input.Scene.MeshCount; i++)
            {
                Mesh mesh            = input.Scene.Meshes[i];
                uint primitiveCount  = (uint)mesh.FaceCount;
                uint baseVertex      = numVertices;
                uint baseIndex       = numIndices;
                uint partNumIndices  = (uint)mesh.FaceCount * 3;
                uint partNumVertices = (uint)mesh.VertexCount;

                numVertices += (uint)mesh.VertexCount;
                numIndices  += (uint)mesh.FaceCount * 3;

                Dictionary <uint, List <VertexWeight> > boneVertWeights = new Dictionary <uint, List <VertexWeight> >();
                if (Skin)
                {
                    ExtractBoneWeights(mesh, boneVertWeights);
                }

                for (int j = 0; j < mesh.VertexCount; j++)
                {
                    vertexBuffer[Minotaur.Graphics.VertexUsage.Position].AddValues(mesh, j);
                    vertexBuffer[Minotaur.Graphics.VertexUsage.Normal].AddValues(mesh, j);
                    vertexBuffer[Minotaur.Graphics.VertexUsage.TextureCoordinate].AddValues(mesh, j);
                    if (Skin)
                    {
                        float[] weights = new float[4];
                        byte[]  indices = new byte[4];
                        GetNormalizedBoneWeights(boneVertWeights[(uint)j], weights, indices);
                        ((BlendWeightsAttribute)vertexBuffer[Minotaur.Graphics.VertexUsage.BlendWeight]).AddValues(weights);
                        ((BlendIndicesAttribute)vertexBuffer[Minotaur.Graphics.VertexUsage.BlendIndices]).AddValues(indices);
                    }
                }

                IndexCollection indecies = new IndexCollection();
                indecies.AddRange(mesh.GetIndices().Select(k => k + (uint)baseVertex));

                ModelMeshPartContent         part           = new ModelMeshPartContent(vertexBuffer, indecies, baseVertex, partNumVertices, baseIndex, partNumIndices, primitiveCount);
                Minotaur.Core.BoundingSphere boundingSphere = Minotaur.Core.BoundingSphere.CreateFromPoints(
                    ((PositionAttribute)vertexBuffer[Minotaur.Graphics.VertexUsage.Position]).Values.Select(
                        p => new OpenTK.Vector3(p.X, p.Y, p.Z)));
                ModelMeshContent modelmesh = new ModelMeshContent(mesh.Name, GetMeshParentBone(input.Scene.RootNode, i), new[] { part }, boundingSphere);
                meshes.Add(modelmesh);

                part.Material = materials[mesh.MaterialIndex];
            }

            List <ExternalReferenceContent <BoneAnimationsContent> > animations = new List <ExternalReferenceContent <BoneAnimationsContent> >();

            foreach (Animation anim in input.Scene.Animations)
            {
                if (ExportAnimations.Count == 0 || ExportAnimations.Contains(anim.Name))
                {
                    animations.Add(context.ContentManager.BuildContent <BoneAnimationsContent>(input.Name, "ModelImporter",
                                                                                               processorName: "BoneAnimProcessor",
                                                                                               processorData: new Dictionary <string, object>()
                    {
                        { "AnimationName", anim.Name },
                        { "NamePre", string.Format("{0}_", Path.GetFileNameWithoutExtension(input.Name)) },
                    },
                                                                                               ignoreBuildItem: true
                                                                                               ));
                }
            }

            return(new ModelContent(rootBone, _bones, meshes, animations));
        }