浏览代码

Added Type checks to make setting parameters for a sky more robust.

/main
Julien Ignace 8 年前
当前提交
eaf661ba
共有 6 个文件被更改,包括 144 次插入131 次删除
  1. 17
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 28
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/HDRISky/HDRISkyRenderer.cs
  3. 35
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/ProceduralSky/ProceduralSkyRenderer.cs
  4. 68
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyManager.cs
  5. 104
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyParameters.cs
  6. 23
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs

17
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


}
#endif
SkyManager m_SkyManager = null;
SkyParameters m_SkyParameters = null;
SkyManager m_SkyManager = new SkyManager();
set { m_SkyParameters = value; }
get { return m_SkyParameters; }
set { m_SkyManager.skyParameters = value; }
get { return m_SkyManager.skyParameters; }
}
public class DebugParameters

m_CameraColorBufferRT = new RenderTargetIdentifier(m_CameraColorBuffer);
m_CameraDepthBufferRT = new RenderTargetIdentifier(m_CameraDepthBuffer);
m_SkyManager = new SkyManager();
if(m_SkyManager == null)
m_SkyManager = new SkyManager();
m_SkyManager.Build();
m_FinalPassMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/FinalPass");

void RenderSky(Camera camera, RenderLoop renderLoop)
{
m_SkyManager.RenderSky(camera, m_lightLoop.GetCurrentSunLight(), m_SkyParameters, m_CameraColorBufferRT, m_CameraDepthBufferRT, renderLoop);
m_SkyManager.RenderSky(camera, m_lightLoop.GetCurrentSunLight(), m_CameraColorBufferRT, m_CameraDepthBufferRT, renderLoop);
}
void RenderForward(CullResults cullResults, Camera camera, RenderLoop renderLoop, bool renderOpaque)

