Exemplo n.º 1
0
        public static void BackUpScene()
        {
            CreateOptRoot();

            XOCp_OptimizerHandler xoh = XOGloble.Root.GetComponent <XOCp_OptimizerHandler>();

            xoh.olst.Clear();
            xoh.nlst.Clear();

            UnityEngine.Renderer[] rs = (UnityEngine.Renderer[])FindObjectsOfType(typeof(UnityEngine.Renderer));
            for (int i = 0; i < rs.Length; i++)
            {
                UnityEngine.Renderer r = rs[i];
                if (r is MeshRenderer)
                {
                    if (r.GetComponent <TextMesh>() != null)
                    {
                        continue;                         //don't add TextMeshes
                    }
                    if (r.GetComponent <XOCp_MarkIgnore>() != null)
                    {
                        continue;
                    }
                    if (r.gameObject.isStatic == true && r.gameObject.activeInHierarchy == true)
                    {
                        if (xoh != null)
                        {
                            xoh.olst.Add(r.gameObject);
                        }

                        if (XOMeshSplit.CanSplit(r.gameObject) == true)
                        {
                            XOMeshSplit.ExtractMaterialMeshes(r.gameObject);
                        }
                        else
                        {
                            UnityEngine.GameObject backobj = NewIterM(r.gameObject);
                            xoh.nlst.Add(backobj);
                            AddObjDic(r.gameObject, backobj);
                        }

                        xoh.bDisplayOrigin = false;
                        xoh.bDisplayNew    = true;
                        r.gameObject.SetActive(false);
                    }
                }
            }

            if (xoh != null)
            {
                xoh.CreateNodes();
            }
        }
