public static void RebuildPrefab(MB3_MeshBakerCommon mom){
		if (MB3_MeshCombiner.EVAL_VERSION) return;
		if (mom is MB3_MeshBaker){
			MB3_MeshBaker mb = (MB3_MeshBaker) mom;
			MB3_MeshCombinerSingle mbs = (MB3_MeshCombinerSingle) mb.meshCombiner;
			GameObject prefabRoot = mom.resultPrefab;
			GameObject rootGO = (GameObject) PrefabUtility.InstantiatePrefab(prefabRoot);
			MB3_MeshCombinerSingle.BuildSceneHierarch(mbs, rootGO, mbs.GetMesh());		
			string prefabPth = AssetDatabase.GetAssetPath(prefabRoot);
			PrefabUtility.ReplacePrefab(rootGO,AssetDatabase.LoadAssetAtPath(prefabPth,typeof(GameObject)),ReplacePrefabOptions.ConnectToPrefab);
			Editor.DestroyImmediate(rootGO);
		} else if (mom is MB3_MultiMeshBaker){
			MB3_MultiMeshBaker mmb = (MB3_MultiMeshBaker) mom;
			MB3_MultiMeshCombiner mbs = (MB3_MultiMeshCombiner) mmb.meshCombiner;
			GameObject prefabRoot = mom.resultPrefab;
			GameObject rootGO = (GameObject) PrefabUtility.InstantiatePrefab(prefabRoot);
			for (int i = 0; i < mbs.meshCombiners.Count; i++){
				MB3_MeshCombinerSingle.BuildSceneHierarch(mbs.meshCombiners[i].combinedMesh, rootGO, mbs.meshCombiners[i].combinedMesh.GetMesh(),true);
			}
			string prefabPth = AssetDatabase.GetAssetPath(prefabRoot);
			PrefabUtility.ReplacePrefab(rootGO,AssetDatabase.LoadAssetAtPath(prefabPth,typeof(GameObject)),ReplacePrefabOptions.ConnectToPrefab);
			Editor.DestroyImmediate(rootGO);				
		} else {
			Debug.LogError("Argument was not a MB3_MeshBaker or an MB3_MultiMeshBaker.");	
		}				
	}
	void testCombine(){
		MB3_MeshCombinerSingle mb = new MB3_MeshCombinerSingle();
		Debug.Log ("About to bake 1");
		mb.AddDeleteGameObjects(listOfObjsToCombineGood,null);
		mb.Apply();
		mb.UpdateGameObjects(listOfObjsToCombineGood);
		mb.Apply ();
		mb.AddDeleteGameObjects(null,listOfObjsToCombineGood);
		mb.Apply ();
		Debug.Log ("Did bake 1");
		Debug.Log ("About to bake 2");
		mb.AddDeleteGameObjects(listOfObjsToCombineBad,null);
		mb.Apply();
		Debug.Log ("Did bake 2");

		Debug.Log("Doing same with multi mesh combiner");
		MB3_MultiMeshCombiner mmb = new MB3_MultiMeshCombiner();
		Debug.Log ("About to bake 3");
		mmb.AddDeleteGameObjects(listOfObjsToCombineGood,null);
		mmb.Apply();
		mmb.UpdateGameObjects(listOfObjsToCombineGood);
		mmb.Apply ();
		mmb.AddDeleteGameObjects(null,listOfObjsToCombineGood);
		mmb.Apply ();
		Debug.Log ("Did bake 3");
		Debug.Log ("About to bake 4");
		mmb.AddDeleteGameObjects(listOfObjsToCombineBad,null);
		mmb.Apply();
		Debug.Log ("Did bake 4");
	}
