Ejemplo n.º 1
0
 public void RemapSkin(int[] vertexRemap)
 {
     if (SkinInformation != null && vertexRemap != null)
     {
         SkinInformation.Remap(vertexRemap);
     }
 }
Ejemplo n.º 2
0
 public void RemapSkin(GraphicsStream vertexRemapStream)
 {
     if (SkinInformation != null && vertexRemapStream != null)
     {
         int[] vertexRemap = vertexRemap = vertexRemapStream.Read(typeof(int), new int[] { MeshData.Mesh.NumberVertices }) as int[];
         SkinInformation.Remap(vertexRemap);
     }
 }
Ejemplo n.º 3
0
            public override MeshContainer CreateMeshContainer(string name, MeshData meshData,
                                                              ExtendedMaterial[] materials, EffectInstance[] effectInstances, GraphicsStream adjacency,
                                                              SkinInformation skinInfo)
            {
                BonsaiMeshContainer mc = new BonsaiMeshContainer(this.xFilePath, name, meshData, materials,
                                                                 effectInstances, adjacency, skinInfo);

                mc.Initialize();
                return(mc);
            }
Ejemplo n.º 4
0
            /// <summary>Create a new mesh container</summary>
            public override MeshContainer CreateMeshContainer(string name,
                                                              MeshData meshData, ExtendedMaterial[] materials,
                                                              EffectInstance[] effectInstances, GraphicsStream adjacency,
                                                              SkinInformation skinInfo)
            {
                // We only handle meshes here
                if (meshData.Mesh == null)
                {
                    throw new ArgumentException();
                }

                // We must have a vertex format mesh
                if (meshData.Mesh.VertexFormat == VertexFormats.None)
                {
                    throw new ArgumentException();
                }

                AnimationMeshContainer mesh = new AnimationMeshContainer();

                mesh.adjency = adjacency;
                mesh.Name    = name;
                int    numFaces = meshData.Mesh.NumberFaces;
                Device dev      = meshData.Mesh.Device;


                // Store the materials
                mesh.SetMaterials(materials);
                mesh.SetAdjacency(adjacency);

                Texture[] meshTextures = new Texture[materials.Length];
                mesh.MeshData = meshData;

                // If there is skinning info, save any required data
                if (skinInfo != null)
                {
                    mesh.SkinInformation = skinInfo;
                    int      numBones       = skinInfo.NumberBones;
                    Matrix[] offsetMatrices = new Matrix[numBones];

                    for (int i = 0; i < numBones; i++)
                    {
                        offsetMatrices[i] = skinInfo.GetBoneOffsetMatrix(i);
                    }

                    mesh.SetOffsetMatrices(offsetMatrices);

                    GenerateSkinnedMesh(mesh, adjacency);
                }

                return(mesh);
            }
Ejemplo n.º 5
0
 private void LoadData()
 {
     try
     {
         imageIdentifiers = identifiers.ReadObject <ImageIdentifiers>();
     }
     catch
     {
         imageIdentifiers = new ImageIdentifiers();
     }
     try
     {
         skinInformation = skininfo.ReadObject <SkinInformation>();
     }
     catch
     {
         skinInformation = new SkinInformation();
     }
     try
     {
         imageUrls = urls.ReadObject <ImageURLs>();
     }
     catch
     {
         imageUrls = new ImageURLs();
     }
     if (skinInformation == null)
     {
         skinInformation = new SkinInformation();
     }
     if (imageIdentifiers == null)
     {
         imageIdentifiers = new ImageIdentifiers();
     }
     if (imageUrls == null)
     {
         imageUrls = new ImageURLs();
     }
 }
Ejemplo n.º 6
0
            //AllocateHierarchy
            public override MeshContainer CreateMeshContainer(
                string name,
                MeshData meshData,
                ExtendedMaterial[] materials,
                EffectInstance[] effectInstances,
                GraphicsStream adjacency,
                SkinInformation skinInfo)
            {
                if (meshData.Mesh == null)
                {
                    throw new OpsException("Expecting Microsoft.DirectX.Direct3D.Mesh not Microsoft.DirectX.Direct3D.ProgressiveMesh or Microsoft.DirectX.Direct3D.PatchMesh.");
                }

                OpsMeshContainer mc = new OpsMeshContainer();

                mc.Name     = name;
                mc.MeshData = meshData;
                mc.SetMaterials(materials);
                mc.SetEffectInstances(effectInstances);
                mc.SetAdjacency(adjacency);
                mc.SkinInformation = skinInfo;

                return(mc);
            }
