Example #1
0
        // File serialization
        public Mesh saveMesh(Mesh mesh, string objectName = "Scene")
        {
            string baseName        = GLTFUtils.cleanName(objectName + ".asset");
            string fullPath        = Path.Combine(_importMeshesDirectory, baseName);
            string meshProjectPath = GLTFUtils.getPathProjectFromAbsolute(fullPath);

            serializeAsset(mesh, meshProjectPath, fullPath, true);
            return(AssetDatabase.LoadAssetAtPath(meshProjectPath, typeof(Mesh)) as Mesh);
        }
        private void LoadClip(Sein_audioClipsExtension.AudioClip clip, string gltfPath, string basePath, int i)
        {
            var uri = Path.Combine(gltfPath, clip.uri);

            if (clip.uri != null && File.Exists(uri))
            {
                var tmp  = clip.uri.Split('/');
                var name = tmp[tmp.Length - 1];

                if (clip.name != null)
                {
                    name = clip.name;
                }

                var path = Path.Combine(basePath, name);

                if (File.Exists(path))
                {
                    if (!IMPORTED_URIS.Contains(clip.uri))
                    {
                        name = Path.GetFileNameWithoutExtension(name) + "-" + i + Path.GetExtension(name);
                        path = Path.Combine(basePath, name);
                        FileUtil.CopyFileOrDirectory(uri, path);
                        IMPORTED_URIS.Add(clip.uri);
                    }
                }
                else
                {
                    FileUtil.CopyFileOrDirectory(uri, path);
                    IMPORTED_URIS.Add(clip.uri);
                }

                AssetDatabase.Refresh();
                var unityClip = AssetDatabase.LoadAssetAtPath <AudioClip>(path);

                var directory = GLTFUtils.getPathProjectFromAbsolute(basePath);
                path = Path.Combine(directory, name + ".asset");
                var seinClip = ScriptableObject.CreateInstance <SeinAudioClip>();
                seinClip.clip   = unityClip;
                seinClip.mode   = clip.mode;
                seinClip.isLazy = clip.isLazy;

                AssetDatabase.CreateAsset(seinClip, path);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();

                seinClip = AssetDatabase.LoadAssetAtPath <SeinAudioClip>(path);

                IMPORTED_CLIPS.Add(seinClip);
            }
            else
            {
                Debug.LogWarning("Audio clip not found");
            }
        }
Example #3
0
        public Material saveMaterial(Material material, int index)
        {
            string baseName            = generateName(material.name.Length > 0 ? material.name.Trim() : "material", index) + ".mat";
            string materialAssetPath   = Path.Combine(_importMaterialsDirectory, baseName);
            string materialProjectPath = GLTFUtils.getPathProjectFromAbsolute(materialAssetPath);

            serializeAsset(material, materialProjectPath, materialAssetPath, true);

            // Reload as asset
            return((Material)AssetDatabase.LoadAssetAtPath(materialProjectPath, typeof(Material)));
        }
Example #4
0
        public Texture2D saveTexture(Texture2D texture, int index = -1, string imageName = "")
        {
            string basename = GLTFUtils.cleanName(texture.name + (index >= 0 ? "_" + index.ToString() : "") + ".png"); // Extension will be overridden
            string fullPath = Path.Combine(_importTexturesDirectory, basename);

            // Write texture
            string newAssetPath = GLTFTextureUtils.writeTextureOnDisk(texture, fullPath, true);

            // Reload as asset
            string    projectPath = GLTFUtils.getPathProjectFromAbsolute(newAssetPath);
            Texture2D tex         = (Texture2D)AssetDatabase.LoadAssetAtPath(projectPath, typeof(Texture2D));

            _parsedImages.Add(tex);
            return(tex);
        }
Example #5
0
        // https://docs.unity3d.com/ScriptReference/Animations.AnimatorController.html
        public void createAnimatorAsset(List <AnimationClip> clips)
        {
            string animatorname = getPrefabName(_prefabName);
            string animatorPath = GLTFUtils.getPathProjectFromAbsolute(_importAnimationDirectory + "/" + animatorname + ".controller");
            var    controller   = AnimatorController.CreateAnimatorControllerAtPath(animatorPath);

            foreach (var clip in clips)
            {
                Motion motion = (Motion)clip;
                controller.AddMotion(motion);
            }

            if (_animatorController == null)
            {
                _animatorController = controller;
            }
        }
Example #6
0
        public void saveAnimationClip(AnimationClip clip)
        {
            if (_importAnimationDirectory == null)
            {
                createAnimationDirectory();
            }

            var directory = GLTFUtils.getPathProjectFromAbsolute(_importAnimationDirectory);
            var path      = directory + "/" + clip.name + ".anim";

            if (File.Exists(path))
            {
                int i = 1;
                while (true)
                {
                    var newDir = directory + "/" + i;
                    path = newDir + "/" + clip.name + ".anim";

                    if (!Directory.Exists(newDir))
                    {
                        Directory.CreateDirectory(newDir);
                        break;
                    }

                    if (!File.Exists(path))
                    {
                        break;
                    }
                }
            }

            AssetDatabase.CreateAsset(clip, path);
            AssetDatabase.Refresh();

            // Add animation to animator
            AnimationClip loadedClip = AssetDatabase.LoadAssetAtPath <AnimationClip>(path);

            loadedClip.wrapMode = WrapMode.Loop;
        }
