Esempio n. 1
0
        public Vector3 GetVertexTangent(int vertexIndex)
        {
            if (m_tangents == null)
            {
                m_importer.ImportData(AssetName, new string[] { MyImporterConstants.TAG_TANGENTS });
                Dictionary <string, object> tagData = m_importer.GetTagData();
                if (tagData.ContainsKey(MyImporterConstants.TAG_TANGENTS))
                {
                    m_tangents = (Byte4[])tagData[MyImporterConstants.TAG_TANGENTS];
                }
            }

            if (m_tangents != null)
            {
                return(VF_Packer.UnpackNormal(m_tangents[vertexIndex]));
            }

            return(Vector3.Zero);
        }
Esempio n. 2
0
        private static bool ExportDataPackedAsHV4(this BinaryWriter writer, string tagName, Vector3[] vectorArray)
        {
            WriteTag(writer, tagName);

            if (vectorArray == null)
            {
                writer.Write(0);
                return(true);
            }

            writer.Write(vectorArray.Length);
            foreach (var vectorVal in vectorArray)
            {
                var v      = vectorVal;
                var vector = VF_Packer.PackPosition(ref v);
                WriteHalfVector4(writer, ref vector);
            }

            return(true);
        }
            public bool ExportDataPackedAsB4(string tagName, Vector3[] vctArr)
            {
                WriteTag(tagName);
                if (vctArr == null)
                {
                    _writer.Write(0);
                    return(true);
                }

                _writer.Write(vctArr.Length);
                foreach (var vector in vctArr)
                {
                    WriteByte4(new Byte4
                    {
                        PackedValue = VF_Packer.PackNormal(vector)
                    });
                }

                return(true);
            }
Esempio n. 4
0
        private static bool ExportDataPackedAsB4(this BinaryWriter writer, string tagName, Vector3[] vectorArray)
        {
            WriteTag(writer, tagName);

            if (vectorArray == null)
            {
                writer.Write(0);
                return(true);
            }

            writer.Write(vectorArray.Length);
            foreach (var vectorVal in vectorArray)
            {
                var v      = vectorVal;
                var vector = new Byte4();
                vector.PackedValue = VF_Packer.PackNormal(ref v);
                WriteByte4(writer, ref vector);
            }

            return(true);
        }
Esempio n. 5
0
        public bool ExportDataPackedAsB4(string tagName, Vector3[] vctArr)
        {
            WriteTag(tagName);

            if (vctArr == null)
            {
                m_writer.Write(0);
                return(true);
            }

            m_writer.Write(vctArr.Length);
            foreach (Vector3 vctVal in vctArr)
            {
                Vector3 v   = vctVal;
                Byte4   vct = new Byte4();
                vct.PackedValue = VF_Packer.PackNormal(ref v);
                WriteByte4(vct);
            }

            return(true);
        }
Esempio n. 6
0
 public Vector3 GetVertexNormal(int vertexIndex)
 {
     return(VF_Packer.UnpackNormal(ref m_vertices[vertexIndex].Normal));
 }
Esempio n. 7
0
 public Vector3 GetVertexInt(int vertexIndex)
 {
     return(VF_Packer.UnpackPosition(ref m_vertices[vertexIndex].Position));
 }
