浏览代码

[PlanarReflection] Update planar sampling

/main
Frédéric Vauchelles 7 年前
当前提交
6611be52
共有 14 个文件被更改,包括 154 次插入41 次删除
  1. 64
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/EditorReflectionSystem.cs
  2. 13
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/PlanarReflectionProbeUI.Drawers.cs
  3. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/RenderLoopSettings/SerializedGlobalLightLoopSettings.cs
  4. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  5. 12
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipelineAsset.asset
  6. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipelineAsset.cs
  7. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
  8. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/GlobalLightLoopSettings.cs
  9. 13
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs
  10. 37
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoopDef.hlsl
  11. 10
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/PlanarReflectionProbe.cs
  12. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/ProbeWrapper.cs
  13. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumes/InfluenceVolume.cs
  14. 15
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl

64
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/EditorReflectionSystem.cs


using System.Reflection;
using UnityEngine;
using UnityEngine.Experimental.Rendering.HDPipeline;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;

RenderTexture.active = a;
rt.Release();
var bytes = target.EncodeToEXR();
try
{
var targetFile = new FileInfo(path);
if (!targetFile.Directory.Exists)
targetFile.Directory.Create();
File.WriteAllBytes(path, bytes);
}
catch (Exception e)
{
Console.WriteLine(e);
if (!WriteAndImportTexture(path, target))
}
return true;
}

if (string.IsNullOrEmpty(assetPath))
assetPath = GetBakePath(probe);
var importAsset = false;
importAsset = true;
if (importAsset)
AssetDatabase.CreateAsset(bakedTexture, assetPath);
else
{
var bytes = bakedTexture.EncodeToEXR();
File.WriteAllBytes(assetPath, bytes);
AssetDatabase.ImportAsset(assetPath);
}
WriteAndImportTexture(assetPath, bakedTexture);
return true;
}

result.gameObject.hideFlags = k_ReflectionSystemDictionaryHideFlags;
return result;
}
static bool WriteAndImportTexture(string path, Texture2D target)
{
var bytes = target.EncodeToEXR();
try
{
var targetFile = new FileInfo(path);
if (!targetFile.Directory.Exists)
targetFile.Directory.Create();
File.WriteAllBytes(path, bytes);
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
AssetDatabase.ImportAsset(path);
var importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer != null)
{
importer.alphaSource = TextureImporterAlphaSource.None;
importer.sRGBTexture = false;
importer.mipmapEnabled = false;
var hdrp = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset;
if (hdrp != null)
{
importer.textureCompression = hdrp.renderPipelineSettings.lightLoopSettings.planarReflectionCacheCompressed
? TextureImporterCompression.Compressed
: TextureImporterCompression.Uncompressed;
}
importer.SaveAndReimport();
}
return true;
}
}
}

13
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/PlanarReflectionProbeUI.Drawers.cs


using System;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.Rendering.HDPipeline;
using UnityEngine.Rendering;
namespace UnityEditor.Experimental.Rendering.HDPipeline

static void Drawer_SectionCaptureSettings(PlanarReflectionProbeUI s, SerializedPlanarReflectionProbe d, Editor o)
{
var hdrp = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset;
GUI.enabled = false;
EditorGUILayout.LabelField(
_.GetContent("Probe Texture Size (Set By HDRP)"),
_.GetContent(hdrp.renderPipelineSettings.lightLoopSettings.planarReflectionTextureSize.ToString()),
EditorStyles.label);
EditorGUILayout.Toggle(
_.GetContent("Probe Compression (Set By HDRP)"),
hdrp.renderPipelineSettings.lightLoopSettings.planarReflectionCacheCompressed);
GUI.enabled = true;
EditorGUILayout.PropertyField(d.captureLocalPosition, _.GetContent("Capture Local Position"));
_.DrawMultipleFields(

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/RenderLoopSettings/SerializedGlobalLightLoopSettings.cs


reflectionCacheCompressed = root.Find((GlobalLightLoopSettings s) => s.reflectionCacheCompressed);
planarReflectionProbeCacheSize = root.Find((GlobalLightLoopSettings s) => s.planarReflectionProbeCacheSize);
planarReflectionCubemapSize = root.Find((GlobalLightLoopSettings s) => s.planarReflectionCubemapSize);
planarReflectionCubemapSize = root.Find((GlobalLightLoopSettings s) => s.planarReflectionTextureSize);
planarReflectionCacheCompressed = root.Find((GlobalLightLoopSettings s) => s.planarReflectionCacheCompressed);
skyReflectionSize = root.Find((GlobalLightLoopSettings s) => s.skyReflectionSize);

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


m_GPUCopy = new GPUCopy(asset.renderPipelineResources.copyChannelCS);
EncodeBC6H.DefaultInstance = EncodeBC6H.DefaultInstance ?? new EncodeBC6H(asset.renderPipelineResources.encodeBC6HCS);
m_ReflectionProbeCullResults = new ReflectionProbeCullResults(ReflectionSystemParameters.Default);
ReflectionSystem.SetParameters(ReflectionSystemParameters.Default);
m_ReflectionProbeCullResults = new ReflectionProbeCullResults(asset.reflectionSystemParameters);
ReflectionSystem.SetParameters(asset.reflectionSystemParameters);
// Scan material list and assign it
m_MaterialList = HDUtils.GetRenderPipelineMaterialList();

12
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipelineAsset.asset


m_EditorClassIdentifier:
m_RenderPipelineResources: {fileID: 11400000, guid: 3ce144cff5783da45aa5d4fdc2da14b7,
type: 2}
serializedFrameSettings:
m_FrameSettings:
enableSSSAndTransmission: 1
enableSubsurfaceScattering: 1
enableTransmission: 1
diffuseGlobalDimmer: 1
specularGlobalDimmer: 1
enableForwardRenderingOnly: 0

supportShadowMask: 1
supportSSR: 1
supportSSAO: 1
supportSSSAndTransmission: 1
supportSubsurfaceScattering: 1
supportAsyncCompute: 0
supportAsyncCompute: 1
lightLoopSettings:
spotCookieSize: 128
cookieTexArraySize: 16

planarReflectionProbeCacheSize: 128
planarReflectionTextureSize: 512
planarReflectionCacheCompressed: 0
skyReflectionSize: 256
skyLightingOverrideLayerMask:
serializedVersion: 2

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipelineAsset.cs


return m_FrameSettingsRuntime;
}
public ReflectionSystemParameters reflectionSystemParameters
{
get
{
return new ReflectionSystemParameters
{
maxPlanarReflectionProbes = 512,
planarReflectionProbeSize = renderPipelineSettings.lightLoopSettings.planarReflectionTextureSize
};
} }
// Store the various RenderPipelineSettings for each platform (for now only one)
public RenderPipelineSettings renderPipelineSettings = new RenderPipelineSettings();

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs


namespace UnityEngine.Experimental.Rendering.HDPipeline
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public static class HDShaderPassNames
{

public static readonly int _CookieCubeTextures = Shader.PropertyToID("_CookieCubeTextures");
public static readonly int _EnvCubemapTextures = Shader.PropertyToID("_EnvCubemapTextures");
public static readonly int _Env2DTextures = Shader.PropertyToID("_Env2DTextures");
public static readonly int _Env2DCapturePositionWS = Shader.PropertyToID("_Env2DCapturePositionWS");
public static readonly int _Env2DCaptureVP = Shader.PropertyToID("_Env2DCaptureVP");
public static readonly int _DirectionalLightDatas = Shader.PropertyToID("_DirectionalLightDatas");
public static readonly int _DirectionalLightCount = Shader.PropertyToID("_DirectionalLightCount");
public static readonly int _LightDatas = Shader.PropertyToID("_LightDatas");

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/GlobalLightLoopSettings.cs


public int reflectionProbeCacheSize = 128;
public int planarReflectionProbeCacheSize = 128;
public int reflectionCubemapSize = 128;
public int planarReflectionCubemapSize = 128;
public int planarReflectionTextureSize = 128;
public bool reflectionCacheCompressed = false;
public bool planarReflectionCacheCompressed = false;
public SkyResolution skyReflectionSize = SkyResolution.SkyResolution256;

13
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs


ReflectionProbeCache m_ReflectionProbeCache;
TextureCache2D m_CookieTexArray;
TextureCacheCubemap m_CubeCookieTexArray;
List<Vector4> m_Env2DCapturePositionWS = new List<Vector4>();
List<Matrix4x4> m_Env2DCaptureVP = new List<Matrix4x4>();
public class LightList
{

m_ReflectionProbeCache = new ReflectionProbeCache(iblFilterGGX, gLightLoopSettings.reflectionProbeCacheSize, gLightLoopSettings.reflectionCubemapSize, probeCacheFormat, true);
TextureFormat planarProbeCacheFormat = gLightLoopSettings.planarReflectionCacheCompressed ? TextureFormat.BC6H : TextureFormat.RGBAHalf;
m_ReflectionPlanarProbeCache = new PlanarReflectionProbeCache(iblFilterGGX, gLightLoopSettings.planarReflectionProbeCacheSize, gLightLoopSettings.planarReflectionCubemapSize, planarProbeCacheFormat, true);
m_ReflectionPlanarProbeCache = new PlanarReflectionProbeCache(iblFilterGGX, gLightLoopSettings.planarReflectionProbeCacheSize, gLightLoopSettings.planarReflectionTextureSize, planarProbeCacheFormat, true);
s_GenAABBKernel = buildScreenAABBShader.FindKernel("ScreenBoundsAABB");

case TextureDimension.Tex2D:
envIndex = m_ReflectionPlanarProbeCache.FetchSlice(cmd, probe.texture);
envIndex = envIndex << 1 | (int)EnvCacheType.Texture2D;
m_Env2DCapturePositionWS.Add(probe.capturePosition);
m_Env2DCaptureVP.Add(probe.capture2DVP);
break;
case TextureDimension.Cube:
envIndex = m_ReflectionProbeCache.FetchSlice(cmd, probe.texture);

// If any light require it, we need to enabled bake shadow mask feature
m_enableBakeShadowMask = false;
m_Env2DCaptureVP.Clear();
m_Env2DCapturePositionWS.Clear();
m_lightList.Clear();
Vector3 camPosWS = camera.transform.position;

cmd.SetGlobalTexture(HDShaderIDs._CookieCubeTextures, m_CubeCookieTexArray.GetTexCache());
cmd.SetGlobalTexture(HDShaderIDs._EnvCubemapTextures, m_ReflectionProbeCache.GetTexCache());
cmd.SetGlobalTexture(HDShaderIDs._Env2DTextures, m_ReflectionPlanarProbeCache.GetTexCache());
if (m_Env2DCaptureVP.Count > 0)
{
cmd.SetGlobalMatrixArray(HDShaderIDs._Env2DCaptureVP, m_Env2DCaptureVP);
cmd.SetGlobalVectorArray(HDShaderIDs._Env2DCapturePositionWS, m_Env2DCapturePositionWS);
}
cmd.SetGlobalBuffer(HDShaderIDs._DirectionalLightDatas, s_DirectionalLightDatas);
cmd.SetGlobalInt(HDShaderIDs._DirectionalLightCount, m_lightList.directionalLights.Count);

37
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoopDef.hlsl


#include "LightLoop.cs.hlsl"
#include "LightLoop.cs.hlsl"
#define MAX_ENV2D_LIGHT 32
CBUFFER_START(UnityTilePass)
uint _NumTileFtplX;

// Use texture array for reflection (or LatLong 2D array for mobile)
TEXTURECUBE_ARRAY_ABSTRACT(_EnvCubemapTextures);
TEXTURE2D_ARRAY(_Env2DTextures);
float3 _Env2DCapturePositionWS[MAX_ENV2D_LIGHT];
float4x4 _Env2DCaptureVP[MAX_ENV2D_LIGHT];
TEXTURE2D(_DeferredShadowTexture);

if (lightLoopContext.sampleReflection == SINGLE_PASS_CONTEXT_SAMPLE_REFLECTION_PROBES)
{
if (cacheType == ENVCACHETYPE_TEXTURE2D)
return SAMPLE_TEXTURE2D_ARRAY_LOD(_Env2DTextures, s_trilinear_clamp_sampler, texCoord.xy, index, lod);
return SAMPLE_TEXTURE2D_ARRAY_LOD(_Env2DTextures, s_trilinear_clamp_sampler, texCoord.xy, index, 0);
else if (cacheType == ENVCACHETYPE_CUBEMAP)
return SAMPLE_TEXTURECUBE_ARRAY_LOD_ABSTRACT(_EnvCubemapTextures, s_trilinear_clamp_sampler, texCoord, index, lod);
return float4(0, 0, 0, 0);

return SampleSkyTexture(texCoord, lod);
}
}
float3 GetSampleEnvCoordinates(LightLoopContext lightLoopContext, int index, float3 texCoord, float lod, out float outWeight)
{
// 31 bit index, 1 bit cache type
uint cacheType = index & 1;
index = index >> 1;
// This code will be inlined as lightLoopContext is hardcoded in the light loop
if (lightLoopContext.sampleReflection == SINGLE_PASS_CONTEXT_SAMPLE_REFLECTION_PROBES)
{
if (cacheType == ENVCACHETYPE_TEXTURE2D)
{
float2 positionNCD = ComputeNormalizedDeviceCoordinates(_Env2DCapturePositionWS[index] + texCoord, _Env2DCaptureVP[index]);
outWeight = any(positionNCD > 1) || any(positionNCD < 0) ? 0 : 1;
return float3(positionNCD, 1);
}
else if (cacheType == ENVCACHETYPE_CUBEMAP)
{
outWeight = 1;
return texCoord;
}
outWeight = 0;
return float4(0, 0, 0, 0);
}
else // SINGLE_PASS_SAMPLE_SKY
{
outWeight = 1;
return texCoord;
}
}

10
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/PlanarReflectionProbe.cs


}
public Bounds bounds { get { return m_InfluenceVolume.GetBoundsAt(transform); } }
public Vector3 captureLocalPosition { get { return m_CaptureLocalPosition; } set { m_CaptureLocalPosition = value; } }
public Matrix4x4 capture2DVP
{
get
{
var fov = ReflectionSystem.GetCaptureCameraFOVFor(this);
var proj = Matrix4x4.Perspective(fov, 1, captureNearPlane, captureFarPlane);
var view = Matrix4x4.TRS(capturePosition, captureRotation, Vector3.one);
return proj * view;
}
}
public float dimmer { get { return m_Dimmer; } }
public ReflectionProbeMode mode { get { return m_Mode; } }
public Vector3 influenceRight { get { return transform.right; } }

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/ProbeWrapper.cs


public abstract Vector3 proxyUp { get; }
public abstract Vector3 proxyForward { get; }
public abstract Vector3 proxyPosition { get; }
public abstract Matrix4x4 capture2DVP { get; }
}
class VisibleReflectionProbeWrapper : ProbeWrapper

