public override Task Send(IConnection connection, string data, string connectionData)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            var webSocket = _webSocket;

            if (webSocket == null)
            {
                Exception ex;
                if (connection.State != ConnectionState.Disconnected)
                {
                    // Make this a faulted task and trigger the OnError even to maintain consistency with the HttpBasedTransports
                    ex = new InvalidOperationException(ResourcesStore.GetResourceString("Error_DataCannotBeSentDuringWebSocketReconnect"));
                    connection.OnError(ex);
                }
                else
                {
                    ex = new InvalidOperationException(ResourcesStore.GetResourceString("Error_DataCannotBeSentDuringWebSocketReconnect"));
                }

                var tcs = new DispatchingTaskCompletionSource <object>();
                tcs.SetException(ex);
                return(tcs.Task);
            }

            return(Send(webSocket, data));
        }
Example #2
0
        public void TestModel(ResourcesStore store)
        {
            var gltf = store.Gltf;

            Assert.That(gltf.Scene, Is.EqualTo(0)); // Alicia model has no value, but migrated by VGltf;

            var buf0 = store.GetOrLoadBufferResourceAt(0);

            Assert.AreEqual(7, gltf.Images.Count);
            for (var i = 0; i < gltf.Images.Count; ++i)
            {
                var img     = gltf.Images[i];
                var imgResN = store.GetOrLoadImageResourceAt(i);

                // imgN.Data
                // Console.WriteLine(img.Name);
            }

            //
            {
                var img = gltf.Images[0];
                Assert.AreEqual("body", img.Name);
                Assert.AreEqual(null, img.Uri);
                Assert.AreEqual(0, img.bufferView);

                var r = store.GetOrLoadImageResourceAt(img.bufferView.Value);

                var imageBytes = new byte[r.Data.Count];
                Array.Copy(r.Data.Array, r.Data.Offset, imageBytes, 0, r.Data.Count);

                Assert.AreEqual(1821190, imageBytes.Count());
                Assert.That(imageBytes.Take(8), Is.EquivalentTo(new byte[] {
                    // Header of PNG
                    0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
                }));
            }

            {
                var img = gltf.Images[1];
                Assert.AreEqual("eye", img.Name);
                Assert.AreEqual(null, img.Uri);
                Assert.AreEqual(1, img.bufferView);

                var r = store.GetOrLoadImageResourceAt(img.bufferView.Value);

                var imageBytes = new byte[r.Data.Count];
                Array.Copy(r.Data.Array, r.Data.Offset, imageBytes, 0, r.Data.Count);

                Assert.AreEqual(65934, imageBytes.Count());
                Assert.That(imageBytes.Take(8), Is.EquivalentTo(new byte[] {
                    // Header of PNG
                    0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
                }));
            }

            Assert.That(gltf.ExtensionsUsed, Is.EquivalentTo(new string[] { "KHR_materials_unlit", "VRM" }));
        }
Example #3
0
        public Importer(GltfContainer container, IResourceLoader loader)
        {
            Container  = container;
            Cache      = new ResourcesCache <int>();
            BufferView = new ResourcesStore(container.Gltf, container.Buffer, loader);

            Nodes     = new NodeImporter(this);
            Meshes    = new MeshImporter(this);
            Materials = new MaterialImporter(this);
            Textures  = new TextureImporter(this);
            Images    = new ImageImporter(this);
        }
Example #4
0
        public void MeshExportTest()
        {
            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);

            using (var exporter = new Exporter())
            {
                exporter.ExportGameObjectAsScene(go);
                var gltfContainer = exporter.IntoGlbContainer();
                var gltf          = gltfContainer.Gltf;
                var store         = new ResourcesStore(gltf, gltfContainer.Buffer, new ResourceLoaderFromEmbedOnly());

                Assert.AreEqual(0, gltf.Scene);
                Assert.AreEqual(1, gltf.Scenes.Count);

                var scene0 = gltf.Scenes[0];
                // TODO:

                Assert.AreEqual(1, gltf.Meshes.Count);

                var mesh0 = gltf.Meshes[0];
                Assert.AreEqual(null, mesh0.Weights);

                var prims = mesh0.Primitives;
                Assert.AreEqual(1, prims.Count);

                var prim0 = prims[0];
                Assert.AreEqual(null, prim0.Targets);
                Assert.AreEqual(0, prim0.Material);
                Assert.AreEqual(4, prim0.Indices);

                var prim0Accessor = gltf.Accessors[prim0.Indices.Value];
                Assert.AreEqual(36, prim0Accessor.Count); // 2(poly per faces) * 3(tri) * 6(faces)

                var prim0Buffer       = store.GetOrLoadTypedBufferByAccessorIndex(prim0.Indices.Value);
                var prim0BufferEntity = prim0Buffer.GetEntity <uint>();
                Assert.AreEqual(36, prim0BufferEntity.Length);
                Assert.That(prim0BufferEntity.GetView().Take(6), Is.EquivalentTo(new int[] {
                    0, 2, 3, 0, 3, 1,
                }));

                var prim0Attr     = prim0.Attributes;
                var prim0Position = prim0Attr["POSITION"];
                Assert.NotNull(prim0Position);
            }
        }
