Exemplo n.º 1
0
        /// <summary>
        /// Create objects from model. Requires FrameMesh/FrameModel to be already set and a model already read into the data.
        /// </summary>
        public void CreateObjectsFromModel()
        {
            frameGeometry = new FrameGeometry();
            frameMaterial = new FrameMaterial();


            //set lods for all data.
            indexBuffers  = new IndexBuffer[model.Lods.Length];
            vertexBuffers = new VertexBuffer[model.Lods.Length];

            List <Vertex[]> vertData = new List <Vertex[]>();

            for (int i = 0; i != model.Lods.Length; i++)
            {
                vertData.Add(model.Lods[i].Vertices);

                if (model.Lods[i].VertexDeclaration.HasFlag(VertexFlags.Tangent))
                {
                    model.Lods[i].VertexDeclaration -= VertexFlags.Tangent;
                }
            }

            frameMesh.Boundings  = BoundingBoxExtenders.CalculateBounds(vertData);
            frameMaterial.Bounds = FrameMesh.Boundings;
            CalculateDecompression();
            UpdateObjectsFromModel();
            BuildIndexBuffer();
            BuildVertexBuffer();

            for (int i = 0; i < model.Lods.Length; i++)
            {
                frameGeometry.LOD[i].IndexBufferRef  = new Hash("M2TK." + model.Name + ".IB" + i);
                frameGeometry.LOD[i].VertexBufferRef = new Hash("M2TK." + model.Name + ".VB" + i);
            }
        }
Exemplo n.º 2
0
 public void ChangeFrameColor(Color color)
 {
     if (FrameMaterial != null)
     {
         FrameMaterial.SetColor(MATERIAL_COLOR_SETTING_NAME, color);
     }
 }
Exemplo n.º 3
0
        public static RenderModel BuildRenderModelFromFrame(FrameObjectSingleMesh Mesh)
        {
            if (Mesh.MaterialIndex == -1 && Mesh.MeshIndex == -1)
            {
                return(null);
            }

            FrameGeometry geom = SceneData.FrameResource.FrameGeometries[Mesh.Refs[FrameEntryRefTypes.Geometry]];
            FrameMaterial mat  = SceneData.FrameResource.FrameMaterials[Mesh.Refs[FrameEntryRefTypes.Material]];

            IndexBuffer[]  indexBuffers  = new IndexBuffer[geom.LOD.Length];
            VertexBuffer[] vertexBuffers = new VertexBuffer[geom.LOD.Length];

            // We need to retrieve buffers first.
            for (int c = 0; c != geom.LOD.Length; c++)
            {
                indexBuffers[c]  = SceneData.IndexBufferPool.GetBuffer(geom.LOD[c].IndexBufferRef.Hash);
                vertexBuffers[c] = SceneData.VertexBufferPool.GetBuffer(geom.LOD[c].VertexBufferRef.Hash);
            }

            if (indexBuffers[0] == null || vertexBuffers[0] == null)
            {
                return(null);
            }
            RenderModel model = new RenderModel();

            model.ConvertFrameToRenderModel(Mesh, geom, mat, indexBuffers, vertexBuffers);
            return(model);
        }
Exemplo n.º 4
0
 public void UpdateMaterials(FrameMaterial mats)
 {
     for (int i = 0; i != LODs.Length; i++)
     {
         for (int z = 0; z != LODs[i].ModelParts.Length; z++)
         {
             ulong hash = mats.Materials[i][z].MaterialHash;
             LODs[i].ModelParts[z].MaterialHash = hash;
             LODs[i].ModelParts[z].Material     = MaterialsManager.LookupMaterialByHash(hash);
         }
     }
     isUpdatedNeeded = true;
 }
