Beispiel #1
0
 public static void Rotate(glTFNode node)
 {
     if (node.matrix != null && node.matrix.Length == 16)
     {
         throw new NotImplementedException("matrix not implemented !");
     }
     else
     {
         if (node.translation != null && node.translation.Length == 3)
         {
             // rotate 180 degrees around the Y axis
             var t = node.translation;
             t[0] = -t[0];
             t[2] = -t[2];
         }
         if (node.rotation != null && node.rotation.Length == 4)
         {
             if (node.rotation[0] == 0 && node.rotation[1] == 0 && node.rotation[2] == 0 && node.rotation[3] == 1)
             {
                 // indentity
             }
             else
             {
                 throw new UnNormalizedException();
             }
         }
         if (node.scale != null && node.scale.Length == 3)
         {
             // do nothing
         }
     }
 }
        public static string ToJson(this glTFNode self)
        {
            var f = new JsonFormatter();

            GltfSerializer.Serialize_gltf_nodes_ITEM(f, self);
            return(f.ToString());
        }
Beispiel #3
0
        public void ExportNodes(Node root, List <Node> nodes, List <MeshGroup> groups, ExportArgs option)
        {
            foreach (var x in nodes)
            {
                var node = new glTFNode
                {
                    name = x.Name,
                };

                node.translation = x.LocalTranslation.ToFloat3();
                node.rotation    = x.LocalRotation.ToFloat4();
                node.scale       = x.LocalScaling.ToFloat3();

                if (x.MeshGroup != null)
                {
                    node.mesh = groups.IndexOfThrow(x.MeshGroup);
                    var skin = x.MeshGroup.Skin;
                    if (skin != null)
                    {
                        var skinIndex = Storage.Gltf.skins.Count;
                        var gltfSkin  = new glTFSkin()
                        {
                            joints = skin.Joints.Select(joint => nodes.IndexOfThrow(joint)).ToArray()
                        };
                        if (skin.InverseMatrices == null)
                        {
                            skin.CalcInverseMatrices();
                        }
                        if (skin.InverseMatrices != null)
                        {
                            gltfSkin.inverseBindMatrices = skin.InverseMatrices.AddAccessorTo(Storage, 0, option.sparse);
                        }
                        if (skin.Root != null)
                        {
                            gltfSkin.skeleton = nodes.IndexOf(skin.Root);
                        }
                        Storage.Gltf.skins.Add(gltfSkin);
                        node.skin = skinIndex;
                    }
                }

                node.children = x.Children.Select(child => nodes.IndexOfThrow(child)).ToArray();

                Storage.Gltf.nodes.Add(node);
            }

            Storage.Gltf.scenes.Add(new gltfScene()
            {
                nodes = root.Children.Select(child => nodes.IndexOfThrow(child)).ToArray()
            });
        }
Beispiel #4
0
        public static IEnumerable <(glTFNode, glTFSkin)> ExportNodes(INativeArrayManager arrayManager, List <Node> nodes, List <MeshGroup> groups, ExportingGltfData data, ExportArgs option)
        {
            foreach (var node in nodes)
            {
                var gltfNode = new glTFNode
                {
                    name = node.Name,
                };
                glTFSkin gltfSkin = default;

                gltfNode.translation = node.LocalTranslation.ToFloat3();
                gltfNode.rotation    = node.LocalRotation.ToFloat4();
                gltfNode.scale       = node.LocalScaling.ToFloat3();

                if (node.MeshGroup != null)
                {
                    gltfNode.mesh = groups.IndexOfThrow(node.MeshGroup);
                    var skin = node.MeshGroup.Skin;
                    if (skin != null)
                    {
                        gltfSkin = new glTFSkin()
                        {
                            joints = skin.Joints.Select(joint => nodes.IndexOfThrow(joint)).ToArray()
                        };
                        if (skin.InverseMatrices == null)
                        {
                            skin.CalcInverseMatrices(arrayManager);
                        }
                        if (skin.InverseMatrices != null)
                        {
                            gltfSkin.inverseBindMatrices = skin.InverseMatrices.AddAccessorTo(data, 0, option.sparse);
                        }
                        if (skin.Root != null)
                        {
                            gltfSkin.skeleton = nodes.IndexOf(skin.Root);
                        }
                    }
                }

                gltfNode.children = node.Children.Select(child => nodes.IndexOfThrow(child)).ToArray();

                yield return(gltfNode, gltfSkin);
            }
        }
Beispiel #5
0
        private T NodeExtensionSerializeAndDeserialize <T>(T src, string extensionName, Action <JsonFormatter, T> serializer, Func <JsonNode, T> deserializer) where T : class
        {
            var node = new glTFNode();

            node.name = "testNode";
            var f = new UniJSON.JsonFormatter();

            serializer(f, src);
            glTFExtensionExport.GetOrCreate(ref node.extensions).Add(extensionName, f.GetStore().Bytes);

            var node2 = new glTFNode();

            node2.extensions = (node.extensions as glTFExtensionExport).Deserialize();

            if (node2.extensions.TryDeserializeExtensions(extensionName, deserializer, out T dst))
            {
                return(dst);
            }
            else
            {
                return(null);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Set up the child GameObject components.
        /// </summary>
        /// <param name="go"></param>
        /// <param name="gltfNode"></param>
        protected virtual void SetupChildComponent(GameObject go, glTFNode gltfNode)
        {
            if (gltfNode.extensions != null)
            {
                if (gltfNode.extensions.VGO_nodes != null)
                {
                    var nodeVGO = gltfNode.extensions.VGO_nodes;

                    // VgoGameObject
                    if (nodeVGO.gameObject != null)
                    {
                        VgoGameObjectConverter.SetGameObjectValue(go, nodeVGO.gameObject);
                    }

                    // Collider[]
                    if (nodeVGO.colliders != null)
                    {
                        var colliders = nodeVGO.colliders;

                        colliders.ForEach(vc => go.AddComponent <Collider>(vc));
                    }

                    // Rigidbody
                    if (nodeVGO.rigidbody != null)
                    {
                        go.AddComponent <Rigidbody>(nodeVGO.rigidbody);
                    }

                    // Light
                    if (nodeVGO.light != null)
                    {
                        go.AddComponent <Light>(nodeVGO.light);
                    }

                    // ParticleSystem
                    if (nodeVGO.particleSystem != null)
                    {
                        VgoParticleSystemImporter.AddComponent(go, nodeVGO.particleSystem, GetMaterials(), GetTextures());
                    }

                    // VgoRight
                    if (nodeVGO.right != null)
                    {
                        go.AddComponent <VgoRight>(nodeVGO.right);
                    }

                    // VgoSkybox
                    if (nodeVGO.skybox != null)
                    {
                        var skybox = go.AddComponent <Skybox>();

                        List <Material> materials = GetMaterials() as List <Material>;

                        if (materials != null)
                        {
                            int vgoMaterialIndex = nodeVGO.skybox.materialIndex;

                            if ((0 <= vgoMaterialIndex) && (vgoMaterialIndex < materials.Count))
                            {
                                skybox.material = materials[vgoMaterialIndex];
                            }
                        }
                    }
                }
            }
        }