Exemplo n.º 1
0
        public void CONFacetsAreAppliedToSimpleTypes()
        {
            IImporterContext context = CreateContext(MethodBase.GetCurrentMethod().Name);

            BDTXsdImporter.ImportXsd(context);

            var aRestrictedTextBDT = AssertThatBDTLibraryContainsBDT(context, "A_Restricted_Text", "String");

            Assert.That(aRestrictedTextBDT.Con.Pattern, Is.EqualTo("[abc]*"));

            var anotherRestrictedTextBDT = AssertThatBDTLibraryContainsBDT(context, "Another_Restricted_Text", "String");

            Assert.That(anotherRestrictedTextBDT.Con.MinimumLength, Is.EqualTo("5"));
            Assert.That(anotherRestrictedTextBDT.Con.MaximumLength, Is.EqualTo("9"));

            var aRestrictedNumberBDT = AssertThatBDTLibraryContainsBDT(context, "A_Restricted_Number", "Decimal");

            Assert.That(aRestrictedNumberBDT.Con.MinimumInclusive, Is.EqualTo("4"));
            Assert.That(aRestrictedNumberBDT.Con.MaximumInclusive, Is.EqualTo("6"));
            Assert.That(aRestrictedNumberBDT.Con.FractionDigits, Is.EqualTo("3"));
            Assert.That(aRestrictedNumberBDT.Con.TotalDigits, Is.EqualTo("5"));

            var furtherRestrictedNumberBDT = AssertThatBDTLibraryContainsBDT(context, "Further_A_Restricted_Number", "Decimal");

            Assert.That(furtherRestrictedNumberBDT.Con.MinimumInclusive, Is.EqualTo("5"));
            Assert.That(furtherRestrictedNumberBDT.Con.MaximumInclusive, Is.EqualTo("6"));
            Assert.That(furtherRestrictedNumberBDT.Con.FractionDigits, Is.EqualTo("3"));
            Assert.That(furtherRestrictedNumberBDT.Con.TotalDigits, Is.EqualTo("5"));

            var anotherRestrictedNumberBDT = AssertThatBDTLibraryContainsBDT(context, "Another_Restricted_Number", "Decimal");

            Assert.That(anotherRestrictedNumberBDT.Con.MinimumExclusive, Is.EqualTo("6"));
            Assert.That(anotherRestrictedNumberBDT.Con.MaximumExclusive, Is.EqualTo("8"));
        }
Exemplo n.º 2
0
        public static GameObject Import(IImporterContext context, Byte[] bytes,
                                        gltfImporter.OnLoadCallback callback           = null,
                                        gltfImporter.CreateMaterialFunc createMaterial = null)
        {
            var chunks = ParseGlbChanks(bytes);

            if (chunks.Count != 2)
            {
                throw new Exception("unknown chunk count: " + chunks.Count);
            }

            if (chunks[0].ChunkType != GlbChunkType.JSON)
            {
                throw new Exception("chunk 0 is not JSON");
            }

            if (chunks[1].ChunkType != GlbChunkType.BIN)
            {
                throw new Exception("chunk 1 is not BIN");
            }

            var jsonBytes = chunks[0].Bytes;
            var json      = Encoding.UTF8.GetString(jsonBytes.Array, jsonBytes.Offset, jsonBytes.Count);

            return(gltfImporter.Import(context,
                                       json,
                                       chunks[1].Bytes,
                                       callback,
                                       null,
                                       createMaterial
                                       ));
        }
Exemplo n.º 3
0
        private static void AssertHasSUP(IImporterContext context, IBdt bdtText, int index, string basicTypeName, string name)
        {
            var expectedBasicType = context.PRIMLibrary.GetPrimByName(basicTypeName);
            var sup = bdtText.Sups.ElementAt(index);

            Assert.That(sup.Name, Is.EqualTo(name));
            Assert.That(sup.BasicType.Id, Is.EqualTo(expectedBasicType.Id));
        }
Exemplo n.º 4
0
        public void SetsTheCONsBasicTypeToTheBuiltinDatatypesOfTheSimpleContentExtensionOfComplexTypes()
        {
            IImporterContext context = CreateContext(ComplexType);

            BDTXsdImporter.ImportXsd(context);

            AssertThatBDTLibraryContainsBDT(context, "Text", "String");
        }