Exemplo n.º 5
0
 public Model(FrameObjectSingleMesh frameMesh, IndexBuffer[] indexBuffers, VertexBuffer[] vertexBuffers)
 {
     this.frameMesh     = frameMesh;
     this.indexBuffers  = indexBuffers;
     this.vertexBuffers = vertexBuffers;
     frameGeometry      = frameMesh.Geometry;
     frameMaterial      = frameMesh.Material;
     model           = new M2TStructure();
     model.IsSkinned = false;
     model.Name      = frameMesh.Name.ToString();
     model.AOTexture = frameMesh.OMTextureHash.String;
     model.BuildLods(frameGeometry, frameMaterial, vertexBuffers, indexBuffers);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Constructor used to build Lods. This is used when you want to compile all mesh data together, ready for exporting.
 /// </summary>
 public Model(FrameObjectModel frameModel, IndexBuffer[] indexBuffers, VertexBuffer[] vertexBuffers,
              FrameGeometry frameGeometry, FrameMaterial frameMaterial)
 {
     this.frameModel    = frameModel;
     this.indexBuffers  = indexBuffers;
     this.vertexBuffers = vertexBuffers;
     this.frameGeometry = frameGeometry;
     this.frameMaterial = frameMaterial;
     model           = new M2TStructure();
     model.IsSkinned = true;
     model.Name      = frameMesh.Name.String;
     model.AOTexture = frameMesh.OMTextureHash.String;
     model.BuildLods(frameGeometry, frameMaterial, vertexBuffers, indexBuffers);
 }
Exemplo n.º 7
0
        /// <summary>
        /// Build Lods from retrieved data.
        /// </summary>
        public void BuildLods(FrameGeometry frameGeometry, FrameMaterial frameMaterial, VertexBuffer[] vertexBuffers, IndexBuffer[] indexBuffers)
        {
            lods = new Lod[frameGeometry.NumLods];
            for (int i = 0; i != lods.Length; i++)
            {
                FrameLOD frameLod = frameGeometry.LOD[i];
                lods[i] = new Lod
                {
                    VertexDeclaration = frameGeometry.LOD[i].VertexDeclaration
                };
                IndexBuffer  indexBuffer  = indexBuffers[i];
                VertexBuffer vertexBuffer = vertexBuffers[i];

                int vertexSize;
                Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = frameLod.GetVertexOffsets(out vertexSize);
                lods[i].Vertices = new Vertex[frameLod.NumVerts];

                if (vertexSize * frameLod.NumVerts != vertexBuffer.Data.Length)
                {
                    throw new System.Exception();
                }
                for (int v = 0; v != lods[i].Vertices.Length; v++)
                {
                    //declare data required and send to decompresser
                    byte[] data = new byte[vertexSize];
                    Array.Copy(vertexBuffers[i].Data, (v * vertexSize), data, 0, vertexSize);
                    lods[i].Vertices[v] = VertexTranslator.DecompressVertex(data, frameGeometry.LOD[i].VertexDeclaration, frameGeometry.DecompressionOffset, frameGeometry.DecompressionFactor, vertexOffsets);
                }

                lods[i].Indices = indexBuffer.GetData();
                MaterialStruct[] materials = frameMaterial.Materials[i];
                lods[i].Parts = new ModelPart[materials.Length];
                for (int x = 0; x != materials.Length; x++)
                {
                    if (string.IsNullOrEmpty(materials[x].MaterialName))
                    {
                        var material = MaterialsManager.LookupMaterialByHash(materials[x].MaterialHash);
                        materials[x].MaterialName = material.GetMaterialName();
                    }

                    ModelPart modelPart = new ModelPart();
                    modelPart.Material   = materials[x].MaterialName;
                    modelPart.StartIndex = (uint)materials[x].StartIndex;
                    modelPart.NumFaces   = (uint)materials[x].NumFaces;
                    lods[i].Parts[x]     = modelPart;
                }
            }
        }
Exemplo n.º 8
0
 public Model(FrameObjectModel frameModel, IndexBuffer[] indexBuffers, VertexBuffer[] vertexBuffers)
 {
     this.frameModel    = frameModel;
     this.indexBuffers  = indexBuffers;
     this.vertexBuffers = vertexBuffers;
     frameGeometry      = frameModel.Geometry;
     frameMaterial      = frameModel.Material;
     blendInfo          = frameModel.BlendInfo;
     skeleton           = frameModel.Skeleton;
     skeletonHierarchy  = frameModel.SkeletonHierarchy;
     model           = new M2TStructure();
     model.IsSkinned = true;
     model.Name      = frameModel.Name.ToString();
     model.AOTexture = frameModel.OMTextureHash.String;
     model.BuildLods(frameModel, indexBuffers, vertexBuffers);
 }
Exemplo n.º 9
0
        /// <summary>
        /// Create objects from model. Requires FrameMesh/FrameModel to be already set and a model already read into the data.
        /// </summary>
        public void CreateObjectsFromModel()
        {
            frameGeometry = new FrameGeometry();
            frameMaterial = new FrameMaterial();


            //set lods for all data.
            indexBuffers  = new IndexBuffer[model.Lods.Length];
            vertexBuffers = new VertexBuffer[model.Lods.Length];

            List <Vertex[]> vertData = new List <Vertex[]>();

            for (int i = 0; i != model.Lods.Length; i++)
            {
                vertData.Add(model.Lods[i].Vertices);
            }

            frameMesh.Boundings  = BoundingBoxExtenders.CalculateBounds(vertData);
            frameMaterial.Bounds = FrameMesh.Boundings;
            CalculateDecompression();
            UpdateObjectsFromModel();
            BuildIndexBuffer();
            BuildVertexBuffer();

            for (int i = 0; i < model.Lods.Length; i++)
            {
                var lod = frameGeometry.LOD[i];

                var size = 0;
                lod.GetVertexOffsets(out size);
                if (vertexBuffers[i].Data.Length != (size * lod.NumVerts))
                {
                    throw new SystemException();
                }
                lod.IndexBufferRef  = new HashName("M2TK." + model.Name + ".IB" + i);
                lod.VertexBufferRef = new HashName("M2TK." + model.Name + ".VB" + i);
            }

            if (model.IsSkinned)
            {
                CreateSkinnedObjectsFromModel();
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Build Lods from retrieved data.
        /// </summary>
        public void BuildLods(FrameGeometry frameGeometry, FrameMaterial frameMaterial, VertexBuffer[] vertexBuffers, IndexBuffer[] indexBuffers)
        {
            lods = new Lod[frameGeometry.NumLods];
            for (int i = 0; i != lods.Length; i++)
            {
                FrameLOD frameLod = frameGeometry.LOD[i];
                lods[i] = new Lod
                {
                    VertexDeclaration = frameGeometry.LOD[i].VertexDeclaration
                };
                IndexBuffer  indexBuffer  = indexBuffers[i];
                VertexBuffer vertexBuffer = vertexBuffers[i];

                int vertexSize;
                Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = frameLod.GetVertexOffsets(out vertexSize);

                lods[i].NumUVChannels = 4;
                lods[i].Vertices      = new Vertex[frameLod.NumVertsPr];

                for (int v = 0; v != lods[i].Vertices.Length; v++)
                {
                    Vertex vertex = new Vertex();
                    vertex.UVs = new Half2[lods[i].NumUVChannels];
                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Position))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.Position = VertexTranslator.ReadPositionDataFromVB(vertexBuffer.Data, startIndex, frameGeometry.DecompressionFactor, frameGeometry.DecompressionOffset);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Tangent))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.Tangent = VertexTranslator.ReadTangentDataFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Normals))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Normals].Offset;
                        vertex.Normal = VertexTranslator.ReadNormalDataFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Skin))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Skin].Offset;
                        vertex.BlendWeight = VertexTranslator.ReadBlendWeightFromVB(vertexBuffer.Data, startIndex);
                        vertex.BoneID      = VertexTranslator.ReadBlendIDFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Color))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Color].Offset;
                        vertex.Color0 = VertexTranslator.ReadColorFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords0].Offset;
                        vertex.UVs[0] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords1].Offset;
                        vertex.UVs[1] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords2].Offset;
                        vertex.UVs[2] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.ShadowTexture))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.ShadowTexture].Offset;
                        vertex.UVs[3] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Color1))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Color1].Offset;
                        vertex.Color1 = VertexTranslator.ReadColorFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.BBCoeffs))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.BBCoeffs].Offset;
                        vertex.BBCoeffs = VertexTranslator.ReadBBCoeffsVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.DamageGroup))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.DamageGroup].Offset;
                        vertex.DamageGroup = VertexTranslator.ReadDamageGroupFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].NormalMapInfoPresent)
                    {
                        vertex.BuildBinormals();
                    }

                    lods[i].Vertices[v] = vertex;
                    lods[i].Indices     = indexBuffer.Data;
                    MaterialStruct[] materials = frameMaterial.Materials[i];
                    lods[i].Parts = new ModelPart[materials.Length];
                    for (int x = 0; x != materials.Length; x++)
                    {
                        ModelPart modelPart = new ModelPart();
                        modelPart.Material   = materials[x].MaterialHash.ToString();
                        modelPart.StartIndex = (uint)materials[x].StartIndex;
                        modelPart.NumFaces   = (uint)materials[x].NumFaces;
                        lods[i].Parts[x]     = modelPart;
                    }
                }
            }
        }
