浏览代码

Merge pull request #25 from EvgeniiG/master

Fix and enable sky IBL
/main
GitHub 8 年前
当前提交
3fb12546
共有 14 个文件被更改,包括 96 次插入36 次删除
  1. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs
  2. 1
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  3. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs
  4. 5
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs.hlsl
  5. 1
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/Lighting.hlsl
  6. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePassLoop.hlsl
  7. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.hlsl
  8. 10
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/AtmosphericScattering.hlsl
  9. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/GGXConvolve.shader
  10. 40
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader
  11. 15
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs
  12. 19
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightUtilities.hlsl
  13. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightUtilities.hlsl.meta
  14. 8
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/README.txt.meta

6
Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs


EditorGUI.indentLevel++;
EditorGUI.BeginChangeCheck();
/*
skyParameters.skyHDRI = (Cubemap)EditorGUILayout.ObjectField("Cubemap", skyParameters.skyHDRI, typeof(Cubemap), false);
skyParameters.skyHDRI = (Texture)EditorGUILayout.ObjectField("Cubemap", skyParameters.skyHDRI, typeof(Cubemap), false);
*/
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(renderLoop); // Repaint

1
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


//Shader.SetGlobalTexture("_CookieTextures", m_CookieTexArray.GetTexCache());
//Shader.SetGlobalTexture("_CubeCookieTextures", m_CubeCookieTexArray.GetTexCache());
Shader.SetGlobalTexture("_EnvTextures", m_CubeReflTexArray.GetTexCache());
m_SkyRenderer.SetGlobalSkyTexture();
if (debugParameters.useSinglePassLightLoop)
{

9
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs


[GenerateHLSL]
public enum EnvShapeType
{
None,
Box,
Sphere
None,
Box,
Sphere,
Sky
};
[GenerateHLSL]

public EnvShapeType envShapeType;
public Vector3 forward;
public float envIndex;
public int envIndex;
public Vector3 up;
public float blendDistance; // blend transition outside the volume

5
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs.hlsl


#define ENVSHAPETYPE_NONE (0)
#define ENVSHAPETYPE_BOX (1)
#define ENVSHAPETYPE_SPHERE (2)
#define ENVSHAPETYPE_SKY (3)
//
// UnityEngine.Experimental.ScriptableRenderLoop.EnvConstants: static fields

float3 positionWS;
int envShapeType;
float3 forward;
float envIndex;
int envIndex;
float3 up;
float blendDistance;
float3 right;

{
return value.forward;
}
float GetEnvIndex(EnvLightData value)
int GetEnvIndex(EnvLightData value)
{
return value.envIndex;
}

1
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/Lighting.hlsl


#ifdef LIGHTLOOP_SINGLE_PASS
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePass.hlsl"
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightUtilities.hlsl"
#elif defined(LIGHTLOOP_TILE_PASS)
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/TilePass.hlsl"
#endif

3
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePassLoop.hlsl


iblSpecularLighting = lerp(iblSpecularLighting, localSpecularLighting, weight.y);
}
/*
InitSkyEnvLightData(0);
*/
diffuseLighting += iblDiffuseLighting;
specularLighting += iblSpecularLighting;

3
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.hlsl