Exemplo n.º 2
0
        public static void AddObjDic(UnityEngine.GameObject origin, UnityEngine.GameObject newone)
        {
            XOCp_OptimizerHandler oph = XOGloble.Root.GetComponent <XOCp_OptimizerHandler>();

            if (oph != null)
            {
                Dictionary <int, OriginItermedia> p2O = oph.p2O;
                if (p2O != null)
                {
                    int instid = origin.GetInstanceID();
                    if (p2O.ContainsKey(instid) == false)
                    {
                        OriginItermedia objdata = new OriginItermedia();
                        objdata.origin = origin;
                        objdata.Iterm  = newone;
                        p2O.Add(instid, objdata);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static UnityEngine.GameObject NewIterM(UnityEngine.GameObject origin)
        {
            UnityEngine.GameObject backobj = UnityEngine.GameObject.Instantiate(origin, origin.transform.position, origin.transform.rotation) as UnityEngine.GameObject;
            backobj.transform.parent     = XOGloble.IterM.transform;
            backobj.transform.localScale = origin.transform.lossyScale;
            backobj.name = XOUtil.Path(origin);
            XOCp_MarkIntermediate mi = backobj.AddComponent <XOCp_MarkIntermediate>();

            mi.OriginInstID = origin.GetInstanceID();
            mi.goOrigin     = origin;
            backobj.SetActive(true);

            XOCp_OptimizerHandler xoh = XOGloble.Root.GetComponent <XOCp_OptimizerHandler>();

            if (xoh != null)
            {
                xoh.AddObjLstByShaderMaterial(backobj);
            }

            XOUtil.Display(backobj, false);
            return(backobj);
        }
Exemplo n.º 4
0
        public static void ExtractMaterialMeshes(UnityEngine.GameObject rObject)
        {
            if (rObject == null)
            {
                return;
            }
            if (!rObject.activeInHierarchy)
            {
                return;
            }

            // If this object has a mesh, extract it
            MeshFilter lMeshFilter = rObject.GetComponent <MeshFilter>();

            if (lMeshFilter == null)
            {
                return;
            }

            // Ensure we're only extracting rendered meshes
            MeshRenderer lMeshRenderer = rObject.GetComponent <MeshRenderer>();

            if (lMeshRenderer == null)
            {
                return;
            }

            // We need to break the mesh into it's sub mesh parts so we can assign materials
            Mesh     lMesh     = null;
            Material lMaterial = null;

            int lSubMeshCount = lMeshFilter.sharedMesh.subMeshCount;

            for (int i = 0; i < lSubMeshCount; i++)
            {
                // This is easy, use the shared instance
                if (i == 0)
                {
                    lMesh     = lMeshFilter.sharedMesh;
                    lMaterial = lMeshRenderer.sharedMaterial;
                }
                // Process each sub-mesh individually
                else
                {
                    // Create a mesh from the sub-mesh
                    lMesh = CreateMesh(lMeshFilter.sharedMesh, i);

                    // Find the material it's using
                    if (lMeshRenderer.sharedMaterials.Length > i)
                    {
                        lMaterial = lMeshRenderer.sharedMaterials[i];
                    }
                    else
                    {
                        lMaterial = lMeshRenderer.sharedMaterials[lMeshRenderer.sharedMaterials.Length - 1];
                    }
                }

                string newname = "split-" + rObject.name + i;
                UnityEngine.GameObject newobj = new UnityEngine.GameObject(newname);
                newobj.transform.parent     = XOGloble.IterM.transform;
                newobj.transform.position   = lMeshFilter.transform.position;
                newobj.transform.rotation   = lMeshFilter.transform.rotation;
                newobj.transform.localScale = lMeshFilter.transform.lossyScale;


                newobj.isStatic = true;


                MeshFilter lMergedMeshFilter = newobj.AddComponent(typeof(MeshFilter)) as MeshFilter;
                lMergedMeshFilter.mesh = lMesh;


                // CombineInstance[] lMeshes = new CombineInstance[1];
                // lMeshes[0].transform = lMeshFilter.transform.localToWorldMatrix;
                // lMeshes[0].mesh = lMesh;

                lMergedMeshFilter.sharedMesh.name = "Merged Mesh of " + lSubMeshCount + " items";
                // lMergedMeshFilter.sharedMesh.CombineMeshes(lMeshes, true);
                // Unwrapping.GenerateSecondaryUVSet(lMergedMeshFilter.sharedMesh);



                MeshRenderer lMergedMeshRenderer = newobj.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
                lMergedMeshRenderer.sharedMaterial = lMaterial;

                XOCp_OptimizerHandler xoh = XOGloble.Root.GetComponent <XOCp_OptimizerHandler>();
                if (xoh != null)
                {
                    xoh.nlst.Add(newobj);
                    xoh.AddObjLstByShaderMaterial(newobj);
                }
            }
        }
Exemplo n.º 5
0
        private static UnityEngine.GameObject MergeMeshes(MaterialMeshes rMaterialMeshes, UnityEngine.GameObject mMergedParent)
        {
            if (rMaterialMeshes == null)
            {
                return(null);
            }
            if (rMaterialMeshes.Meshes.Count == 0)
            {
                return(null);
            }

            int lMeshCount = rMaterialMeshes.Meshes.Count;

            // Determine the position of the new merged object
            UnityEngine.Vector3 lCenter = UnityEngine.Vector3.zero;
            for (int i = 0; i < lMeshCount; i++)
            {
                lCenter.x += rMaterialMeshes.Transform[i].m03;
                lCenter.y += rMaterialMeshes.Transform[i].m13;
                lCenter.z += rMaterialMeshes.Transform[i].m23;
            }

            lCenter /= lMeshCount;

            // Create the list of meshes that will be merged
            CombineInstance[] lMeshes = new CombineInstance[lMeshCount];
            for (int i = 0; i < lMeshCount; i++)
            {
                // Ensure our meshes are positioned relative to the center
                Matrix4x4 lMatrix = rMaterialMeshes.Transform[i];
                lMatrix.m03 -= lCenter.x;
                lMatrix.m13 -= lCenter.y;
                lMatrix.m23 -= lCenter.z;

                // Process sub meshes
                lMeshes[i].transform = lMatrix;
                lMeshes[i].mesh      = rMaterialMeshes.Meshes[i];
            }

            // Create the object that will represent the new mesh
            UnityEngine.GameObject lMergedObject = new UnityEngine.GameObject();
            lMergedObject.name = "Merged Mesh:" + rMaterialMeshes.Meshes.Count;
            lMergedObject.transform.position = lCenter;

            // Combine the meshes in the new object
            MeshFilter lMergedMeshFilter = lMergedObject.AddComponent(typeof(MeshFilter)) as MeshFilter;

            lMergedMeshFilter.mesh            = new Mesh();
            lMergedMeshFilter.sharedMesh.name = "Merged Mesh of " + lMeshCount + " items";
            lMergedMeshFilter.sharedMesh.CombineMeshes(lMeshes, true);

            // Generate UV2 for lightmapping
            Unwrapping.GenerateSecondaryUVSet(lMergedMeshFilter.sharedMesh);

            // Set the material(s) for the new object
            MeshRenderer lMergedMeshRenderer = lMergedObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;

            lMergedMeshRenderer.sharedMaterial = rMaterialMeshes.Material;

            // Assign the parent of the first selected object's parent
            if (mMergedParent != null)
            {
                lMergedObject.transform.parent = mMergedParent.transform;
            }

            lMergedObject.isStatic = true;
            XOCp_OptimizerHandler xoh = XOGloble.Root.GetComponent <XOCp_OptimizerHandler>();

            if (xoh != null)
            {
                xoh.nlst.Add(lMergedObject);
            }

            // Return the newly created game object
            return(lMergedObject);
        }
Exemplo n.º 6
0
        //在这里方法中就可以绘制面板。
        public override void OnInspectorGUI()
        {
            //得到Test对象
            XOCp_OptimizerHandler hdl = (XOCp_OptimizerHandler)target;


            // serializedObject.Update();
            // EditorGUILayout.PropertyField(serializedObject.FindProperty("integers"), true);
            // serializedObject.ApplyModifiedProperties();



            EditorGUILayout.BeginVertical();

            if (GUILayout.Button("DisplayOrigin"))
            {
                if (hdl.bDisplayOrigin == true)
                {
                    hdl.bDisplayOrigin = false;
                    XOCp_OptimizerHandler xoh = hdl.Root.GetComponent <XOCp_OptimizerHandler>();
                    if (xoh != null)
                    {
                        foreach (UnityEngine.GameObject go in xoh.olst)
                        {
                            go.SetActive(false);
                        }
                    }
                }
                else if (hdl.bDisplayOrigin == false)
                {
                    hdl.bDisplayOrigin = true;
                    XOCp_OptimizerHandler xoh = hdl.Root.GetComponent <XOCp_OptimizerHandler>();
                    if (xoh != null)
                    {
                        foreach (UnityEngine.GameObject go in xoh.olst)
                        {
                            go.SetActive(true);
                        }
                    }
                }
            }

            if (GUILayout.Button("DeleteOrigin"))
            {
                XOCp_OptimizerHandler xoh = hdl.Root.GetComponent <XOCp_OptimizerHandler>();
                if (xoh != null)
                {
                    foreach (UnityEngine.GameObject go in xoh.olst)
                    {
                        UnityEngine.GameObject.DestroyImmediate(go);
                    }
                }
            }


            if (GUILayout.Button("DisplayNew"))
            {
                if (hdl.bDisplayNew == true)
                {
                    hdl.bDisplayNew = false;
                    XOCp_OptimizerHandler xoh = hdl.Root.GetComponent <XOCp_OptimizerHandler>();
                    if (xoh != null)
                    {
                        foreach (UnityEngine.GameObject go in xoh.nlst)
                        {
                            go.SetActive(false);
                        }
                    }
                }
                else if (hdl.bDisplayNew == false)
                {
                    hdl.bDisplayNew = true;
                    XOCp_OptimizerHandler xoh = hdl.Root.GetComponent <XOCp_OptimizerHandler>();
                    if (xoh != null)
                    {
                        foreach (UnityEngine.GameObject go in xoh.nlst)
                        {
                            go.SetActive(true);
                        }
                    }
                }
            }

            EditorGUILayout.EndVertical();
        }