Example #1
0
    public void UniGLTFSimpleSceneTest()
    {
        var go      = CreateSimpelScene();
        var context = new ImporterContext();

        try
        {
            // export
            var gltf = new glTF();
            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(go);
                exporter.Export();

                // import
                context.Json = gltf.ToJson();
                Debug.LogFormat("{0}", context.Json);
                gltfImporter.Import <glTF>(context, new ArraySegment <byte>());

                AssertAreEqual(go.transform, context.Root.transform);
            }
        }
        finally
        {
            //Debug.LogFormat("Destory, {0}", go.name);
            GameObject.DestroyImmediate(go);
            context.Destroy(true);
        }
    }
Example #2
0
        /// <summary>
        /// Export GLB.
        /// </summary>
        public static void ExportGlb()
        {
            EditorApplication.isPlaying = false;

            try
            {
                string errorMessage;

                if (!Validate(out errorMessage))
                {
                    Debug.LogAssertion(errorMessage);

                    EditorUtility.DisplayDialog("Error", errorMessage, "OK");

                    return;
                }

                GameObject root = Selection.activeObject as GameObject;

                string path = EditorUtility.SaveFilePanel(title: "Save File Dialog", directory: "", defaultName: root.name + ".glb", extension: "glb");

                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                var gltf = new glTF();

                using (var exporter = new gltfExporter(gltf))
                {
                    exporter.Prepare(root);
                    exporter.Export();
                }

                byte[] bytes = gltf.ToGlbBytes();

                File.WriteAllBytes(path, bytes);

                Debug.LogFormat("Export VGO file.\nGameObject: {0}, output: {1}", root.name, path);

                if (path.StartsWithUnityAssetPath())
                {
                    AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                    AssetDatabase.Refresh();
                }
            }
            catch
            {
                throw;
            }
        }
        public static glTF_VRM_BlendShapeBind Create(Transform root, BlendShapeBinding binding,
                                                     gltfExporter exporter)
        {
            var transform = UniGLTF.UnityExtensions.GetFromPath(root.transform, binding.RelativePath);
            var renderer  = transform.GetComponent <SkinnedMeshRenderer>();

            if (renderer == null)
            {
                return(null);
            }

            if (!renderer.gameObject.activeInHierarchy)
            {
                return(null);
            }

            var mesh      = renderer.sharedMesh;
            var meshIndex = exporter.Meshes.IndexOf(mesh);

            if (meshIndex == -1)
            {
                return(null);
            }

            if (!exporter.MeshBlendShapeIndexMap.TryGetValue(mesh, out Dictionary <int, int> blendShapeIndexMap))
            {
                // この Mesh は  エクスポートされていない
                return(null);
            }

            if (!blendShapeIndexMap.TryGetValue(binding.Index, out int blendShapeIndex))
            {
                // この blendShape は エクスポートされていない(空だった?)
                return(null);
            }

            return(new glTF_VRM_BlendShapeBind
            {
                mesh = meshIndex,
                index = blendShapeIndex,
                weight = binding.Weight,
            });
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="master"></param>
        /// <param name="clip"></param>
        /// <param name="transform"></param>
        /// <param name="meshes"></param>
        /// <param name="blendShapeIndexMap">エクスポート中にBlendShapeIndexが変わったかもしれない</param>
        public static void Add(this glTF_VRM_BlendShapeMaster master,
                               BlendShapeClip clip, gltfExporter exporter)
        {
            var list = new List <glTF_VRM_BlendShapeBind>();

            if (clip.Values != null)
            {
                foreach (var value in clip.Values)
                {
                    var bind = Create(exporter.Copy.transform, value, exporter);
                    if (bind == null)
                    {
                        // Debug.LogFormat("{0}: skip blendshapebind", clip.name);
                        continue;
                    }
                    list.Add(bind);
                }
            }

            var materialList = new List <glTF_VRM_MaterialValueBind>();

            if (clip.MaterialValues != null)
            {
                materialList.AddRange(clip.MaterialValues.Select(y => new glTF_VRM_MaterialValueBind
                {
                    materialName = y.MaterialName,
                    propertyName = y.ValueName,
                    targetValue  = y.TargetValue.ToArray(),
                }));
            }

            var group = new glTF_VRM_BlendShapeGroup
            {
                name           = clip.BlendShapeName,
                presetName     = clip.Preset.ToString().ToLower(),
                isBinary       = clip.IsBinary,
                binds          = list,
                materialValues = materialList,
            };

            master.blendShapeGroups.Add(group);
        }
Example #5
0
        public static glTF_VRM_BlendShapeGroup Serialize(this BlendShapeClip clip, gltfExporter exporter)
        {
            var bindList = new List <glTF_VRM_BlendShapeBind>();

            if (clip.Values != null && exporter != null)
            {
                foreach (var value in clip.Values)
                {
                    var bind = Create(exporter.Copy.transform, value, exporter);
                    if (bind == null)
                    {
                        // Debug.LogFormat("{0}: skip blendshapebind", clip.name);
                        continue;
                    }
                    bindList.Add(bind);
                }
            }

            var materialValueBinds = new List <glTF_VRM_MaterialValueBind>();

            if (clip.MaterialValues != null)
            {
                materialValueBinds.AddRange(clip.MaterialValues.Select(y => new glTF_VRM_MaterialValueBind
                {
                    materialName = y.MaterialName,
                    propertyName = y.ValueName,
                    targetValue  = y.TargetValue.ToArray(),
                }));
            }

            return(new glTF_VRM_BlendShapeGroup
            {
                name = clip.BlendShapeName,
                presetName = clip.Preset.ToString().ToLowerInvariant(),
                isBinary = clip.IsBinary,
                binds = bindList,
                materialValues = materialValueBinds,
            });
        }
 public static glTF_VRM_BlendShapeBind Cerate(Transform root, BlendShapeBinding binding,
                                              gltfExporter exporter)
 {
     return(Create(root, binding, exporter));
 }
Example #7
0
 public static void Add(this glTF_VRM_BlendShapeMaster master,
                        BlendShapeClip clip, gltfExporter exporter)
 {
     master.blendShapeGroups.Add(clip.Serialize(exporter));
 }
Example #8
0
        public static glTF_VRM_BlendShapeBind Create(Transform root, BlendShapeBinding binding,
                                                     gltfExporter exporter)
        {
            if (root == null || exporter == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty((binding.RelativePath)))
            {
                Debug.LogWarning("binding.RelativePath is null");
                return(null);
            }
            var found = root.transform.Find(binding.RelativePath);

            if (found == null)
            {
                var name = binding.RelativePath.Split('/').Last();
                found = root.GetComponentsInChildren <Transform>().Where(x => x.name == name).First();
                if (found == null)
                {
                    Debug.LogWarning($"{binding.RelativePath} not found");
                    return(null);
                }
                else
                {
                    Debug.LogWarning($"fall back '{binding.RelativePath}' => '{found.RelativePathFrom(root)}'");
                }
            }
            var renderer = found.GetComponent <SkinnedMeshRenderer>();

            if (renderer == null)
            {
                return(null);
            }

            if (!renderer.gameObject.activeInHierarchy)
            {
                return(null);
            }

            var mesh      = renderer.sharedMesh;
            var meshIndex = exporter.Meshes.IndexOf(mesh);

            if (meshIndex == -1)
            {
                return(null);
            }

            if (!exporter.MeshBlendShapeIndexMap.TryGetValue(mesh, out Dictionary <int, int> blendShapeIndexMap))
            {
                // この Mesh は  エクスポートされていない
                return(null);
            }

            if (!blendShapeIndexMap.TryGetValue(binding.Index, out int blendShapeIndex))
            {
                // この blendShape は エクスポートされていない(空だった?)
                return(null);
            }

            return(new glTF_VRM_BlendShapeBind
            {
                mesh = meshIndex,
                index = blendShapeIndex,
                weight = binding.Weight,
            });
        }