float distFade = max(length(positionLS) - lightData.innerDistance.x, 0.0);
weight.y = saturate(1.0 - distFade / max(lightData.blendDistance, 0.0001)); // avoid divide by zero
}
else // ENVSHAPETYPE_BOX or ENVSHAPETYPE_NONE
else if (lightData.envShapeType == ENVSHAPETYPE_BOX ||
lightData.envShapeType == ENVSHAPETYPE_NONE)
{
// Calculate falloff value, so reflections on the edges of the volume would gradually blend to previous reflection.
float distFade = DistancePointBox(positionLS, -lightData.innerDistance, lightData.innerDistance);

10
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/AtmosphericScattering.hlsl


float4 baseUV = float4(uv.x, uv.y, 0.f, 0.f);
float cDepth = SAMPLE_TEXTURE2D_LOD(_CameraDepthTexture, SRL_BilinearSampler, baseUV, 0.f).r;
cDepth = LinearEyeDepth(cDepth);
cDepth = LinearEyeDepth(cDepth, _ZBufferParams);
float4 xDepth;
baseUV.xy = uv + _DepthTextureScaledTexelSize.zy; xDepth.x = SAMPLE_TEXTURE2D_LOD(_CameraDepthTexture, SRL_BilinearSampler, baseUV);

xDepth.x = LinearEyeDepth4(xDepth.x);
xDepth.y = LinearEyeDepth4(xDepth.y);
xDepth.z = LinearEyeDepth4(xDepth.z);
xDepth.w = LinearEyeDepth4(xDepth.w);
xDepth.x = LinearEyeDepth(xDepth.x, _ZBufferParams);
xDepth.y = LinearEyeDepth(xDepth.y, _ZBufferParams);
xDepth.z = LinearEyeDepth(xDepth.z, _ZBufferParams);
xDepth.w = LinearEyeDepth(xDepth.w, _ZBufferParams);
float4 diffDepth = xDepth - cDepth.rrrr;
float4 maskDepth = abs(diffDepth) < _OcclusionDepthThreshold;

3
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/GGXConvolve.shader


half4 Frag(Varyings input) : SV_Target
{
float3 N = input.eyeVector;
// Vector interpolation is not magnitude-preserving.
float3 N = normalize(input.eyeVector);
float3 V = N;
float perceptualRoughness = mipmapLevelToPerceptualRoughness(_Level);

40
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader


#pragma vertex Vert
#pragma fragment Frag
#pragma multi_compile _ ATMOSPHERICS_DEBUG
#pragma multi_compile _ ATMOSPHERICS_DEBUG
#pragma multi_compile _ PERFORM_SKY_OCCLUSION_TEST
#ifndef PERFORM_SKY_OCCLUSION_TEST
#define IS_RENDERING_SKY
#endif
#define IS_RENDERING_SKY
#include "AtmosphericScattering.hlsl"
TEXTURECUBE(_Cubemap);

{
// TODO: implement SV_vertexID full screen quad
Varyings output;
output.positionCS = float4(input.positionCS.xy, UNITY_RAW_FAR_CLIP_VALUE
, 1.0);
output.eyeVector = input.eyeVector;
output.positionCS = float4(input.positionCS.xy, UNITY_RAW_FAR_CLIP_VALUE, 1.0);
output.eyeVector = input.eyeVector;
return output;
}

float3 rotDirY = float3(sinPhi, 0, cosPhi);
dir = float3(dot(rotDirX, dir), dir.y, dot(rotDirY, dir));
float3 skyDome = ClampToFloat16Max(SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir, 0).rgb * exp2(_SkyParam.x) * _SkyParam.y);
// Get the depth value of the scene, or use 0.01 otherwise.
float rawDepth = max(LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).r, 0.01);
float3 positionWS = UnprojectToWorld(rawDepth, coord.positionSS, _InvViewProjMatrix);
// If the sky box is too far away (depth set to 0), the resulting look is too foggy.
const float skyDepth = 0.01;
// Do not perform blending with the environment map if the sky is occluded.
float skyDomeWeight = (rawDepth > 0.01) ? 0.0 : 1.0;
#ifdef PERFORM_SKY_OCCLUSION_TEST
// Determine whether the sky is occluded by the scene geometry.
// Do not perform blending with the environment map if the sky is occluded.
float rawDepth = max(skyDepth, LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).r);
float skyTexWeight = (rawDepth > skyDepth) ? 0.0 : 1.0;
#else
float rawDepth = skyDepth;
float skyTexWeight = 1.0;
#endif
float3 positionWS = UnprojectToWorld(rawDepth, coord.positionSS, _InvViewProjMatrix);
float4 c1, c2, c3;
VolundTransferScatter(positionWS, c1, c2, c3);

}
#endif
// Blend with the color of the scene.
return float4(skyDome * (skyDomeWeight * extinction) + scatter, 0.0);
float3 skyColor = ClampToFloat16Max(SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir, 0).rgb * exp2(_SkyParam.x) * _SkyParam.y);
// Apply extinction to the scene color when performing alpha-blending.
return float4(skyColor * (skyTexWeight * extinction) + scatter, extinction);
}
ENDHLSL

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