Exemplo n.º 5
0
        private static void AssertBDTIsBasedOn(IImporterContext context, IBdt bdt, string cdtName)
        {
            var cdt = context.CDTLibrary.GetCdtByName(cdtName);

            Assert.That(bdt.BasedOn, Is.Not.Null, "BasedOn is null");
            Assert.That(bdt.BasedOn, Is.Not.Null, "BasedOn.CDT is null");
            Assert.That(bdt.BasedOn.Id, Is.EqualTo(cdt.Id), string.Format("Based on wrong CDT:\nExpected: <{0}>\nBut was:  <{1}>", cdt.Name, bdt.BasedOn.Name));
        }
Exemplo n.º 6
0
        public void CreatesBDTsWithoutSUPsForSimpleTypes()
        {
            IImporterContext context = CreateContext(SimpleType);

            BDTXsdImporter.ImportXsd(context);

            var bdtText = AssertThatBDTLibraryContainsBDT(context, "Text", "String");

            AssertHasSUPs(bdtText, 0);
        }
Exemplo n.º 7
0
        public void CreatesSUPsForXsdAttributesInComplexTypes()
        {
            IImporterContext context = CreateContext(ComplexType);

            BDTXsdImporter.ImportXsd(context);

            var bdtText = AssertThatBDTLibraryContainsBDT(context, "Text", "String");

            AssertHasSUPs(bdtText, 1);
            AssertHasSUP(context, bdtText, 0, "String", "propertyTermName");
        }
Exemplo n.º 8
0
        private static IBdt AssertThatBDTLibraryContainsBDT(IImporterContext context, string bdtName, string conBasicTypeName)
        {
            var  expectedCONBasicType = context.PRIMLibrary.GetPrimByName(conBasicTypeName);
            IBdt bdtText = context.BDTLibrary.GetBdtByName(bdtName);

            Assert.That(bdtText, Is.Not.Null, string.Format("Expected BDT named '{0}' not generated.", bdtName));
            Assert.That(bdtText.Con, Is.Not.Null, string.Format("CON of BDT {0} is null", bdtName));
            Assert.That(bdtText.Con.BasicType, Is.Not.Null, string.Format("BasicType of BDT {0} is null", bdtName));
            Assert.That(bdtText.Con.BasicType.Id, Is.EqualTo(expectedCONBasicType.Id), string.Format("Wrong basic type for CON of BDT {0}:\nExpected: <{1}>\nBut was: <{2}>", bdtName, expectedCONBasicType.Name, bdtText.Con.BasicType.Name));
            return(bdtText);
        }
Exemplo n.º 9
0
        public void SUPBasicTypesAreDeterminedFromXsdTypes()
        {
            IImporterContext context = CreateContext(MethodBase.GetCurrentMethod().Name);

            BDTXsdImporter.ImportXsd(context);

            var bdtText = AssertThatBDTLibraryContainsBDT(context, "BDT", "String");

            AssertHasSUPs(bdtText, 2);
            AssertHasSUP(context, bdtText, 0, "Decimal", "decimalSUP");
            AssertHasSUP(context, bdtText, 1, "String", "stringSUP");
        }
Exemplo n.º 10
0
        public void BDTInheritsCONFromParentBDT()
        {
            IImporterContext context = CreateContext(MethodBase.GetCurrentMethod().Name);

            BDTXsdImporter.ImportXsd(context);

            AssertThatBDTLibraryContainsBDT(context, "Text", "Decimal");
            AssertThatBDTLibraryContainsBDT(context, "Restricted_Text", "Decimal");

            AssertThatBDTLibraryContainsBDT(context, "SimpleText", "Decimal");
            AssertThatBDTLibraryContainsBDT(context, "Restricted_SimpleText", "Decimal");
        }