Example #7
0
        private static Texture2D SplitRoughnessTexture(Texture2D texture)
        {
            int             width     = texture.width;
            int             height    = texture.height;
            string          assetPath = AssetDatabase.GetAssetPath(texture);
            TextureImporter im        = AssetImporter.GetAtPath(assetPath) as TextureImporter;

            im.isReadable = true;
            im.SaveAndReimport();
            var iColor = texture.GetPixels();

            im.isReadable = false;
            im.SaveAndReimport();

            // Let's consider that the three textures have the same resolution
            Color[] colors = new Color[width * height];
            for (int i = 0; i < colors.Length; i += 1)
            {
                float a = 1 - iColor[i].a;

                colors[i] = new Color(a, a, a);
            }

            var res = new Texture2D(width, height);

            res.SetPixels(colors);

            string basename = Path.GetFileNameWithoutExtension(assetPath) + "_rg";
            string fullPath = Path.GetFullPath(Path.GetDirectoryName(assetPath)) + "/" + basename + ".png";

            string    newAssetPath = GLTFTextureUtils.writeTextureOnDisk(res, fullPath, true);
            string    projectPath  = GLTFUtils.getPathProjectFromAbsolute(newAssetPath);
            Texture2D tex          = (Texture2D)AssetDatabase.LoadAssetAtPath(projectPath, typeof(Texture2D));

            return(tex);
        }
Example #8
0
        public GameObject savePrefab(GameObject sceneObject, string _importDirectory, bool instanciateInScene = false)
        {
            string baseName            = getPrefabName(sceneObject.name) + ".prefab";
            string fullPath            = Path.Combine(_importDirectory, baseName);
            string prefabPathInProject = GLTFUtils.getPathProjectFromAbsolute(fullPath);

            if (File.Exists(fullPath))
            {
                var list = new List <GameObjectAndPath>();
                var temp = new List <GameObjectAndPath> {
                    new GameObjectAndPath {
                        path = new List <string>(),
                        obj  = sceneObject
                    }
                };

                var children = new List <GameObjectAndPath>();

                var        prefabResPath = prefabPathInProject.Replace("Assets/Resources/", "").Replace(".prefab", "");
                GameObject originPrefab  = Resources.Load(prefabResPath) as GameObject;
                // Get all children and path
                while (true)
                {
                    children.Clear();
                    temp.ForEach(item => children.Add(item));
                    temp.Clear();

                    if (children.Count == 0)
                    {
                        break;
                    }

                    foreach (GameObjectAndPath child in children)
                    {
                        list.Add(child);

                        foreach (Transform tr in child.obj.transform)
                        {
                            var obj  = tr.gameObject;
                            var path = new List <string>(child.path);
                            path.Add(obj.name);
                            temp.Add(new GameObjectAndPath {
                                path = path, obj = obj
                            });
                        }
                    }
                }

                // 遍历并更新新prefab中物体的transform和组件
                // 最终保存新的的prefab
                // 之后重新绑定prefab
                copyComponets(originPrefab, sceneObject);
                list.RemoveAt(0);
                foreach (GameObjectAndPath child in list)
                {
                    var tr = originPrefab.transform.Find(string.Join("/", child.path.ToArray()));

                    if (tr == null)
                    {
                        continue;
                    }

                    var origObj = tr.gameObject;
                    var obj     = child.obj;

                    copyComponets(origObj, obj);
                }

                File.Delete(fullPath);
                File.Delete(fullPath + ".meta");
                AssetDatabase.Refresh();
            }

            if (_animatorController != null)
            {
                var animator = sceneObject.GetComponent <Animator>();

                if (animator == null && !hasAnimatorExtension)
                {
                    animator = sceneObject.AddComponent <Animator>();
                    animator.runtimeAnimatorController = _animatorController;
                }
            }

            _generatedFiles.Add(fullPath);
            return(PrefabUtility.CreatePrefab(prefabPathInProject, sceneObject));
        }