Exemplo n.º 11
0
        public bool ConvertFrameToRenderModel(FrameObjectSingleMesh mesh, FrameGeometry geom, FrameMaterial mats, IndexBuffer[] indexBuffers, VertexBuffer[] vertexBuffers)
        {
            if (mesh == null || geom == null || mats == null || indexBuffers[0] == null || vertexBuffers[0] == null)
            {
                return(false);
            }

            aoHash = mesh.OMTextureHash;
            SetTransform(mesh.WorldTransform);
            //DoRender = (mesh.SecondaryFlags == 4097 ? true : false);
            BoundingBox = mesh.Boundings;
            LODs        = new LOD[geom.NumLods];

            for (int i = 0; i != geom.NumLods; i++)
            {
                LOD lod = new LOD();
                lod.Indices    = indexBuffers[i].GetData();
                lod.ModelParts = new ModelPart[mats.LodMatCount[i]];

                for (int z = 0; z != mats.Materials[i].Length; z++)
                {
                    lod.ModelParts[z]              = new ModelPart();
                    lod.ModelParts[z].NumFaces     = (uint)mats.Materials[i][z].NumFaces;
                    lod.ModelParts[z].StartIndex   = (uint)mats.Materials[i][z].StartIndex;
                    lod.ModelParts[z].MaterialHash = mats.Materials[i][z].MaterialHash;
                    lod.ModelParts[z].Material     = MaterialsManager.LookupMaterialByHash(lod.ModelParts[z].MaterialHash);
                }

                lod.Vertices = new VertexLayouts.NormalLayout.Vertex[geom.LOD[i].NumVerts];
                int vertexSize;
                Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = geom.LOD[i].GetVertexOffsets(out vertexSize);
                try
                {
                    for (int x = 0; x != lod.Vertices.Length; x++)
                    {
                        VertexLayouts.NormalLayout.Vertex vertex = new VertexLayouts.NormalLayout.Vertex();

                        //declare data required and send to decompresser
                        byte[] data = new byte[vertexSize];
                        Array.Copy(vertexBuffers[i].Data, (x * vertexSize), data, 0, vertexSize);
                        Vertex decompressed = VertexTranslator.DecompressVertex(data, geom.LOD[i].VertexDeclaration, geom.DecompressionOffset, geom.DecompressionFactor, vertexOffsets);

                        //retrieve the data we require
                        vertex.Position  = decompressed.Position;
                        vertex.Normal    = decompressed.Normal;
                        vertex.Tangent   = decompressed.Tangent;
                        vertex.Binormal  = decompressed.Binormal;
                        vertex.TexCoord0 = decompressed.UVs[0];
                        vertex.TexCoord7 = decompressed.UVs[3];

                        lod.Vertices[x] = vertex;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Error when creating renderable {1}!: \n{0}", ex.Message, mesh.Name.ToString()), "Toolkit");
                    return(false);
                }
                LODs[i] = lod;
            }

            SetupShaders();
            return(true);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Build Lods from retrieved data.
        /// </summary>
        public void BuildLods(FrameGeometry frameGeometry, FrameMaterial frameMaterial, VertexBuffer[] vertexBuffers, IndexBuffer[] indexBuffers)
        {
            lods = new Lod[frameGeometry.NumLods];
            for (int i = 0; i != lods.Length; i++)
            {
                FrameLOD frameLod = frameGeometry.LOD[i];
                lods[i] = new Lod
                {
                    VertexDeclaration = frameGeometry.LOD[i].VertexDeclaration
                };
                IndexBuffer  indexBuffer  = indexBuffers[i];
                VertexBuffer vertexBuffer = vertexBuffers[i];

                int vertexSize;
                Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = frameLod.GetVertexOffsets(out vertexSize);
                lods[i].Vertices = new Vertex[frameLod.NumVertsPr];

                if (vertexSize * frameLod.NumVertsPr != vertexBuffer.Data.Length)
                {
                    throw new System.Exception();
                }
                for (int v = 0; v != lods[i].Vertices.Length; v++)
                {
                    Vertex vertex = new Vertex();
                    vertex.UVs = new Half2[4];

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Position))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        var output     = VertexTranslator.ReadPositionDataFromVB(vertexBuffer.Data, startIndex, frameGeometry.DecompressionFactor, frameGeometry.DecompressionOffset);
                        vertex.Position = Vector3Extenders.FromVector4(output);
                        vertex.Binormal = new Vector3(output.X);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Tangent))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.Tangent = VertexTranslator.ReadTangentDataFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Normals))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Normals].Offset;
                        vertex.Normal = VertexTranslator.ReadNormalDataFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Skin))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Skin].Offset;
                        vertex.BoneWeights = VertexTranslator.ReadWeightsFromVB(vertexBuffer.Data, startIndex);
                        vertex.BoneIDs     = VertexTranslator.ReadBonesFromVB(vertexBuffer.Data, startIndex + 4);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Color))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Color].Offset;
                        vertex.Color0 = VertexTranslator.ReadColorFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Color1))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Color1].Offset;
                        vertex.Color1 = VertexTranslator.ReadColorFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords0].Offset;
                        vertex.UVs[0] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords1].Offset;
                        vertex.UVs[1] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords2].Offset;
                        vertex.UVs[2] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.ShadowTexture))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.ShadowTexture].Offset;
                        vertex.UVs[3] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.BBCoeffs))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.BBCoeffs].Offset;
                        vertex.BBCoeffs = VertexTranslator.ReadBBCoeffsVB(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.DamageGroup))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.DamageGroup].Offset;
                        vertex.DamageGroup = VertexTranslator.ReadDamageGroupFromVB(vertexBuffer.Data, startIndex);
                    }

                    lods[i].Vertices[v] = vertex;
                }

                lods[i].Indices = indexBuffer.Data;
                MaterialStruct[] materials = frameMaterial.Materials[i];
                lods[i].Parts = new ModelPart[materials.Length];
                for (int x = 0; x != materials.Length; x++)
                {
                    if (string.IsNullOrEmpty(materials[x].MaterialName))
                    {
                        var material = MaterialsManager.LookupMaterialByHash(materials[x].MaterialHash);
                        materials[x].MaterialName = material.MaterialName;
                    }

                    ModelPart modelPart = new ModelPart();
                    modelPart.Material   = materials[x].MaterialName;
                    modelPart.StartIndex = (uint)materials[x].StartIndex;
                    modelPart.NumFaces   = (uint)materials[x].NumFaces;
                    lods[i].Parts[x]     = modelPart;
                }
            }
        }