Exemplo n.º 11
0
 public Texture2D GetMetallicRoughnessConverted(IImporterContext ctx)
 {
     if (m_metallicRoughness == null)
     {
         //m_metallicRoughness = new Texture2D(Texture.width, Texture.height, TextureFormat.ARGB32, true);
         var texture = CopyTexture();
         texture.SetPixels32(texture.GetPixels32().Select(ConvertMetallicRoughness).ToArray());
         texture.name        = this.Texture.name + ".converted";
         m_metallicRoughness = texture;
         ctx.AddObjectToAsset(texture.name, texture);
     }
     return(m_metallicRoughness);
 }
Exemplo n.º 12
0
        public void SetsBasedOnDependencyForImportedBDTs()
        {
            IImporterContext context = CreateContext(SimpleType);

            BDTXsdImporter.ImportXsd(context);

            var bdtText = AssertThatBDTLibraryContainsBDT(context, "Text", "String");

            AssertBDTIsBasedOn(context, bdtText, "Text");

            var bdtQualifiedText = AssertThatBDTLibraryContainsBDT(context, "Qualified_Text", "String");

            AssertBDTIsBasedOn(context, bdtQualifiedText, "Text");
        }
Exemplo n.º 13
0
        public void ResolvesBDTTypeNamesAccordingToTheNDR()
        {
            IImporterContext context = CreateContext(MethodBase.GetCurrentMethod().Name);

            BDTXsdImporter.ImportXsd(context);

            AssertThatBDTLibraryContainsBDT(context, "SimpleText", "String");
            AssertThatBDTLibraryContainsBDT(context, "Qualified_SimpleText", "String");
            AssertThatBDTLibraryContainsBDT(context, "Qualifier1_Qualifier2_SimpleText", "String");

            AssertThatBDTLibraryContainsBDT(context, "ComplexText", "String");
            AssertThatBDTLibraryContainsBDT(context, "Qualified_ComplexText", "String");
            AssertThatBDTLibraryContainsBDT(context, "Qualifier1_Qualifier2_ComplexText", "String");
        }
Exemplo n.º 14
0
        public void BDTCanProhibitInheritanceOfSUPFromParentBDT()
        {
            IImporterContext context = CreateContext(MethodBase.GetCurrentMethod().Name);

            BDTXsdImporter.ImportXsd(context);

            var bdtText = AssertThatBDTLibraryContainsBDT(context, "Text", "Decimal");

            AssertHasSUPs(bdtText, 1);
            AssertHasSUP(context, bdtText, 0, "String", "sup");

            var bdtRestrictedText = AssertThatBDTLibraryContainsBDT(context, "Restricted_Text", "Decimal");

            AssertHasSUPs(bdtRestrictedText, 0);
        }
Exemplo n.º 15
0
        public static void ImportXsd(IImporterContext context)
        {
            var bdtSchema = new BDTSchema(context);

            bdtSchema.CreateBDTs();
        }