public override Vector3 influenceUp { get { return probe.localToWorld.GetColumn(1).normalized; } }
public override Vector3 influenceForward { get { return probe.localToWorld.GetColumn(2).normalized; } }
public override Vector3 capturePosition { get { return probe.localToWorld.GetColumn(3); } }
public override Matrix4x4 capture2DVP { get { return Matrix4x4.identity; } }
public override Vector3 influencePosition { get { return capturePosition + probe.center; } }
public override Texture texture { get { return probe.texture; } }
public override ReflectionProbeMode mode { get { return probe.probe.mode; } }

public override Vector3 influenceUp { get { return probe.influenceUp; } }
public override Vector3 influenceForward { get { return probe.influenceForward; } }
public override Vector3 capturePosition { get { return probe.capturePosition; } }
public override Matrix4x4 capture2DVP { get { return probe.capture2DVP; } }
public override Texture texture { get { return probe.texture; } }
public override EnvShapeType influenceShapeType { get { return ConvertShape(probe.influenceVolume.shapeType); } }
public override float dimmer { get { return probe.dimmer; } }

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumes/InfluenceVolume.cs


[SerializeField]
Vector3 m_BoxInfluenceNormalNegativeFade;
[SerializeField]
Vector3 m_BoxPositiveFaceFade;
Vector3 m_BoxPositiveFaceFade = Vector3.one;
Vector3 m_BoxNegativeFaceFade;
Vector3 m_BoxNegativeFaceFade = Vector3.one;
// Sphere
[SerializeField]

