JavaScript - BakeMaterial.js
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;
}
如果你还有什么不懂的,可以百度搜下:编程回忆录,他们现在正在录制这方面的教程,都是零基础开始,由浅入深。
}