Exemplo n.º 13
0
        private void ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }

            if (e.ClickedItem.Name == "contextExtract3D")
            {
                FrameObjectSingleMesh mesh = treeView1.SelectedNode.Tag as FrameObjectSingleMesh;

                FrameGeometry  geom          = SceneData.FrameResource.FrameGeometries[mesh.Refs["Mesh"]];
                FrameMaterial  mat           = SceneData.FrameResource.FrameMaterials[mesh.Refs["Material"]];
                IndexBuffer[]  indexBuffers  = new IndexBuffer[geom.LOD.Length];
                VertexBuffer[] vertexBuffers = new VertexBuffer[geom.LOD.Length];

                //we need to retrieve buffers first.
                for (int c = 0; c != geom.LOD.Length; c++)
                {
                    indexBuffers[c]  = SceneData.IndexBufferPool.GetBuffer(geom.LOD[c].IndexBufferRef.uHash);
                    vertexBuffers[c] = SceneData.VertexBufferPool.GetBuffer(geom.LOD[c].VertexBufferRef.uHash);
                }

                Model newModel = new Model(mesh, indexBuffers, vertexBuffers, geom, mat);
                newModel.ModelStructure.ExportToM2T(ToolkitSettings.ExportPath + "\\");
                switch (ToolkitSettings.Format)
                {
                case 0:
                    newModel.ModelStructure.ExportToFbx(ToolkitSettings.ExportPath + "\\", false);
                    break;

                case 1:
                    newModel.ModelStructure.ExportToFbx(ToolkitSettings.ExportPath + "\\", true);
                    break;

                case 2:
                    newModel.ModelStructure.ExportToM2T(ToolkitSettings.ExportPath + "\\");
                    break;

                default:
                    Log.WriteLine("Error! Unknown value set for ToolkitSettings.Format!", LoggingTypes.ERROR);
                    break;
                }
            }
            else if (e.ClickedItem.Name == "contextUpdateParents")
            {
                FrameObjectBase obj    = treeView1.SelectedNode.Tag as FrameObjectBase;
                ListWindow      window = new ListWindow();
                window.PopulateForm(true);
                window.ShowDialog();
                if (window.type == -1)
                {
                    return;
                }

                int refID = (window.chosenObject as FrameEntry).RefID;
                obj.IsOnFrameTable     = true;
                obj.ParentIndex2.Index = window.chosenObjectIndex;
                obj.ParentIndex2.RefID = refID;
                obj.SubRef(FrameEntryRefTypes.Parent2);
                obj.AddRef(FrameEntryRefTypes.Parent2, refID);
                obj.UpdateNode();

                treeView1.Nodes.Remove(treeView1.SelectedNode);
                TreeNode newNode = CreateTreeNode(obj);
                treeView1.Nodes[window.chosenObjectIndex].Nodes.Add(newNode);
            }
        }
