浏览代码
Merge pull request #1219 from Unity-Technologies/FactorLTCAreaCode
Merge pull request #1219 from Unity-Technologies/FactorLTCAreaCode
HDRenderPipeline: FactorLTCAreaCode/main
GitHub
7 年前
当前提交
ff5089b7
共有 14 个文件被更改,包括 179 次插入 和 92 次删除
-
80ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.cs
-
21ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl
-
6ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.DisneyDiffuse.cs
-
2ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.GGX.cs
-
8ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight.meta
-
124ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.cs
-
11ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.cs.meta
-
10ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.hlsl
-
9ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LTCAreaLight.hlsl.meta
-
0/ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.GGX.cs.meta
-
0/ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.DisneyDiffuse.cs.meta
-
0/ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.DisneyDiffuse.cs
-
0/ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/LTCAreaLight/LtcData.GGX.cs
|
|||
fileFormatVersion: 2 |
|||
guid: 0cc808eb94961c54791ef0b41f113496 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using UnityEngine.Rendering; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering.HDPipeline |
|||
{ |
|||
public partial class LTCAreaLight |
|||
{ |
|||
static LTCAreaLight s_Instance; |
|||
|
|||
public static LTCAreaLight instance |
|||
{ |
|||
get |
|||
{ |
|||
if (s_Instance == null) |
|||
s_Instance = new LTCAreaLight(); |
|||
|
|||
return s_Instance; |
|||
} |
|||
} |
|||
|
|||
bool m_isInit; |
|||
int m_refCounting; |
|||
|
|||
// For area lighting - We pack all texture inside a texture array to reduce the number of resource required
|
|||
Texture2DArray m_LtcData; // 0: m_LtcGGXMatrix - RGBA, 2: m_LtcDisneyDiffuseMatrix - RGBA, 3: m_LtcMultiGGXFresnelDisneyDiffuse - RGB, A unused
|
|||
|
|||
const int k_LtcLUTMatrixDim = 3; // size of the matrix (3x3)
|
|||
const int k_LtcLUTResolution = 64; |
|||
|
|||
LTCAreaLight() |
|||
{ |
|||
m_isInit = false; |
|||
m_refCounting = 0; |
|||
} |
|||
|
|||
// Load LUT with one scalar in alpha of a tex2D
|
|||
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, float[] LUTScalar) |
|||
{ |
|||
const int count = k_LtcLUTResolution * k_LtcLUTResolution; |
|||
Color[] pixels = new Color[count]; |
|||
|
|||
for (int i = 0; i < count; i++) |
|||
{ |
|||
pixels[i] = new Color(0, 0, 0, LUTScalar[i]); |
|||
} |
|||
|
|||
tex.SetPixels(pixels, arrayElement); |
|||
} |
|||
|
|||
// Load LUT with 3x3 matrix in RGBA of a tex2D (some part are zero)
|
|||
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, double[,] LUTTransformInv) |
|||
{ |
|||
const int count = k_LtcLUTResolution * k_LtcLUTResolution; |
|||
Color[] pixels = new Color[count]; |
|||
|
|||
for (int i = 0; i < count; i++) |
|||
{ |
|||
// Both GGX and Disney Diffuse BRDFs have zero values in columns 1, 3, 5, 7.
|
|||
// Column 8 contains only ones.
|
|||
pixels[i] = new Color((float)LUTTransformInv[i, 0], |
|||
(float)LUTTransformInv[i, 2], |
|||
(float)LUTTransformInv[i, 4], |
|||
(float)LUTTransformInv[i, 6]); |
|||
} |
|||
|
|||
tex.SetPixels(pixels, arrayElement); |
|||
} |
|||
|
|||
// Special-case function for 'm_LtcMultiGGXFresnelDisneyDiffuse'.
|
|||
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, float[] LtcGGXMagnitudeData, float[] LtcGGXFresnelData, float[] LtcDisneyDiffuseMagnitudeData) |
|||
{ |
|||
const int count = k_LtcLUTResolution * k_LtcLUTResolution; |
|||
Color[] pixels = new Color[count]; |
|||
|
|||
for (int i = 0; i < count; i++) |
|||
{ |
|||
// We store the result of the subtraction as a run-time optimization.
|
|||
// See the footnote 2 of "LTC Fresnel Approximation" by Stephen Hill.
|
|||
pixels[i] = new Color(LtcGGXMagnitudeData[i] - LtcGGXFresnelData[i], LtcGGXFresnelData[i], LtcDisneyDiffuseMagnitudeData[i], 1); |
|||
} |
|||
|
|||
tex.SetPixels(pixels, arrayElement); |
|||
} |
|||
|
|||
public void Build() |
|||
{ |
|||
m_refCounting++; |
|||
|
|||
m_LtcData = new Texture2DArray(k_LtcLUTResolution, k_LtcLUTResolution, 3, TextureFormat.RGBAHalf, false /*mipmap*/, true /* linear */) |
|||
{ |
|||
hideFlags = HideFlags.HideAndDontSave, |
|||
wrapMode = TextureWrapMode.Clamp, |
|||
filterMode = FilterMode.Bilinear, |
|||
name = CoreUtils.GetTextureAutoName(k_LtcLUTResolution, k_LtcLUTResolution, TextureFormat.RGBAHalf, depth: 3, dim: TextureDimension.Tex2DArray, name: "LTC_LUT") |
|||
}; |
|||
|
|||
LoadLUT(m_LtcData, 0, TextureFormat.RGBAHalf, s_LtcGGXMatrixData); |
|||
LoadLUT(m_LtcData, 1, TextureFormat.RGBAHalf, s_LtcDisneyDiffuseMatrixData); |
|||
// TODO: switch to RGBA64 when it becomes available.
|
|||
LoadLUT(m_LtcData, 2, TextureFormat.RGBAHalf, s_LtcGGXMagnitudeData, s_LtcGGXFresnelData, s_LtcDisneyDiffuseMagnitudeData); |
|||
|
|||
m_LtcData.Apply(); |
|||
|
|||
m_isInit = true; |
|||
} |
|||
|
|||
public void Cleanup() |
|||
{ |
|||
m_refCounting--; |
|||
|
|||
if (m_refCounting <= 0) |
|||
{ |
|||
CoreUtils.Destroy(m_LtcData); |
|||
} |
|||
|
|||
m_isInit = false; |
|||
} |
|||
|
|||
public void Bind() |
|||
{ |
|||
Shader.SetGlobalTexture("_LtcData", m_LtcData); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: e6d8f4a97e711d6488965da2f3baeb51 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
// Area light textures |
|||
TEXTURE2D_ARRAY(_LtcData); // We pack all Ltc data inside one texture array to limit the number of resource used |
|||
|
|||
#define LTC_GGX_MATRIX_INDEX 0 // RGBA |
|||
#define LTC_DISNEY_DIFFUSE_MATRIX_INDEX 1 // RGBA |
|||
#define LTC_MULTI_GGX_FRESNEL_DISNEY_DIFFUSE_INDEX 2 // RGB, A unused |
|||
|
|||
#define LTC_LUT_SIZE 64 |
|||
#define LTC_LUT_SCALE ((LTC_LUT_SIZE - 1) * rcp(LTC_LUT_SIZE)) |
|||
#define LTC_LUT_OFFSET (0.5 * rcp(LTC_LUT_SIZE)) |
|
|||
fileFormatVersion: 2 |
|||
guid: 726ce9f653b80b940b9e5a2244acfcc2 |
|||
ShaderImporter: |
|||
externalObjects: {} |
|||
defaultTextures: [] |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue