|
|
|
|
|
|
using System; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using Object = UnityEngine.Object; |
|
|
|
|
|
|
|
namespace UnityEditor.Experimental.Rendering.HDPipeline |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
public static void BakeCustomReflectionProbe(PlanarReflectionProbe probe, bool usePreviousAssetPath, bool custom) |
|
|
|
public static bool IsCollidingWithOtherProbes(string targetPath, ReflectionProbe targetProbe, out ReflectionProbe collidingProbe) |
|
|
|
throw new NotImplementedException(); |
|
|
|
ReflectionProbe[] probes = Object.FindObjectsOfType<ReflectionProbe>().ToArray(); |
|
|
|
collidingProbe = null; |
|
|
|
foreach (var probe in probes) |
|
|
|
{ |
|
|
|
if (probe == targetProbe || probe.customBakedTexture == null) |
|
|
|
continue; |
|
|
|
string path = AssetDatabase.GetAssetPath(probe.customBakedTexture); |
|
|
|
if (path == targetPath) |
|
|
|
{ |
|
|
|
collidingProbe = probe; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
public static void BakeCustomReflectionProbe(ReflectionProbe probe, bool usePreviousAssetPath, bool custom) |
|
|
|
public static bool IsCollidingWithOtherProbes(string targetPath, PlanarReflectionProbe targetProbe, out PlanarReflectionProbe collidingProbe) |
|
|
|
if (!custom && probe.bakedTexture != null) |
|
|
|
probe.customBakedTexture = probe.bakedTexture; |
|
|
|
PlanarReflectionProbe[] probes = Object.FindObjectsOfType<PlanarReflectionProbe>().ToArray(); |
|
|
|
collidingProbe = null; |
|
|
|
foreach (var probe in probes) |
|
|
|
{ |
|
|
|
if (probe == targetProbe || probe.customTexture == null) |
|
|
|
continue; |
|
|
|
var path = AssetDatabase.GetAssetPath(probe.customTexture); |
|
|
|
if (path == targetPath) |
|
|
|
{ |
|
|
|
collidingProbe = probe; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
string path = ""; |
|
|
|
if (usePreviousAssetPath) |
|
|
|
path = AssetDatabase.GetAssetPath(probe.customBakedTexture); |
|
|
|
public static void BakeCustomReflectionProbe(PlanarReflectionProbe probe, bool usePreviousAssetPath) |
|
|
|
{ |
|
|
|
string path; |
|
|
|
if (!GetCustomBakePath(probe.name, probe.customTexture, true, usePreviousAssetPath, out path)) |
|
|
|
return; |
|
|
|
string targetExtension = probe.hdr ? "exr" : "png"; |
|
|
|
if (string.IsNullOrEmpty(path) || Path.GetExtension(path) != "." + targetExtension) |
|
|
|
PlanarReflectionProbe collidingProbe; |
|
|
|
if (IsCollidingWithOtherProbes(path, probe, out collidingProbe)) |
|
|
|
// We use the path of the active scene as the target path
|
|
|
|
var targetPath = SceneManager.GetActiveScene().path; |
|
|
|
targetPath = Path.Combine(Path.GetDirectoryName(targetPath), Path.GetFileNameWithoutExtension(targetPath)); |
|
|
|
if (string.IsNullOrEmpty(targetPath)) |
|
|
|
targetPath = "Assets"; |
|
|
|
else if (Directory.Exists(targetPath) == false) |
|
|
|
Directory.CreateDirectory(targetPath); |
|
|
|
if (!EditorUtility.DisplayDialog("Texture is used by other reflection probe", |
|
|
|
string.Format("'{0}' path is used by the game object '{1}', do you really want to overwrite it?", |
|
|
|
path, collidingProbe.name), "Yes", "No")) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
string fileName = probe.name + (probe.hdr ? "-reflectionHDR" : "-reflection") + "." + targetExtension; |
|
|
|
fileName = Path.GetFileNameWithoutExtension(AssetDatabase.GenerateUniqueAssetPath(Path.Combine(targetPath, fileName))); |
|
|
|
EditorUtility.DisplayProgressBar("Planar Reflection Probes", "Baking " + path, 0.5f); |
|
|
|
if (!BakePlanarReflectionProbe(probe, path)) |
|
|
|
Debug.LogError("Failed to bake reflection probe to " + path); |
|
|
|
EditorUtility.ClearProgressBar(); |
|
|
|
} |
|
|
|
path = EditorUtility.SaveFilePanelInProject("Save reflection probe's cubemap.", fileName, targetExtension, "", targetPath); |
|
|
|
if (string.IsNullOrEmpty(path)) |
|
|
|
return; |
|
|
|
public static void BakeAllPlanarReflectionProbes() |
|
|
|
{ |
|
|
|
var probes = Object.FindObjectsOfType<PlanarReflectionProbe>(); |
|
|
|
for (var i = 0; i < probes.Length; i++) |
|
|
|
{ |
|
|
|
EditorUtility.DisplayProgressBar( |
|
|
|
"Baking Planar Probes", |
|
|
|
string.Format("Probe {0} / {1}", i + 1, probes.Length), |
|
|
|
(i + 1) / (float)probes.Length); |
|
|
|
ReflectionProbe collidingProbe; |
|
|
|
if (HDReflectionProbeEditorUtility.IsCollidingWithOtherProbes(path, probe, out collidingProbe)) |
|
|
|
var probe = probes[i]; |
|
|
|
var bakePath = GetBakePathFor(probe); |
|
|
|
var bakePathInfo = new FileInfo(bakePath); |
|
|
|
if (!bakePathInfo.Directory.Exists) |
|
|
|
bakePathInfo.Directory.Create(); |
|
|
|
BakePlanarReflectionProbe(probe, bakePath); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static string GetBakePathFor(PlanarReflectionProbe probe) |
|
|
|
{ |
|
|
|
var scene = probe.gameObject.scene; |
|
|
|
var directory = Path.Combine(Path.GetDirectoryName(scene.path), Path.GetFileNameWithoutExtension(scene.path)); |
|
|
|
var filename = string.Format("PlanarReflectionProbe-{0}.exr", 0); |
|
|
|
|
|
|
|
return Path.Combine(directory, filename); |
|
|
|
} |
|
|
|
|
|
|
|
public static bool BakePlanarReflectionProbe(PlanarReflectionProbe probe, string path) |
|
|
|
{ |
|
|
|
var rt = ReflectionSystem.NewRenderTarget(probe); |
|
|
|
ReflectionSystem.Render(probe, rt); |
|
|
|
var target = new Texture2D(rt.width, rt.height, TextureFormat.RGBAHalf, false, true); |
|
|
|
var a = RenderTexture.active; |
|
|
|
RenderTexture.active = rt; |
|
|
|
target.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0, false); |
|
|
|
RenderTexture.active = a; |
|
|
|
rt.Release(); |
|
|
|
|
|
|
|
var bytes = target.EncodeToEXR(); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
var targetFile = new FileInfo(path); |
|
|
|
if (!targetFile.Directory.Exists) |
|
|
|
targetFile.Directory.Create(); |
|
|
|
File.WriteAllBytes(path, bytes); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
Console.WriteLine(e); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
public static void BakeCustomReflectionProbe(ReflectionProbe probe, bool usePreviousAssetPath) |
|
|
|
{ |
|
|
|
string path; |
|
|
|
if (!GetCustomBakePath(probe.name, probe.customBakedTexture, probe.hdr, usePreviousAssetPath, out path)) |
|
|
|
return; |
|
|
|
|
|
|
|
ReflectionProbe collidingProbe; |
|
|
|
if (IsCollidingWithOtherProbes(path, probe, out collidingProbe)) |
|
|
|
{ |
|
|
|
if (!EditorUtility.DisplayDialog("Cubemap is used by other reflection probe", |
|
|
|
string.Format("'{0}' path is used by the game object '{1}', do you really want to overwrite it?", |
|
|
|
path, collidingProbe.name), "Yes", "No")) |
|
|
|
if (!EditorUtility.DisplayDialog("Cubemap is used by other reflection probe", |
|
|
|
string.Format("'{0}' path is used by the game object '{1}', do you really want to overwrite it?", |
|
|
|
path, collidingProbe.name), "Yes", "No")) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
public static bool BakeAllReflectionProbesSnapshots() |
|
|
|
{ |
|
|
|
return (bool)k_Lightmapping_BakeAllReflectionProbesSnapshots.Invoke(null, new object[0]); |
|
|
|
} |
|
|
|
|
|
|
|
static bool GetCustomBakePath(string probeName, Texture customBakedTexture, bool hdr, bool usePreviousAssetPath, out string path) |
|
|
|
{ |
|
|
|
path = ""; |
|
|
|
if (usePreviousAssetPath) |
|
|
|
path = AssetDatabase.GetAssetPath(customBakedTexture); |
|
|
|
|
|
|
|
var targetExtension = hdr ? "exr" : "png"; |
|
|
|
if (string.IsNullOrEmpty(path) || Path.GetExtension(path) != "." + targetExtension) |
|
|
|
{ |
|
|
|
// We use the path of the active scene as the target path
|
|
|
|
var targetPath = SceneManager.GetActiveScene().path; |
|
|
|
targetPath = Path.Combine(Path.GetDirectoryName(targetPath), Path.GetFileNameWithoutExtension(targetPath)); |
|
|
|
if (string.IsNullOrEmpty(targetPath)) |
|
|
|
targetPath = "Assets"; |
|
|
|
else if (Directory.Exists(targetPath) == false) |
|
|
|
Directory.CreateDirectory(targetPath); |
|
|
|
|
|
|
|
var fileName = probeName + (hdr ? "-reflectionHDR" : "-reflection") + "." + targetExtension; |
|
|
|
fileName = Path.GetFileNameWithoutExtension(AssetDatabase.GenerateUniqueAssetPath(Path.Combine(targetPath, fileName))); |
|
|
|
|
|
|
|
path = EditorUtility.SaveFilePanelInProject("Save reflection probe's cubemap.", fileName, targetExtension, "", targetPath); |
|
|
|
if (string.IsNullOrEmpty(path)) |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |