Beispiel #1
0
        public void MergeGlbFiles()
        {
            var testPath  = "../../../models/MergeGlTF/Ours.glb";
            var ours      = Interface.LoadModel(testPath);
            var buffers   = ours.Buffers.ToList();
            var buffViews = ours.BufferViews.ToList();
            var accessors = ours.Accessors.ToList();
            var meshes    = ours.Meshes.ToList();
            var materials = ours.Materials.ToList();
            var images    = ours.Images != null?ours.Images.ToList() : new List <Image>();

            var textures = ours.Textures != null?ours.Textures.ToList() : new List <Texture>();

            var samplers = ours.Samplers != null?ours.Samplers.ToList() : new List <Sampler>();

            using (var testStream = GltfExtensions.GetGlbStreamFromPath(testPath))
            {
                var bufferByteArrays = ours.GetAllBufferByteArrays(testStream);

                using (var avocadoStream = GltfExtensions.GetGlbStreamFromPath("../../../models/MergeGlTF/Avocado.glb"))
                {
                    GltfMergingUtils.AddAllMeshesFromFromGlb(avocadoStream,
                                                             buffers,
                                                             bufferByteArrays,
                                                             buffViews,
                                                             accessors,
                                                             meshes,
                                                             materials,
                                                             textures,
                                                             images,
                                                             samplers,
                                                             true
                                                             );
                }

                ours.Buffers     = buffers.ToArray();
                ours.BufferViews = buffViews.ToArray();
                ours.Accessors   = accessors.ToArray();
                ours.Meshes      = meshes.ToArray();
                ours.Materials   = materials.ToArray();
                ours.Images      = images.ToArray();
                ours.Textures    = textures.ToArray();
                if (samplers.Count > 0)
                {
                    ours.Samplers = samplers.ToArray();
                }

                var nodeList  = ours.Nodes.ToList();
                var transform = new Transform(new Vector3(1, 1, 0), Vector3.XAxis, Vector3.YAxis.Negate()).Scaled(20);
                NodeUtilities.CreateNodeForMesh(ours.Meshes.Length - 1, nodeList, transform);
                ours.Nodes = nodeList.ToArray();

                var savepath = "../../../GltfTestResult.gltf";
                ours.SaveBuffersAndAddUris(savepath, bufferByteArrays);
                ours.SaveModel(savepath);

                var mergedBuffer = ours.CombineBufferAndFixRefs(bufferByteArrays.ToArray());
                ours.SaveBinaryModel(mergedBuffer, "../../../GltfTestMerged.glb");
            }
        }
Beispiel #2
0
        public void ThinObjectsGenerateCorrectly()
        {
            var json  = File.ReadAllText("../../../models/Geometry/Single-Panel.json");
            var panel = JsonConvert.DeserializeObject <Panel>(json);
            var model = new Model();

            model.AddElement(panel);
            var modelsDir = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "models");
            var gltfPath  = Path.Combine(modelsDir, "Single-Panel.gltf");

            model.ToGlTF(gltfPath, false);
            var gltfJson = File.ReadAllText(gltfPath);

            using (var glbStream = GltfExtensions.GetGlbStreamFromPath(gltfPath))
            {
                var loadingStream = new MemoryStream();
                glbStream.Position = 0;
                glbStream.CopyTo(loadingStream);
                loadingStream.Position = 0;
                var loaded      = Interface.LoadModel(loadingStream);
                var vertexCount = loaded.Accessors.First(a => a.Type == Accessor.TypeEnum.VEC3).Count;
                Assert.True(vertexCount == 12, $"The stored mesh should contain 12 vertices, instead it contains {vertexCount}");
            }
        }