Example #9
0
        public override void Import(EditorImporter importer, GameObject gameObject, Node gltfNode, Extension extension)
        {
            var seinAnimator = (Sein_animatorExtension)extension;
            var id           = "";

            for (var i = 0; i < seinAnimator.modelAnimations.Length; i += 1)
            {
                var prefix = (seinAnimator.prefixes != null && seinAnimator.prefixes.Length > i) ? seinAnimator.prefixes[i] : seinAnimator.prefix;
                var name   = seinAnimator.modelAnimations[i];

                if (prefix != null && prefix.Length > 0)
                {
                    id += prefix + "@" + name;
                }
                else
                {
                    id += name;
                }
            }

            AnimatorController controller;

            if (IMPORTED_CONTROLLERS.ContainsKey(id))
            {
                controller = IMPORTED_CONTROLLERS[id];
            }
            else
            {
                var controllerName = seinAnimator.name != null && seinAnimator.name != "" ? seinAnimator.name : gameObject.name;
                var controllerPath = Path.Combine(new string[] { importer.importDirectoryPath, "animations", controllerName + ".controller" });
                if (File.Exists(controllerPath))
                {
                    int index = 1;
                    while (true)
                    {
                        controllerPath = Path.Combine(new string[] { importer.importDirectoryPath, "animations", controllerName + "-" + index + ".controller" });

                        if (!File.Exists(controllerPath))
                        {
                            break;
                        }
                    }
                }

                controller = AnimatorController.CreateAnimatorControllerAtPath(GLTFUtils.getPathProjectFromAbsolute(controllerPath));

                for (var i = 0; i < seinAnimator.modelAnimations.Length; i += 1)
                {
                    var prefix = (seinAnimator.prefixes != null && seinAnimator.prefixes.Length > i) ? seinAnimator.prefixes[i] : seinAnimator.prefix;
                    var name   = seinAnimator.modelAnimations[i];

                    if (prefix != null && prefix.Length > 0)
                    {
                        name = prefix + "@" + name;
                    }

                    if (!IMPORTED_CLIPS.ContainsKey(name))
                    {
                        continue;
                    }

                    controller.AddMotion((Motion)IMPORTED_CLIPS[name]);
                }
            }

            var animator = gameObject.AddComponent <Animator>();

            animator.runtimeAnimatorController = controller;
        }
        public override void Import(EditorImporter importer, GameObject gameObject, Node gltfNode, Extension extension)
        {
            var seinAnimator = (Sein_animatorExtension)extension;
            var id           = "";

            for (var i = 0; i < seinAnimator.modelAnimations.Length; i += 1)
            {
                var prefix = (seinAnimator.prefixes != null && seinAnimator.prefixes.Length > i) ? seinAnimator.prefixes[i] : seinAnimator.prefix;
                var name   = seinAnimator.modelAnimations[i];

                if (prefix != null && prefix.Length > 0)
                {
                    id += prefix + "@" + name;
                }
                else
                {
                    id += name;
                }
            }

            AnimatorController controller;

            if (IMPORTED_CONTROLLERS.ContainsKey(id))
            {
                controller = IMPORTED_CONTROLLERS[id];
            }
            else
            {
                var controllerName = seinAnimator.name != null && seinAnimator.name != "" ? seinAnimator.name : gameObject.name;
                var controllerPath = Path.Combine(new string[] { importer.importDirectoryPath, "animations", controllerName + ".controller" });
                if (File.Exists(controllerPath))
                {
                    int index = 1;
                    while (true)
                    {
                        controllerPath = Path.Combine(new string[] { importer.importDirectoryPath, "animations", controllerName + "-" + index + ".controller" });

                        if (!File.Exists(controllerPath))
                        {
                            break;
                        }
                    }
                }

                controller = AnimatorController.CreateAnimatorControllerAtPath(GLTFUtils.getPathProjectFromAbsolute(controllerPath));

                for (var i = 0; i < seinAnimator.modelAnimations.Length; i += 1)
                {
                    var prefix = (seinAnimator.prefixes != null && seinAnimator.prefixes.Length > i) ? seinAnimator.prefixes[i] : seinAnimator.prefix;
                    var name   = seinAnimator.modelAnimations[i];

                    if (prefix != null && prefix.Length > 0)
                    {
                        name = prefix + "@" + name;
                    }

                    if (!IMPORTED_CLIPS.ContainsKey(name))
                    {
                        continue;
                    }

                    var clip = IMPORTED_CLIPS[name];
                    controller.AddMotion((Motion)clip);

                    var nodePath = AnimationUtility.CalculateTransformPath(gameObject.transform, importer.sceneObject.transform);
                    var temp     = new List <Temp>();
                    foreach (var binding in AnimationUtility.GetCurveBindings(clip))
                    {
                        var path         = binding.path;
                        var realPath     = path.Substring(nodePath.Length);
                        var propertyName = binding.propertyName;
                        var type         = binding.type;
                        var curve        = AnimationUtility.GetEditorCurve(clip, binding);

                        temp.Add(new Temp {
                            path = realPath, propertyName = propertyName, type = type, curve = curve
                        });
                    }

                    clip.ClearCurves();

                    foreach (var binding in temp)
                    {
                        clip.SetCurve(binding.path, binding.type, binding.propertyName, binding.curve);
                    }
                }
            }

            var animator = gameObject.AddComponent <Animator>();

            animator.runtimeAnimatorController = controller;
        }