65 lines
2.7 KiB
C#
65 lines
2.7 KiB
C#
#if UNITY_EDITOR
|
|
using System.IO;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
/// <summary>White-albedo XTerrain capture to prove lightmap shade is reaching the ground shader.</summary>
|
|
public static class DiagnoseGroundWhiteGi
|
|
{
|
|
const string Root = "Assets/Bundles/Scenes/Town/HomeCamp";
|
|
const string ScenePath = Root + "/HomeCamp.unity";
|
|
const string Review = "D:/UD/AI/AIGame/Tools/output/homecamp_review";
|
|
|
|
public static string Run()
|
|
{
|
|
AssetDatabase.ImportAsset("Assets/Bundles/Shaders/XShader/XTerrain.shader", ImportAssetOptions.ForceSynchronousImport);
|
|
var scene = EditorSceneManager.OpenScene(ScenePath, OpenSceneMode.Single);
|
|
var root = scene.GetRootGameObjects().Single(g => g.name == "HomeCamp");
|
|
var ground = root.transform.Find("Ground")?.GetComponentInChildren<MeshRenderer>();
|
|
if (!ground) return "FAIL";
|
|
|
|
var prev = ground.sharedMaterial;
|
|
var mat = Object.Instantiate(prev);
|
|
var white = Texture2D.whiteTexture;
|
|
mat.SetTexture("_LayerTex0", white); mat.SetTexture("_LayerTex1", white);
|
|
mat.SetTexture("_LayerTex2", white); mat.SetTexture("_LayerTex3", white);
|
|
mat.SetFloat("_LightmapContrast", 1f);
|
|
mat.SetFloat("_BumpScale", 0f); mat.SetFloat("_Roughness", 1.5f); mat.SetFloat("_Metallic", 0f);
|
|
ground.sharedMaterial = mat;
|
|
|
|
ShaderUtil.allowAsyncCompilation = false;
|
|
var cam = root.GetComponentInChildren<Camera>();
|
|
Capture(cam, Review + "/diag_xterrain_white_c1.png");
|
|
|
|
mat.SetFloat("_LightmapContrast", 2.2f);
|
|
Capture(cam, Review + "/diag_xterrain_white_c22.png");
|
|
|
|
ground.sharedMaterial = prev;
|
|
Object.DestroyImmediate(mat);
|
|
return "wrote white-gi captures contrastProp=" + prev.GetFloat("_LightmapContrast");
|
|
}
|
|
|
|
static void Capture(Camera camera, string file)
|
|
{
|
|
var rt = RenderTexture.GetTemporary(720, 1280, 24, RenderTextureFormat.ARGB32);
|
|
var old = RenderTexture.active; var oldT = camera.targetTexture; var oldA = camera.aspect;
|
|
var tex = new Texture2D(720, 1280, TextureFormat.RGB24, false);
|
|
try
|
|
{
|
|
camera.aspect = 720f / 1280; camera.targetTexture = rt; RenderTexture.active = rt;
|
|
camera.Render(); camera.Render();
|
|
tex.ReadPixels(new Rect(0, 0, 720, 1280), 0, 0); tex.Apply();
|
|
File.WriteAllBytes(file, tex.EncodeToPNG());
|
|
}
|
|
finally
|
|
{
|
|
camera.targetTexture = oldT; camera.aspect = oldA; RenderTexture.active = old;
|
|
RenderTexture.ReleaseTemporary(rt); Object.DestroyImmediate(tex);
|
|
}
|
|
}
|
|
}
|
|
#endif
|