15
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl


else if (influenceShapeType == ENVSHAPETYPE_BOX)
weight = InfluenceBoxWeight(lightData, bsdfData, positionWS, positionLS, dirLS);
// Smooth weighting
weight = Smoothstep01(weight);
// When we are rough, we tend to see outward shifting of the reflection when at the boundary of the projection volume
// Also it appear like more sharp. To avoid these artifact and at the same time get better match to reference we lerp to original unmodified reflection.

float3 F = preLightData.specularFGD;
float iblMipLevel = PerceptualRoughnessToMipmapLevel(preLightData.iblPerceptualRoughness);
float4 preLD = SampleEnv(lightLoopContext, lightData.envIndex, R, iblMipLevel);
float sampleWeight = 1;
float3 texCoord = GetSampleEnvCoordinates(lightLoopContext, lightData.envIndex, R, iblMipLevel, sampleWeight);
weight *= sampleWeight;
float4 preLD = SampleEnv(lightLoopContext, lightData.envIndex, texCoord, iblMipLevel);
// Smooth weighting
weight = Smoothstep01(weight);
if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFLECTION)
{

envLighting *= Sq(1.0 - preLightData.coatIblF);
// Evaluate the Clear Coat color
float4 preLD = SampleEnv(lightLoopContext, lightData.envIndex, coatR, 0.0);
texCoord = GetSampleEnvCoordinates(lightLoopContext, lightData.envIndex, coatR, 0.0, sampleWeight);
float4 preLD = SampleEnv(lightLoopContext, lightData.envIndex, texCoord, 0.0);
envLighting += preLightData.coatIblF * preLD.rgb;
// Can't attenuate diffuse lighting here, may try to apply something on bakeLighting in PostEvaluateBSDF

正在加载...
取消
保存