// TODO: This is the wrong way to handle resize/allocation. We can have several different camera here, mean that the loop on camera will allocate and deallocate
// the below buffer which is bad. Best is to have a set of buffer for each camera that is persistent and reallocate resource if need
// For now consider we have only one camera that go to this code, the main one.
m_SkyManager.Resize(m_SkyParameters); // TODO: Also a bad naming, here we just want to realloc texture if skyparameters change (usefull for lookdev)
m_SkyManager.Resize(); // TODO: Also a bad naming, here we just want to realloc texture if skyparameters change (usefull for lookdev)
if (camera.pixelWidth != m_currentWidth || camera.pixelHeight != m_currentHeight || m_lightLoop.NeedResize())
{

public void PushGlobalParams(Camera camera, RenderLoop renderLoop)
{
if (m_SkyManager.IsSkyValid(m_SkyParameters))
if (m_SkyManager.IsSkyValid())
{
m_SkyManager.SetGlobalSkyTexture();
Shader.SetGlobalInt("_EnvLightSkyEnabled", 1);

28
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/HDRISky/HDRISkyRenderer.cs


namespace UnityEngine.Experimental.ScriptableRenderLoop
{
public class HDRISkyRenderer
: SkyRenderer
: SkyRenderer<HDRISkyParameters>
{
Material m_SkyHDRIMaterial = null; // Renders a cubemap into a render texture (can be cube or 2D)

override public void Cleanup()
{
Utilities.Destroy(m_SkyHDRIMaterial);
}
HDRISkyParameters GetHDRISkyParameters(SkyParameters parameters)
{
HDRISkyParameters hdriSkyParams = parameters as HDRISkyParameters;
if (hdriSkyParams == null)
{
Debug.LogWarning("HDRISkyRenderer needs an instance of HDRISkyParameters to be able to render.");
return null;
}
return hdriSkyParams;
HDRISkyParameters hdriSkyParams = GetHDRISkyParameters(skyParameters);
if (hdriSkyParams == null)
{
return false;
}
return hdriSkyParams.skyHDRI != null;
return GetParameters(skyParameters).skyHDRI != null;
HDRISkyParameters hdriSkyParams = GetHDRISkyParameters(skyParameters);
if(hdriSkyParams == null)
{
return;
}
HDRISkyParameters hdriSkyParams = GetParameters(skyParameters);
m_SkyHDRIMaterial.SetTexture("_Cubemap", hdriSkyParams.skyHDRI);
m_SkyHDRIMaterial.SetVector("_SkyParam", new Vector4(hdriSkyParams.exposure, hdriSkyParams.multiplier, hdriSkyParams.rotation, 0.0f));

35
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/ProceduralSky/ProceduralSkyRenderer.cs


namespace UnityEngine.Experimental.ScriptableRenderLoop
{
public class ProceduralSkyRenderer
: SkyRenderer
: SkyRenderer<ProceduralSkyParameters>
Material m_ProceduralSkyMaterial = null; // Renders a cubemap into a render texture (can be cube or 2D)
Gradient m_DefaultWorldRayleighColorRamp = null;
Gradient m_DefaultWorldMieColorRamp = null;
Material m_ProceduralSkyMaterial = null; // Renders a cubemap into a render texture (can be cube or 2D)
Gradient m_DefaultWorldRayleighColorRamp = null;
Gradient m_DefaultWorldMieColorRamp = null;
override public void Build()
{

{
Utilities.Destroy(m_ProceduralSkyMaterial);
}
ProceduralSkyParameters GetProceduralSkyParameters(SkyParameters parameters)
{
ProceduralSkyParameters proceduralSkyParameters = parameters as ProceduralSkyParameters;
if (proceduralSkyParameters == null)
{
Debug.LogWarning("ProceduralSkyRenderer needs an instance of ProceduralSkyParameters to be able to render.");
return null;
}
return proceduralSkyParameters;
}
ProceduralSkyParameters proceduralSkyParams = GetProceduralSkyParameters(skyParameters);
if (proceduralSkyParams == null)
{
return false;
}
//ProceduralSkyParameters proceduralSkyParams = GetParameters(skyParameters);
return true; // TODO: See with Evgenii what makes it valid or invalid.
}
return true; // TODO: How is it valid?
}
void UpdateKeywords(bool enable, ProceduralSkyParameters param)
{
m_ProceduralSkyMaterial.DisableKeyword("ATMOSPHERICS_OCCLUSION");

override public void RenderSky(BuiltinSkyParameters builtinParams, SkyParameters skyParameters)
{
ProceduralSkyParameters proceduralSkyParams = GetProceduralSkyParameters(skyParameters);
if (proceduralSkyParams == null)
{
return;
}
ProceduralSkyParameters proceduralSkyParams = GetParameters(skyParameters);
// Define select preprocessor symbols.
UpdateKeywords(true, proceduralSkyParams);

68
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyManager.cs


int m_SkyParametersHash = 0;
bool m_NeedUpdateEnvironment = false;
SkyParameters m_SkyParameters = null;
public SkyParameters skyParameters
{
set
{
if (value == null || IsSkyParameterValid(value))
{
m_SkyParameters = value;
}
else
{
Debug.LogWarning("Sky renderer needs an instance of " + GetSkyParameterType().ToString() + " to be able to render.");
}
}
get { return m_SkyParameters; }
}
protected Mesh BuildSkyMesh(Vector3 cameraPosition, Matrix4x4 cameraInvViewProjectionMatrix, bool forceUVBottom)
{
Vector4 vertData0 = new Vector4(-1.0f, -1.0f, 1.0f, 1.0f);

Shader.SetGlobalTexture("_SkyTexture", m_SkyboxGGXCubemapRT);
}
public void Resize(SkyParameters skyParameters)
public void Resize()
{
// When loading RenderDoc, RenderTextures will go null
RebuildTextures(skyParameters);

m_Renderer.Cleanup();
}
public bool IsSkyValid(SkyParameters parameters)
public bool IsSkyValid()
return m_Renderer != null && m_Renderer.IsSkyValid(parameters);
return m_Renderer != null && m_Renderer.IsParameterValid(skyParameters) && m_Renderer.IsSkyValid(skyParameters);
}
private void RenderSkyToCubemap(BuiltinSkyParameters builtinParams, SkyParameters skyParameters, RenderTexture target)

}
}
public void RenderSky(Camera camera, Light sunLight, SkyParameters skyParameters, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, RenderLoop renderLoop)
public bool IsSkyParameterValid(SkyParameters parameters)
{
return m_Renderer.IsParameterValid(parameters);
}
public Type GetSkyParameterType()
{
return m_Renderer.GetSkyParameterType();
}
public void RenderSky(Camera camera, Light sunLight, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, RenderLoop renderLoop)
if (IsSkyValid(skyParameters))
if (IsSkyValid())
{
m_BuiltinParameters.renderLoop = renderLoop;
m_BuiltinParameters.sunLight = sunLight;

}
else
{
if(m_SkyParametersHash != 0)
{
// Clear sky light probe
RenderSettings.skybox = null;
RenderSettings.ambientIntensity = 1.0f; // fix this to 1, this parameter should not exist!
RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Skybox; // Force skybox for our HDRI
RenderSettings.reflectionIntensity = 1.0f;
RenderSettings.customReflection = null;
DynamicGI.UpdateEnvironment();
// Disabled for now.
// We need to remove RenderSkyToCubemap from the RenderCubemapGGXConvolution first as it needs the skyparameter to be valid.
//if(m_SkyParametersHash != 0)
//{
// // Clear sky light probe
// RenderSettings.skybox = null;
// RenderSettings.ambientIntensity = 1.0f; // fix this to 1, this parameter should not exist!
// RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Skybox; // Force skybox for our HDRI
// RenderSettings.reflectionIntensity = 1.0f;
// RenderSettings.customReflection = null;
// DynamicGI.UpdateEnvironment();
// Clear temp cubemap and redo GGX from black
Utilities.SetRenderTarget(renderLoop, m_SkyboxCubemapRT, ClearFlag.ClearColor);
RenderCubemapGGXConvolution(renderLoop, m_BuiltinParameters, skyParameters, m_SkyboxCubemapRT, m_SkyboxGGXCubemapRT);
// // Clear temp cubemap and redo GGX from black
// Utilities.SetRenderTarget(renderLoop, m_SkyboxCubemapRT, ClearFlag.ClearColor);
// RenderCubemapGGXConvolution(renderLoop, m_BuiltinParameters, skyParameters, m_SkyboxCubemapRT, m_SkyboxGGXCubemapRT);
m_SkyParametersHash = 0;
}
// m_SkyParametersHash = 0;
//}
}
}
}

104
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyParameters.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.ScriptableRenderLoop;
[ExecuteInEditMode]
public class SkyParameters : MonoBehaviour
namespace UnityEngine.Experimental.ScriptableRenderLoop
public float rotation = 0.0f;
public float exposure = 0.0f;
public float multiplier = 1.0f;
public SkyResolution resolution = SkyResolution.SkyResolution256;
[ExecuteInEditMode]
public class SkyParameters : MonoBehaviour
{
public float rotation = 0.0f;
public float exposure = 0.0f;
public float multiplier = 1.0f;
public SkyResolution resolution = SkyResolution.SkyResolution256;
private FieldInfo[] m_Properties;
private FieldInfo[] m_Properties;
HDRenderLoop GetHDRenderLoop()
{
HDRenderLoop renderLoop = UnityEngine.Rendering.GraphicsSettings.GetRenderPipeline() as HDRenderLoop;
if (renderLoop == null)
HDRenderLoop GetHDRenderLoop()
Debug.LogWarning("SkyParameters component can only be used with HDRenderLoop custom RenderPipeline.");
return null;
}
HDRenderLoop renderLoop = UnityEngine.Rendering.GraphicsSettings.GetRenderPipeline() as HDRenderLoop;
if (renderLoop == null)
{
Debug.LogWarning("SkyParameters component can only be used with HDRenderLoop custom RenderPipeline.");
return null;
}
return renderLoop;
}
return renderLoop;
}
protected void OnEnable()
{
HDRenderLoop renderLoop = GetHDRenderLoop();
if(renderLoop == null)
protected void OnEnable()
return;
}
HDRenderLoop renderLoop = GetHDRenderLoop();
if (renderLoop == null)
{
return;
}
// Enumerate properties in order to compute the hash more quickly later on.
m_Properties = GetType()
.GetFields(BindingFlags.Public | BindingFlags.Instance)
.ToArray();
// Enumerate properties in order to compute the hash more quickly later on.
m_Properties = GetType()
.GetFields(BindingFlags.Public | BindingFlags.Instance)
.ToArray();
if (renderLoop.skyParameters == null)
renderLoop.skyParameters = this;
else if(renderLoop.skyParameters != this)
Debug.LogWarning("Tried to setup another SkyParameters component although there is already one enabled.");
}
if (renderLoop.skyParameters == null)
renderLoop.skyParameters = this;
else if (renderLoop.skyParameters != this)
Debug.LogWarning("Tried to setup another SkyParameters component although there is already one enabled.");
}
protected void OnDisable()
{
HDRenderLoop renderLoop = GetHDRenderLoop();
if (renderLoop == null)
protected void OnDisable()
return;
}
HDRenderLoop renderLoop = GetHDRenderLoop();
if (renderLoop == null)
{
return;
}
if (renderLoop.skyParameters == this)
renderLoop.skyParameters = null;
}
// Reset the current sky parameter on the render loop
if (renderLoop.skyParameters == this)
renderLoop.skyParameters = null;
}
public int GetHash()
{
unchecked
public int GetHash()
int hash = 13;
foreach (var p in m_Properties)
unchecked
System.Object obj = p.GetValue(this);
if(obj != null) // Sometimes it can be a null reference.
hash = hash * 23 + obj.GetHashCode();
int hash = 13;
foreach (var p in m_Properties)
{
System.Object obj = p.GetValue(this);
if (obj != null) // Sometimes it can be a null reference.
hash = hash * 23 + obj.GetHashCode();
}
return hash;
return hash;
}
}
}

23
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs


namespace UnityEngine.Experimental.ScriptableRenderLoop
{
abstract public class SkyRenderer
{
abstract public void Build();

virtual public bool IsParameterValid(SkyParameters skyParameters) { return false; }
virtual public Type GetSkyParameterType() { return typeof(SkyParameters); }
}
abstract public class SkyRenderer<ParameterType> : SkyRenderer
where ParameterType : SkyParameters
{
override public bool IsParameterValid(SkyParameters skyParameters)
{
return GetParameters(skyParameters) != null;
}
override public Type GetSkyParameterType()
{
return typeof(ParameterType);
}
protected ParameterType GetParameters(SkyParameters parameters)
{
return parameters as ParameterType;
}
}
}
正在加载...
取消
保存