浏览代码

[ReflectionProbe] Support clearing baked data

/feature-ReflectionProbeBaking
Frédéric Vauchelles 6 年前
当前提交
dc30cd0a
共有 3 个文件被更改,包括 142 次插入47 次删除
  1. 65
      SampleScenes/HDTest/ReflectionProbeTest.unity
  2. 63
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/EditorReflectionSystem.cs
  3. 61
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/HDLightingDataAsset.cs

65
SampleScenes/HDTest/ReflectionProbeTest.unity


LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 11
m_GIWorkflowMode: 1
m_GIWorkflowMode: 0
m_GISettings:
serializedVersion: 2
m_BounceScale: 1

m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_ShowResolutionOverlay: 1
m_LightingDataAsset: {fileID: 112000036, guid: a0c1a0bf98f5b7541884af370f78c9f0,
type: 2}
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 0
--- !u!196 &4
NavMeshSettings:

m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 117398287}
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &208116710
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 208116712}
- component: {fileID: 208116711}
m_Layer: 0
m_Name: ProxyVolume
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &208116711
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 208116710}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d5c61b7a04f29ad49a0985f4fb6c436d, type: 3}
m_Name:
m_EditorClassIdentifier:
m_ProxyVolume:
m_ShapeType: 0
m_BoxSize: {x: 10, y: 10, z: 30}
m_BoxOffset: {x: 0, y: 0, z: 10}
m_BoxInfiniteProjection: 0
m_SphereRadius: 1
m_SphereOffset: {x: 0, y: 0, z: 0}
m_SphereInfiniteProjection: 0
--- !u!4 &208116712
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 208116710}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &377870779
GameObject:
m_ObjectHideFlags: 0

blendDistanceNegative: {x: 0, y: 0, z: 0}
blendNormalDistancePositive: {x: 0, y: 0, z: 0}
blendNormalDistanceNegative: {x: 0, y: 0, z: 0}
boxSideFadePositive: {x: 1, y: 1, z: 1}
boxSideFadePositive: {x: 1, y: 0, z: 1}
proxyVolumeComponent: {fileID: 0}
proxyVolumeComponent: {fileID: 208116711}
--- !u!23 &1017139650
MeshRenderer:
m_ObjectHideFlags: 2

m_Enabled: 0
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 1
m_DynamicOccludee: 1

blendNormalDistanceNegative: {x: 0, y: 0, z: 0}
boxSideFadePositive: {x: 1, y: 1, z: 1}
boxSideFadeNegative: {x: 1, y: 1, z: 1}
proxyVolumeComponent: {fileID: 0}
proxyVolumeComponent: {fileID: 208116711}
--- !u!23 &1314116690
MeshRenderer:
m_ObjectHideFlags: 2

blendNormalDistanceNegative: {x: 0, y: 0, z: 0}
boxSideFadePositive: {x: 1, y: 1, z: 1}
boxSideFadeNegative: {x: 1, y: 1, z: 1}
proxyVolumeComponent: {fileID: 0}
proxyVolumeComponent: {fileID: 208116711}
--- !u!23 &1521507376
MeshRenderer:
m_ObjectHideFlags: 2

m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2086739505}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.26, y: 2.95, z: 0.96}
m_LocalPosition: {x: -0.49, y: 2.655, z: 0.96}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1271190213}

63
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/EditorReflectionSystem.cs


using System.Linq;
using System.Reflection;
using UnityEditor.Callbacks;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Experimental.Rendering;

Directory.CreateDirectory(targetPath);
var fileName = string.Format("Probe {0}.exr", id);
return AssetDatabase.GenerateUniqueAssetPath(Path.Combine(targetPath, fileName));
return Path.Combine(targetPath, fileName);
}
static string GetSceneBakeDirectoryPath(Scene scene)

return targetPath;
}
static HDLightingDataAsset GetLightingDataAssetForScene(Scene scene)
static HDLightingDataAsset GetOrCreateLightingDataAssetForScene(Scene scene)
{
var filePath = GetLightingDataAssetPathForScene(scene);
var filePathInfo = new FileInfo(filePath);

asset = ScriptableObject.CreateInstance<HDLightingDataAsset>();
AssetDatabase.CreateAsset(asset, filePath);
}
return asset;
}
static HDLightingDataAsset GetLightingDataAssetForScene(Scene scene)
{
var filePath = GetLightingDataAssetPathForScene(scene);
var asset = AssetDatabase.LoadAssetAtPath<HDLightingDataAsset>(filePath);
return asset;
}

