Beispiel #1
0
 public ColliderUpdater(Gltf gltf, IDictionary <int, Unity_collidersCollider> colliders)
 {
     this.gltf      = gltf;
     this.colliders = colliders;
 }
Beispiel #2
0
 private void FetchBuffers(Gltf gltfFile, string gltfPath)
 {
     FetchBuffers(gltfFile, gltfPath, Enumerable.Range(0, gltfFile.Buffers.Length));
 }
Beispiel #3
0
 private float[] GetComponentAtIndex(Gltf gltf, Accessor accessor, int index)
 {
     var bufferView = gltf.BufferViews[accessor.BufferView ?? throw new Exception($"Accessor '{accessor.Name ?? "?"}' has no buffer-view!")];
 private GltfModelLoader(GraphicsDevice device, Gltf gltf, byte[][] buffers)
 {
     GraphicsDevice = device;
     this.gltf      = gltf;
     this.buffers   = buffers;
 }
Beispiel #5
0
        internal static int AddLineLoop(this Gltf gltf, string name, List <byte> buffer, double[] vertices, ushort[] indices, double[] vMin, double[] vMax, ushort iMin, ushort iMax, int materialId, Transform transform = null)
        {
            var m = new glTFLoader.Schema.Mesh();

            m.Name = name;
            var vBuff = gltf.AddBufferView(0, buffer.Count(), vertices.Length * sizeof(float), null, null);
            var iBuff = gltf.AddBufferView(0, buffer.Count() + vertices.Length * sizeof(float), indices.Length * sizeof(ushort), null, null);

            buffer.AddRange(vertices.SelectMany(v => BitConverter.GetBytes((float)v)));
            buffer.AddRange(indices.SelectMany(v => BitConverter.GetBytes(v)));

            while (buffer.Count() % 4 != 0)
            {
                // Console.WriteLine("Padding...");
                buffer.Add(0);
            }

            var vAccess = gltf.AddAccessor(vBuff, 0, Accessor.ComponentTypeEnum.FLOAT, vertices.Length / 3, new[] { (float)vMin[0], (float)vMin[1], (float)vMin[2] }, new[] { (float)vMax[0], (float)vMax[1], (float)vMax[2] }, Accessor.TypeEnum.VEC3);
            var iAccess = gltf.AddAccessor(iBuff, 0, Accessor.ComponentTypeEnum.UNSIGNED_SHORT, indices.Length, new[] { (float)iMin }, new[] { (float)iMax }, Accessor.TypeEnum.SCALAR);

            var prim = new MeshPrimitive();

            prim.Indices    = iAccess;
            prim.Material   = materialId;
            prim.Mode       = MeshPrimitive.ModeEnum.LINE_STRIP;
            prim.Attributes = new Dictionary <string, int> {
                { "POSITION", vAccess }
            };

            m.Primitives = new[] { prim };

            // Add mesh to gltf
            if (gltf.Meshes != null)
            {
                var meshes = gltf.Meshes.ToList();
                meshes.Add(m);
                gltf.Meshes = meshes.ToArray();
            }
            else
            {
                gltf.Meshes = new[] { m };
            }

            var parentId = 0;

            if (transform != null)
            {
                var a = transform.XAxis;
                var b = transform.YAxis;
                var c = transform.ZAxis;

                var transNode = new Node();

                transNode.Matrix = new[] {
                    (float)a.X, (float)a.Y, (float)a.Z, 0.0f,
                    (float)b.X, (float)b.Y, (float)b.Z, 0.0f,
                    (float)c.X, (float)c.Y, (float)c.Z, 0.0f,
                    (float)transform.Origin.X, (float)transform.Origin.Y, (float)transform.Origin.Z, 1.0f
                };

                parentId = gltf.AddNode(transNode, 0);
            }

            // Add mesh node to gltf
            var node = new Node();

            node.Mesh = gltf.Meshes.Length - 1;
            gltf.AddNode(node, parentId);

            return(gltf.Meshes.Length - 1);
        }
 public GltfModelToGL(Stream stream)
 {
     gltf      = Interface.LoadModel(stream);
     glBuffers = CreateBuffers(gltf);
     //var nodes = from scene in gltf.Scenes select scene.Nodes;
 }
        public Core.Entities.Mesh LoadFromFile(string path)
        {
            if (!path.EndsWith("gltf") && !path.EndsWith("glb"))
            {
                return(null);
            }

            Gltf deserializedFile = Interface.LoadModel(path);

            //Only one mesh currently supported.
            Mesh[] meshes = deserializedFile.Meshes;

            Core.Entities.Mesh loadedModel = new Core.Entities.Mesh(_recalculateNormals);

            // read all buffers
            for (int i = 0; i < deserializedFile.Buffers?.Length; i++)
            {
                byte[] bufferBytes           = deserializedFile.LoadBinaryBuffer(i, path);
                int    indecesAttributevalue = meshes[0].Primitives[0].Indices ?? 0;
                int    indecesBufferView     = deserializedFile.Accessors[indecesAttributevalue].BufferView ?? 0;
                int[]  indeces =
                    bufferBytes.ParseBufferViews(deserializedFile.Accessors[indecesAttributevalue],
                                                 deserializedFile.BufferViews[indecesBufferView]);
                foreach (KeyValuePair <string, int> attribute in deserializedFile.Meshes[0].Primitives[0].Attributes)
                {
                    Console.WriteLine(attribute.Key);
                }

                AttributeParser posParser     = new AttributeParser(0, 0, deserializedFile, bufferBytes, "POSITION");
                AttributeParser normParser    = new AttributeParser(0, 0, deserializedFile, bufferBytes, "NORMAL");
                AttributeParser uv0Parser     = new AttributeParser(0, 0, deserializedFile, bufferBytes, "TEXCOORD_0");
                AttributeParser uv1Parser     = new AttributeParser(0, 0, deserializedFile, bufferBytes, "TEXCOORD_1");
                AttributeParser tangentParser = new AttributeParser(0, 0, deserializedFile, bufferBytes, "TANGENT");

                //Animations
                AttributeParser jointsParser  = new AttributeParser(0, 0, deserializedFile, bufferBytes, "JOINTS_0");
                AttributeParser weightsParser = new AttributeParser(0, 0, deserializedFile, bufferBytes, "WEIGHTS_0");

                List <Vector3> vertexCoords  = posParser.Parse() ? posParser.ParsedAttribute : null;
                List <Vector3> normalCoords  = normParser.Parse() ? normParser.ParsedAttribute : null;
                List <Vector2> uv0Coords     = uv0Parser.Parse() ? uv0Parser.ParsedAttribute : null;
                List <Vector2> uv1Coords     = uv1Parser.Parse() ? uv1Parser.ParsedAttribute : null;
                List <Vector4> tangentCoords = tangentParser.Parse() ? tangentParser.ParsedAttribute : null;

                //Animations
                List <Vector4> jointsValues  = jointsParser.Parse() ? jointsParser.ParsedAttribute : null;
                List <Vector4> weightsValues = weightsParser.Parse() ? weightsParser.ParsedAttribute : null;

                List <Vector3> decodedVertices = new List <Vector3>();
                List <Vector3> decodedNormals  = new List <Vector3>();
                List <Vector2> decodedUvCoords = new List <Vector2>();


                for (int j = 0; j < indeces.Length; j++)
                {
                    decodedVertices.Add(vertexCoords == null || vertexCoords.Count() == 0 ? Vector3.Zero : vertexCoords[indeces[j]]);
                    decodedNormals.Add(normalCoords == null || normalCoords.Count() == 0 ? Vector3.Zero : normalCoords[indeces[j]]);
                    decodedUvCoords.Add(uv0Coords == null || uv0Coords.Count() == 0 ? Vector2.Zero : uv0Coords[indeces[j]]);
                }

                loadedModel.Vertices = decodedVertices.ToArray();
                loadedModel.Normals  = decodedNormals.ToArray();
                loadedModel.UvCoords = decodedUvCoords.ToArray();
                loadedModel.Indeces  = indeces;
            }
            return(loadedModel);
        }
        public Node_Attribute(List <string> imageList)
        {
            var baseColorTextureImage = UseTexture(imageList, "BaseColor_Nodes");

            // There are no common properties in this model group that are reported in the readme.

            Model CreateModel(Action <List <Property>, Runtime.Node> setProperties)
            {
                var properties = new List <Property>();
                var gltf       = Gltf.CreateMultiNode();
                var nodes      = new[]
                {
                    gltf.Scenes.First().Nodes.First(),
                    gltf.Scenes.First().Nodes.First().Children.First(),
                };

                // Apply the common properties to the gltf.
                foreach (var node in nodes)
                {
                    node.Mesh.MeshPrimitives.First().Material = new Runtime.Material()
                    {
                        MetallicRoughnessMaterial = new Runtime.PbrMetallicRoughness()
                        {
                            BaseColorTexture = new Runtime.Texture()
                            {
                                Source = baseColorTextureImage
                            },
                        },
                    };
                    node.Mesh.MeshPrimitives.First().Normals  = null;
                    node.Mesh.MeshPrimitives.First().Tangents = null;
                }

                // Apply the properties that are specific to this gltf.
                setProperties(properties, nodes[1]);

                // Create the gltf object
                return(new Model
                {
                    Properties = properties,
                    GLTF = CreateGLTF(() => gltf.Scenes.First()),
                });
            }

            void SetTranslation(List <Property> properties, Runtime.Node node)
            {
                node.Translation = new Vector3(-2, 2, -2);
                properties.Add(new Property(PropertyName.Translation, node.Translation));
            }

            void SetTranslationX(List <Property> properties, Runtime.Node node)
            {
                node.Translation = new Vector3(-2, 0, 0);
                properties.Add(new Property(PropertyName.Translation, node.Translation));
            }

            void SetTranslationY(List <Property> properties, Runtime.Node node)
            {
                node.Translation = new Vector3(0, 2, 0);
                properties.Add(new Property(PropertyName.Translation, node.Translation));
            }

            void SetTranslationZ(List <Property> properties, Runtime.Node node)
            {
                node.Translation = new Vector3(0, 0, -2);
                properties.Add(new Property(PropertyName.Translation, node.Translation));
            }

            void SetRotation(List <Property> properties, Runtime.Node node)
            {
                var rotation = new Quaternion(0.0f, 1.0f, 0.0f, 0.0f);

                node.Rotation = rotation;
                properties.Add(new Property(PropertyName.Rotation, rotation));
            }

            void SetScale(List <Property> properties, Runtime.Node node)
            {
                node.Scale = new Vector3(1.2f, 1.2f, 1.2f);
                properties.Add(new Property(PropertyName.Scale, node.Scale));
            }

            void SetMatrix(List <Property> properties, Runtime.Node node)
            {
                var matrixT   = Matrix4x4.CreateTranslation(new Vector3(-2, 2, -2));
                var matrixR   = Matrix4x4.CreateRotationY(FloatMath.Pi);
                var matrixS   = Matrix4x4.CreateScale(1.2f);
                var matrixTRS = Matrix4x4.Multiply(Matrix4x4.Multiply(matrixS, matrixR), matrixT);

                node.Matrix = matrixTRS;
                properties.Add(new Property(PropertyName.Matrix, matrixTRS));
            }

            this.Models = new List <Model>
            {
                CreateModel((properties, node) => {
                    // There are no properties set on this model.
                }),
                CreateModel((properties, node) => {
                    SetTranslation(properties, node);
                }),
                CreateModel((properties, node) => {
                    SetTranslationX(properties, node);
                }),
                CreateModel((properties, node) => {
                    SetTranslationY(properties, node);
                }),
                CreateModel((properties, node) => {
                    SetTranslationZ(properties, node);
                }),
                CreateModel((properties, node) => {
                    SetRotation(properties, node);
                }),
                CreateModel((properties, node) => {
                    SetScale(properties, node);
                }),
                CreateModel((properties, node) => {
                    SetTranslation(properties, node);
                    SetRotation(properties, node);
                    SetScale(properties, node);
                }),
                CreateModel((properties, node) => {
                    SetMatrix(properties, node);
                }),
            };

            GenerateUsedPropertiesList();
        }