Exemplo n.º 16
0
        public static GameObject Import(IImporterContext ctx, string json,
                                        ArraySegment <Byte> glbBinChunk,
                                        OnLoadCallback callback             = null,
                                        GetBlendShapeName getBlendShapeName = null,
                                        CreateMaterialFunc createMaterial   = null
                                        )
        {
            if (getBlendShapeName == null)
            {
                getBlendShapeName = DefaultGetBlendShapeName;
            }

            // exclude not gltf-2.0
            var parsed = json.ParseAsJson();

            try
            {
                if (parsed["asset"]["version"].GetString() != "2.0")
                {
                    Debug.LogWarningFormat("is not gltf-2.0: {0}", ctx.Path);
                    return(null);
                }
            }
            catch (Exception)
            {
                Debug.LogWarningFormat("{0}: fail to parse json", ctx.Path);
                return(null);
            }

            // parse json
            glTF gltf = null;

            try
            {
                gltf = JsonUtility.FromJson <glTF>(json);
            }
            catch (Exception)
            {
                Debug.LogWarningFormat("{0}: fail to parse json", ctx.Path);
                return(null);
            }
            if (gltf == null)
            {
                Debug.LogWarningFormat("{0}: fail to parse json", ctx.Path);
                return(null);
            }

            if (gltf.asset.version != "2.0")
            {
                Debug.LogWarningFormat("unknown gltf version {0}", gltf.asset.version);
                return(null);
            }

            gltf.baseDir = Path.GetDirectoryName(ctx.Path);
            //Debug.LogFormat("{0}: {1}", ctx.Path, gltf);

            foreach (var buffer in gltf.buffers)
            {
                buffer.OpenStorage(gltf.baseDir, glbBinChunk);
            }

            // textures
            var textures = ImportTextures(gltf)
                           .Select(x =>
            {
                var samplerIndex = gltf.textures[x.TextureIndex].sampler;
                var sampler      = gltf.samplers[samplerIndex];

                if (x.Texture == null)
                {
                    Debug.LogWarningFormat("May be import order, not yet texture is not imported. Later, manualy reimport {0}", ctx.Path);
                }
                else
                {
                    SetSampler(x.Texture, sampler);

                    if (!x.IsAsset)
                    {
                        ctx.AddObjectToAsset(x.Texture.name, x.Texture);
                    }
                }
                return(x);
            })
                           .ToArray();

            if (createMaterial == null)
            {
                createMaterial = CreateMaterialFuncFromShader(Shader.Find("Standard"));
            }

            // materials
            List <Material> materials = new List <Material>();

            if (gltf.materials == null || !gltf.materials.Any())
            {
                materials.Add(createMaterial(ctx, 0, null, textures));
            }
            else
            {
                for (int i = 0; i < gltf.materials.Count; ++i)
                {
                    Material mat = null;

                    // [Maquette Code]
                    if (Maquette.Unity.MqAddonUtility.NeedsMaquetteMaterial(gltf.materials[i].name))
                    {
                        string materialName = gltf.materials[i].name;
                        materialName = materialName.Replace(" (Instance)", "");
                        mat          = Maquette.Unity.MqAddonUtility.CreateMaquetteMaterial(materialName);
                        if (gltf.materials[i].pbrMetallicRoughness.baseColorTexture != null)
                        {
                            int baseTextureIndex = gltf.materials[i].pbrMetallicRoughness.baseColorTexture.index;
                            if (baseTextureIndex >= 0 && baseTextureIndex < textures.Length)
                            {
                                var texture = textures[baseTextureIndex];
                                mat.mainTexture = texture.Texture;
                            }
                        }
                    }
                    else
                    {
                        mat = createMaterial(ctx, i, gltf.materials[i], textures);
                    }

                    materials.Add(mat);
                }
            }
            foreach (var material in materials)
            {
                if (material != null)
                {
                    ctx.AddObjectToAsset(material.name, material);
                }
            }

            // meshes
            var meshes = gltf.meshes.Select((x, i) =>
            {
                var meshWithMaterials = ImportMesh(gltf, i, x, materials, getBlendShapeName);
                var mesh = meshWithMaterials.Mesh;
                if (string.IsNullOrEmpty(mesh.name))
                {
                    mesh.name = string.Format("UniGLTF import#{0}", i);
                }

                ctx.AddObjectToAsset(mesh.name, mesh);

                return(meshWithMaterials);
            }).ToArray();

            // nodes
            var _nodes = gltf.nodes.Select(x => ImportNode(x)).ToArray();

            var nodes = _nodes.Select((go, i) =>
            {
                if (string.IsNullOrEmpty(go.name))
                {
                    go.name = string.Format("node{0:000}", i);
                }

                var nodeWithSkin = new TransformWithSkin
                {
                    Transform = go.transform,
                };

                var node = gltf.nodes[i];

                //
                // build hierachy
                //
                if (node.children != null)
                {
                    foreach (var child in node.children)
                    {
                        _nodes[child].transform.SetParent(_nodes[i].transform,
                                                          false // node has local transform
                                                          );
                    }
                }

                //
                // attach mesh
                //
                if (node.mesh != -1)
                {
                    var mesh = meshes[node.mesh];
                    if (mesh.Mesh.blendShapeCount == 0 && node.skin == -1)
                    {
                        // without blendshape and bone skinning
                        var filter               = go.AddComponent <MeshFilter>();
                        filter.sharedMesh        = mesh.Mesh;
                        var renderer             = go.AddComponent <MeshRenderer>();
                        renderer.sharedMaterials = mesh.Materials;
                    }
                    else
                    {
                        var renderer = go.AddComponent <SkinnedMeshRenderer>();

                        if (node.skin != -1)
                        {
                            nodeWithSkin.SkinIndex = node.skin;
                        }

                        renderer.sharedMesh      = mesh.Mesh;
                        renderer.sharedMaterials = mesh.Materials;
                    }
                }

                return(nodeWithSkin);
            }).ToArray();

            //
            // fix node's coordinate. z-back to z-forward
            //
            var globalTransformMap = nodes.ToDictionary(x => x.Transform, x => new PosRot
            {
                Position = x.Transform.position,
                Rotation = x.Transform.rotation,
            });

            foreach (var x in gltf.rootnodes)
            {
                // fix nodes coordinate
                // reverse Z in global
                var t = nodes[x].Transform;
                //t.SetParent(root.transform, false);

                foreach (var transform in t.Traverse())
                {
                    var g = globalTransformMap[transform];
                    transform.position = g.Position.ReverseZ();
                    transform.rotation = g.Rotation.ReverseZ();
                }
            }

            // skinning
            foreach (var x in nodes)
            {
                var skinnedMeshRenderer = x.Transform.GetComponent <SkinnedMeshRenderer>();
                if (skinnedMeshRenderer != null)
                {
                    var mesh = skinnedMeshRenderer.sharedMesh;
                    if (x.SkinIndex.HasValue)
                    {
                        if (mesh == null)
                        {
                            throw new Exception();
                        }
                        if (skinnedMeshRenderer == null)
                        {
                            throw new Exception();
                        }

                        if (x.SkinIndex.Value < gltf.skins.Count)
                        {
                            var skin = gltf.skins[x.SkinIndex.Value];

                            skinnedMeshRenderer.sharedMesh = null;

                            var joints = skin.joints.Select(y => nodes[y].Transform).ToArray();
                            skinnedMeshRenderer.bones = joints;
                            //skinnedMeshRenderer.rootBone = nodes[skin.skeleton].Transform;

                            if (skin.inverseBindMatrices != -1)
                            {
                                // BlendShape only ?
#if false
                                // https://docs.unity3d.com/ScriptReference/Mesh-bindposes.html
                                var hipsParent          = nodes[0].Transform;
                                var calculatedBindPoses = joints.Select(y => y.worldToLocalMatrix * hipsParent.localToWorldMatrix).ToArray();
                                mesh.bindposes = calculatedBindPoses;
#else
                                var bindPoses = gltf.GetArrayFromAccessor <Matrix4x4>(skin.inverseBindMatrices)
                                                .Select(y => y.ReverseZ())
                                                .ToArray()
                                ;
                                mesh.bindposes = bindPoses;
#endif
                            }

                            skinnedMeshRenderer.sharedMesh = mesh;
                        }
                    }
                }
            }

            var root = new GameObject("_root_");
            ctx.SetMainGameObject("root", root);

            foreach (var x in gltf.rootnodes)
            {
                // fix nodes coordinate
                // reverse Z in global
                var t = nodes[x].Transform;
                t.SetParent(root.transform, false);
            }

            // animation
            if (gltf.animations != null && gltf.animations.Any())
            {
                var clip = new AnimationClip();
                clip.name = ANIMATION_NAME;
                clip.ClearCurves();

                ImportAnimation(root.transform, clip, gltf.animations, nodes.Select(x => x.Transform).ToArray(), gltf);

                clip.legacy   = true;
                clip.name     = "legacy";
                clip.wrapMode = WrapMode.Loop;
                var animation = root.AddComponent <Animation>();
                animation.clip = clip;

                ctx.AddObjectToAsset(ANIMATION_NAME, clip);
            }

            if (callback != null)
            {
                callback(ctx, json,
                         nodes.Select(x => x.Transform).ToArray(),
                         meshes.Select(x => x.Mesh).ToList()
                         );
            }

            Debug.LogFormat("Import {0}", ctx.Path);

            return(root);
        }
Exemplo n.º 17
0
 public BDTSchema(IImporterContext context)
 {
     Context = context;
 }