Esempio n. 1
0
        public static ModelMeshData LoadGeometry(ModelData data, ModelBoneData parentBoneData, ColladaGeometry geometry)
        {
            ModelMeshData modelMeshData = new ModelMeshData();

            data.Meshes.Add(modelMeshData);
            modelMeshData.Name            = parentBoneData.Name;
            modelMeshData.ParentBoneIndex = data.Bones.IndexOf(parentBoneData);
            bool flag = false;

            foreach (ColladaPolygons polygon in geometry.Mesh.Polygons)
            {
                ModelMeshPartData modelMeshPartData = LoadPolygons(data, polygon);
                modelMeshData.MeshParts.Add(modelMeshPartData);
                modelMeshData.BoundingBox = (flag ? BoundingBox.Union(modelMeshData.BoundingBox, modelMeshPartData.BoundingBox) : modelMeshPartData.BoundingBox);
                flag = true;
            }
            return(modelMeshData);
        }
Esempio n. 2
0
        public static ModelMeshPartData LoadPolygons(ModelData data, ColladaPolygons polygons)
        {
            ModelMeshPartData modelMeshPartData = new ModelMeshPartData();
            int num = 0;
            Dictionary <VertexElement, ColladaInput> dictionary = new Dictionary <VertexElement, ColladaInput>();

            foreach (ColladaInput input in polygons.Inputs)
            {
                string str = (input.Set == 0) ? string.Empty : input.Set.ToString(CultureInfo.InvariantCulture);
                if (input.Semantic == "POSITION")
                {
                    dictionary[new VertexElement(num, VertexElementFormat.Vector3, "POSITION" + str)] = input;
                    num += 12;
                }
                else if (input.Semantic == "NORMAL")
                {
                    dictionary[new VertexElement(num, VertexElementFormat.Vector3, "NORMAL" + str)] = input;
                    num += 12;
                }
                else if (input.Semantic == "TEXCOORD")
                {
                    dictionary[new VertexElement(num, VertexElementFormat.Vector2, "TEXCOORD" + str)] = input;
                    num += 8;
                }
                else if (input.Semantic == "COLOR")
                {
                    dictionary[new VertexElement(num, VertexElementFormat.NormalizedByte4, "COLOR" + str)] = input;
                    num += 4;
                }
            }
            VertexDeclaration vertexDeclaration = new VertexDeclaration(dictionary.Keys.ToArray());
            ModelBuffersData  modelBuffersData  = data.Buffers.FirstOrDefault((ModelBuffersData vd) => vd.VertexDeclaration == vertexDeclaration);

            if (modelBuffersData == null)
            {
                modelBuffersData = new ModelBuffersData();
                data.Buffers.Add(modelBuffersData);
                modelBuffersData.VertexDeclaration = vertexDeclaration;
            }
            modelMeshPartData.BuffersDataIndex = data.Buffers.IndexOf(modelBuffersData);
            int        num2 = polygons.P.Count / polygons.Inputs.Count;
            List <int> list = new List <int>();

            if (polygons.VCount.Count == 0)
            {
                int num3 = 0;
                for (int i = 0; i < num2 / 3; i++)
                {
                    list.Add(num3);
                    list.Add(num3 + 2);
                    list.Add(num3 + 1);
                    num3 += 3;
                }
            }
            else
            {
                int num4 = 0;
                using (List <int> .Enumerator enumerator2 = polygons.VCount.GetEnumerator())
                {
                    while (enumerator2.MoveNext())
                    {
                        switch (enumerator2.Current)
                        {
                        case 3:
                            list.Add(num4);
                            list.Add(num4 + 2);
                            list.Add(num4 + 1);
                            num4 += 3;
                            break;

                        case 4:
                            list.Add(num4);
                            list.Add(num4 + 2);
                            list.Add(num4 + 1);
                            list.Add(num4 + 2);
                            list.Add(num4);
                            list.Add(num4 + 3);
                            num4 += 4;
                            break;

                        default:
                            throw new NotSupportedException("Collada polygons with less than 3 or more than 4 vertices are not supported.");
                        }
                    }
                }
            }
            int vertexStride = modelBuffersData.VertexDeclaration.VertexStride;
            int num5         = modelBuffersData.Vertices.Length;

            modelBuffersData.Vertices = ExtendArray(modelBuffersData.Vertices, list.Count * vertexStride);
            using (BinaryWriter binaryWriter = new BinaryWriter(new MemoryStream(modelBuffersData.Vertices, num5, list.Count * vertexStride)))
            {
                bool flag = false;
                foreach (KeyValuePair <VertexElement, ColladaInput> item in dictionary)
                {
                    VertexElement key   = item.Key;
                    ColladaInput  value = item.Value;
                    if (key.Semantic.StartsWith("POSITION"))
                    {
                        for (int j = 0; j < list.Count; j++)
                        {
                            float[] array  = value.Source.Accessor.Source.Array;
                            int     offset = value.Source.Accessor.Offset;
                            int     stride = value.Source.Accessor.Stride;
                            int     num6   = polygons.P[list[j] * polygons.Inputs.Count + value.Offset];
                            binaryWriter.BaseStream.Position = j * vertexStride + key.Offset;
                            float num7 = array[offset + stride * num6];
                            float num8 = array[offset + stride * num6 + 1];
                            float num9 = array[offset + stride * num6 + 2];
                            modelMeshPartData.BoundingBox = (flag ? BoundingBox.Union(modelMeshPartData.BoundingBox, new Vector3(num7, num8, num9)) : new BoundingBox(num7, num8, num9, num7, num8, num9));
                            flag = true;
                            binaryWriter.Write(num7);
                            binaryWriter.Write(num8);
                            binaryWriter.Write(num9);
                        }
                    }
                    else if (key.Semantic.StartsWith("NORMAL"))
                    {
                        for (int k = 0; k < list.Count; k++)
                        {
                            float[] array2  = value.Source.Accessor.Source.Array;
                            int     offset2 = value.Source.Accessor.Offset;
                            int     stride2 = value.Source.Accessor.Stride;
                            int     num10   = polygons.P[list[k] * polygons.Inputs.Count + value.Offset];
                            binaryWriter.BaseStream.Position = k * vertexStride + key.Offset;
                            float num11 = array2[offset2 + stride2 * num10];
                            float num12 = array2[offset2 + stride2 * num10 + 1];
                            float num13 = array2[offset2 + stride2 * num10 + 2];
                            float num14 = 1f / MathUtils.Sqrt(num11 * num11 + num12 * num12 + num13 * num13);
                            binaryWriter.Write(num14 * num11);
                            binaryWriter.Write(num14 * num12);
                            binaryWriter.Write(num14 * num13);
                        }
                    }
                    else if (key.Semantic.StartsWith("TEXCOORD"))
                    {
                        for (int l = 0; l < list.Count; l++)
                        {
                            float[] array3  = value.Source.Accessor.Source.Array;
                            int     offset3 = value.Source.Accessor.Offset;
                            int     stride3 = value.Source.Accessor.Stride;
                            int     num15   = polygons.P[list[l] * polygons.Inputs.Count + value.Offset];
                            binaryWriter.BaseStream.Position = l * vertexStride + key.Offset;
                            binaryWriter.Write(array3[offset3 + stride3 * num15]);
                            binaryWriter.Write(1f - array3[offset3 + stride3 * num15 + 1]);
                        }
                    }
                    else
                    {
                        if (!key.Semantic.StartsWith("COLOR"))
                        {
                            throw new Exception();
                        }
                        for (int m = 0; m < list.Count; m++)
                        {
                            float[] array4  = value.Source.Accessor.Source.Array;
                            int     offset4 = value.Source.Accessor.Offset;
                            int     stride4 = value.Source.Accessor.Stride;
                            int     num16   = polygons.P[list[m] * polygons.Inputs.Count + value.Offset];
                            binaryWriter.BaseStream.Position = m * vertexStride + key.Offset;
                            Color color = new Color(array4[offset4 + stride4 * num16], array4[offset4 + stride4 * num16 + 1], array4[offset4 + stride4 * num16 + 2], array4[offset4 + stride4 * num16 + 3]);
                            binaryWriter.Write(color.PackedValue);
                        }
                    }
                }
            }
            modelMeshPartData.StartIndex   = num5 / vertexStride;
            modelMeshPartData.IndicesCount = list.Count;
            return(modelMeshPartData);
        }