Ejemplo n.º 7
0
            //AllocateHierarchy
            public override MeshContainer CreateMeshContainer(
                string name,
                MeshData meshData,
                ExtendedMaterial[] materials,
                EffectInstance[] effectInstances,
                GraphicsStream adjacency,
                SkinInformation skinInfo)
            { 
                if(meshData.Mesh == null )
                    throw new OpsException("Expecting Microsoft.DirectX.Direct3D.Mesh not Microsoft.DirectX.Direct3D.ProgressiveMesh or Microsoft.DirectX.Direct3D.PatchMesh.");

                OpsMeshContainer mc= new OpsMeshContainer();
                mc.Name= name;
                mc.MeshData= meshData;
                mc.SetMaterials(materials);
                mc.SetEffectInstances(effectInstances);
                mc.SetAdjacency(adjacency);
                mc.SkinInformation = skinInfo;

                return mc;
            }    
Ejemplo n.º 8
0
        /// <summary>
        /// Create a new packmesh container
        /// </summary>
        /// <returns>The newly created container</returns>
        ///
        public override MeshContainer CreateMeshContainer(string name,
                                                          MeshData meshData, ExtendedMaterial[] materials,
                                                          EffectInstance effectInstances, GraphicsStream adjacency,
                                                          SkinInformation skinInfo)
        {
            // We only handle meshes here
            if (meshData.Mesh == null)
            {
                throw new ArgumentException();
            }

            // We must have a vertex format packmesh
            if (meshData.Mesh.VertexFormat == VertexFormats.None)
            {
                throw new ArgumentException();
            }

            Packmesh packmesh = new Packmesh();

            packmesh.Name = name;
            int    numFaces = meshData.Mesh.NumberFaces;
            Device dev      = meshData.Mesh.Device;

            // Make sure there are normals
            if ((meshData.Mesh.VertexFormat & VertexFormats.Normal) == 0)
            {
                // Clone the packmesh
                Mesh tempMesh = meshData.Mesh.Clone(meshData.Mesh.Options.Value,
                                                    meshData.Mesh.VertexFormat | VertexFormats.Normal, dev);

                meshData.Mesh = tempMesh;
                meshData.Mesh.ComputeNormals();
            }

            // Store the materials
            packmesh.SetMaterials(materials);
            packmesh.SetAdjacency(adjacency);
            Texture[] texturas = new Texture[materials.Length];

            // Create any textures
            for (int i = 0; i < materials.Length; i++)
            {
                if (materials[i].TextureFilename != null)
                {
                    texturas[i] = TextureLoader.FromFile(dev, @"..\..\" +
                                                         materials[i].TextureFilename);
                }
            }
            packmesh.SetTextures(texturas);
            packmesh.MeshData = meshData;

            // If there is skinning info, save any required data
            if (skinInfo != null)
            {
                packmesh.SkinInformation = skinInfo;
                int      nOssos         = skinInfo.NumberBones;
                Matrix[] offsetMatrices = new Matrix[nOssos];

                for (int i = 0; i < nOssos; i++)
                {
                    offsetMatrices[i] = skinInfo.GetBoneOffsetMatrix(i);
                }

                packmesh.SetOffsetMatrices(offsetMatrices);

                app.gerarModeloElastico(packmesh);
            }

            return(packmesh);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Constructor, called by HierarchyAllocator
        /// </summary>
        /// <param name="subfolder">textures are found in ../Cells/subfolder, where subfolder is the cell group name</param>
        /// <param name="name">mesh name (read from the X file)</param>
        /// <param name="mesh">the source mesh (as a standard mesh)</param>
        /// <param name="extmaterials">Materials and texture filenames</param>
        /// <param name="effectInstances">Effects</param>
        /// <param name="adjacency">Mesh adjacency data</param>
        /// <param name="skinInfo">Skin information for the mesh</param>
        public Cytoplasm(
            string subfolder,
            string name,
            Mesh mesh,
            ExtendedMaterial[] extmaterials,
            EffectInstance[] effectInstances,
            GraphicsStream adjacency,
            SkinInformation skinInfo)
        {
            // Store the name
            Name = name;

            // Keep the original mesh because this is needed for MousePick()
            // (ProgressiveMeshes don't have a .Intersect() method)
            originalMesh = mesh;

            // Store the materials
            int matlength = 0;
            if (extmaterials != null)
                matlength = extmaterials.Length;
            materials = new Material[matlength];
            textures = new Texture[matlength];
            bumps = new Texture[matlength];
            for (int i = 0; i < matlength; i++)
            {
                materials[i] = extmaterials[i].Material3D;
                // TRUESPACE HACK: Truespace doesn't allow me to set any ambient values in materials,
                // which means that everything comes out black if it isn't lit by the diffuse light.
                // So add a default ambient value here for any black cells, to simulate reflection from terrain
                if ((materials[i].Ambient.R + materials[i].Ambient.G + materials[i].Ambient.B) == 0)
                    materials[i].Ambient = Color.FromArgb(68, 68, 68);

                if ((extmaterials[i].TextureFilename != null) && (extmaterials[i].TextureFilename !=
                    string.Empty))
                {
                    try
                    {
                        // We have a texture file, rather than an inline texture, so try to load it from ./cells/group/type
                        textures[i] = TextureLoader.FromFile(Engine.Device,
                            FileResource.Fsp(subfolder, extmaterials[i].TextureFilename));
                        // Also attempt to load a normal map, if any
                        try
                        {
                            string filename = System.IO.Path.GetFileNameWithoutExtension(extmaterials[i].TextureFilename) + " Normal.PNG";
                            bumps[i] = TextureLoader.FromFile(Engine.Device,
                                FileResource.Fsp(subfolder, filename));
                        }
                        catch { }
                    }
                    catch
                    {
                        throw new SDKException("Failed to load texture " + FileResource.Fsp(subfolder, extmaterials[i].TextureFilename));
                    }
                }
            }

            // Get access to the vertices to allow various operations
            // get the input vertices as an array (in case I want to modify them)
            VertexBuffer vb = mesh.VertexBuffer;						// Retrieve the vertex buffer data
            System.Array vert = null;                                   // We don't know if array will be PositionNormal or PositionNormalTextured, so use generic type

            if (mesh.VertexFormat == VertexFormats.PositionNormal)
            {
                vert = vb.Lock(0,
                    typeof(CustomVertex.PositionNormal),
                    LockFlags.ReadOnly,
                    mesh.NumberVertices);
            }
            else
            {
                vert = vb.Lock(0,
                    typeof(CustomVertex.PositionNormalTextured),
                    LockFlags.ReadOnly,
                    mesh.NumberVertices);
            }

            // compute the bounding sphere radius & centre from the vertices
            // NOTE: THIS VALUE IS FOR THE UNSCALED VERTICES and needs to be transformed by the combined transformation matrix
            boundRadius = Geometry.ComputeBoundingSphere(vert,
                                                            mesh.VertexFormat,
                                                            out boundCentre);

            // Calculate a bounding box for fine collision detection
            // NOTE: THIS VALUE IS FOR THE UNSCALED VERTICES and needs to be transformed by the combined transformation matrix
            Geometry.ComputeBoundingBox(vert, mesh.VertexFormat, out minOBB, out maxOBB);

            // gather useful debug info while we have the vertices
            //			Debug.WriteLine(String.Format("		Loaded mesh [{0}] from disk. {1} vertices, {2} textures, {3} materials",
            //											name,md.Mesh.NumberVertices,textures.Length, materials.Length));
            //						Debug.WriteLine(String.Format("Mesh is centred on {0} with bound radius {1}; OBB is {2}:{3}",
            //											boundcentre, boundradius, OBBmin, OBBmax));

            vb.Unlock();
            vb.Dispose();														// vertices no longer needed

            // create a cleaned progressive mesh from the input
            using (Mesh clean = Mesh.Clean(CleanType.Optimization, mesh, adjacency, adjacency))
            {
                // From the cleaned mesh, create one that has binormals and tangents as well as normals
                // (ThreeDee.BinormalVertex). These are needed for normal mapping in the shader
                using (Mesh clone = clean.Clone(MeshFlags.Managed, BinormalVertex.VertexElements, Engine.Device))
                {
                    // Add tangents and binormals
                    clone.ComputeTangent(0, 0, 0, 0);
                    //clone.ComputeTangentFrame(0);

                    // Create a new progressive mesh from the clean version
                    MeshData md = new MeshData();
                    md.ProgressiveMesh = new ProgressiveMesh(clone,
                                                                adjacency,
                                                                null,
                                                                12,					// min acceptable # faces
                                                                MeshFlags.SimplifyFace);
                    this.MeshData = md;
                }

            }

            // Add a device reset handler to reload resources (if required)
            /////Engine.Device.DeviceReset += new System.EventHandler(OnReset);
        }
Ejemplo n.º 10
0
 public BonsaiMeshContainer(string xFilePath, string name, MeshData meshData, ExtendedMaterial[] materials,
                            EffectInstance[] effectInstances, GraphicsStream adjacency, SkinInformation skinInfo)
     : base()
 {
     this.Name     = name;
     this.MeshData = meshData;
     this.SetMaterials(materials);
     this.SetEffectInstances(effectInstances);
     this.SetAdjacency(adjacency);
     this.SkinInformation = skinInfo;
     this.xFilePath       = xFilePath;
 }
        public override MeshContainer CreateMeshContainer(string name, MeshData meshData, ExtendedMaterial[] materials, EffectInstance[] effectInstances, GraphicsStream adjacency, SkinInformation skinInfo)
        {
            if (meshData.Mesh == null)
            {
                throw new ArgumentException();
            }
            if (meshData.Mesh.VertexFormat == VertexFormats.None)
            {
                throw new ArgumentException();
            }
            MeshContainerDerived mesh = new MeshContainerDerived();

            mesh.Name = name;
            int    numFaces = meshData.Mesh.NumberFaces;
            Device dev      = meshData.Mesh.Device;

            if ((meshData.Mesh.VertexFormat & VertexFormats.Normal) == 0)
            {
                Mesh tempMesh = meshData.Mesh.Clone(meshData.Mesh.Options.Value, meshData.Mesh.VertexFormat | VertexFormats.Normal, dev);
                meshData.Mesh = tempMesh;
                meshData.Mesh.ComputeNormals();
            }
            mesh.SetMaterials(materials);
            mesh.SetAdjacency(adjacency);
            Texture[] meshTextures = new Texture[materials.Length];

            for (int i = 0; i < materials.Length; i++)
            {
                if (!string.IsNullOrEmpty(materials[i].TextureFilename))
                {
                    meshTextures[i] = TextureLoader.FromStream(dev, ResourcesLoader.LoadStream(materials[i].TextureFilename));
                }
            }
            mesh.meshTextures = meshTextures;
            mesh.MeshData     = meshData;

            if (skinInfo != null)
            {
                mesh.SkinInformation = skinInfo;
                int      numBones       = skinInfo.NumberBones;
                Matrix[] offsetMatrices = new Matrix[numBones];

                for (int i = 0; i < numBones; i++)
                {
                    offsetMatrices[i] = skinInfo.GetBoneOffsetMatrix(i);
                }
                mesh.offsetMatrices = offsetMatrices;
                MeshContainerDerived.GenerateSkinnedMesh(mesh);
            }
            return(mesh);
        }
Ejemplo n.º 12
0
        private static List <ModelGeometry> LoadController(XElement element, SemanticAssociations association, out SkinInformation skinInfo)
        {
            skinInfo = new SkinInformation();
            XElement skin = element.GetReference().GetChild("skin");

            if (skin == null)
            {
                //only skinned animation supported
                throw new NotSupportedException("Only Skin Animation supported");
            }

            //target geometry
            string geometryUrl = skin.GetAttribute("source");

            //bind matrix
            XElement bind = skin.GetChild("bind_shape_matrix");

            if (bind == null)
            {
                skinInfo.BindMatrix = Matrix.Identity;
            }
            else
            {
                skinInfo.BindMatrix = MatrixFromString(bind.Value);
            }

            //load joint and input
            XElement joints = skin.GetChild("joints");

            var jointName = joints.GetChildren("input").Where(i => i.GetAttribute("semantic") == "JOINT").First();

            skinInfo.JointNames = jointName.GetSource().Value.Replace('\n', ' ').Split(' ').Where(s => !string.IsNullOrWhiteSpace(s)).ToList();


            var bindMatrix = joints.GetChildren("input").Where(i => i.GetAttribute("semantic") == "INV_BIND_MATRIX").First();
            var values     = GetFloatList(bindMatrix.GetSource().Value);

            for (int i = 0; i < values.Count; i += 16)
            {
                skinInfo.InverseBinding.Add(new Matrix(values.GetRange(i, 16).ToArray()).ToTranspose());
            }


            //Weight
            XElement vertex_weights = skin.GetChild("vertex_weights");
            var      weights        = GetFloatList(vertex_weights.GetChildren("input").Where(i => i.GetAttribute("semantic") == "WEIGHT").First().GetSource().Value);
            var      v      = GetIntList(vertex_weights.GetChild("v").Value);
            var      vCount = GetIntList(vertex_weights.GetChild("vcount").Value);

            //Prepare vertex data
            int            k           = 0;
            List <Vector4> weightsTemp = new List <Vector4>();
            List <Vector4> jointsTemp  = new List <Vector4>();

            foreach (int i in vCount)
            {
                Vector4 wei = new Vector4();
                Vector4 joy = new Vector4();


                for (int j = 0; j < i; j++)
                {
                    switch (j)
                    {
                    case 0:
                        joy.X = v[k]; k++;
                        wei.X = weights[v[k]]; k++;
                        break;

                    case 1:
                        joy.Y = v[k]; k++;
                        wei.Y = weights[v[k]]; k++;
                        break;

                    case 2:
                        joy.Z = v[k]; k++;
                        wei.Z = weights[v[k]]; k++;
                        break;

                    case 3:
                        joy.W = v[k]; k++;
                        wei.W = weights[v[k]]; k++;
                        break;

                    default:
                        break;
                    }
                }

                float sum = wei.X + wei.Y + wei.Z + wei.W;
                wei *= 1.0F / sum;

                weightsTemp.Add(wei);
                jointsTemp.Add(joy);
            }

            return(LoadGeometries(element.Document.GetByID(geometryUrl.Replace("#", "")), association, weightsTemp, jointsTemp));
        }