Exemple #3
0
    void testCombine()
    {
        MB3_MeshCombinerSingle mb = new MB3_MeshCombinerSingle();

        Debug.Log("About to bake 1");
        mb.AddDeleteGameObjects(listOfObjsToCombineGood, null);
        mb.Apply();
        mb.UpdateGameObjects(listOfObjsToCombineGood);
        mb.Apply();
        mb.AddDeleteGameObjects(null, listOfObjsToCombineGood);
        mb.Apply();
        Debug.Log("Did bake 1");
        Debug.Log("About to bake 2 should get error that one material doesn't match");
        mb.AddDeleteGameObjects(listOfObjsToCombineBad, null);
        mb.Apply();
        Debug.Log("Did bake 2");

        Debug.Log("Doing same with multi mesh combiner");
        MB3_MultiMeshCombiner mmb = new MB3_MultiMeshCombiner();

        Debug.Log("About to bake 3");
        mmb.AddDeleteGameObjects(listOfObjsToCombineGood, null);
        mmb.Apply();
        mmb.UpdateGameObjects(listOfObjsToCombineGood);
        mmb.Apply();
        mmb.AddDeleteGameObjects(null, listOfObjsToCombineGood);
        mmb.Apply();
        Debug.Log("Did bake 3");
        Debug.Log("About to bake 4  should get error that one material doesn't match");
        mmb.AddDeleteGameObjects(listOfObjsToCombineBad, null);
        mmb.Apply();
        Debug.Log("Did bake 4");
    }