Example #5
0
        public void FromGlbTest(string[] modelPath, ModelTester.IModelTester tester)
        {
            var path = modelPath.Aggregate("SampleModels", (b, p) => Path.Combine(b, p));

            using (var fs = new FileStream(path, FileMode.Open))
            {
                var c = GltfContainer.FromGlb(fs);

                var schema = VJson.Schema.JsonSchemaAttribute.CreateFromClass <Types.Gltf>();
                var ex     = schema.Validate(c.Gltf);
                Assert.Null(ex);

                var loader = new ResourceLoaderFromEmbedOnly(); // Glb files should be packed.

                var store = new ResourcesStore(c.Gltf, c.Buffer, loader);
                tester.TestModel(store);
            }
        }
Example #6
0
        public void FromGltfTest(string[] modelPath, ModelTester.IModelTester tester)
        {
            var path = modelPath.Aggregate("SampleModels", (b, p) => Path.Combine(b, p));

            using (var fs = new FileStream(path, FileMode.Open))
            {
                var c = GltfContainer.FromGltf(fs);

                var schema = VJson.Schema.JsonSchemaAttribute.CreateFromClass <Types.Gltf>();
                var ex     = schema.Validate(c.Gltf);
                Assert.Null(ex);

                var storageDir = Directory.GetParent(path).ToString();
                var loader     = new ResourceLoaderFromFileStorage(storageDir);

                var store = new ResourcesStore(c.Gltf, c.Buffer, loader);
                tester.TestModel(store);
            }
        }