Esempio n. 8
0
        MyRenderMeshInfo LoadMesh(string assetName, out MyLODDescriptor[] LodDescriptors)
        {
            //Debug.Assert(assetName.EndsWith(".mwm"));
            #region Temporary for mwm endings
            if (!assetName.EndsWith(".mwm"))
            {
                assetName += ".mwm";
            }
            #endregion

            var meshVertexInput = MyVertexInputLayout.Empty;
            LodDescriptors = null;
            MyRenderMeshInfo result = new MyRenderMeshInfo();

            var importer = new MyModelImporter();
            var fsPath   = Path.IsPathRooted(assetName) ? assetName : Path.Combine(MyFileSystem.ContentPath, assetName);

            if (!MyFileSystem.FileExists(fsPath))
            {
                System.Diagnostics.Debug.Fail("Model " + assetName + " does not exists!");

                return(MyAssetsLoader.GetDebugMesh().LODs[0].m_meshInfo);
            }


            string contentPath = null;
            if (Path.IsPathRooted(assetName) && assetName.ToLower().Contains("models"))
            {
                contentPath = assetName.Substring(0, assetName.ToLower().IndexOf("models"));
            }

            try
            {
                importer.ImportData(fsPath, new string[]
                {
                    MyImporterConstants.TAG_VERTICES,
                    MyImporterConstants.TAG_BLENDINDICES,
                    MyImporterConstants.TAG_BLENDWEIGHTS,
                    MyImporterConstants.TAG_NORMALS,
                    MyImporterConstants.TAG_TEXCOORDS0,
                    MyImporterConstants.TAG_TANGENTS,
                    MyImporterConstants.TAG_BINORMALS,
                    MyImporterConstants.TAG_BONES,
                    MyImporterConstants.TAG_MESH_PARTS,
                    MyImporterConstants.TAG_BOUNDING_BOX,
                    MyImporterConstants.TAG_BOUNDING_SPHERE,
                    MyImporterConstants.TAG_LODS,
                });
                Dictionary <string, object> tagData = importer.GetTagData();

                // extract data
                var positions = (HalfVector4[])tagData[MyImporterConstants.TAG_VERTICES];
                System.Diagnostics.Debug.Assert(positions.Length > 0);
                var verticesNum     = positions.Length;
                var boneIndices     = (Vector4I[])tagData[MyImporterConstants.TAG_BLENDINDICES];
                var boneWeights     = (Vector4[])tagData[MyImporterConstants.TAG_BLENDWEIGHTS];
                var normals         = (Byte4[])tagData[MyImporterConstants.TAG_NORMALS];
                var texcoords       = (HalfVector2[])tagData[MyImporterConstants.TAG_TEXCOORDS0];
                var tangents        = (Byte4[])tagData[MyImporterConstants.TAG_TANGENTS];
                var bintangents     = (Byte4[])tagData[MyImporterConstants.TAG_BINORMALS];
                var tangentBitanSgn = new Byte4[verticesNum];
                for (int i = 0; i < verticesNum; i++)
                {
                    var N = VF_Packer.UnpackNormal(normals[i].PackedValue);
                    var T = VF_Packer.UnpackNormal(tangents[i].PackedValue);
                    var B = VF_Packer.UnpackNormal(bintangents[i].PackedValue);

                    var tanW = new Vector4(T.X, T.Y, T.Z, 0);

                    tanW.W             = T.Cross(N).Dot(B) < 0 ? -1 : 1;
                    tangentBitanSgn[i] = VF_Packer.PackTangentSignB4(ref tanW);
                }
                bool hasBonesInfo = false;
                if (boneIndices.Length > 0 && boneWeights.Length > 0)
                {
                    hasBonesInfo = true;
                }
                var bones = (MyModelBone[])tagData[MyImporterConstants.TAG_BONES];

                //
                var           vertexBuffers = new List <VertexBufferId>();
                IndexBufferId indexBuffer   = IndexBufferId.NULL;
                var           submeshes     = new Dictionary <string, List <MyDrawSubmesh> >();
                var           submeshes2    = new Dictionary <string, List <MySubmeshInfo> >();
                var           submeshesMeta = new List <MySubmeshInfo>();

                int  indicesNum      = 0;
                bool missingMaterial = false;
                if (tagData.ContainsKey(MyImporterConstants.TAG_MESH_PARTS))
                {
                    var  indices  = new List <uint>(positions.Length);
                    uint maxIndex = 0;

                    var meshParts = tagData[MyImporterConstants.TAG_MESH_PARTS] as List <MyMeshPartInfo>;
                    foreach (MyMeshPartInfo meshPart in meshParts)
                    {
                        # region Bones indirection
                        int[] bonesRemapping = null;
                        if (boneIndices.Length > 0 && bones.Length > MyRender11Constants.SHADER_MAX_BONES)
                        {
                            Dictionary <int, int> vertexChanged = new Dictionary <int, int>();

                            Dictionary <int, int> bonesUsed = new Dictionary <int, int>();

                            int trianglesNum = meshPart.m_indices.Count / 3;
                            for (int i = 0; i < trianglesNum; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    int index = meshPart.m_indices[i * 3 + j];
                                    if (boneWeights[index].X > 0)
                                    {
                                        bonesUsed[boneIndices[index].X] = 1;
                                    }
                                    if (boneWeights[index].Y > 0)
                                    {
                                        bonesUsed[boneIndices[index].Y] = 1;
                                    }
                                    if (boneWeights[index].Z > 0)
                                    {
                                        bonesUsed[boneIndices[index].Z] = 1;
                                    }
                                    if (boneWeights[index].W > 0)
                                    {
                                        bonesUsed[boneIndices[index].W] = 1;
                                    }
                                }
                            }

                            if (bonesUsed.Count > MyRender11Constants.SHADER_MAX_BONES)
                            {
                                Debug.Assert(bonesUsed.Count <= MyRender11Constants.SHADER_MAX_BONES, "Model \"" + assetName + "\"'s part uses more than 60 bones, please split model on more parts");
                            }

                            var partBones = new List <int>(bonesUsed.Keys);
                            partBones.Sort();
                            if (partBones.Count > 0 && partBones[partBones.Count - 1] >= MyRender11Constants.SHADER_MAX_BONES)
                            {
                                for (int i = 0; i < partBones.Count; i++)
                                {
                                    bonesUsed[partBones[i]] = i;
                                }

                                Dictionary <int, int> vertexTouched = new Dictionary <int, int>();

                                for (int i = 0; i < trianglesNum; i++)
                                {
                                    for (int j = 0; j < 3; j++)
                                    {
                                        int index = meshPart.m_indices[i * 3 + j];
                                        if (!vertexTouched.ContainsKey(index))
                                        {
                                            if (boneWeights[index].X > 0)
                                            {
                                                boneIndices[index].X = bonesUsed[boneIndices[index].X];
                                            }
                                            if (boneWeights[index].Y > 0)
                                            {
                                                boneIndices[index].Y = bonesUsed[boneIndices[index].Y];
                                            }
                                            if (boneWeights[index].Z > 0)
                                            {
                                                boneIndices[index].Z = bonesUsed[boneIndices[index].Z];
                                            }
                                            if (boneWeights[index].W > 0)
                                            {
                                                boneIndices[index].W = bonesUsed[boneIndices[index].W];
                                            }

                                            vertexTouched[index] = 1;

                                            int changes = 0;
                                            vertexChanged.TryGetValue(index, out changes);
                                            vertexChanged[index] = changes + 1;
                                        }
                                    }
                                }

                                bonesRemapping = partBones.ToArray();
                            }

                            if (vertexChanged.Values.Count > 0)
                            {
                                Debug.Assert(vertexChanged.Values.Max() < 2, "Vertex shared between model parts, will likely result in wrong skinning");
                            }
                        }

                        #endregion

                        int startIndex = indices.Count;
                        int indexCount = meshPart.m_indices.Count;

                        uint minIndex = (uint)meshPart.m_indices[0];
                        foreach (var i in meshPart.m_indices)
                        {
                            indices.Add((uint)i);
                            minIndex = Math.Min(minIndex, (uint)i);
                        }

                        uint baseVertex = minIndex;

                        for (int i = startIndex; i < startIndex + indexCount; i++)
                        {
                            indices[i] -= minIndex;
                            maxIndex    = Math.Max(maxIndex, indices[i]);
                        }

                        #region Material
                        var materialDesc = meshPart.m_MaterialDesc;

                        var matId        = MyMeshMaterials1.GetMaterialId(materialDesc, contentPath);
                        var partKey      = MyMeshMaterials1.Table[matId.Index].Technique;
                        var materialName = MyMeshMaterials1.Table[matId.Index].Name;

                        var list = submeshes.SetDefault(partKey, new List <MyDrawSubmesh>());
                        list.Add(new MyDrawSubmesh(indexCount, startIndex, (int)baseVertex, MyMeshMaterials1.GetProxyId(matId), bonesRemapping));

                        submeshesMeta.Add(new MySubmeshInfo
                        {
                            IndexCount   = indexCount,
                            StartIndex   = startIndex,
                            BaseVertex   = (int)baseVertex,
                            BonesMapping = bonesRemapping,
                            Material     = materialName.ToString(),
                            Technique    = partKey
                        });

                        var list2 = submeshes2.SetDefault(partKey, new List <MySubmeshInfo>());
                        list2.Add(submeshesMeta[submeshesMeta.Count - 1]);

                        #endregion
                    }
                    indicesNum = indices.Count;

                    #region Fill gpu buffes
                    unsafe
                    {
                        if (maxIndex <= ushort.MaxValue)
                        {
                            // create 16 bit indices
                            var indices16 = new ushort[indices.Count];
                            for (int i = 0; i < indices.Count; i++)
                            {
                                indices16[i] = (ushort)indices[i];
                            }

                            result.Indices = indices16;

                            fixed(ushort *I = indices16)
                            {
                                indexBuffer = MyHwBuffers.CreateIndexBuffer(indices16.Length, Format.R16_UInt, BindFlags.IndexBuffer, ResourceUsage.Immutable, new IntPtr(I), assetName + " index buffer");
                            }
                        }
                        else
                        {
                            var indicesArray = indices.ToArray();
                            fixed(uint *I = indicesArray)
                            {
                                indexBuffer = MyHwBuffers.CreateIndexBuffer(indices.Count, Format.R32_UInt, BindFlags.IndexBuffer, ResourceUsage.Immutable, new IntPtr(I), assetName + " index buffer");
                            }
                        }
                    }
                    unsafe
                    {
                        if (!hasBonesInfo)
                        {
                            var vertices = new MyVertexFormatPositionH4[verticesNum];

                            for (int i = 0; i < verticesNum; i++)
                            {
                                vertices[i] = new MyVertexFormatPositionH4(positions[i]);
                            }
                            meshVertexInput = meshVertexInput.Append(MyVertexInputComponentType.POSITION_PACKED);

                            result.VertexPositions = vertices;

                            fixed(MyVertexFormatPositionH4 *V = vertices)
                            {
                                vertexBuffers.Add(MyHwBuffers.CreateVertexBuffer(verticesNum, sizeof(MyVertexFormatPositionH4), BindFlags.VertexBuffer, ResourceUsage.Immutable, new IntPtr(V), assetName + " vertex buffer " + vertexBuffers.Count));
                            }
                        }
                        else
                        {
                            var vertices = new MyVertexFormatPositionSkinning[verticesNum];
                            for (int i = 0; i < verticesNum; i++)
                            {
                                vertices[i] = new MyVertexFormatPositionSkinning(
                                    positions[i],
                                    new Byte4(boneIndices[i].X, boneIndices[i].Y, boneIndices[i].Z, boneIndices[i].W),
                                    boneWeights[i]);
                            }
                            meshVertexInput = meshVertexInput.Append(MyVertexInputComponentType.POSITION_PACKED)
                                              .Append(MyVertexInputComponentType.BLEND_WEIGHTS).Append(MyVertexInputComponentType.BLEND_INDICES);

                            fixed(MyVertexFormatPositionSkinning *V = vertices)
                            {
                                vertexBuffers.Add(MyHwBuffers.CreateVertexBuffer(verticesNum, sizeof(MyVertexFormatPositionSkinning), BindFlags.VertexBuffer, ResourceUsage.Immutable, new IntPtr(V), assetName + " vertex buffer " + vertexBuffers.Count));
                            }
                        }
                        // add second stream
                        {
                            var vertices = new MyVertexFormatTexcoordNormalTangent[verticesNum];
                            for (int i = 0; i < verticesNum; i++)
                            {
                                vertices[i] = new MyVertexFormatTexcoordNormalTangent(texcoords[i], normals[i], tangentBitanSgn[i]);
                            }

                            fixed(MyVertexFormatTexcoordNormalTangent *V = vertices)
                            {
                                vertexBuffers.Add(MyHwBuffers.CreateVertexBuffer(verticesNum, sizeof(MyVertexFormatTexcoordNormalTangent), BindFlags.VertexBuffer, ResourceUsage.Immutable, new IntPtr(V), assetName + " vertex buffer " + vertexBuffers.Count));
                            }

                            result.VertexExtendedData = vertices;

                            meshVertexInput = meshVertexInput
                                              .Append(MyVertexInputComponentType.NORMAL, 1)
                                              .Append(MyVertexInputComponentType.TANGENT_SIGN_OF_BITANGENT, 1)
                                              .Append(MyVertexInputComponentType.TEXCOORD0_H, 1);
                        }
                    }
                    #endregion
                }
                #region Extract lods
                if (tagData.ContainsKey(MyImporterConstants.TAG_LODS))
                {
                    var tagLODs = tagData[MyImporterConstants.TAG_LODS];
                    if (((MyLODDescriptor[])tagLODs).Length > 0)
                    {
                    }
                    LodDescriptors = (MyLODDescriptor[])((MyLODDescriptor[])tagLODs).Clone();
                }
                #endregion

                if (missingMaterial)
                {
                    Debug.WriteLine(String.Format("Mesh {0} has missing material", assetName));
                }

                //indexBuffer.SetDebugName(assetName + " index buffer");
                int c = 0;
                //vertexBuffers.ForEach(x => x.SetDebugName(assetName + " vertex buffer " + c++));

                //
                result.BoundingBox    = (BoundingBox)tagData[MyImporterConstants.TAG_BOUNDING_BOX];
                result.BoundingSphere = (BoundingSphere)tagData[MyImporterConstants.TAG_BOUNDING_SPHERE];
                result.VerticesNum    = verticesNum;
                result.IndicesNum     = indicesNum;
                result.VertexLayout   = meshVertexInput;
                result.IB             = indexBuffer;
                result.VB             = vertexBuffers.ToArray();
                result.IsAnimated     = hasBonesInfo;
                result.Parts          = submeshes.ToDictionary(x => x.Key, x => x.Value.ToArray());
                result.PartsMetadata  = submeshes2.ToDictionary(x => x.Key, x => x.Value.ToArray());
                result.m_submeshes    = submeshesMeta;

                IsAnimated |= result.IsAnimated;

                importer.Clear();
                return(result);
            }
