Skip to content

Commit 7714082

Browse files
Added the possibility to override lighting with a user specified cubemap (this will be provided to both enlighten and the default reflection probe)
1 parent 8aa0f18 commit 7714082

6 files changed

Lines changed: 105 additions & 2 deletions

File tree

Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/HDRISky/Editor/HDRISkyEditor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private class Styles
1919
public readonly GUIContent skyMultiplier = new GUIContent("Multiplier", "Intensity multiplier for the sky.");
2020
public readonly GUIContent environmentUpdateMode = new GUIContent("Environment Update Mode", "Specify how the environment lighting should be updated.");
2121
public readonly GUIContent environmentUpdatePeriod = new GUIContent("Environment Update Period", "If environment update is set to realtime, period in seconds at which it is updated (0.0 means every frame).");
22+
public readonly GUIContent lightingOverride = new GUIContent("Lighting Override", "If a lighting override cubemap is provided, this cubemap will be used to compute lighting instead of the result from the visible sky.");
2223
}
2324

2425
private static Styles s_Styles = null;
@@ -39,6 +40,7 @@ private static Styles styles
3940
private SerializedProperty m_SkyRotation;
4041
private SerializedProperty m_EnvUpdateMode;
4142
private SerializedProperty m_EnvUpdatePeriod;
43+
private SerializedProperty m_LightingOverride;
4244

4345
void OnEnable()
4446
{
@@ -49,6 +51,7 @@ void OnEnable()
4951
m_SkyRotation = serializedObject.FindProperty("rotation");
5052
m_EnvUpdateMode = serializedObject.FindProperty("updateMode");
5153
m_EnvUpdatePeriod = serializedObject.FindProperty("updatePeriod");
54+
m_LightingOverride = serializedObject.FindProperty("lightingOverride");
5255
}
5356

5457
public override void OnInspectorGUI()
@@ -66,6 +69,7 @@ public override void OnInspectorGUI()
6669
{
6770
EditorGUILayout.PropertyField(m_EnvUpdatePeriod, styles.environmentUpdatePeriod);
6871
}
72+
EditorGUILayout.PropertyField(m_LightingOverride, styles.lightingOverride);
6973

7074
serializedObject.ApplyModifiedProperties();
7175
}

Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/SkyManager.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class SkyManager
5151
RenderTexture m_SkyboxConditionalCdfRT = null;
5252

5353
Material m_StandardSkyboxMaterial = null; // This is the Unity standard skybox material. Used to pass the correct cubemap to Enlighten.
54+
Material m_BlitCubemapMaterial = null;
5455

5556
IBLFilterGGX m_iblFilterGgx = null;
5657

@@ -275,7 +276,9 @@ public void Build()
275276
m_iblFilterGgx = new IBLFilterGGX();
276277

277278
// TODO: We need to have an API to send our sky information to Enlighten. For now use a workaround through skybox/cubemap material...
278-
m_StandardSkyboxMaterial = Utilities.CreateEngineMaterial("Skybox/Cubemap");
279+
m_StandardSkyboxMaterial = Utilities.CreateEngineMaterial("Skybox/Cubemap");
280+
281+
m_BlitCubemapMaterial = Utilities.CreateEngineMaterial("Hidden/BlitCubemap");
279282

280283
m_CurrentUpdateTime = 0.0f;
281284
}
@@ -312,6 +315,24 @@ private void RenderSkyToCubemap(BuiltinSkyParameters builtinParams, SkySettings
312315
}
313316
}
314317