Example #7
0
        public void TestModel(ResourcesStore store)
        {
            var gltf = store.Gltf;

            var mesh      = gltf.Meshes[0];
            var primitive = mesh.Primitives[0];

            Assert.AreEqual(1, primitive.Attributes["JOINTS_0"]);
            Assert.AreEqual(4, primitive.Attributes["WEIGHTS_0"]);

            // Index 1
            var joints0 = store.GetOrLoadTypedBufferByAccessorIndex(primitive.Attributes["JOINTS_0"]);

            Assert.AreEqual(96, joints0.GetEntity <Vec4UShort>().GetEnumerable().Count());

            // Index 4
            var weights0 = store.GetOrLoadTypedBufferByAccessorIndex(primitive.Attributes["WEIGHTS_0"]);

            Assert.AreEqual(96, weights0.GetEntity <Vec4Float>().GetEnumerable().Count());
        }
        public void TestModel(ResourcesStore store)
        {
            var gltf = store.Gltf;

            Assert.That(gltf.Scene, Is.EqualTo(0)); // Alicia model has no value, but migrated by VGltf;
            Assert.That(gltf.RootNodesIndices, Is.EquivalentTo(new int[] {
                0, 1, 114, 115, 116, 117, 118, 119, 120,
            }));

            var buf0 = store.GetOrLoadBufferResourceAt(0);

            Assert.AreEqual(7, gltf.Images.Count);
            for (var i = 0; i < gltf.Images.Count; ++i)
            {
                var img     = gltf.Images[i];
                var imgResN = store.GetOrLoadImageResourceAt(i);
            }

            Assert.AreEqual(248, gltf.Accessors.Count);
            for (var i = 0; i < gltf.Accessors.Count; ++i)
            {
                var typedBuffer = store.GetOrLoadTypedBufferByAccessorIndex(i);
            }

            Assert.AreEqual(7, gltf.Meshes.Count);

            // Test eyes mesh
            {
                var mesh = gltf.Meshes[2];
                Assert.That(mesh.Name, Is.EqualTo("eye.baked"));

                Assert.That(mesh.Primitives.Count, Is.EqualTo(1));

                var attr = mesh.Primitives[0].Attributes;
                Assert.That(attr[Types.Mesh.PrimitiveType.AttributeName.POSITION], Is.EqualTo(17));
                Assert.That(attr[Types.Mesh.PrimitiveType.AttributeName.NORMAL], Is.EqualTo(18));
                Assert.That(attr[Types.Mesh.PrimitiveType.AttributeName.TANGENT], Is.EqualTo(19));
                Assert.That(attr[Types.Mesh.PrimitiveType.AttributeName.TEXCOORD_0], Is.EqualTo(20));
                Assert.That(attr[Types.Mesh.PrimitiveType.AttributeName.JOINTS_0], Is.EqualTo(22));
                Assert.That(attr[Types.Mesh.PrimitiveType.AttributeName.WEIGHTS_0], Is.EqualTo(21));
            }

            Assert.AreEqual(7, gltf.Skins.Count);

            // Test eyes mesh
            {
                var skin = gltf.Skins[2];
                Assert.That(skin.InverseBindMatrices, Is.EqualTo(243));
                Assert.That(skin.Joints, Is.EqualTo(new int[] {
                    37, 40, 38, 39,
                }));
                Assert.That(skin.Skeleton, Is.EqualTo(null));

                {
                    var node = gltf.Nodes[37]; // The eye is affected by this node
                    Assert.That(node.Name, Is.EqualTo("eye_L"));
                    Assert.That(node.Translation, Is.EquivalentTo(new float[] {
                        -0.0276441593f, 0.0464049615f, -0.01160347f,
                    }));
                }
            }

            // Assert.That(gltf.ExtensionsUsed, Is.EquivalentTo( new string[]{} ));
        }
        public void TestModel(ResourcesStore store)
        {
            var gltf = store.Gltf;

            Assert.NotNull(gltf.Scene);
            var rootNodes = gltf.RootNodes.ToList();

            Assert.AreEqual(1, rootNodes.Count);

            var node = rootNodes[0];

            Assert.That(node.Matrix, Is.EquivalentTo(new float[] {
                1.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 0.0f, -1.0f, 0.0f,
                0.0f, 1.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 0.0f, 1.0f,
            }));
            Assert.That(node.Children, Is.EquivalentTo(new int[] { 1 }));

            var childNode = gltf.Nodes[node.Children[0]];

            Assert.NotNull(childNode);

            Assert.AreEqual(0, childNode.Mesh);

            var mesh = gltf.Meshes[childNode.Mesh.Value];

            Assert.AreEqual("Mesh", mesh.Name);
            Assert.AreEqual(1, mesh.Primitives.Count);

            var primitive = mesh.Primitives[0];

            Assert.AreEqual(1, primitive.Attributes["NORMAL"]);
            Assert.AreEqual(2, primitive.Attributes["POSITION"]);
            Assert.AreEqual(3, primitive.Attributes["TEXCOORD_0"]);
            Assert.AreEqual(0, primitive.Indices);
            Assert.AreEqual(Types.Mesh.PrimitiveType.ModeEnum.TRIANGLES, primitive.Mode);
            Assert.AreEqual(0, primitive.Material);

            // Accesses
            // index 1
            var normal = store.GetOrLoadTypedBufferByAccessorIndex(primitive.Attributes["NORMAL"]);

            Assert.AreEqual(24, normal.GetEntity <Vec3Float>().GetEnumerable().Count());

            // index 2
            var position = store.GetOrLoadTypedBufferByAccessorIndex(primitive.Attributes["POSITION"]);

            Assert.AreEqual(24, position.GetEntity <Vec3Float>().GetEnumerable().Count());

            // index 3
            var texCoord0 = store.GetOrLoadTypedBufferByAccessorIndex(primitive.Attributes["TEXCOORD_0"]);

            Assert.AreEqual(24, texCoord0.GetEntity <Vec2Float>().GetEnumerable().Count());

            // index 0
            var indicies = store.GetOrLoadTypedBufferByAccessorIndex(primitive.Indices.Value);

            Assert.AreEqual(36, indicies.GetPrimitivesAsCasted <int>().Count());

            // Materials
            // index0
            var material = gltf.Materials[primitive.Material.Value];

            Assert.AreEqual("Texture", material.Name);

            Assert.NotNull(material.PbrMetallicRoughness);
            Assert.NotNull(material.PbrMetallicRoughness.BaseColorTexture);
            Assert.AreEqual(0, material.PbrMetallicRoughness.BaseColorTexture.Index);
            Assert.AreEqual(0.0f, material.PbrMetallicRoughness.MetallicFactor);

            // Textures
            var texture = gltf.Textures[material.PbrMetallicRoughness.BaseColorTexture.Index];

            Assert.AreEqual(0, texture.Sampler);
            Assert.AreEqual(0, texture.Source);

            // Samplers
            var sampler = gltf.Samplers[texture.Sampler.Value];

            // Images(source)
            var imageResource = store.GetOrLoadImageResourceAt(texture.Source.Value);

            Assert.AreEqual(23516, imageResource.Data.Count);

            var imageBytes = new byte[imageResource.Data.Count];

            Array.Copy(imageResource.Data.Array, imageResource.Data.Offset, imageBytes, 0, imageResource.Data.Count);

            // Compare to the base texture image
            var baseImagePath = new string[] {
                "BoxTextured",
                "glTF",
                "CesiumLogoFlat.png"
            }.Aggregate("SampleModels", (b, p) => Path.Combine(b, p));
            var baseImageBytes = File.ReadAllBytes(baseImagePath);

            Assert.AreEqual(BitConverter.ToString(baseImageBytes),
                            BitConverter.ToString(imageBytes));
        }
        public void TestModel(ResourcesStore store)
        {
            var gltf = store.Gltf;

            Assert.AreEqual(2, gltf.Accessors.Count);

            // indicesView
            {
                var typedBuffer = store.GetOrLoadTypedBufferByAccessorIndex(0);
                Assert.AreEqual(Types.Accessor.ComponentTypeEnum.UNSIGNED_SHORT,
                                typedBuffer.Accessor.ComponentType);
                Assert.AreEqual(Types.Accessor.TypeEnum.Scalar, typedBuffer.Accessor.Type);

                var entiry = typedBuffer.GetEntity <ushort>();
                Assert.AreEqual(36, entiry.Length);
            }

            // positionView
            {
                var typedBuffer = store.GetOrLoadTypedBufferByAccessorIndex(1);
                Assert.AreEqual(Types.Accessor.ComponentTypeEnum.FLOAT,
                                typedBuffer.Accessor.ComponentType);
                Assert.AreEqual(Types.Accessor.TypeEnum.Vec3, typedBuffer.Accessor.Type);

                Assert.NotNull(typedBuffer.Accessor.Sparse);
                Assert.AreEqual(3, typedBuffer.Accessor.Sparse.Count);

                var entiry = typedBuffer.GetEntity <Vec3Float>();
                Assert.AreEqual(14, entiry.Length);

                // For indices
                var indices = entiry.SparseIndices;
                Assert.AreEqual(3, indices.GetEnumerable().Count());
                Assert.That(indices.GetEnumerable(), Is.EquivalentTo(new int[] { 8, 10, 12 }));

                // For values
                var values = entiry.SparseValues;
                Assert.AreEqual(3, values.GetEnumerable().Count());
                Assert.That(values.GetEnumerable(), Is.EquivalentTo(new Vec3Float[] {
                    new Vec3Float(1.0f, 2.0f, 0.0f),
                    new Vec3Float(3.0f, 3.0f, 0.0f),
                    new Vec3Float(5.0f, 4.0f, 0.0f),
                }));

                // For merged view
                Assert.That(entiry.GetEnumerable(), Is.EquivalentTo(new Vec3Float[] {
                    new Vec3Float(0.0f, 0.0f, 0.0f),
                    new Vec3Float(1.0f, 0.0f, 0.0f),
                    new Vec3Float(2.0f, 0.0f, 0.0f),
                    new Vec3Float(3.0f, 0.0f, 0.0f),
                    new Vec3Float(4.0f, 0.0f, 0.0f),
                    new Vec3Float(5.0f, 0.0f, 0.0f),
                    new Vec3Float(6.0f, 0.0f, 0.0f),
                    new Vec3Float(0.0f, 1.0f, 0.0f),
                    new Vec3Float(1.0f, 2.0f, 0.0f),             // 8 (sparse)
                    new Vec3Float(2.0f, 1.0f, 0.0f),
                    new Vec3Float(3.0f, 3.0f, 0.0f),             // 10 (sparse)
                    new Vec3Float(4.0f, 1.0f, 0.0f),
                    new Vec3Float(5.0f, 4.0f, 0.0f),             // 12 (sparse)
                    new Vec3Float(6.0f, 1.0f, 0.0f),
                }));
            }
        }