Exemple #4
0
    private void testCombine()
    {
        MB3_MeshCombinerSingle mb3_MeshCombinerSingle = new MB3_MeshCombinerSingle();

        Debug.Log("About to bake 1");
        mb3_MeshCombinerSingle.AddDeleteGameObjects(this.listOfObjsToCombineGood, null, true);
        mb3_MeshCombinerSingle.Apply();
        mb3_MeshCombinerSingle.UpdateGameObjects(this.listOfObjsToCombineGood, true, true, true, true, false, false, false, false, false, false);
        mb3_MeshCombinerSingle.Apply();
        mb3_MeshCombinerSingle.AddDeleteGameObjects(null, this.listOfObjsToCombineGood, true);
        mb3_MeshCombinerSingle.Apply();
        Debug.Log("Did bake 1");
        Debug.Log("About to bake 2");
        mb3_MeshCombinerSingle.AddDeleteGameObjects(this.listOfObjsToCombineBad, null, true);
        mb3_MeshCombinerSingle.Apply();
        Debug.Log("Did bake 2");
        Debug.Log("Doing same with multi mesh combiner");
        MB3_MultiMeshCombiner mb3_MultiMeshCombiner = new MB3_MultiMeshCombiner();

        Debug.Log("About to bake 3");
        mb3_MultiMeshCombiner.AddDeleteGameObjects(this.listOfObjsToCombineGood, null, true);
        mb3_MultiMeshCombiner.Apply();
        mb3_MultiMeshCombiner.UpdateGameObjects(this.listOfObjsToCombineGood, true, true, true, true, false, false, false, false, false, false);
        mb3_MultiMeshCombiner.Apply();
        mb3_MultiMeshCombiner.AddDeleteGameObjects(null, this.listOfObjsToCombineGood, true);
        mb3_MultiMeshCombiner.Apply();
        Debug.Log("Did bake 3");
        Debug.Log("About to bake 4");
        mb3_MultiMeshCombiner.AddDeleteGameObjects(this.listOfObjsToCombineBad, null, true);
        mb3_MultiMeshCombiner.Apply();
        Debug.Log("Did bake 4");
    }
		static public Mesh BakeOneMesh(MB3_MeshCombinerSingle mom, string newMeshFilePath, GameObject objToBake){
			Mesh outMesh = null;
			if (objToBake == null){
				Debug.LogError("An object on the list of objects to combine is 'None'. Use Command-Delete on Mac OS X; Delete or Shift-Delete on Windows to remove this one element.");
				return null;					
			}
			
			MB3_EditorMethods editorMethods = new MB3_EditorMethods();
			GameObject[] objs = new GameObject[] {objToBake};
			Renderer r = MB_Utility.GetRenderer(objToBake);
			if (r is SkinnedMeshRenderer){
				mom.renderType = MB_RenderType.skinnedMeshRenderer;	
			} else if (r is MeshRenderer) {
				mom.renderType = MB_RenderType.meshRenderer;
			} else {
				Debug.LogError("Unsupported Renderer type on object. Must be SkinnedMesh or MeshFilter.");
				return null;	
			}
			if (newMeshFilePath == null && newMeshFilePath.Length != 0){ //todo check directory exists
				Debug.LogError("File path was not in assets folder.");
				return null;				
			}
			if (mom.AddDeleteGameObjects(objs,null,false)){
				mom.Apply();
				Mesh mf = MB_Utility.GetMesh(objToBake);
				if (mf != null){
					Debug.Log("Creating mesh for " + objToBake.name + " with adjusted UVs at: " + newMeshFilePath);
					AssetDatabase.CreateAsset(mom.GetMesh(),  newMeshFilePath);
					outMesh = (Mesh) AssetDatabase.LoadAssetAtPath(newMeshFilePath, typeof(Mesh));
				}
			}
			mom.DestroyMeshEditor(editorMethods);
			return outMesh;
		}
		public static Mesh BakeMeshesInPlace(MB3_MeshCombinerSingle mom, List<GameObject> objsToMesh, string saveFolder, ProgressUpdateDelegate updateProgressBar){
			if (MB3_MeshCombiner.EVAL_VERSION) return null; 
	
			if (!Directory.Exists(Application.dataPath + saveFolder.Substring(6))){
				Debug.Log((Application.dataPath + saveFolder.Substring(6)));
				Debug.Log(Path.GetFullPath(Application.dataPath + saveFolder.Substring(6)));
				Debug.LogError("The selected Folder For Meshes does not exist or is not inside the projects Assets folder. Please 'Choose Folder For Bake In Place Meshes' that is inside the project's assets folder.");	
				return null;
			}

			MB3_EditorMethods editorMethods = new MB3_EditorMethods();
			mom.DestroyMeshEditor(editorMethods);
			
			MB_RenderType originalRenderType = mom.renderType;
			Mesh outMesh = null;
			for (int i = 0; i < objsToMesh.Count; i++){
				if (objsToMesh[i] == null){
					Debug.LogError("The " + i + "th object on the list of objects to combine is 'None'. Use Command-Delete on Mac OS X; Delete or Shift-Delete on Windows to remove this one element.");
					return null;					
				}
				string[] objNames = GenerateNames(objsToMesh);
				outMesh = BakeOneMesh(mom, saveFolder + "/" + objNames[i], objsToMesh[i]);
				if (updateProgressBar != null) updateProgressBar("Created mesh saving mesh on " + objsToMesh[i].name + " to asset " + objNames[i],.6f);				
			}
			mom.renderType = originalRenderType;
			return outMesh;
		}
		public bool isDirty = false; //needs apply
		
		public CombinedMesh(int maxNumVertsInMesh, GameObject resultSceneObject, MB2_LogLevel ll){
		 	combinedMesh = new MB3_MeshCombinerSingle();
			combinedMesh.resultSceneObject = resultSceneObject;
			combinedMesh.LOG_LEVEL = ll;
		 	extraSpace = maxNumVertsInMesh;
			numVertsInListToDelete = 0;
			numVertsInListToAdd = 0;
		 	gosToAdd = new List<GameObject>();
		 	gosToDelete = new List<int>();
			gosToUpdate = new List<GameObject>();
		}