318+
private void BlitCubemap(ScriptableRenderContext renderContext, Cubemap source, RenderTexture dest)
319+
{
320+
321+
MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
322+
323+
for (int i = 0; i < 6; ++i)
324+
{
325+
Utilities.SetRenderTarget(renderContext, dest, ClearFlag.ClearNone, 0, (CubemapFace)i);
326+
var cmd = new CommandBuffer { name = "" };
327+
propertyBlock.SetTexture("_MainTex", source);
328+
propertyBlock.SetFloat("_faceIndex", (float)i);
329+
cmd.DrawProcedural(Matrix4x4.identity, m_BlitCubemapMaterial, 0, MeshTopology.Triangles, 3, 1, propertyBlock);
330+
renderContext.ExecuteCommandBuffer(cmd);
331+
cmd.Dispose();
332+
}
333+
334+
}
335+
315336
private void RenderCubemapGGXConvolution(ScriptableRenderContext renderContext, BuiltinSkyParameters builtinParams, SkySettings skyParams, Texture input, RenderTexture target)
316337
{
317338
using (new Utilities.ProfilingSample("Sky Pass: GGX Convolution", renderContext))
@@ -387,8 +408,12 @@ public void UpdateEnvironment(HDCamera camera, Light sunLight, ScriptableRenderC
387408
)
388409
{
389410
// Render sky into a cubemap - doesn't happen every frame, can be controlled
390-
RenderSkyToCubemap(m_BuiltinParameters, skySettings, m_SkyboxCubemapRT);
391411
// Note that m_SkyboxCubemapRT is created with auto-generate mipmap, it mean that here we have also our mipmap correctly box filtered for importance sampling.
412+
if(m_SkySettings.lightingOverride == null)
413+
RenderSkyToCubemap(m_BuiltinParameters, skySettings, m_SkyboxCubemapRT);
414+
// In case the user overrides the lighting, we already have a cubemap ready but we need to blit it anyway for potential resize and so that we can generate proper mipmaps for enlighten.
415+
else
416+
BlitCubemap(renderContext, m_SkySettings.lightingOverride, m_SkyboxCubemapRT);
392417

393418
// Convolve downsampled cubemap
394419
RenderCubemapGGXConvolution(renderContext, m_BuiltinParameters, skySettings, m_SkyboxCubemapRT, m_SkyboxGGXCubemapRT);

Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/SkySettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ protected class Unhashed : System.Attribute {}
1414
public SkyResolution resolution = SkyResolution.SkyResolution256;
1515
public EnvironementUpdateMode updateMode = EnvironementUpdateMode.OnChanged;
1616
public float updatePeriod = 0.0f;
17+
public Cubemap lightingOverride = null;
1718

1819
private FieldInfo[] m_Properties;
1920

Assets/ScriptableRenderPipeline/common/Resources.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Shader "Hidden/BlitCubemap" {
2+
SubShader {
3+
// Cubemap blit. Takes a face index.
4+
Pass {
5+
ZTest Always Cull Off ZWrite Off
6+
7+
HLSLPROGRAM
8+
#pragma vertex vert
9+
#pragma fragment frag
10+
#pragma target 4.5
11+
12+
#include "../../ShaderLibrary/Common.hlsl"
13+
14+
TEXTURECUBE(_MainTex);
15+
SAMPLERCUBE(sampler_MainTex);
16+
float _faceIndex;
17+
18+
struct appdata_t {
19+
uint vertexID : SV_VertexID;
20+
};
21+
22+
struct v2f {
23+
float4 vertex : SV_POSITION;
24+
float3 texcoord : TEXCOORD0;
25+
};
26+
27+
static const float3 faceU[6] = { float3(0, 0, -1), float3(0, 0, 1), float3(1, 0, 0), float3(1, 0, 0), float3(1, 0, 0), float3(-1, 0, 0) };
28+
static const float3 faceV[6] = { float3(0, -1, 0), float3(0, -1, 0), float3(0, 0, 1), float3(0, 0, -1), float3(0, -1, 0), float3(0, -1, 0) };
29+
30+
v2f vert (appdata_t v)
31+
{
32+
v2f o;
33+
34+
o.vertex = GetFullScreenTriangleVertexPosition(v.vertexID);
35+
float2 uv = GetFullScreenTriangleTexcoord(v.vertexID) * 2.0 - 1.0;
36+
37+
int idx = (int)_faceIndex;
38+
float3 transformU = faceU[idx];
39+
float3 transformV = faceV[idx];
40+
41+
float3 n = cross(transformV, transformU);
42+
o.texcoord = n + uv.x * transformU + uv.y * transformV;
43+
return o;
44+
}
45+
46+
float4 frag (v2f i) : SV_Target
47+
{
48+
return SAMPLE_TEXTURECUBE(_MainTex, sampler_MainTex, i.texcoord);
49+
}
50+
ENDHLSL
51+
52+
}
53+
}
54+
Fallback Off
55+
}

Assets/ScriptableRenderPipeline/common/Resources/BlitCubemap.shader.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)