Exemplo n.º 14
0
        private void ExportModels(List <object> meshes)
        {
            string[]   fileNames = new string[meshes.Count];
            Vector3[]  filePos   = new Vector3[meshes.Count];
            Matrix33[] rotPos    = new Matrix33[meshes.Count];

            CustomEDD frameEDD = new CustomEDD();

            frameEDD.Entries = new List <CustomEDD.Entry>();

            Parallel.For(0, meshes.Count, i =>
            {
                CustomEDD.Entry entry        = new CustomEDD.Entry();
                FrameObjectSingleMesh mesh   = (meshes[i] as FrameObjectSingleMesh);
                FrameGeometry geom           = SceneData.FrameResource.FrameGeometries[mesh.Refs["Mesh"]];
                FrameMaterial mat            = SceneData.FrameResource.FrameMaterials[mesh.Refs["Material"]];
                IndexBuffer[] indexBuffers   = new IndexBuffer[geom.LOD.Length];
                VertexBuffer[] vertexBuffers = new VertexBuffer[geom.LOD.Length];

                //we need to retrieve buffers first.
                for (int c = 0; c != geom.LOD.Length; c++)
                {
                    indexBuffers[c]  = SceneData.IndexBufferPool.GetBuffer(geom.LOD[c].IndexBufferRef.uHash);
                    vertexBuffers[c] = SceneData.VertexBufferPool.GetBuffer(geom.LOD[c].VertexBufferRef.uHash);
                }

                Model newModel = new Model(mesh, indexBuffers, vertexBuffers, geom, mat);

                //if (mesh.ParentIndex1.Index != -1)
                //{
                //    FrameObjectBase parent = (SceneData.FrameResource.EntireFrame[mesh.ParentIndex1.Index] as FrameObjectBase);
                //    filePos[i] = RetrieveParent1Position(mesh);
                //}

                //if (((mesh.ParentIndex1.Index != -1)) && ((mesh.ParentIndex1.Index == mesh.ParentIndex2.Index)))
                //{
                //    FrameObjectFrame frame = SceneData.FrameResource.EntireFrame[mesh.ParentIndex1.Index] as FrameObjectFrame;
                //    if (frame.Item != null)
                //    {
                //        filePos[i] = frame.Item.Position;
                //    }
                //}

                entry.LodCount = newModel.ModelStructure.Lods.Length;
                entry.LODNames = new string[entry.LodCount];

                for (int c = 0; c != newModel.ModelStructure.Lods.Length; c++)
                {
                    newModel.ModelStructure.ExportToM2T(ToolkitSettings.ExportPath + "\\");
                    switch (ToolkitSettings.Format)
                    {
                    case 0:
                        newModel.ModelStructure.ExportToFbx(ToolkitSettings.ExportPath + "\\", false);
                        break;

                    case 1:
                        newModel.ModelStructure.ExportToFbx(ToolkitSettings.ExportPath + "\\", true);
                        break;

                    case 2:
                        newModel.ModelStructure.ExportToM2T(ToolkitSettings.ExportPath + "\\");
                        break;

                    default:
                        Log.WriteLine("Error! Unknown value set for ToolkitSettings.Format!", LoggingTypes.ERROR);
                        break;
                    }
                    Console.WriteLine(newModel.FrameMesh.Name.String);
                    if (newModel.FrameMesh.Name.String == "")
                    {
                        entry.LODNames[c] = newModel.FrameGeometry.LOD[c].VertexBufferRef.String;
                    }
                    else
                    {
                        entry.LODNames[c] = newModel.FrameMesh.Name.String;
                    }
                }
                entry.Position = mesh.Matrix.Position;
                entry.Rotation = mesh.Matrix.Rotation;

                frameEDD.Entries.Add(entry);
            });
            frameEDD.EntryCount = frameEDD.Entries.Count;
            using (BinaryWriter writer = new BinaryWriter(File.Create(ToolkitSettings.ExportPath + "\\" + "frame.edd")))
            {
                frameEDD.WriteToFile(writer);
            }
        }
