浏览代码

Updated persistent ID generation

/feature-ReflectionProbeBaking
Frédéric Vauchelles 7 年前
当前提交
75a2ac52
共有 7 个文件被更改,包括 101 次插入107 次删除
  1. 13
      ScriptableRenderPipeline/Core/CoreRP/CoreUtils.cs
  2. 56
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/EditorReflectionSystem.cs
  3. 3
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/HDReflectionProbeEditorUtility.cs
  4. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/HDLightingDataAsset.cs.meta
  5. 67
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/HDLightingDataAsset.cs
  6. 67
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ReflectionSystemSceneDictionary.cs
  7. 0
      /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/HDLightingDataAsset.cs.meta

13
ScriptableRenderPipeline/Core/CoreRP/CoreUtils.cs


public static Texture2D CopyCubemapToTexture2D(RenderTexture source)
{
Assert.AreEqual(TextureDimension.Cube, source.dimension);
Assert.AreEqual(RenderTextureFormat.ARGBFloat, source.format);
TextureFormat format = TextureFormat.RGBAFloat;
switch (source.format)
{
case RenderTextureFormat.ARGBFloat: format = TextureFormat.RGBAFloat; break;
case RenderTextureFormat.ARGBHalf: format = TextureFormat.RGBAHalf; break;
default:
Assert.IsFalse(true, "Unmanaged format");
break;
}
var result = new Texture2D(resolution * 6, resolution, TextureFormat.RGBAFloat, false);
var result = new Texture2D(resolution * 6, resolution, format, false);
var offset = 0;
for (var i = 0; i < 6; ++i)

result.Apply();
offset += resolution;
}
Graphics.SetRenderTarget(null);
return result;
}

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


public static class EditorReflectionSystem
{
static int _Cubemap = Shader.PropertyToID("_Cubemap");
const HideFlags k_ReflectionSystemDictionaryHideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.DontSaveInBuild;
static PlanarReflectionProbeBaker s_PlanarReflectionProbeBaker = new PlanarReflectionProbeBaker();
static ReflectionProbeBaker s_ReflectionProbeBaker = new ReflectionProbeBaker();

static string GetBakePath(Component probe)
{
var id = GetComponentPersistentID(probe);
if (id == -1)
var id = EditorUtility.GetSceneObjectIdentifierFor(probe);
if (id == SceneObjectIdentifier.Null)
return string.Empty;
var scene = probe.gameObject.scene;

Directory.CreateDirectory(targetPath);
var fileName = probe.name + "-reflectionHDR.exr";
var fileName = string.Format("Probe {0}.exr", id);
return AssetDatabase.GenerateUniqueAssetPath(Path.Combine(targetPath, fileName));
}

return targetPath;
}
static int GetComponentPersistentID(Component probe)
static HDLightingDataAsset GetLightingDataAssetForScene(Scene scene)
var scene = probe.gameObject.scene;
if (!scene.IsValid())
return -1;
var reflectionDictionary = GetReflectionDictionaryFor(scene);
return reflectionDictionary.GetIdFor(probe);
}
static ReflectionSystemSceneDictionary GetReflectionDictionaryFor(Scene scene)
{
ReflectionSystemSceneDictionary result = null;
var roots = new List<GameObject>();
scene.GetRootGameObjects(roots);
for (var i = 0; i < roots.Count; i++)
{
result = roots[i].GetComponent<ReflectionSystemSceneDictionary>();
if (result != null)
break;
}
var filePath = GetLightingDataAssetPathForScene(scene);
var filePathInfo = new FileInfo(filePath);
if (!filePathInfo.Directory.Exists)
filePathInfo.Directory.Create();
if (result == null)
var asset = AssetDatabase.LoadAssetAtPath<HDLightingDataAsset>(filePath);
if (asset == null)
result = EditorUtility.CreateGameObjectWithHideFlags(
"Reflection System Dictionary",
k_ReflectionSystemDictionaryHideFlags,
typeof(ReflectionSystemSceneDictionary))
.GetComponent<ReflectionSystemSceneDictionary>();
SceneManager.MoveGameObjectToScene(result.gameObject, scene);
result.gameObject.SetActive(false);
asset = ScriptableObject.CreateInstance<HDLightingDataAsset>();
AssetDatabase.CreateAsset(asset, filePath);
result.gameObject.hideFlags = k_ReflectionSystemDictionaryHideFlags;
return asset;
}
return result;
static string GetLightingDataAssetPathForScene(Scene scene)
{
var parentFolder = Path.GetFileNameWithoutExtension(scene.path);
return Path.Combine(Path.GetDirectoryName(scene.path), Path.Combine(parentFolder, "HDLightingData.asset"));
}
static bool WriteAndImportTexture(string path, Texture2D target)

