I'm just wondering if anyone uses (or has used) the Bake Material To Texture script - http://www.unifycommunity.com/wiki/index.php?title=Bake_Material_to_Texture I've installed it but am having trouble getting it to work correctly. Seems to run fine for the most part except for me it's not showing a preview in the preview window and, more importantly, it's not completing the bake. It seems to only do a third of the texture. Every time I run it it's doing a different amount and section of the texture but never the completed thing. Seems very useful for mobile development if I can get it to work.
Here is a js version that works with Unity 3 or 4: And could somebody so kind to upload it into the wiki because I have no user rights! Please Code (csharp): class BakeMaterialSettings { private static var kEditorPrefsName = "BakeMaterialSettings"; static var kBakingLayerShouldBeUnusedInScene = 30; static var kStandardTexNames = new Array ("_MainTex", "_BumpMap", "_Detail", "_ParallaxMap", "_Parallax"); var bakeAlpha = false; var bakeMainTexAsWhite = false; var minTextureResolution = 8; var maxTextureResolution = 2048; var emptyScene = false; var useCustomLights = false; var ambient = Color.black; static var kLights = 3; var enableLight = new boolean[kLights]; var colorLight = new Color[kLights]; var dirLight = new Vector2[kLights]; function BakeMaterialSettings () { Load (); } function Load () { bakeAlpha = EditorPrefs.GetBool(kEditorPrefsName + ".bakeAlpha"); bakeMainTexAsWhite = EditorPrefs.GetBool(kEditorPrefsName + ".bakeMainTexAsWhite"); minTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".minTextureResolution", 8); maxTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".maxTextureResolution", 2048); emptyScene = EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene"); useCustomLights = EditorPrefs.GetBool(kEditorPrefsName + ".useCustomLights"); ambient.r = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.r"); ambient.g = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.g"); ambient.b = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.b"); ambient.a = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.a", 1.0f); for (var q = 0; q < kLights; ++q) { enableLight[q] = EditorPrefs.GetBool(kEditorPrefsName + ".enableLight" + q); colorLight[q].r = EditorPrefs.GetFloat(kEditorPrefsName + ".color.r" + q, 0.5f); colorLight[q].g = EditorPrefs.GetFloat(kEditorPrefsName + ".color.g" + q, 0.5f); colorLight[q].b = EditorPrefs.GetFloat(kEditorPrefsName + ".color.b" + q, 0.5f); colorLight[q].a = EditorPrefs.GetFloat(kEditorPrefsName + ".color.a" + q, 1.0f); dirLight[q].x = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.x" + q); dirLight[q].y = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.y" + q); } } function Save () { EditorPrefs.SetBool(kEditorPrefsName + ".bakeAlpha", bakeAlpha); EditorPrefs.SetBool(kEditorPrefsName + ".bakeMainTexAsWhite", bakeMainTexAsWhite); EditorPrefs.SetInt(kEditorPrefsName + ".minTextureResolution", minTextureResolution); EditorPrefs.SetInt(kEditorPrefsName + ".maxTextureResolution", maxTextureResolution); EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene", emptyScene); EditorPrefs.SetBool(kEditorPrefsName + ".useCustomLights", useCustomLights); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.r", ambient.r); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.g", ambient.g); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.b", ambient.b); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.a", ambient.a); for (var q = 0; q < kLights; ++q) { EditorPrefs.SetBool(kEditorPrefsName + ".enableLight" + q, enableLight[q]); EditorPrefs.SetFloat(kEditorPrefsName + ".color.r" + q, colorLight[q].r); EditorPrefs.SetFloat(kEditorPrefsName + ".color.g" + q, colorLight[q].g); EditorPrefs.SetFloat(kEditorPrefsName + ".color.b" + q, colorLight[q].b); EditorPrefs.SetFloat(kEditorPrefsName + ".color.a" + q, colorLight[q].a); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.x" + q, dirLight[q].x); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.y" + q, dirLight[q].y); } } } class BakeMaterial extends EditorWindow { private static var kMateriBakeNodeName = "__MateriaBakeSetup"; private static var kWindowMinSize = Vector2 (300, 386); private static var settings : BakeMaterialSettings; private static var visible : boolean = false; private var camera : GameObject; private var plane : GameObject; private var previewTexture : Texture; private var lights : GameObject[] = new GameObject[BakeMaterialSettings.kLights]; private var stateChanged = false; private var texViewScrollPosition = Vector2.zero; private var lastMaterial : Material; private var originalScene = ""; private var scheduleBakeOnNextUpdate = false; private function SetupScene () { DestroyScene (); var oldGo = GameObject.Find(kMateriBakeNodeName); if (oldGo) DestroyImmediate (oldGo); camera = new GameObject (kMateriBakeNodeName, Camera); plane = GameObject.CreatePrimitive (PrimitiveType.Plane); var cam = camera; cam.camera.backgroundColor = Color.black; cam.camera.clearFlags = CameraClearFlags.SolidColor; cam.camera.orthographic = true; cam.camera.orthographicSize = 5.0; cam.camera.cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene; plane.transform.parent = cam.transform; plane.transform.position = Vector3.forward * 10.0; plane.transform.rotation = Quaternion.Euler (0, 0, 180) * Quaternion.Euler (-90, 0, 0); plane.layer = settings.kBakingLayerShouldBeUnusedInScene; for (var l in lights) { l = new GameObject ("Light", Light); l.light.type = LightType.Directional; l.light.cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene; l.transform.parent = cam.transform; l.active = false; } } private function UpdateScene (m : Material) { for (q = 0; q < settings.kLights; ++q) { lights[q].active = settings.useCustomLights settings.enableLight[q]; lights[q].light.color = settings.colorLight[q]; lights[q].transform.rotation = Quaternion.AngleAxis(settings.dirLight[q].x, Vector3.up) * Quaternion.AngleAxis(settings.dirLight[q].y, Vector3.right); } if (settings.useCustomLights) RenderSettings.ambientLight = settings.ambient; else if (settings.emptyScene) RenderSettings.ambientLight = Color.white; plane.renderer.material = m; } private function DestroyScene () { GameObject.DestroyImmediate (camera); GameObject.DestroyImmediate (plane); GameObject.DestroyImmediate (previewTexture); } function UpdateMaterialPreview (m : Material) : RenderTexture { if (!m) return; var saveAmbientLight = RenderSettings.ambientLight; var saveMainTexture = m.mainTexture; if (settings.bakeMainTexAsWhite) m.mainTexture = null; // setup if (!camera) SetupScene (); camera.SetActiveRecursively(true); UpdateScene (m); var res = FindLargestTextureResolution (plane.renderer.sharedMaterial, settings.minTextureResolution, settings.maxTextureResolution); var rt = RenderCameraToRenderTexture (camera.camera, res.x, res.y); // restore camera.SetActiveRecursively(false); RenderSettings.ambientLight = saveAmbientLight; m.mainTexture = saveMainTexture; previewTexture = rt; return rt; } function CaptureMaterial(m : Material) { var matAssetPath = AssetDatabase.GetAssetPath (m); var assetPath = System.IO.Path.Combine (System.IO.Path.GetDirectoryName (matAssetPath), System.IO.Path.GetFileNameWithoutExtension (matAssetPath)); var rt = UpdateMaterialPreview (m); RenderTextureToPNG (rt, settings.bakeAlpha, assetPath + ".png"); } function OnEnable () { if (!settings) settings = new BakeMaterialSettings (); SetupScene (); visible = true; } function OnDisable () { DestroyScene (); settings.Save (); visible = false; } static function GetTargetMaterial () : Material { return EditorUtility.InstanceIDToObject (Selection.activeInstanceID) as Material; } function OnSelectionChange () { Repaint (); } function Update () { var rebuildScene = false; if (scheduleBakeOnNextUpdate) { Bake (); scheduleBakeOnNextUpdate = false; rebuildScene = true; } if (originalScene == "" EditorApplication.currentScene == "") settings.emptyScene = true; if (settings.emptyScene originalScene == "" EditorApplication.currentScene != "") { DestroyScene (); if (EditorApplication.SaveCurrentSceneIfUserWantsTo ()) { originalScene = EditorApplication.currentScene; EditorApplication.NewScene (); } else settings.emptyScene = false; rebuildScene = true; } else if (!settings.emptyScene originalScene != "") { EditorApplication.OpenScene (originalScene); rebuildScene = true; originalScene = ""; } if (rebuildScene) { SetupScene (); } if (rebuildScene || stateChanged || !settings.emptyScene) { UpdateMaterialPreview (lastMaterial); Repaint (); stateChanged = false; } } function OnGUI () { var material = GetTargetMaterial (); if (lastMaterial != material) UpdateMaterialPreview (material); if (material) lastMaterial = material; EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200)); if (!(originalScene == "" EditorApplication.currentScene == "")) { settings.emptyScene = !EditorGUILayout.BeginToggleGroup("Scene ligthing", !settings.emptyScene); EditorGUILayout.EndToggleGroup(); } settings.useCustomLights = EditorGUILayout.BeginToggleGroup("Custom lighting", settings.useCustomLights); if (settings.useCustomLights) { EditorGUI.indentLevel = 1; settings.ambient = EditorGUILayout.ColorField("Ambient", settings.ambient); for (var q = 0; q < settings.kLights; ++q) { settings.enableLight[q] = EditorGUILayout.BeginToggleGroup("Light", settings.enableLight[q]); EditorGUI.indentLevel = 2; settings.colorLight[q] = EditorGUILayout.ColorField("Color", settings.colorLight[q]); settings.dirLight[q] = EditorGUILayout.Vector2Field("Direction", settings.dirLight[q]); EditorGUILayout.EndToggleGroup(); } } EditorGUI.indentLevel = 0; EditorGUILayout.EndToggleGroup(); settings.bakeAlpha = EditorGUILayout.Toggle("Bake Alpha", settings.bakeAlpha); settings.bakeMainTexAsWhite = !EditorGUILayout.Toggle("MainTex", !settings.bakeMainTexAsWhite); settings.minTextureResolution = EditorGUILayout.IntField("Min Resolution", settings.minTextureResolution); settings.maxTextureResolution = EditorGUILayout.IntField("Max Resolution", settings.maxTextureResolution); settings.minTextureResolution = Mathf.Max(2, settings.minTextureResolution); settings.maxTextureResolution = Mathf.Max(settings.minTextureResolution, settings.maxTextureResolution); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Bake")) { CaptureMaterial (lastMaterial); } if (GUILayout.Button("Bake Selected")) { scheduleBakeOnNextUpdate = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); texViewScrollPosition = EditorGUILayout.BeginScrollView (texViewScrollPosition); var r = GUILayoutUtility.GetAspectRect(1.0f); if (previewTexture) EditorGUI.DrawPreviewTexture(r, previewTexture); EditorGUILayout.EndScrollView(); EditorGUILayout.EndHorizontal(); if (GUI.changed) { stateChanged = true; } } @MenuItem("Custom/Bake Material ...", false, 5) static function CreateBakeEditor() { var window : BakeMaterial = EditorWindow.GetWindow(BakeMaterial); window.title = "Bake Material"; window.minSize = kWindowMinSize; window.Show(); } @MenuItem("Custom/Bake Selected Materials", false, 4) static function Bake() { var instanceIDs = Selection.instanceIDs; var currentScene = EditorApplication.currentScene; var wasAlreadyVisible = BakeMaterial.visible; var window : BakeMaterial = EditorWindow.GetWindow(BakeMaterial); if (window.settings.emptyScene) { if (!EditorApplication.SaveCurrentSceneIfUserWantsTo ()) return; EditorApplication.NewScene (); } window.SetupScene (); for (var i in instanceIDs) { var m : Material = EditorUtility.InstanceIDToObject (i) as Material; if (m) window.CaptureMaterial (m); } window.DestroyScene (); if (window.settings.emptyScene currentScene) { EditorApplication.OpenScene (currentScene); } if (!wasAlreadyVisible) window.Close (); } static function FindLargestTextureResolution (m : Material, minTexRes : int, maxTexRes : int) : Vector2 { var res = Vector2 (minTexRes, minTexRes); for (var n in BakeMaterialSettings.kStandardTexNames) { if (!m.HasProperty (n)) continue; var t : Texture = m.GetTexture (n); if (!t) continue; res.x = Mathf.Max (res.x, t.width); res.y = Mathf.Max (res.y, t.height); } res.x = Mathf.Min (res.x, maxTexRes); res.y = Mathf.Min (res.y, maxTexRes); return res; } static function RenderCameraToRenderTexture (cam : Camera, width : int, height : int) : RenderTexture { var rt = cam.camera.targetTexture; if (rt rt.width != width rt.height != height) DestroyImmediate(rt); if (!rt) rt = new RenderTexture (width, height, 24); cam.camera.targetTexture = rt; cam.camera.Render (); return rt; } static function RenderTextureToPNG (rt : RenderTexture, bakeAlpha : boolean, assetPath : String) { RenderTexture.active = rt; var screenShot = new Texture2D (rt.width, rt.height, bakeAlpha? TextureFormat.ARGB32 : TextureFormat.RGB24, false); screenShot.ReadPixels (Rect (0, 0, rt.width, rt.height), 0, 0); RenderTexture.active = null; var bytes = screenShot.EncodeToPNG (); System.IO.File.WriteAllBytes (assetPath, bytes); AssetDatabase.ImportAsset (assetPath, ImportAssetOptions.ForceUpdate); } }
Has someone used this Script with version 4.2 of Unity ?? For me neither this version or the one on the Wiki works. I select a unity's standard bumped specular material and when I press "Bake Material..." Unity crashes and closes the project. :neutral: I've tried using different shaders but nothing worked... Any insights on why it might not be working??
he existe somethinks similair i got ArgumentException: Invalid path System.IO.Path.GetDirectoryName (System.String path) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.IO/Path.cs:215) BakeMaterial.CaptureMaterial (UnityEngine.Material m) (at Assets/baketexture.js:193) BakeMaterial.OnGUI () (at Assets/baketexture.js:311) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
4.5.2f1 compatible version. Many thanks to Rej. Save it in a js script. Use it from Menu-> Custom->Bake. Code (csharp): classBakeMaterialSettings { privatestaticvarkEditorPrefsName = "BakeMaterialSettings"; staticvarkBakingLayerShouldBeUnusedInScene = 30; staticvarkStandardTexNames = newArray ("_MainTex", "_BumpMap", "_Detail", "_ParallaxMap", "_Parallax"); varbakeAlpha = false; varbakeMainTexAsWhite = false; varminTextureResolution = 8; varmaxTextureResolution = 2048; varemptyScene = false; varuseCustomLights = false; varambient = Color.black; staticvarkLights = 3; varenableLight = newboolean[kLights]; varcolorLight = newColor[kLights]; vardirLight = newVector2[kLights]; functionBakeMaterialSettings () { Load (); } functionLoad () { bakeAlpha = EditorPrefs.GetBool(kEditorPrefsName + ".bakeAlpha"); bakeMainTexAsWhite = EditorPrefs.GetBool(kEditorPrefsName + ".bakeMainTexAsWhite"); minTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".minTextureResolution", 8); maxTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".maxTextureResolution", 2048); emptyScene = EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene"); useCustomLights = EditorPrefs.GetBool(kEditorPrefsName + ".useCustomLights"); ambient.r = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.r"); ambient.g = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.g"); ambient.b = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.b"); ambient.a = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.a", 1.0f); for (varq = 0; q < kLights; ++q) { enableLight[q] = EditorPrefs.GetBool(kEditorPrefsName + ".enableLight" + q); colorLight[q].r = EditorPrefs.GetFloat(kEditorPrefsName + ".color.r" + q, 0.5f); colorLight[q].g = EditorPrefs.GetFloat(kEditorPrefsName + ".color.g" + q, 0.5f); colorLight[q].b = EditorPrefs.GetFloat(kEditorPrefsName + ".color.b" + q, 0.5f); colorLight[q].a = EditorPrefs.GetFloat(kEditorPrefsName + ".color.a" + q, 1.0f); dirLight[q].x = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.x" + q); dirLight[q].y = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.y" + q); } } functionSave () { EditorPrefs.SetBool(kEditorPrefsName + ".bakeAlpha", bakeAlpha); EditorPrefs.SetBool(kEditorPrefsName + ".bakeMainTexAsWhite", bakeMainTexAsWhite); EditorPrefs.SetInt(kEditorPrefsName + ".minTextureResolution", minTextureResolution); EditorPrefs.SetInt(kEditorPrefsName + ".maxTextureResolution", maxTextureResolution); EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene", emptyScene); EditorPrefs.SetBool(kEditorPrefsName + ".useCustomLights", useCustomLights); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.r", ambient.r); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.g", ambient.g); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.b", ambient.b); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.a", ambient.a); for (varq = 0; q < kLights; ++q) { EditorPrefs.SetBool(kEditorPrefsName + ".enableLight" + q, enableLight[q]); EditorPrefs.SetFloat(kEditorPrefsName + ".color.r" + q, colorLight[q].r); EditorPrefs.SetFloat(kEditorPrefsName + ".color.g" + q, colorLight[q].g); EditorPrefs.SetFloat(kEditorPrefsName + ".color.b" + q, colorLight[q].b); EditorPrefs.SetFloat(kEditorPrefsName + ".color.a" + q, colorLight[q].a); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.x" + q, dirLight[q].x); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.y" + q, dirLight[q].y); } } } classBakeMaterialextendsEditorWindow { privatestaticvarkMateriBakeNodeName = "__MateriaBakeSetup"; privatestaticvarkWindowMinSize = Vector2 (300, 386); privatestaticvarsettings : BakeMaterialSettings; privatestaticvarvisible : boolean = false; privatevarcamera : GameObject; privatevarplane : GameObject; privatevarpreviewTexture : Texture; privatevarlights : GameObject[] = newGameObject[BakeMaterialSettings.kLights]; privatevarstateChanged = false; privatevartexViewScrollPosition = Vector2.zero; privatevarlastMaterial : Material; privatevaroriginalScene = ""; privatevarscheduleBakeOnNextUpdate = false; privatefunctionSetupScene () { DestroyScene (); varoldGo = GameObject.Find(kMateriBakeNodeName); if (oldGo) DestroyImmediate (oldGo); camera = newGameObject (kMateriBakeNodeName, Camera); plane = GameObject.CreatePrimitive (PrimitiveType.Plane); varcam = camera; cam.camera.backgroundColor = Color.black; cam.camera.clearFlags = CameraClearFlags.SolidColor; cam.camera.orthographic = true; cam.camera.orthographicSize = 5.0; cam.camera.cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene; plane.transform.parent = cam.transform; plane.transform.position = Vector3.forward * 10.0; plane.transform.rotation = Quaternion.Euler (0, 0, 180) * Quaternion.Euler (-90, 0, 0); plane.layer = settings.kBakingLayerShouldBeUnusedInScene; for (varlinlights) { l = newGameObject ("Light", Light); l.light.type = LightType.Directional; l.light.cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene; l.transform.parent = cam.transform; l.SetActive(false); } } privatefunctionUpdateScene (m : Material) { for (q = 0; q < settings.kLights; ++q) { lights[q].SetActive(settings.useCustomLights && settings.enableLight[q]); lights[q].light.color = settings.colorLight[q]; lights[q].transform.rotation = Quaternion.AngleAxis(settings.dirLight[q].x, Vector3.up) * Quaternion.AngleAxis(settings.dirLight[q].y, Vector3.right); } if (settings.useCustomLights) RenderSettings.ambientLight = settings.ambient; elseif (settings.emptyScene) RenderSettings.ambientLight = Color.white; plane.renderer.material = m; } privatefunctionDestroyScene () { GameObject.DestroyImmediate (camera); GameObject.DestroyImmediate (plane); GameObject.DestroyImmediate (previewTexture); } functionUpdateMaterialPreview (m : Material) : RenderTexture { if (!m) return; varsaveAmbientLight = RenderSettings.ambientLight; varsaveMainTexture = m.mainTexture; if (settings.bakeMainTexAsWhite) m.mainTexture = null; //setup if (!camera) SetupScene (); camera.SetActive(true); UpdateScene (m); varres = FindLargestTextureResolution (plane.renderer.sharedMaterial, settings.minTextureResolution, settings.maxTextureResolution); varrt = RenderCameraToRenderTexture (camera.camera, res.x, res.y); //restore camera.SetActive(false); RenderSettings.ambientLight = saveAmbientLight; m.mainTexture = saveMainTexture; previewTexture = rt; returnrt; } functionCaptureMaterial(m : Material) { varmatAssetPath = AssetDatabase.GetAssetPath (m); varassetPath = System.IO.Path.Combine (System.IO.Path.GetDirectoryName (matAssetPath), System.IO.Path.GetFileNameWithoutExtension (matAssetPath)); varrt = UpdateMaterialPreview (m); RenderTextureToPNG (rt, settings.bakeAlpha, assetPath + ".png"); } functionOnEnable () { if (!settings) settings = newBakeMaterialSettings (); SetupScene (); visible = true; } functionOnDisable () { DestroyScene (); settings.Save (); visible = false; } staticfunctionGetTargetMaterial () : Material { returnEditorUtility.InstanceIDToObject (Selection.activeInstanceID) asMaterial; } functionOnSelectionChange () { Repaint (); } functionUpdate () { varrebuildScene = false; if (scheduleBakeOnNextUpdate) { Bake (); scheduleBakeOnNextUpdate = false; rebuildScene = true; } if (originalScene == "" && EditorApplication.currentScene == "") settings.emptyScene = true; if (settings.emptyScene && originalScene == "" && EditorApplication.currentScene != "") { DestroyScene (); if (EditorApplication.SaveCurrentSceneIfUserWantsTo ()) { originalScene = EditorApplication.currentScene; EditorApplication.NewScene (); } else settings.emptyScene = false; rebuildScene = true; } elseif (!settings.emptyScene && originalScene != "") { EditorApplication.OpenScene (originalScene); rebuildScene = true; originalScene = ""; } if (rebuildScene) { SetupScene (); } if (rebuildScene || stateChanged || !settings.emptyScene) { UpdateMaterialPreview (lastMaterial); Repaint (); stateChanged = false; } } functionOnGUI () { varmaterial = GetTargetMaterial (); if (lastMaterial != material) UpdateMaterialPreview (material); if (material) lastMaterial = material; EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200)); if (!(originalScene == "" && EditorApplication.currentScene == "")) { settings.emptyScene = !EditorGUILayout.BeginToggleGroup("Sceneligthing", !settings.emptyScene); EditorGUILayout.EndToggleGroup(); } settings.useCustomLights = EditorGUILayout.BeginToggleGroup("Customlighting", settings.useCustomLights); if (settings.useCustomLights) { EditorGUI.indentLevel = 1; settings.ambient = EditorGUILayout.ColorField("Ambient", settings.ambient); for (varq = 0; q < settings.kLights; ++q) { settings.enableLight[q] = EditorGUILayout.BeginToggleGroup("Light", settings.enableLight[q]); EditorGUI.indentLevel = 2; settings.colorLight[q] = EditorGUILayout.ColorField("Color", settings.colorLight[q]); settings.dirLight[q] = EditorGUILayout.Vector2Field("Direction", settings.dirLight[q]); EditorGUILayout.EndToggleGroup(); } } EditorGUI.indentLevel = 0; EditorGUILayout.EndToggleGroup(); settings.bakeAlpha = EditorGUILayout.Toggle("BakeAlpha", settings.bakeAlpha); settings.bakeMainTexAsWhite = !EditorGUILayout.Toggle("MainTex", !settings.bakeMainTexAsWhite); settings.minTextureResolution = EditorGUILayout.IntField("MinResolution", settings.minTextureResolution); settings.maxTextureResolution = EditorGUILayout.IntField("MaxResolution", settings.maxTextureResolution); settings.minTextureResolution = Mathf.Max(2, settings.minTextureResolution); settings.maxTextureResolution = Mathf.Max(settings.minTextureResolution, settings.maxTextureResolution); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Bake")) { CaptureMaterial (lastMaterial); } if (GUILayout.Button("BakeSelected")) { scheduleBakeOnNextUpdate = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); texViewScrollPosition = EditorGUILayout.BeginScrollView (texViewScrollPosition); varr = GUILayoutUtility.GetAspectRect(1.0f); if (previewTexture) EditorGUI.DrawPreviewTexture(r, previewTexture); EditorGUILayout.EndScrollView(); EditorGUILayout.EndHorizontal(); if (GUI.changed) { stateChanged = true; } } @MenuItem("Custom/BakeMaterial ...", false, 5) staticfunctionCreateBakeEditor() { varwindow = EditorWindow.GetWindow(BakeMaterial); window.title = "BakeMaterial"; window.minSize = kWindowMinSize; window.Show(); } @MenuItem("Custom/BakeSelectedMaterials", false, 4) staticfunctionBake() { varinstanceIDs = Selection.instanceIDs; varcurrentScene = EditorApplication.currentScene; varwasAlreadyVisible = BakeMaterial.visible; varwindow = EditorWindow.GetWindow(BakeMaterial); if (window.settings.emptyScene) { if (!EditorApplication.SaveCurrentSceneIfUserWantsTo ()) return; EditorApplication.NewScene (); } window.SetupScene (); for (variininstanceIDs) { varm : Material = EditorUtility.InstanceIDToObject (i) asMaterial; if (m) window.CaptureMaterial (m); } window.DestroyScene (); if (window.settings.emptyScene && currentScene) { EditorApplication.OpenScene (currentScene); } if (!wasAlreadyVisible) window.Close (); } staticfunctionFindLargestTextureResolution (m : Material, minTexRes : int, maxTexRes : int) : Vector2 { varres = Vector2 (minTexRes, minTexRes); for (varninBakeMaterialSettings.kStandardTexNames) { if (!m.HasProperty (n)) continue; vart : Texture = m.GetTexture (n); if (!t) continue; res.x = Mathf.Max (res.x, t.width); res.y = Mathf.Max (res.y, t.height); } res.x = Mathf.Min (res.x, maxTexRes); res.y = Mathf.Min (res.y, maxTexRes); returnres; } staticfunctionRenderCameraToRenderTexture (cam : Camera, width : int, height : int) : RenderTexture { varrt = cam.camera.targetTexture; if (rt && rt.width != width && rt.height != height) DestroyImmediate(rt); if (!rt) rt = newRenderTexture (width, height, 24); cam.camera.targetTexture = rt; cam.camera.Render (); returnrt; } staticfunctionRenderTextureToPNG (rt : RenderTexture, bakeAlpha : boolean, assetPath : String) { RenderTexture.active = rt; varscreenShot = newTexture2D (rt.width, rt.height, bakeAlpha? TextureFormat.ARGB32 : TextureFormat.RGB24, false); screenShot.ReadPixels (Rect (0, 0, rt.width, rt.height), 0, 0); RenderTexture.active = null; varbytes = screenShot.EncodeToPNG (); System.IO.File.WriteAllBytes (assetPath, bytes); AssetDatabase.ImportAsset (assetPath, ImportAssetOptions.ForceUpdate); } }
i have copy the script and i got i don't reconize 'DestroyImmediate' it's maybie js ? Unknown identifier: 'BakeMaterial'. he want string for reference Ambiguous reference 'HasProperty': UnityEngine.Material.HasProperty(int), UnityEngine.Material.HasProperty(String) like copy past here work not realy well i have maybie a misstake somewhere
ok i have fix one next time don't attack brain like this at Monday morning lol i have settings' is not a member of 'UnityEditor.EditorWindow'. maybie because is one var window = EditorWindow.GetWindow(BakeMaterial); so i replace window by BakeMaterial is look work i will try
This works with Version 4.5.5f1 Save it in 'Editor' folder. Then select Material and Menu>Bake Selected Material Code (JavaScript): #pragma strict class BakeMaterialSettings { private static var kEditorPrefsName = "BakeMaterialSettings"; static var kBakingLayerShouldBeUnusedInScene = 30; static var kStandardTexNames = new Array ("_MainTex", "_BumpMap", "_Detail", "_ParallaxMap","_Parallax"); var bakeAlpha = false; var bakeMainTexAsWhite = false; var minTextureResolution = 8; var maxTextureResolution = 2048; var emptyScene = false; var useCustomLights = false; var ambient = Color.black; static var kLights = 3; var enableLight = new boolean[kLights]; var colorLight = new Color[kLights]; var dirLight = new Vector2[kLights]; function BakeMaterialSettings () { Load (); } function Load () { bakeAlpha = EditorPrefs.GetBool(kEditorPrefsName + ".bakeAlpha"); bakeMainTexAsWhite = EditorPrefs.GetBool(kEditorPrefsName + ".bakeMainTexAsWhite"); minTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".minTextureResolution", 8); maxTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".maxTextureResolution", 2048); emptyScene = EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene"); useCustomLights = EditorPrefs.GetBool(kEditorPrefsName + ".useCustomLights"); ambient.r = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.r"); ambient.g = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.g"); ambient.b = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.b"); ambient.a = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.a", 1.0f); for (var q = 0; q < kLights; ++q) { enableLight[q] = EditorPrefs.GetBool(kEditorPrefsName + ".enableLight" + q); colorLight[q].r = EditorPrefs.GetFloat(kEditorPrefsName + ".color.r" + q, 0.5f); colorLight[q].g = EditorPrefs.GetFloat(kEditorPrefsName + ".color.g" + q, 0.5f); colorLight[q].b = EditorPrefs.GetFloat(kEditorPrefsName + ".color.b" + q, 0.5f); colorLight[q].a = EditorPrefs.GetFloat(kEditorPrefsName + ".color.a" + q, 1.0f); dirLight[q].x = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.x" + q); dirLight[q].y = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.y" + q); } } function Save () { EditorPrefs.SetBool(kEditorPrefsName + ".bakeAlpha", bakeAlpha); EditorPrefs.SetBool(kEditorPrefsName + ".bakeMainTexAsWhite", bakeMainTexAsWhite); EditorPrefs.SetInt(kEditorPrefsName + ".minTextureResolution", minTextureResolution); EditorPrefs.SetInt(kEditorPrefsName + ".maxTextureResolution", maxTextureResolution); EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene", emptyScene); EditorPrefs.SetBool(kEditorPrefsName + ".useCustomLights", useCustomLights); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.r", ambient.r); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.g", ambient.g); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.b", ambient.b); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.a", ambient.a); for (var q = 0; q < kLights; ++q) { EditorPrefs.SetBool(kEditorPrefsName + ".enableLight" + q, enableLight[q]); EditorPrefs.SetFloat(kEditorPrefsName + ".color.r" + q, colorLight[q].r); EditorPrefs.SetFloat(kEditorPrefsName + ".color.g" + q, colorLight[q].g); EditorPrefs.SetFloat(kEditorPrefsName + ".color.b" + q, colorLight[q].b); EditorPrefs.SetFloat(kEditorPrefsName + ".color.a" + q, colorLight[q].a); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.x" + q, dirLight[q].x); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.y" + q, dirLight[q].y); } } } class BakeMaterial extends EditorWindow { private static var kMateriBakeNodeName = "__MateriaBakeSetup"; private static var kWindowMinSize = Vector2 (300, 386); private static var settings : BakeMaterialSettings; private static var visible : boolean = false; private var camera : GameObject; private var plane : GameObject; private var previewTexture : Texture; private var lights : GameObject[] = new GameObject[BakeMaterialSettings.kLights]; private var stateChanged = false; private var texViewScrollPosition = Vector2.zero; private var lastMaterial : Material; private var originalScene = ""; private var scheduleBakeOnNextUpdate = false; private function SetupScene () { DestroyScene (); var oldGo = GameObject.Find(kMateriBakeNodeName); if (oldGo) DestroyImmediate (oldGo); camera = new GameObject (kMateriBakeNodeName, Camera); plane = GameObject.CreatePrimitive (PrimitiveType.Plane); var cam = camera; cam.camera.backgroundColor = Color.black; cam.camera.clearFlags = CameraClearFlags.SolidColor; cam.camera.orthographic = true; cam.camera.orthographicSize = 5.0; cam.camera.cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene; plane.transform.parent = cam.transform; plane.transform.position = Vector3.forward * 10.0; plane.transform.rotation = Quaternion.Euler (0, 0, 180) * Quaternion.Euler (-90, 0, 0); plane.layer = settings.kBakingLayerShouldBeUnusedInScene; for (var l in lights) { l = new GameObject ("Light", Light); l.light.type = LightType.Directional; l.light.cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene; l.transform.parent = cam.transform; l.SetActive(false); } } private function UpdateScene (m : Material) { for (var q = 0; q < settings.kLights; ++q) { lights[q].SetActive(settings.useCustomLights && settings.enableLight[q]); lights[q].light.color = settings.colorLight[q]; lights[q].transform.rotation = Quaternion.AngleAxis(settings.dirLight[q].x, Vector3.up) * Quaternion.AngleAxis(settings.dirLight[q].y, Vector3.right); } if (settings.useCustomLights) RenderSettings.ambientLight = settings.ambient; else if (settings.emptyScene) RenderSettings.ambientLight = Color.white; plane.renderer.material = m; } private function DestroyScene () { GameObject.DestroyImmediate (camera); GameObject.DestroyImmediate (plane); GameObject.DestroyImmediate (previewTexture); } function UpdateMaterialPreview (m : Material) : RenderTexture { if (!m) return; var saveAmbientLight = RenderSettings.ambientLight; var saveMainTexture = m.mainTexture; if (settings.bakeMainTexAsWhite) m.mainTexture = null; //setup if (!camera) SetupScene (); camera.SetActive(true); UpdateScene (m); var res = FindLargestTextureResolution (plane.renderer.sharedMaterial, settings.minTextureResolution, settings.maxTextureResolution); var rt = RenderCameraToRenderTexture (camera.camera, res.x, res.y); //restore camera.SetActive(false); RenderSettings.ambientLight = saveAmbientLight; m.mainTexture = saveMainTexture; previewTexture = rt; return rt; } function CaptureMaterial(m : Material) { var matAssetPath = AssetDatabase.GetAssetPath (m); var assetPath = System.IO.Path.Combine (System.IO.Path.GetDirectoryName (matAssetPath), System.IO.Path.GetFileNameWithoutExtension (matAssetPath)); var rt = UpdateMaterialPreview (m); RenderTextureToPNG (rt, settings.bakeAlpha, assetPath + ".png"); } function OnEnable () { if (!settings) settings = new BakeMaterialSettings (); SetupScene (); visible = true; } function OnDisable () { DestroyScene (); settings.Save (); visible = false; } static function GetTargetMaterial () : Material { return EditorUtility.InstanceIDToObject (Selection.activeInstanceID) as Material; } function OnSelectionChange () { Repaint (); } function [URL='http://forum.unity3d.com/#'][U][B]Update[U][B][IMG]http://cdncache-a.akamaihd.net/items/it/img/arrow-10x10.png[/IMG][/B][/U][/B][/U][/URL] () { var rebuildScene = false; if (scheduleBakeOnNextUpdate) { Bake (); scheduleBakeOnNextUpdate = false; rebuildScene = true; } if (originalScene == "" && EditorApplication.currentScene == "") settings.emptyScene = true; if (settings.emptyScene && originalScene == "" && EditorApplication.currentScene != "") { DestroyScene (); if (EditorApplication.SaveCurrentSceneIfUserWantsTo ()) { originalScene = EditorApplication.currentScene; EditorApplication.NewScene (); } else settings.emptyScene = false; rebuildScene = true; } else if (!settings.emptyScene && originalScene != "") { EditorApplication.OpenScene (originalScene); rebuildScene = true; originalScene = ""; } if (rebuildScene) { SetupScene (); } if (rebuildScene || stateChanged || !settings.emptyScene) { UpdateMaterialPreview (lastMaterial); Repaint (); stateChanged = false; } } function OnGUI () { var material = GetTargetMaterial (); if (lastMaterial != material) UpdateMaterialPreview (material); if (material) lastMaterial = material; EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200)); if (!(originalScene == "" && EditorApplication.currentScene == "")) { settings.emptyScene = !EditorGUILayout.BeginToggleGroup("Sceneligthing", !settings.emptyScene); EditorGUILayout.EndToggleGroup(); } settings.useCustomLights = EditorGUILayout.BeginToggleGroup("Customlighting", settings.useCustomLights); if (settings.useCustomLights) { EditorGUI.indentLevel = 1; settings.ambient = EditorGUILayout.ColorField("Ambient", settings.ambient); for (var q = 0; q < settings.kLights; ++q) { settings.enableLight[q] = EditorGUILayout.BeginToggleGroup("Light", settings.enableLight[q]); EditorGUI.indentLevel = 2; settings.colorLight[q] = EditorGUILayout.ColorField("Color", settings.colorLight[q]); settings.dirLight[q] = EditorGUILayout.Vector2Field("Direction", settings.dirLight[q]); EditorGUILayout.EndToggleGroup(); } } EditorGUI.indentLevel = 0; EditorGUILayout.EndToggleGroup(); settings.bakeAlpha = EditorGUILayout.Toggle("BakeAlpha", settings.bakeAlpha); settings.bakeMainTexAsWhite = !EditorGUILayout.Toggle("MainTex", !settings.bakeMainTexAsWhite); settings.minTextureResolution = EditorGUILayout.IntField("MinResolution", settings.minTextureResolution); settings.maxTextureResolution = EditorGUILayout.IntField("MaxResolution", settings.maxTextureResolution); settings.minTextureResolution = Mathf.Max(2, settings.minTextureResolution); settings.maxTextureResolution = Mathf.Max(settings.minTextureResolution, settings.maxTextureResolution); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Bake")) { CaptureMaterial (lastMaterial); } if (GUILayout.Button("BakeSelected")) { scheduleBakeOnNextUpdate = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); texViewScrollPosition = EditorGUILayout.BeginScrollView (texViewScrollPosition); var r = GUILayoutUtility.GetAspectRect(1.0f); if (previewTexture) EditorGUI.DrawPreviewTexture(r, previewTexture); EditorGUILayout.EndScrollView(); EditorGUILayout.EndHorizontal(); if (GUI.changed) { stateChanged = true; } } @MenuItem("Custom/BakeMaterial ...", false, 5) static function CreateBakeEditor() { var window = EditorWindow.GetWindow(BakeMaterial); window.title = "BakeMaterial"; window.minSize = kWindowMinSize; window.Show(); } @MenuItem("Custom/BakeSelectedMaterials", false, 4) static function Bake() { var instanceIDs = Selection.instanceIDs; var currentScene = EditorApplication.currentScene; var wasAlreadyVisible = BakeMaterial.visible; var window :BakeMaterial = EditorWindow.GetWindow(BakeMaterial); if (window.settings.emptyScene) { if (!EditorApplication.SaveCurrentSceneIfUserWantsTo ()) return; EditorApplication.NewScene (); } window.SetupScene (); for (var i in instanceIDs) { var m : Material = EditorUtility.InstanceIDToObject (i) as Material; if (m) window.CaptureMaterial (m); } window.DestroyScene (); if (window.settings.emptyScene && currentScene) { EditorApplication.OpenScene (currentScene); } if (!wasAlreadyVisible) window.Close (); } static function FindLargestTextureResolution (m : Material, minTexRes : int, maxTexRes : int) : Vector2 { var res = Vector2 (minTexRes, minTexRes); for (var n :String in BakeMaterialSettings.kStandardTexNames) { var t : Texture; Debug.Log("try get"+n); Debug.Log("has"+m.HasProperty (n)); if (m.HasProperty (n)){ t = m.GetTexture (n); } if (t==null)continue; res.x = Mathf.Max (res.x, t.width); res.y = Mathf.Max (res.y, t.height); } res.x = Mathf.Min (res.x, maxTexRes); res.y = Mathf.Min (res.y, maxTexRes); return res; } static function RenderCameraToRenderTexture (cam : Camera, width : int, height : int) : RenderTexture { var rt = cam.camera.targetTexture; if (rt && rt.width != width && rt.height != height) DestroyImmediate(rt); if (!rt) rt = new RenderTexture (width, height, 24); cam.camera.targetTexture = rt; cam.camera.Render (); return rt; } static function RenderTextureToPNG (rt : RenderTexture, bakeAlpha : boolean, assetPath : String) { RenderTexture.active = rt; var screenShot = new Texture2D (rt.width, rt.height, bakeAlpha? TextureFormat.ARGB32 : TextureFormat.RGB24, false); screenShot.ReadPixels (Rect (0, 0, rt.width, rt.height), 0, 0); RenderTexture.active = null; var bytes = screenShot.EncodeToPNG (); System.IO.File.WriteAllBytes (assetPath, bytes); AssetDatabase.ImportAsset (assetPath, ImportAssetOptions.ForceUpdate); } }
Just curious if anyone has gotten this script to work with 4.6? When I run the script on selected material, the script generates a bake texture (png) however it only bakes about 1/4 of the texture and then stops.
Thanks for checking. Were you able to generate any kind of texture, even partial? After selecting the material and then clicking the menu Custom -> Bake Material, I am presented with he settings dialog. From here I check Scene lighting and click Bake. This will generate the partially complete png, placing it into the same folder as the material. I'm using the free version of Unity 4.6.2 f1
Works fine with Unity 5.2 Code (JavaScript): /* Instructions: 1.Select material to bake 2.Go Custom/Bake Material... */ class BakeMaterialSettings { private static var kEditorPrefsName = "BakeMaterialSettings"; static var kBakingLayerShouldBeUnusedInScene = 30; static var kStandardTexNames = new Array ("_MainTex", "_BumpMap", "_Detail", "_ParallaxMap", "_Parallax"); var bakeAlpha = false; var bakeMainTexAsWhite = false; var minTextureResolution = 8; var maxTextureResolution = 2048; var emptyScene = false; var useCustomLights = false; var ambient = Color.black; static var kLights = 3; var enableLight = new boolean[kLights]; var colorLight = new Color[kLights]; var dirLight = new Vector2[kLights]; function BakeMaterialSettings () { Load (); } function Load () { bakeAlpha = EditorPrefs.GetBool(kEditorPrefsName + ".bakeAlpha"); bakeMainTexAsWhite = EditorPrefs.GetBool(kEditorPrefsName + ".bakeMainTexAsWhite"); minTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".minTextureResolution", 8); maxTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".maxTextureResolution", 2048); emptyScene = EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene"); useCustomLights = EditorPrefs.GetBool(kEditorPrefsName + ".useCustomLights"); ambient.r = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.r"); ambient.g = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.g"); ambient.b = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.b"); ambient.a = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.a", 1.0f); for (var q = 0; q < kLights; ++q) { enableLight[q] = EditorPrefs.GetBool(kEditorPrefsName + ".enableLight" + q); colorLight[q].r = EditorPrefs.GetFloat(kEditorPrefsName + ".color.r" + q, 0.5f); colorLight[q].g = EditorPrefs.GetFloat(kEditorPrefsName + ".color.g" + q, 0.5f); colorLight[q].b = EditorPrefs.GetFloat(kEditorPrefsName + ".color.b" + q, 0.5f); colorLight[q].a = EditorPrefs.GetFloat(kEditorPrefsName + ".color.a" + q, 1.0f); dirLight[q].x = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.x" + q); dirLight[q].y = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.y" + q); } } function Save () { EditorPrefs.SetBool(kEditorPrefsName + ".bakeAlpha", bakeAlpha); EditorPrefs.SetBool(kEditorPrefsName + ".bakeMainTexAsWhite", bakeMainTexAsWhite); EditorPrefs.SetInt(kEditorPrefsName + ".minTextureResolution", minTextureResolution); EditorPrefs.SetInt(kEditorPrefsName + ".maxTextureResolution", maxTextureResolution); EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene", emptyScene); EditorPrefs.SetBool(kEditorPrefsName + ".useCustomLights", useCustomLights); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.r", ambient.r); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.g", ambient.g); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.b", ambient.b); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.a", ambient.a); for (var q = 0; q < kLights; ++q) { EditorPrefs.SetBool(kEditorPrefsName + ".enableLight" + q, enableLight[q]); EditorPrefs.SetFloat(kEditorPrefsName + ".color.r" + q, colorLight[q].r); EditorPrefs.SetFloat(kEditorPrefsName + ".color.g" + q, colorLight[q].g); EditorPrefs.SetFloat(kEditorPrefsName + ".color.b" + q, colorLight[q].b); EditorPrefs.SetFloat(kEditorPrefsName + ".color.a" + q, colorLight[q].a); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.x" + q, dirLight[q].x); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.y" + q, dirLight[q].y); } } } class BakeMaterial extends EditorWindow { private static var kMateriBakeNodeName = "__MateriaBakeSetup"; private static var kWindowMinSize = Vector2 (300, 386); private static var settings : BakeMaterialSettings; private static var visible : boolean = false; private var camera : GameObject; private var plane : GameObject; private var previewTexture : Texture; private var lights : GameObject[] = new GameObject[BakeMaterialSettings.kLights]; private var stateChanged = false; private var texViewScrollPosition = Vector2.zero; private var lastMaterial : Material; private var originalScene = ""; private var scheduleBakeOnNextUpdate = false; private function SetupScene () { DestroyScene (); var oldGo = GameObject.Find(kMateriBakeNodeName); if (oldGo) DestroyImmediate (oldGo); camera = new GameObject (kMateriBakeNodeName, Camera); plane = GameObject.CreatePrimitive (PrimitiveType.Plane); var cam = camera; cam.GetComponent.<Camera>().backgroundColor = Color.black; cam.GetComponent.<Camera>().clearFlags = CameraClearFlags.SolidColor; cam.GetComponent.<Camera>().orthographic = true; cam.GetComponent.<Camera>().orthographicSize = 5.0; cam.GetComponent.<Camera>().cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene; plane.transform.parent = cam.transform; plane.transform.position = Vector3.forward * 10.0; plane.transform.rotation = Quaternion.Euler (0, 0, 180) * Quaternion.Euler (-90, 0, 0); plane.layer = settings.kBakingLayerShouldBeUnusedInScene; for (var l in lights) { l = new GameObject ("Light", Light); l.GetComponent.<Light>().type = LightType.Directional; l.GetComponent.<Light>().cullingMask = 1 << settings.kBakingLayerShouldBeUnusedInScene; l.transform.parent = cam.transform; l.SetActive(false); } } private function UpdateScene (m : Material) { for (q = 0; q < settings.kLights; ++q) { lights[q].SetActive(settings.useCustomLights && settings.enableLight[q]); lights[q].GetComponent.<Light>().color = settings.colorLight[q]; lights[q].transform.rotation = Quaternion.AngleAxis(settings.dirLight[q].x, Vector3.up) * Quaternion.AngleAxis(settings.dirLight[q].y, Vector3.right); } if (settings.useCustomLights) RenderSettings.ambientLight = settings.ambient; else if (settings.emptyScene) RenderSettings.ambientLight = Color.white; plane.GetComponent.<Renderer>().material = m; } private function DestroyScene () { GameObject.DestroyImmediate (camera); GameObject.DestroyImmediate (plane); GameObject.DestroyImmediate (previewTexture); } function UpdateMaterialPreview (m : Material) : RenderTexture { if (!m) return; var saveAmbientLight = RenderSettings.ambientLight; var saveMainTexture = m.mainTexture; if (settings.bakeMainTexAsWhite) m.mainTexture = null; // setup if (!camera) SetupScene (); camera.SetActive(true); UpdateScene (m); var res = FindLargestTextureResolution (plane.GetComponent.<Renderer>().sharedMaterial, settings.minTextureResolution, settings.maxTextureResolution); var rt = RenderCameraToRenderTexture (camera.GetComponent.<Camera>(), res.x, res.y); // restore camera.SetActive(false); RenderSettings.ambientLight = saveAmbientLight; m.mainTexture = saveMainTexture; previewTexture = rt; return rt; } function CaptureMaterial(m : Material) { var matAssetPath = AssetDatabase.GetAssetPath (m); var assetPath = System.IO.Path.Combine (System.IO.Path.GetDirectoryName (matAssetPath), System.IO.Path.GetFileNameWithoutExtension (matAssetPath)); var rt = UpdateMaterialPreview (m); RenderTextureToPNG (rt, settings.bakeAlpha, assetPath + ".png"); } function OnEnable () { if (!settings) settings = new BakeMaterialSettings (); SetupScene (); visible = true; } function OnDisable () { DestroyScene (); settings.Save (); visible = false; } static function GetTargetMaterial () : Material { return EditorUtility.InstanceIDToObject (Selection.activeInstanceID) as Material; } function OnSelectionChange () { Repaint (); } function Update () { var rebuildScene = false; if (scheduleBakeOnNextUpdate) { Bake (); scheduleBakeOnNextUpdate = false; rebuildScene = true; } if (originalScene == "" && EditorApplication.currentScene == "") settings.emptyScene = true; if (settings.emptyScene && originalScene == "" && EditorApplication.currentScene != "") { DestroyScene (); if (EditorApplication.SaveCurrentSceneIfUserWantsTo ()) { originalScene = EditorApplication.currentScene; EditorApplication.NewScene (); } else settings.emptyScene = false; rebuildScene = true; } else if (!settings.emptyScene && originalScene != "") { EditorApplication.OpenScene (originalScene); rebuildScene = true; originalScene = ""; } if (rebuildScene) { SetupScene (); } if (rebuildScene || stateChanged || !settings.emptyScene) { UpdateMaterialPreview (lastMaterial); Repaint (); stateChanged = false; } } function OnGUI () { var material = GetTargetMaterial (); if (lastMaterial != material) UpdateMaterialPreview (material); if (material) lastMaterial = material; EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200)); if (!(originalScene == "" && EditorApplication.currentScene == "")) { settings.emptyScene = !EditorGUILayout.BeginToggleGroup("Scene ligthing", !settings.emptyScene); EditorGUILayout.EndToggleGroup(); } settings.useCustomLights = EditorGUILayout.BeginToggleGroup("Custom lighting", settings.useCustomLights); if (settings.useCustomLights) { EditorGUI.indentLevel = 1; settings.ambient = EditorGUILayout.ColorField("Ambient", settings.ambient); for (var q = 0; q < settings.kLights; ++q) { settings.enableLight[q] = EditorGUILayout.BeginToggleGroup("Light", settings.enableLight[q]); EditorGUI.indentLevel = 2; settings.colorLight[q] = EditorGUILayout.ColorField("Color", settings.colorLight[q]); settings.dirLight[q] = EditorGUILayout.Vector2Field("Direction", settings.dirLight[q]); EditorGUILayout.EndToggleGroup(); } } EditorGUI.indentLevel = 0; EditorGUILayout.EndToggleGroup(); settings.bakeAlpha = EditorGUILayout.Toggle("Bake Alpha", settings.bakeAlpha); settings.bakeMainTexAsWhite = !EditorGUILayout.Toggle("MainTex", !settings.bakeMainTexAsWhite); settings.minTextureResolution = EditorGUILayout.IntField("Min Resolution", settings.minTextureResolution); settings.maxTextureResolution = EditorGUILayout.IntField("Max Resolution", settings.maxTextureResolution); settings.minTextureResolution = Mathf.Max(2, settings.minTextureResolution); settings.maxTextureResolution = Mathf.Max(settings.minTextureResolution, settings.maxTextureResolution); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Bake")) { CaptureMaterial (lastMaterial); } if (GUILayout.Button("Bake Selected")) { scheduleBakeOnNextUpdate = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); texViewScrollPosition = EditorGUILayout.BeginScrollView (texViewScrollPosition); var r = GUILayoutUtility.GetAspectRect(1.0f); if (previewTexture) EditorGUI.DrawPreviewTexture(r, previewTexture); EditorGUILayout.EndScrollView(); EditorGUILayout.EndHorizontal(); if (GUI.changed) { stateChanged = true; } } @MenuItem("Custom/Bake Material ...", false, 5) static function CreateBakeEditor() { var window = EditorWindow.GetWindow(BakeMaterial); window.title = "Bake Material"; window.minSize = kWindowMinSize; window.Show(); } @MenuItem("Custom/Bake Selected Materials", false, 4) static function Bake() { var instanceIDs = Selection.instanceIDs; var currentScene = EditorApplication.currentScene; var wasAlreadyVisible = BakeMaterial.visible; var window = EditorWindow.GetWindow(BakeMaterial); if (window.settings.emptyScene) { if (!EditorApplication.SaveCurrentSceneIfUserWantsTo ()) return; EditorApplication.NewScene (); } window.SetupScene (); for (var i in instanceIDs) { var m : Material = EditorUtility.InstanceIDToObject (i) as Material; if (m) window.CaptureMaterial (m); } window.DestroyScene (); if (window.settings.emptyScene && currentScene) { EditorApplication.OpenScene (currentScene); } if (!wasAlreadyVisible) window.Close (); } static function FindLargestTextureResolution (m : Material, minTexRes : int, maxTexRes : int) : Vector2 { var res = Vector2 (minTexRes, minTexRes); for (var n in BakeMaterialSettings.kStandardTexNames) { if (!m.HasProperty (n)) continue; var t : Texture = m.GetTexture (n); if (!t) continue; res.x = Mathf.Max (res.x, t.width); res.y = Mathf.Max (res.y, t.height); } res.x = Mathf.Min (res.x, maxTexRes); res.y = Mathf.Min (res.y, maxTexRes); return res; } static function RenderCameraToRenderTexture (cam : Camera, width : int, height : int) : RenderTexture { var rt = cam.GetComponent.<Camera>().targetTexture; if (rt && rt.width != width && rt.height != height) DestroyImmediate(rt); if (!rt) rt = new RenderTexture (width, height, 24); cam.GetComponent.<Camera>().targetTexture = rt; cam.GetComponent.<Camera>().Render (); return rt; } static function RenderTextureToPNG (rt : RenderTexture, bakeAlpha : boolean, assetPath : String) { RenderTexture.active = rt; var screenShot = new Texture2D (rt.width, rt.height, bakeAlpha? TextureFormat.ARGB32 : TextureFormat.RGB24, false); screenShot.ReadPixels (Rect (0, 0, rt.width, rt.height), 0, 0); RenderTexture.active = null; var bytes = screenShot.EncodeToPNG (); System.IO.File.WriteAllBytes (assetPath, bytes); AssetDatabase.ImportAsset (assetPath, ImportAssetOptions.ForceUpdate); } }
Hi ,Everyone ,and excuse me for my english. I have a little trouble.Can I with this script bake material from selected object ? Because, I need to bake shadows and highlight from object.
Thanks for script though nothing is changing in script.... **edit** Getting this errors Code (CSharp): Releasing render texture that is set as Camera.targetTexture! UnityEngine.Object:DestroyImmediate(Object) BakeMaterial:RenderCameraToRenderTexture(Camera, Int32, Int32) (at Assets/Editor/BakeMaterial.js:342) BakeMaterial:UpdateMaterialPreview(Material) (at Assets/Editor/BakeMaterial.js:151) BakeMaterial:CaptureMaterial(Material) (at Assets/Editor/BakeMaterial.js:163) BakeMaterial:BakeMaterial$CaptureMaterial$UnityEngine.Material(Object, Object[]) UnityScript.Lang.UnityRuntimeServices:Invoke(Object, String, Object[], Type) BakeMaterial:Bake() (at Assets/Editor/BakeMaterial.js:311) BakeMaterial:Update() (at Assets/Editor/BakeMaterial.js:192) UnityEditor.EditorApplication:Internal_CallUpdateFunctions() **edit 2** its working,my shaders were unlit..damn
Works with 5.3 C# version Code (CSharp): using UnityEngine; using UnityEditor; using System.Collections; /* Instructions: 1.Select material to bake 2.Go Custom/Bake Material... */ class BakeMaterialSettings { static public string[] kStandardTexNames = { "_MainTex", "_BumpMap", "_Detail", "_ParallaxMap" /*, "_Parallax"*/ }; static public int kLights = 3; static public int kBakingLayerShouldBeUnusedInScene = 30; public bool bakeAlpha = false; public bool bakeMainTexAsWhite = false; public int minTextureResolution = 8; public int maxTextureResolution = 2048; public bool emptyScene = false; public bool useCustomLights = false; public Color ambient = Color.black; public bool[] enableLight = new bool[kLights]; public Color[] colorLight = new Color[kLights]; public Vector2[] dirLight = new Vector2[kLights]; private static string kEditorPrefsName = "BakeMaterialSettings"; public BakeMaterialSettings() { Load(); } public void Load () { bakeAlpha = EditorPrefs.GetBool(kEditorPrefsName + ".bakeAlpha"); bakeMainTexAsWhite = EditorPrefs.GetBool(kEditorPrefsName + ".bakeMainTexAsWhite"); minTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".minTextureResolution", 8); maxTextureResolution = EditorPrefs.GetInt(kEditorPrefsName + ".maxTextureResolution", 2048); emptyScene = EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene"); useCustomLights = EditorPrefs.GetBool(kEditorPrefsName + ".useCustomLights"); ambient.r = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.r"); ambient.g = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.g"); ambient.b = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.b"); ambient.a = EditorPrefs.GetFloat(kEditorPrefsName + ".ambient.a", 1.0f); for (var q = 0; q < kLights; ++q) { enableLight[q] = EditorPrefs.GetBool(kEditorPrefsName + ".enableLight" + q); colorLight[q].r = EditorPrefs.GetFloat(kEditorPrefsName + ".color.r" + q, 0.5f); colorLight[q].g = EditorPrefs.GetFloat(kEditorPrefsName + ".color.g" + q, 0.5f); colorLight[q].b = EditorPrefs.GetFloat(kEditorPrefsName + ".color.b" + q, 0.5f); colorLight[q].a = EditorPrefs.GetFloat(kEditorPrefsName + ".color.a" + q, 1.0f); dirLight[q].x = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.x" + q); dirLight[q].y = EditorPrefs.GetFloat(kEditorPrefsName + ".dir.y" + q); } } public void Save () { EditorPrefs.SetBool(kEditorPrefsName + ".bakeAlpha", bakeAlpha); EditorPrefs.SetBool(kEditorPrefsName + ".bakeMainTexAsWhite", bakeMainTexAsWhite); EditorPrefs.SetInt(kEditorPrefsName + ".minTextureResolution", minTextureResolution); EditorPrefs.SetInt(kEditorPrefsName + ".maxTextureResolution", maxTextureResolution); EditorPrefs.GetBool(kEditorPrefsName + ".emptyScene", emptyScene); EditorPrefs.SetBool(kEditorPrefsName + ".useCustomLights", useCustomLights); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.r", ambient.r); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.g", ambient.g); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.b", ambient.b); EditorPrefs.SetFloat(kEditorPrefsName + ".ambient.a", ambient.a); for (var q = 0; q < kLights; ++q) { EditorPrefs.SetBool(kEditorPrefsName + ".enableLight" + q, enableLight[q]); EditorPrefs.SetFloat(kEditorPrefsName + ".color.r" + q, colorLight[q].r); EditorPrefs.SetFloat(kEditorPrefsName + ".color.g" + q, colorLight[q].g); EditorPrefs.SetFloat(kEditorPrefsName + ".color.b" + q, colorLight[q].b); EditorPrefs.SetFloat(kEditorPrefsName + ".color.a" + q, colorLight[q].a); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.x" + q, dirLight[q].x); EditorPrefs.SetFloat(kEditorPrefsName + ".dir.y" + q, dirLight[q].y); } } } class BakeMaterial : UnityEditor.EditorWindow { private static BakeMaterialSettings settings; private static string kMateriBakeNodeName = "__MateriaBakeSetup"; private static Vector2 kWindowMinSize = new Vector2 (300, 386); private static bool visible = false; private GameObject camera; private GameObject plane; private Texture previewTexture; private GameObject[] lights = new GameObject[BakeMaterialSettings.kLights]; private bool stateChanged = false; private Vector2 texViewScrollPosition = Vector2.zero; private Material lastMaterial; private string originalScene = ""; private bool scheduleBakeOnNextUpdate = false; private void SetupScene () { DestroyScene (); var oldGo = GameObject.Find(kMateriBakeNodeName); if (oldGo) DestroyImmediate (oldGo); camera = new GameObject(kMateriBakeNodeName, typeof(Camera)); plane = GameObject.CreatePrimitive(PrimitiveType.Plane); var cam = camera; cam.GetComponent<Camera>().backgroundColor = Color.black; cam.GetComponent<Camera>().clearFlags = CameraClearFlags.SolidColor; cam.GetComponent<Camera>().orthographic = true; cam.GetComponent<Camera>().orthographicSize = 5.0f; cam.GetComponent<Camera>().cullingMask = 1 << BakeMaterialSettings.kBakingLayerShouldBeUnusedInScene; plane.transform.parent = cam.transform; plane.transform.position = Vector3.forward * 10.0f; plane.transform.rotation = Quaternion.Euler (0f, 0f, 180f) * Quaternion.Euler (-90f, 0f, 0f); plane.layer = BakeMaterialSettings.kBakingLayerShouldBeUnusedInScene; for (int i=0; i<lights.Length; i++) { lights[i] = new GameObject("Light", typeof(Light)); lights[i].GetComponent<Light>().type = LightType.Directional; lights[i].GetComponent<Light>().cullingMask = 1 << BakeMaterialSettings.kBakingLayerShouldBeUnusedInScene; lights[i].transform.parent = cam.transform; lights[i].SetActive(false); } } private void UpdateScene (Material m) { for (int q = 0; q < BakeMaterialSettings.kLights; ++q) { lights[q].SetActive(settings.useCustomLights && settings.enableLight[q]); lights[q].GetComponent<Light>().color = settings.colorLight[q]; lights[q].transform.rotation = Quaternion.AngleAxis(settings.dirLight[q].x, Vector3.up) * Quaternion.AngleAxis(settings.dirLight[q].y, Vector3.right); } if (settings.useCustomLights) RenderSettings.ambientLight = settings.ambient; else if (settings.emptyScene) RenderSettings.ambientLight = Color.white; plane.GetComponent<Renderer>().material = m; } private void DestroyScene () { GameObject.DestroyImmediate (camera); GameObject.DestroyImmediate (plane); GameObject.DestroyImmediate (previewTexture); } RenderTexture UpdateMaterialPreview (Material m) { if (!m) return null; var saveAmbientLight = RenderSettings.ambientLight; var saveMainTexture = m.mainTexture; if (settings.bakeMainTexAsWhite) m.mainTexture = null; // setup if (!camera) SetupScene (); camera.SetActive(true); UpdateScene (m); var res = FindLargestTextureResolution(plane.GetComponent<Renderer>().sharedMaterial, settings.minTextureResolution, settings.maxTextureResolution); var rt = RenderCameraToRenderTexture(camera.GetComponent<Camera>(), (int)res.x, (int)res.y); // restore camera.SetActive(false); RenderSettings.ambientLight = saveAmbientLight; m.mainTexture = saveMainTexture; previewTexture = rt; return rt; } void CaptureMaterial(Material m) { var matAssetPath = AssetDatabase.GetAssetPath (m); var assetPath = System.IO.Path.Combine (System.IO.Path.GetDirectoryName (matAssetPath), System.IO.Path.GetFileNameWithoutExtension (matAssetPath)); var rt = UpdateMaterialPreview (m); RenderTextureToPNG (rt, settings.bakeAlpha, assetPath + ".png"); } void OnEnable () { if (settings == null) settings = new BakeMaterialSettings (); SetupScene (); visible = true; } void OnDisable () { DestroyScene (); settings.Save (); visible = false; } static Material GetTargetMaterial() { return EditorUtility.InstanceIDToObject (Selection.activeInstanceID) as Material; } void OnSelectionChange () { Repaint (); } void Update () { var rebuildScene = false; if (scheduleBakeOnNextUpdate) { Bake (); scheduleBakeOnNextUpdate = false; rebuildScene = true; } if (originalScene == "" && EditorApplication.currentScene == "") settings.emptyScene = true; if (settings.emptyScene && originalScene == "" && EditorApplication.currentScene != "") { DestroyScene (); if (EditorApplication.SaveCurrentSceneIfUserWantsTo ()) { originalScene = EditorApplication.currentScene; EditorApplication.NewScene (); } else settings.emptyScene = false; rebuildScene = true; } else if (!settings.emptyScene && originalScene != "") { EditorApplication.OpenScene (originalScene); rebuildScene = true; originalScene = ""; } if (rebuildScene) { SetupScene (); } if (rebuildScene || stateChanged || !settings.emptyScene) { UpdateMaterialPreview (lastMaterial); Repaint (); stateChanged = false; } } void OnGUI () { var material = GetTargetMaterial (); if (lastMaterial != material) UpdateMaterialPreview (material); if (material) lastMaterial = material; EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200)); if (!(originalScene == "" && EditorApplication.currentScene == "")) { settings.emptyScene = !EditorGUILayout.BeginToggleGroup("Scene ligthing", !settings.emptyScene); EditorGUILayout.EndToggleGroup(); } settings.useCustomLights = EditorGUILayout.BeginToggleGroup("Custom lighting", settings.useCustomLights); if (settings.useCustomLights) { EditorGUI.indentLevel = 1; settings.ambient = EditorGUILayout.ColorField("Ambient", settings.ambient); for (var q = 0; q < BakeMaterialSettings.kLights; ++q) { settings.enableLight[q] = EditorGUILayout.BeginToggleGroup("Light", settings.enableLight[q]); EditorGUI.indentLevel = 2; settings.colorLight[q] = EditorGUILayout.ColorField("Color", settings.colorLight[q]); settings.dirLight[q] = EditorGUILayout.Vector2Field("Direction", settings.dirLight[q]); EditorGUILayout.EndToggleGroup(); } } EditorGUI.indentLevel = 0; EditorGUILayout.EndToggleGroup(); settings.bakeAlpha = EditorGUILayout.Toggle("Bake Alpha", settings.bakeAlpha); settings.bakeMainTexAsWhite = !EditorGUILayout.Toggle("MainTex", !settings.bakeMainTexAsWhite); settings.minTextureResolution = EditorGUILayout.IntField("Min Resolution", settings.minTextureResolution); settings.maxTextureResolution = EditorGUILayout.IntField("Max Resolution", settings.maxTextureResolution); settings.minTextureResolution = Mathf.Max(2, settings.minTextureResolution); settings.maxTextureResolution = Mathf.Max(settings.minTextureResolution, settings.maxTextureResolution); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Bake")) { CaptureMaterial (lastMaterial); } if (GUILayout.Button("Bake Selected")) { scheduleBakeOnNextUpdate = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); texViewScrollPosition = EditorGUILayout.BeginScrollView (texViewScrollPosition); var r = GUILayoutUtility.GetAspectRect(1.0f); if (previewTexture) EditorGUI.DrawPreviewTexture(r, previewTexture); EditorGUILayout.EndScrollView(); EditorGUILayout.EndHorizontal(); if (GUI.changed) { stateChanged = true; } } [MenuItem("Custom/Bake Material", false, 5)] static void CreateBakeEditor() { var window = UnityEditor.EditorWindow.GetWindow<BakeMaterial>("Bake Material"); window.minSize = kWindowMinSize; window.Show(); } [MenuItem("Custom/Bake Selected Materials", false, 4)] static void Bake() { var instanceIDs = Selection.instanceIDs; var currentScene = EditorApplication.currentScene; var wasAlreadyVisible = BakeMaterial.visible; var window = UnityEditor.EditorWindow.GetWindow<BakeMaterial>("Bake Selected Material"); if (BakeMaterial.settings.emptyScene) { if (!UnityEditor.EditorApplication.SaveCurrentSceneIfUserWantsTo ()) return; UnityEditor.EditorApplication.NewScene (); } window.SetupScene (); foreach (var i in instanceIDs) { Material m = UnityEditor.EditorUtility.InstanceIDToObject(i) as Material; if (m) window.CaptureMaterial (m); } window.DestroyScene (); if (BakeMaterial.settings.emptyScene && currentScene != "") { UnityEditor.EditorApplication.OpenScene(currentScene); } if (!wasAlreadyVisible) window.Close (); } static Vector2 FindLargestTextureResolution (Material m, int minTexRes, int maxTexRes) { Vector2 res = new Vector2 (minTexRes, minTexRes); foreach (var n in BakeMaterialSettings.kStandardTexNames) { if (!m.HasProperty(n)) continue; Texture t = m.GetTexture(n); if (!t) continue; res.x = Mathf.Max(res.x, t.width); res.y = Mathf.Max(res.y, t.height); } res.x = Mathf.Min (res.x, maxTexRes); res.y = Mathf.Min (res.y, maxTexRes); return res; } static RenderTexture RenderCameraToRenderTexture (Camera cam, int width, int height) { var rt = cam.GetComponent<Camera>().targetTexture; if (rt && rt.width != width && rt.height != height) DestroyImmediate(rt); if (!rt) rt = new RenderTexture (width, height, 24); cam.GetComponent<Camera>().targetTexture = rt; cam.GetComponent<Camera>().Render (); return rt; } static void RenderTextureToPNG (RenderTexture rt, bool bakeAlpha, string assetPath) { RenderTexture.active = rt; var screenShot = new Texture2D (rt.width, rt.height, bakeAlpha? TextureFormat.ARGB32 : TextureFormat.RGB24, false); screenShot.ReadPixels (new Rect(0, 0, rt.width, rt.height), 0, 0); RenderTexture.active = null; var bytes = screenShot.EncodeToPNG (); System.IO.File.WriteAllBytes (assetPath, bytes); AssetDatabase.ImportAsset (assetPath, ImportAssetOptions.ForceUpdate); } }