浏览代码

[PlanarReflection] (wip) Updated bake API

/main
Frédéric Vauchelles 7 年前
当前提交
9967a514
共有 5 个文件被更改,包括 176 次插入64 次删除
  1. 178
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/EditorReflectionSystem.cs
  2. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/EditorReflectionSystemGUI.cs
  3. 19
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/HDReflectionProbeEditorUtility.cs
  4. 5
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/ReflectionSystem.cs
  5. 30
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/ReflectionSystemInternal.cs

178
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/EditorReflectionSystem.cs


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;
}
}
}

8
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/EditorReflectionSystemGUI.cs


{
if (probe != null)
{
EditorReflectionSystem.BakeCustomReflectionProbe(probe, false, true);
EditorReflectionSystem.BakeCustomReflectionProbe(probe, false);
EditorReflectionSystem.BakeCustomReflectionProbe(planarProbe, false, true);
EditorReflectionSystem.BakeCustomReflectionProbe(planarProbe, false);
EditorReflectionSystem.ResetProbeSceneTextureInMaterial(planarProbe);
}
}

if (probe != null)
{
EditorReflectionSystem.BakeCustomReflectionProbe(probe, true, true);
EditorReflectionSystem.BakeCustomReflectionProbe(probe, true);
EditorReflectionSystem.BakeCustomReflectionProbe(planarProbe, true, true);
EditorReflectionSystem.BakeCustomReflectionProbe(planarProbe, true);
EditorReflectionSystem.ResetProbeSceneTextureInMaterial(planarProbe);
}
GUIUtility.ExitGUI();

19
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/HDReflectionProbeEditorUtility.cs


using System.Reflection;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.Rendering.HDPipeline;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;

center = b.center;
size = b.size;
return true;
}
public static bool IsCollidingWithOtherProbes(string targetPath, ReflectionProbe targetProbe, out ReflectionProbe collidingProbe)
{
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 ResetProbeSceneTextureInMaterial(ReflectionProbe p)

5
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/ReflectionSystem.cs


s_Instance.RenderAllRealtimeProbes();
}
public static RenderTexture NewRenderTarget(PlanarReflectionProbe probe)
{
return s_Instance.NewRenderTarget(probe);
}
public static void Render(PlanarReflectionProbe probe, RenderTexture target)
{
s_Instance.Render(probe, target);

30
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/ReflectionSystemInternal.cs


{
if( probe.realtimeTexture != null)
probe.realtimeTexture.Release();
var desc = hdCamera.renderTextureDesc;
desc.width = m_Parameters.planarReflectionProbeSize;
desc.height = m_Parameters.planarReflectionProbeSize;
desc.colorFormat = RenderTextureFormat.ARGBHalf;
probe.realtimeTexture = new RenderTexture(desc);
probe.realtimeTexture = NewRenderTarget(probe);
}
}

}
}
public RenderTexture NewRenderTarget(PlanarReflectionProbe probe)
{
var desc = GetRenderHDCamera(probe).renderTextureDesc;
desc.width = m_Parameters.planarReflectionProbeSize;
desc.height = m_Parameters.planarReflectionProbeSize;
desc.colorFormat = RenderTextureFormat.ARGBHalf;
var rt = new RenderTexture(desc);
return rt;
}
bool IsRealtimeTextureValid(RenderTexture renderTexture, HDCamera hdCamera)
{
return renderTexture != null

public void Render(PlanarReflectionProbe probe, RenderTexture target)
{
var renderCamera = GetRenderCamera(probe);
renderCamera.targetTexture = target;
var renderCamera = GetRenderHDCamera(probe);
renderCamera.camera.targetTexture = target;
SetupCameraForRender(renderCamera, probe);
SetupCameraForRender(renderCamera.camera, probe);
renderCamera.Render();
renderCamera.targetTexture = null;
renderCamera.camera.Render();
renderCamera.camera.targetTexture = null;
}
void SetProbeBoundsDirty(PlanarReflectionProbe planarProbe)

throw new NotImplementedException();
}
static Camera GetRenderCamera(PlanarReflectionProbe probe)
static HDCamera GetRenderHDCamera(PlanarReflectionProbe probe)
return camera;
return HDCamera.Get(camera, null, probe.frameSettings);
}
static Camera GetRenderCamera()

正在加载...
取消
保存