Example #1
0
        /// <param name="src">GameObject instance in scene or prefab</param>
        public static bool Execute(GameObject src, bool onlyBlendShapeRenderers)
        {
            var results = new List <MeshIntegrationResult>();

            // instance or prefab => copy
            var copy = GameObject.Instantiate(src);

            copy.name = copy.name + "_mesh_integration";

            // integrate
            if (onlyBlendShapeRenderers)
            {
                results.Add(MeshIntegratorUtility.Integrate(copy, onlyBlendShapeRenderers: MeshEnumerateOption.OnlyWithBlendShape));
                results.Add(MeshIntegratorUtility.Integrate(copy, onlyBlendShapeRenderers: MeshEnumerateOption.OnlyWithoutBlendShape));
            }
            else
            {
                results.Add(MeshIntegratorUtility.Integrate(copy, onlyBlendShapeRenderers: MeshEnumerateOption.All));
            }

            // replace
            MeshIntegratorUtility.ReplaceMeshWithResults(copy, results);

            // write mesh asset.
            foreach (var result in results)
            {
                var mesh      = result.IntegratedRenderer.sharedMesh;
                var assetPath = GetMeshWritePath(mesh);
                Debug.LogFormat("CreateAsset: {0}", assetPath);
                AssetDatabase.CreateAsset(mesh, assetPath);
            }

            if (src.GetGameObjectType() == GameObjectType.AssetPrefab)
            {
                // write prefab.
                {
                    var prefabPath = UnityPath.FromAsset(src);
                    prefabPath = prefabPath.Parent.Child($"{prefabPath.FileNameWithoutExtension}_integrated.prefab");
                    Debug.LogFormat("WritePrefab: {0}", prefabPath);
                    PrefabUtility.SaveAsPrefabAsset(copy, prefabPath.Value);
                }

                // destroy copy in scene.
                GameObject.DestroyImmediate(copy);
            }
            else
            {
                // do nothing. keep copy.
            }

            return(true);
        }
Example #2
0
        public static void MeshIntegrator(GameObject go)
        {
            MeshIntegratorUtility.Integrate(go, onlyBlendShapeRenderers: true);
            MeshIntegratorUtility.Integrate(go, onlyBlendShapeRenderers: false);

            var outputObject = GameObject.Instantiate(go);

            outputObject.name = outputObject.name + "_mesh_integration";
            var skinnedMeshes = outputObject.GetComponentsInChildren <SkinnedMeshRenderer>();
            var normalMeshes  = outputObject.GetComponentsInChildren <MeshFilter>();

            // destroy integrated meshes in the source
            foreach (var skinnedMesh in go.GetComponentsInChildren <SkinnedMeshRenderer>())
            {
                if (skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_NAME ||
                    skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_BLENDSHAPE_NAME)
                {
                    GameObject.DestroyImmediate(skinnedMesh.gameObject);
                }
            }
            foreach (var skinnedMesh in skinnedMeshes)
            {
                // destroy original meshes in the copied GameObject
                if (!(skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_NAME ||
                      skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_BLENDSHAPE_NAME))
                {
                    GameObject.DestroyImmediate(skinnedMesh);
                }
                // check if the integrated mesh is empty
                else if (skinnedMesh.sharedMesh.subMeshCount == 0)
                {
                    GameObject.DestroyImmediate(skinnedMesh.gameObject);
                }
                // save mesh data
                else if (skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_NAME ||
                         skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_BLENDSHAPE_NAME)
                {
                    SaveMeshData(skinnedMesh.sharedMesh);
                }
            }
            foreach (var normalMesh in normalMeshes)
            {
                if (normalMesh.sharedMesh.name != MeshIntegratorUtility.INTEGRATED_MESH_NAME)
                {
                    if (normalMesh.gameObject.GetComponent <MeshRenderer>())
                    {
                        GameObject.DestroyImmediate(normalMesh.gameObject.GetComponent <MeshRenderer>());
                    }
                    GameObject.DestroyImmediate(normalMesh);
                }
            }
        }