CoreUtils.Destroy(tex2D);
File.WriteAllBytes(assetPath, bytes);
textureImporter.SaveAndReimport();
AssetDatabase.ImportAsset(assetPath);
target.Release();
// 6. Assign texture

var bytes = ((Texture2D)bakedTexture).EncodeToEXR(Texture2D.EXRFlags.CompressZIP);
File.WriteAllBytes(assetPath, bytes);
textureImporter.SaveAndReimport();
AssetDatabase.ImportAsset(assetPath);
target.Release();
// 6. Assign texture

3
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/HDReflectionProbeEditorUtility.cs


|| material.shader != s_PreviewMaterial.shader)
{
material = Object.Instantiate(s_PreviewMaterial);
material.SetTexture(_Cubemap, p.texture);
material.SetTexture(_Cubemap, p.texture);
meshRenderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/HDLightingDataAsset.cs.meta


fileFormatVersion: 2
guid: deb5229fc0604de4cb529aedf764520f
guid: 212f0dcffca077f418e895da7c7b31e2
MonoImporter:
externalObjects: {}
serializedVersion: 2

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


using System;
using System.Collections.Generic;
using UnityEditor;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public class HDLightingDataAsset : ScriptableObject, ISerializationCallbackReceiver
{
[Serializable]
class ObjectIDPair
{
public SceneObjectIdentifier Key;
public Object Value;
}
[SerializeField]
List<ObjectIDPair> m_ObjectList = new List<ObjectIDPair>();
Dictionary<Object, SceneObjectIdentifier> m_ObjectIndex = new Dictionary<Object, SceneObjectIdentifier>();
Dictionary<SceneObjectIdentifier, Object> m_IDIndex = new Dictionary<SceneObjectIdentifier, Object>();
public SceneObjectIdentifier GetIdFor(Object probe)
{
if (m_ObjectIndex.ContainsKey(probe))
return m_ObjectIndex[probe];
var id = EditorUtility.GetSceneObjectIdentifierFor(probe);
if (id == SceneObjectIdentifier.Null)
{
Debug.LogWarningFormat("Could not get the scene object id for {0}", probe);
return SceneObjectIdentifier.Null;
}
m_ObjectList.Add(new ObjectIDPair
{
Key = id,
Value = probe
});
m_ObjectIndex[probe] = id;
m_IDIndex[id] = probe;
return id;
}
public void OnBeforeSerialize()
{
for (var i = m_ObjectList.Count - 1; i >= 0; --i)
{
if (m_ObjectList[i].Value == null)
m_ObjectList.RemoveAt(i);
}
}
public void OnAfterDeserialize()
{
for (var i = 0; i < m_ObjectList.Count; i++)
{
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;
}
}
}
}

67
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ReflectionSystemSceneDictionary.cs


using System;
using System.Collections.Generic;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public class ReflectionSystemSceneDictionary : MonoBehaviour, ISerializationCallbackReceiver
{
[Serializable]
class ObjectIDPair
{
public int Key;
public Object Value;
}
[SerializeField]
List<ObjectIDPair> m_ObjectList = new List<ObjectIDPair>();
Dictionary<Object, int> m_ObjectIndex = new Dictionary<Object, int>();
Dictionary<int, Object> m_IDIndex = new Dictionary<int, Object>();
public int GetIdFor(Object probe)
{
if (m_ObjectIndex.ContainsKey(probe))
return m_ObjectIndex[probe];
var id = FindNextId();
m_ObjectList.Add(new ObjectIDPair
{
Key = id,
Value = probe
});
m_ObjectIndex[probe] = id;
m_IDIndex[id] = probe;
return id;
}
public void OnBeforeSerialize()
{
for (var i = m_ObjectList.Count - 1; i >= 0; --i)
{
if (m_ObjectList[i].Value == null)
m_ObjectList.RemoveAt(i);
}
}
public void OnAfterDeserialize()
{
for (int i = 0; i < m_ObjectList.Count; i++)
{
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;
}
}
int FindNextId()
{
var id = 0;
while (m_IDIndex.ContainsKey(id)) ++id;
return id;
}
}
}

/ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ReflectionSystemSceneDictionary.cs.meta → /ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/HDLightingDataAsset.cs.meta

正在加载...
取消
保存