Exemple #8
0
    // The serialized object reference is necessary to work around a nasty unity bug.
    public static void RebuildPrefab(MB3_MeshBakerCommon mom, ref SerializedObject so)
    {
        if (MB3_MeshCombiner.EVAL_VERSION)
        {
            return;
        }

        if (mom.meshCombiner.LOG_LEVEL >= MB2_LogLevel.debug)
        {
            Debug.Log("Rebuilding Prefab: " + mom.resultPrefab);
        }
        GameObject prefabRoot = mom.resultPrefab;
        GameObject rootGO     = (GameObject)PrefabUtility.InstantiatePrefab(prefabRoot);

        //remove all renderer childeren of rootGO
        MBVersionEditor.UnpackPrefabInstance(rootGO, ref so);
        Renderer[] rs = rootGO.GetComponentsInChildren <Renderer>();
        for (int i = 0; i < rs.Length; i++)
        {
            if (rs[i] != null && rs[i].transform.parent == rootGO.transform)
            {
                MB_Utility.Destroy(rs[i].gameObject);
            }
        }

        if (mom is MB3_MeshBaker)
        {
            MB3_MeshBaker          mb  = (MB3_MeshBaker)mom;
            MB3_MeshCombinerSingle mbs = (MB3_MeshCombinerSingle)mb.meshCombiner;
            MB3_MeshCombinerSingle.BuildPrefabHierarchy(mbs, rootGO, mbs.GetMesh());
        }
        else if (mom is MB3_MultiMeshBaker)
        {
            MB3_MultiMeshBaker    mmb = (MB3_MultiMeshBaker)mom;
            MB3_MultiMeshCombiner mbs = (MB3_MultiMeshCombiner)mmb.meshCombiner;
            for (int i = 0; i < mbs.meshCombiners.Count; i++)
            {
                MB3_MeshCombinerSingle.BuildPrefabHierarchy(mbs.meshCombiners[i].combinedMesh, rootGO, mbs.meshCombiners[i].combinedMesh.GetMesh(), true);
            }
        }
        else
        {
            Debug.LogError("Argument was not a MB3_MeshBaker or an MB3_MultiMeshBaker.");
        }

        string prefabPth = AssetDatabase.GetAssetPath(prefabRoot);

        MBVersionEditor.ReplacePrefab(rootGO, prefabPth, MB_ReplacePrefabOption.connectToPrefab);
        if (mom.meshCombiner.renderType != MB_RenderType.skinnedMeshRenderer)
        {
            // For Skinned meshes, leave the prefab instance in the scene so source game objects can moved into the prefab.
            Editor.DestroyImmediate(rootGO);
        }
    }
    public static void RebuildPrefab(MB3_MeshBakerCommon mom)
    {
        if (MB3_MeshCombiner.EVAL_VERSION)
        {
            return;
        }
        GameObject prefabRoot = mom.resultPrefab;
        GameObject rootGO     = (GameObject)PrefabUtility.InstantiatePrefab(prefabRoot);

        //remove all renderer childeren of rootGO
        Renderer[] rs = rootGO.GetComponentsInChildren <Renderer>();
        for (int i = 0; i < rs.Length; i++)
        {
            if (rs[i] != null && rs[i].transform.parent == rootGO.transform)
            {
                MB_Utility.Destroy(rs[i].gameObject);
            }
        }
        if (mom is MB3_MeshBaker)
        {
            MB3_MeshBaker          mb  = (MB3_MeshBaker)mom;
            MB3_MeshCombinerSingle mbs = (MB3_MeshCombinerSingle)mb.meshCombiner;
            MB3_MeshCombinerSingle.BuildPrefabHierarchy(mbs, rootGO, mbs.GetMesh());
        }
        else if (mom is MB3_MultiMeshBaker)
        {
            MB3_MultiMeshBaker    mmb = (MB3_MultiMeshBaker)mom;
            MB3_MultiMeshCombiner mbs = (MB3_MultiMeshCombiner)mmb.meshCombiner;
            for (int i = 0; i < mbs.meshCombiners.Count; i++)
            {
                MB3_MeshCombinerSingle.BuildPrefabHierarchy(mbs.meshCombiners[i].combinedMesh, rootGO, mbs.meshCombiners[i].combinedMesh.GetMesh(), true);
            }
        }
        else
        {
            Debug.LogError("Argument was not a MB3_MeshBaker or an MB3_MultiMeshBaker.");
        }
        string prefabPth = AssetDatabase.GetAssetPath(prefabRoot);

        PrefabUtility.ReplacePrefab(rootGO, AssetDatabase.LoadAssetAtPath(prefabPth, typeof(GameObject)), ReplacePrefabOptions.ConnectToPrefab);
        if (mom.meshCombiner.renderType != MB_RenderType.skinnedMeshRenderer)
        {
            Editor.DestroyImmediate(rootGO);
        }
    }
    private static bool ProcessMesh(Renderer r, Mesh m, List <UnityTransform> unityTransforms, MB3_MeshBaker mb)
    {
        unityTransforms.Clear();
        // position rotation and scale are baked into combined mesh.
        // Remember all the transforms settings then
        // record transform values to root of hierarchy
        Transform t = r.transform;

        if (t != t.root)
        {
            do
            {
                unityTransforms.Add(new UnityTransform(t));
                t = t.parent;
            } while (t != null && t != t.root);
        }

        //add the root
        unityTransforms.Add(new UnityTransform(t.root));

        //position at identity
        for (int k = 0; k < unityTransforms.Count; k++)
        {
            unityTransforms[k].t.localPosition = Vector3.zero;
            unityTransforms[k].t.localRotation = Quaternion.identity;
            unityTransforms[k].t.localScale    = Vector3.one;
        }

        //bake the mesh
        MB3_MeshCombinerSingle mc = (MB3_MeshCombinerSingle)mb.meshCombiner;

        if (!MB3_BakeInPlace.BakeOneMesh(mc, m, r.gameObject))
        {
            return(false);
        }

        //replace the mesh
        if (r is MeshRenderer)
        {
            MeshFilter mf = r.gameObject.GetComponent <MeshFilter>();
            mf.sharedMesh = m;
        }
        else
        { //skinned mesh
            SkinnedMeshRenderer smr = r.gameObject.GetComponent <SkinnedMeshRenderer>();
            smr.sharedMesh = m;
            smr.bones      = ((SkinnedMeshRenderer)mc.targetRenderer).bones;
        }

        if (mc.targetRenderer != null)
        {
            SetMaterials(mc.targetRenderer.sharedMaterials, r);
        }

        //restore the transforms
        for (int k = 0; k < unityTransforms.Count; k++)
        {
            unityTransforms[k].t.localPosition = unityTransforms[k].p;
            unityTransforms[k].t.localRotation = unityTransforms[k].q;
            unityTransforms[k].t.localScale    = unityTransforms[k].s;
        }

        mc.SetMesh(null);
        return(true);
    }
	void _setMBValues(MB3_MeshCombinerSingle targ){
		targ.validationLevel = _validationLevel;
		targ.renderType = renderType;
		targ.outputOption = MB2_OutputOptions.bakeIntoSceneObject;
		targ.lightmapOption = lightmapOption;
		targ.textureBakeResults = textureBakeResults;
		targ.doNorm = doNorm;
		targ.doTan = doTan;
		targ.doCol = doCol;	
		targ.doUV = doUV;
		targ.doUV3 = doUV3;
        targ.doUV4 = doUV4;		
	}
 private void _setMBValues(MB3_MeshCombinerSingle targ)
 {
     targ.validationLevel = this._validationLevel;
     targ.renderType = this.renderType;
     targ.outputOption = MB2_OutputOptions.bakeIntoSceneObject;
     targ.lightmapOption = this.lightmapOption;
     targ.textureBakeResults = this.textureBakeResults;
     targ.doNorm = this.doNorm;
     targ.doTan = this.doTan;
     targ.doCol = this.doCol;
     targ.doUV = this.doUV;
     targ.doUV3 = this.doUV3;
     targ.doUV4 = this.doUV4;
     targ.uv2UnwrappingParamsHardAngle = this.uv2UnwrappingParamsHardAngle;
     targ.uv2UnwrappingParamsPackMargin = this.uv2UnwrappingParamsPackMargin;
 }