eyeVectorData[3] = new Vector3(direction0.x, direction0.y, direction0.z).normalized;
eyeVectorData[2] = new Vector3(direction1.x, direction1.y, direction1.z).normalized;
eyeVectorData[1] = new Vector3(direction2.x, direction2.y, direction2.z).normalized;
eyeVectorData[0] = new Vector3(direction3.x, direction3.y, direction3.z).normalized;
eyeVectorData[0] = new Vector3(direction3.x, direction3.y, direction3.z).normalized;
}
else
{

}
}
// Sets the global MIP-mapped cubemap '_SkyTexture' in the shader.
// The texture being set is the sky (environment) map pre-convolved with GGX.
public void SetGlobalSkyTexture()
{
Shader.SetGlobalTexture("_SkyTexture", m_SkyboxGGXCubemapRT);
}
public void Rebuild()
{
// TODO: We need to have an API to send our sky information to Enlighten. For now use a workaround through skybox/cubemap material...

new Vector3(0.0f, 0.0f, 1.0f),
new Vector3(0.0f, 0.0f, -1.0f),
};
Vector3[] UpVectorList = {
new Vector3(0.0f, 1.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f),

{
Mesh skyMesh = BuildSkyMesh(camera, forceUVBottom);
Shader.EnableKeyword("PERFORM_SKY_OCCLUSION_TEST");
m_RenderSkyPropertyBlock.SetTexture("_Cubemap", skyParameters.skyHDRI);
m_RenderSkyPropertyBlock.SetVector("_SkyParam", new Vector4(skyParameters.exposure, skyParameters.multiplier, skyParameters.rotation, 0.0f));
m_RenderSkyPropertyBlock.SetMatrix("_InvViewProjMatrix", Utilities.GetViewProjectionMatrix(camera).inverse);

private void RenderSkyToCubemap(SkyParameters skyParameters, RenderTexture target, RenderLoop renderLoop)
{
Shader.DisableKeyword("PERFORM_SKY_OCCLUSION_TEST");
for (int i = 0; i < 6; ++i)
{
Utilities.SetRenderTarget(renderLoop, target, 0, (CubemapFace)i);

19
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightUtilities.hlsl


#ifndef UNITY_LIGHT_UTILITIES_INCLUDED
#define UNITY_LIGHT_UTILITIES_INCLUDED
// The EnvLightData of the sky light contains a bunch of compile-time constants.
// This function sets them directly to allow the compiler to propagate them and optimize the code.
void InitSkyEnvLightData(int index)
{
_EnvLightSky.envShapeType = ENVSHAPETYPE_SKY;
_EnvLightSky.envIndex = index;
_EnvLightSky.forward = float3(0.0, 0.0, 1.0);
_EnvLightSky.up = float3(0.0, 1.0, 0.0);
_EnvLightSky.right = float3(1.0, 0.0, 0.0);
_EnvLightSky.positionWS = float3(0.0, 0.0, 0.0);
_EnvLightSky.offsetLS = float3(0.0, 0.0, 0.0);
_EnvLightSky.innerDistance = float3(0.0, 0.0, 0.0);
_EnvLightSky.blendDistance = 1.0;
}
#endif // UNITY_LIGHT_UTILITIES_INCLUDED

9
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightUtilities.hlsl.meta


fileFormatVersion: 2
guid: be2a44d9e57c5b943ab80fa5fc334d99
timeCreated: 1480423337
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

8
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/README.txt.meta


fileFormatVersion: 2
guid: 4eead1bf6efbd0a4e852367accddc070
timeCreated: 1480334378
licenseType: Pro
TextScriptImporter:
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存