Exemplo n.º 15
0
        public bool ConvertFrameToRenderModel(FrameObjectSingleMesh mesh, FrameGeometry geom, FrameMaterial mats, IndexBuffer[] indexBuffers, VertexBuffer[] vertexBuffers)
        {
            if (mesh == null || geom == null || mats == null || indexBuffers == null || vertexBuffers == null)
            {
                return(false);
            }

            aoHash = mesh.OMTextureHash;
            SetTransform(mesh.Matrix.Position, mesh.Matrix.Matrix);
            //DoRender = (mesh.SecondaryFlags == 4097 ? true : false);
            BoundingBox = new RenderBoundingBox();
            BoundingBox.Init(mesh.Boundings);
            BoundingBox.SetTransform(Transform);
            BoundingBox.DoRender = false;
            LODs = new LOD[geom.NumLods];

            for (int i = 0; i != geom.NumLods; i++)
            {
                LOD lod = new LOD();
                lod.Indices    = indexBuffers[i].Data;
                lod.ModelParts = new ModelPart[mats.LodMatCount[i]];

                for (int z = 0; z != mats.Materials[i].Length; z++)
                {
                    lod.ModelParts[z]              = new ModelPart();
                    lod.ModelParts[z].NumFaces     = (uint)mats.Materials[i][z].NumFaces;
                    lod.ModelParts[z].StartIndex   = (uint)mats.Materials[i][z].StartIndex;
                    lod.ModelParts[z].MaterialHash = mats.Materials[i][z].MaterialHash;
                    lod.ModelParts[z].Material     = MaterialsManager.LookupMaterialByHash(lod.ModelParts[z].MaterialHash);
                }

                lod.Vertices = new VertexLayouts.NormalLayout.Vertex[geom.LOD[i].NumVertsPr];
                int vertexSize;
                Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = geom.LOD[i].GetVertexOffsets(out vertexSize);

                for (int x = 0; x != lod.Vertices.Length; x++)
                {
                    VertexLayouts.NormalLayout.Vertex vertex = new VertexLayouts.NormalLayout.Vertex();

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Position))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.Position = VertexTranslator.ReadPositionDataFromVB(vertexBuffers[i].Data, startIndex, geom.DecompressionFactor, geom.DecompressionOffset);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Tangent))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.Tangent = VertexTranslator.ReadTangentDataFromVB(vertexBuffers[i].Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Normals))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.Normals].Offset;
                        vertex.Normal = VertexTranslator.ReadNormalDataFromVB(vertexBuffers[i].Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Skin))
                    {
                        //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.BlendData].Offset;
                        // vertex.BlendWeight = VertexTranslator.ReadBlendWeightFromVB(vertexBuffer.Data, startIndex);
                        // vertex.BoneID = VertexTranslator.ReadBlendIDFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Color))
                    {
                        //unknown
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.TexCoords0].Offset;
                        vertex.TexCoord0 = VertexTranslator.ReadTexcoordFromVB(vertexBuffers[i].Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1))
                    {
                        //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords1].Offset;
                        //vertex.UVs[1] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2))
                    {
                        //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords2].Offset;
                        //vertex.UVs[2] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.ShadowTexture))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.ShadowTexture].Offset;
                        vertex.TexCoord7 = VertexTranslator.ReadTexcoordFromVB(vertexBuffers[i].Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Color1))
                    {
                        //unknown
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.BBCoeffs))
                    {
                        //unknown
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.DamageGroup))
                    {
                        //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.DamageGroup].Offset;
                        //vertex.DamageGroup = VertexTranslator.ReadDamageGroupFromVB(vertexBuffer.Data, startIndex);
                    }

                    lod.Vertices[x] = vertex;
                }
                LODs[i] = lod;
            }

            SetupShaders();
            return(true);
        }