Exemplo n.º 1
0
        private Model CreateModel(
            ContentManager contentManager,
            W3dFile w3dFile,
            W3dHierarchyDef w3dHierarchy)
        {
            ModelBone[] bones;
            if (w3dHierarchy != null)
            {
                bones = new ModelBone[w3dHierarchy.Pivots.Items.Count];

                for (var i = 0; i < w3dHierarchy.Pivots.Items.Count; i++)
                {
                    var pivot = w3dHierarchy.Pivots.Items[i];

                    var parent = pivot.ParentIdx == -1
                        ? null
                        : bones[pivot.ParentIdx];

                    bones[i] = new ModelBone(
                        i,
                        pivot.Name,
                        parent,
                        pivot.Translation,
                        pivot.Rotation);
                }
            }
            else
            {
                bones    = new ModelBone[1];
                bones[0] = new ModelBone(0, null, null, Vector3.Zero, Quaternion.Identity);
            }

            //BoundingSphere boundingSphere = default(BoundingSphere);

            var w3dMeshes = w3dFile.GetMeshes();
            var w3dHLod   = w3dFile.GetHLod();

            var subObjects = new List <ModelSubObject>();

            if (w3dHLod != null)
            {
                foreach (var w3dSubObject in w3dHLod.Lods[0].SubObjects)
                {
                    // TODO: Collision boxes
                    var w3dMesh = w3dMeshes.FirstOrDefault(x => x.Header.ContainerName + "." + x.Header.MeshName == w3dSubObject.Name);
                    if (w3dMesh == null)
                    {
                        continue;
                    }

                    var bone = bones[(int)w3dSubObject.BoneIndex];

                    var mesh = CreateModelMesh(
                        contentManager,
                        w3dMesh);

                    //var meshBoundingSphere = mesh.BoundingSphere.Transform(bone.Transform);

                    //boundingSphere = (i == 0)
                    //    ? meshBoundingSphere
                    //    : BoundingSphere.CreateMerged(boundingSphere, meshBoundingSphere);

                    subObjects.Add(new ModelSubObject(w3dSubObject.Name, bone, mesh));
                }
            }
            else
            {
                // Simple models can have only one mesh with no HLod chunk.
                if (w3dMeshes.Count != 1)
                {
                    throw new InvalidOperationException();
                }

                var w3dMesh = w3dMeshes[0];

                var mesh = CreateModelMesh(
                    contentManager,
                    w3dMesh);

                subObjects.Add(new ModelSubObject(
                                   w3dMesh.Header.MeshName,
                                   bones[0],
                                   mesh));
            }

            LoadAnimations(w3dFile, contentManager);

            return(new Model(
                       new ModelBoneHierarchy(bones),
                       subObjects.ToArray()));
        }
Exemplo n.º 2
0
        private static Model CreateModel(
            AssetLoadContext context,
            W3dFile w3dFile,
            ModelBoneHierarchy boneHierarchy)
        {
            //BoundingSphere boundingSphere = default(BoundingSphere);

            var w3dMeshes = w3dFile.GetMeshes();
            var w3dHLod   = w3dFile.GetHLod();

            var subObjects = new List <ModelSubObject>();

            if (w3dHLod != null)
            {
                foreach (var w3dSubObject in w3dHLod.Lods[0].SubObjects)
                {
                    // TODO: Collision boxes
                    var w3dMesh = w3dMeshes.FirstOrDefault(x => x.Header.ContainerName + "." + x.Header.MeshName == w3dSubObject.Name);
                    if (w3dMesh == null)
                    {
                        continue;
                    }

                    var bone = boneHierarchy.Bones[(int)w3dSubObject.BoneIndex];

                    var mesh = new ModelMesh(w3dMesh, context);

                    //var meshBoundingSphere = mesh.BoundingSphere.Transform(bone.Transform);

                    //boundingSphere = (i == 0)
                    //    ? meshBoundingSphere
                    //    : BoundingSphere.CreateMerged(boundingSphere, meshBoundingSphere);

                    subObjects.Add(new ModelSubObject(w3dSubObject.Name, bone, mesh));
                }
            }
            else if (w3dMeshes.Count > 0)
            {
                // Simple models can have only one mesh with no HLod chunk.
                if (w3dMeshes.Count != 1)
                {
                    throw new InvalidOperationException();
                }

                var w3dMesh = w3dMeshes[0];

                var mesh = new ModelMesh(w3dMesh, context);

                subObjects.Add(new ModelSubObject(
                                   w3dMesh.Header.MeshName,
                                   boneHierarchy.Bones[0],
                                   mesh));
            }
            else
            {
                // TODO: Some .w3d files contain a single W3D_BOX.
            }

            return(new Model(
                       Path.GetFileNameWithoutExtension(w3dFile.FilePath),
                       boneHierarchy,
                       subObjects.ToArray()));
        }