{
RenderSettings.enableLegacyReflectionProbeSystem = false;
Lightmapping.BakeReflectionProbeRequest += LightmappingOnBakeReflectionProbeRequest;
Lightmapping.ClearBakedReflectionProbeRequest += LightmappingOnClearBakedReflectionProbeRequest;
static void LightmappingOnClearBakedReflectionProbeRequest()
{
for (int i = 0, c = SceneManager.sceneCount; i < c; ++i)
{
var scene = SceneManager.GetSceneAt(i);
if (!scene.IsValid() || !scene.isLoaded)
continue;
var asset = GetLightingDataAssetForScene(scene);
if (asset == null)
continue;
asset.DeleteAssets();
var path = AssetDatabase.GetAssetPath(asset);
Assert.IsFalse(string.IsNullOrEmpty(path));
AssetDatabase.DeleteAsset(path);
}
}
static void Update()
{
if (s_CurrentBakeJob != null)

if (bakedTexture != null)
assetPath = AssetDatabase.GetAssetPath(bakedTexture);
if (string.IsNullOrEmpty(assetPath))
{
bakedTexture = AssetDatabase.LoadAssetAtPath<Cubemap>(assetPath);
}
Assert.IsFalse(string.IsNullOrEmpty(assetPath));

// 5. Write texture into asset file and import
var tex2D = CoreUtils.CopyCubemapToTexture2D(target);
target.Release();
AssetDatabase.ImportAsset(assetPath);
target.Release();
bakedTexture = AssetDatabase.LoadAssetAtPath<Cubemap>(assetPath);
// 7. Register baking information
var id = EditorUtility.GetSceneObjectIdentifierFor(probe);
var asset = GetOrCreateLightingDataAssetForScene(probe.gameObject.scene);
if (id != SceneObjectIdentifier.Null && asset != null)
{
asset.SetBakedTexture(id, bakedTexture);
EditorUtility.SetDirty(asset);
}
++job.m_StageIndex;
}

if (bakedTexture != null)
assetPath = AssetDatabase.GetAssetPath(bakedTexture);
if (string.IsNullOrEmpty(assetPath))
{
bakedTexture = AssetDatabase.LoadAssetAtPath<Cubemap>(assetPath);
}
Assert.IsFalse(string.IsNullOrEmpty(assetPath));

// 6. Assign texture
probe.bakedTexture = bakedTexture;
EditorUtility.SetDirty(probe);
// 7. Register baking information
var id = EditorUtility.GetSceneObjectIdentifierFor(probe);
var asset = GetOrCreateLightingDataAssetForScene(probe.gameObject.scene);
if (id != SceneObjectIdentifier.Null && asset != null)
{
asset.SetBakedTexture(id, bakedTexture);
EditorUtility.SetDirty(asset);
}
++job.m_StageIndex;
}

61
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/HDLightingDataAsset.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Assertions;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{

class ObjectIDPair
class ProbeInformation
public SceneObjectIdentifier Key;
public Object Value;
public SerializableSceneObjectIdentifier ID;
public Texture BakedTexture;
List<ObjectIDPair> m_ObjectList = new List<ObjectIDPair>();
List<ProbeInformation> m_ObjectList = new List<ProbeInformation>();
Dictionary<Object, SceneObjectIdentifier> m_ObjectIndex = new Dictionary<Object, SceneObjectIdentifier>();
Dictionary<SceneObjectIdentifier, Object> m_IDIndex = new Dictionary<SceneObjectIdentifier, Object>();
Dictionary<SceneObjectIdentifier, ProbeInformation> m_IndexByID = new Dictionary<SceneObjectIdentifier, ProbeInformation>();
public SceneObjectIdentifier GetIdFor(Object probe)
public void SetBakedTexture(SceneObjectIdentifier id, Texture bakedTexture)
if (m_ObjectIndex.ContainsKey(probe))
return m_ObjectIndex[probe];
Assert.AreNotEqual(SceneObjectIdentifier.Null, id);
var id = EditorUtility.GetSceneObjectIdentifierFor(probe);
if (id == SceneObjectIdentifier.Null)
{
Debug.LogWarningFormat("Could not get the scene object id for {0}", probe);
return SceneObjectIdentifier.Null;
}
if (!m_IndexByID.ContainsKey(id))
m_IndexByID[id] = new ProbeInformation { ID = id };
m_IndexByID[id].BakedTexture = bakedTexture;
}
m_ObjectList.Add(new ObjectIDPair
public void DeleteAssets()
{
foreach (var probeInformation in m_IndexByID)
Key = id,
Value = probe
});
if (probeInformation.Value.BakedTexture == null)
continue;
m_ObjectIndex[probe] = id;
m_IDIndex[id] = probe;
var path = AssetDatabase.GetAssetPath(probeInformation.Value.BakedTexture);
if (string.IsNullOrEmpty(path))
continue;
return id;
AssetDatabase.DeleteAsset(path);
}
for (var i = m_ObjectList.Count - 1; i >= 0; --i)
{
if (m_ObjectList[i].Value == null)
m_ObjectList.RemoveAt(i);
}
m_ObjectList.Clear();
m_ObjectList.AddRange(m_IndexByID.Select(s => s.Value));
{
if (m_IDIndex.ContainsKey(m_ObjectList[i].Key))
Debug.LogErrorFormat(this, "ID {0} is a duplicated in ReflectionSystemSceneDictionary ({1}) for {2}", m_ObjectList[i].Key, this, m_ObjectList[i].Value);
m_ObjectIndex[m_ObjectList[i].Value] = m_ObjectList[i].Key;
m_IDIndex[m_ObjectList[i].Key] = m_ObjectList[i].Value;
}
m_IndexByID.Add(m_ObjectList[i].ID, m_ObjectList[i]);
}
}
}
正在加载...
取消
保存