Esempio n. 9
0
 internal MyVertexFormatPositionH4(Vector3 position)
 {
     Position = VF_Packer.PackPosition(ref position);
 }
Esempio n. 10
0
 internal MyVertexFormatTexcoordNormalTangent(HalfVector2 texcoord, Vector3 normal, Vector4 tangent)
 {
     Texcoord = texcoord;
     Normal   = VF_Packer.PackNormalB4(ref normal);
     Tangent  = VF_Packer.PackTangentSignB4(ref tangent);
 }
Esempio n. 11
0
        public static MaterialBvh Create(Dictionary <string, object> tags, int shapesPerNode = 8)
        {
            var verts    = (Vector3[])tags.GetValueOrDefault(MyImporterConstants.TAG_VERTICES);
            var normals  = (Byte4[])tags.GetValueOrDefault(MyImporterConstants.TAG_NORMALS);
            var box      = (BoundingBox)tags.GetValueOrDefault(MyImporterConstants.TAG_BOUNDING_BOX);
            var parts    = (List <MyMeshPartInfo>)tags.GetValueOrDefault(MyImporterConstants.TAG_MESH_PARTS);
            var sections = (List <MyMeshSectionInfo>)tags.GetValueOrDefault(MyImporterConstants.TAG_MESH_SECTIONS);

            var strings   = new HashSet <string>();
            var triangles = new List <TriangleData>(parts.Sum(x => x.m_indices.Count / 3));

            void AddTriangle(string section, string material, int v0, int v1, int v2)
            {
                section  = section ?? "";
                material = material ?? "";
                strings.Add(section);
                strings.Add(material);
                var normal = normals != null
                    ? (VF_Packer.UnpackNormal(normals[v0]) + VF_Packer.UnpackNormal(normals[v1]) + VF_Packer.UnpackNormal(normals[v2]))
                    : (Vector3?)null;

                triangles.Add(new TriangleData(new Triangle(in verts[v0], in verts[v1], in verts[v2], normal), section, material));
            }

            foreach (var part in parts)
            {
                var materialName = part.m_MaterialDesc?.MaterialName ?? "<<no material>>";
                using (PoolManager.Get(out List <KeyValuePair <string, MyMeshSectionMeshInfo> > mtlSections))
                {
                    foreach (var section in sections)
                    {
                        foreach (var mesh in section.Meshes)
                        {
                            if (mesh.MaterialName == materialName)
                            {
                                mtlSections.Add(new KeyValuePair <string, MyMeshSectionMeshInfo>(section.Name, mesh));
                            }
                        }
                    }

                    mtlSections.Sort((a, b) => a.Value.StartIndex.CompareTo(b.Value.StartIndex));

                    var processed = 0;
                    var idx       = part.m_indices;
                    foreach (var section in mtlSections)
                    {
                        for (var i = processed; i < section.Value.StartIndex - 2; i += 3)
                        {
                            AddTriangle(null, materialName, idx[i], idx[i + 1], idx[i + 2]);
                        }
                        var endOfSection = section.Value.StartIndex + section.Value.IndexCount;
                        for (var i = section.Value.StartIndex; i < endOfSection - 2; i += 3)
                        {
                            AddTriangle(section.Key, materialName, idx[i], idx[i + 1], idx[i + 2]);
                        }
                        processed = endOfSection;
                    }

                    for (var i = processed; i < idx.Count - 2; i += 3)
                    {
                        AddTriangle(null, materialName, idx[i], idx[i + 1], idx[i + 2]);
                    }
                }
            }

            PackedBvh bvh;

            using (ArrayPool <BoundingBox> .Get(triangles.Count, out var array))
            {
                for (var i = 0; i < triangles.Count; i++)
                {
                    var tri = triangles[i].Triangle;
                    array[i].Min = Vector3.Min(Vector3.Min(tri.A, tri.B), tri.C);
                    array[i].Max = Vector3.Max(Vector3.Max(tri.A, tri.B), tri.C);
                }

                using (var builder = new SahBvhBuilder(shapesPerNode, new EqReadOnlySpan <BoundingBox>(array, 0, triangles.Count)))
                    bvh = builder.Build();
            }

            return(new MaterialBvh(bvh, triangles.ToArray(), strings));
        }
Esempio n. 12
0
 public void SetVertexPosition(int vertexIndex, ref Vector3 newPosition)
 {
     m_vertices[vertexIndex].Position = VF_Packer.PackPosition(newPosition);
 }