Remi Slysz
7 年前
当前提交
2f639eeb
共有 2 个文件被更改,包括 149 次插入 和 145 次删除
-
230com.unity.render-pipelines.high-definition/HDRP/Lighting/SphericalHarmonics.cs
-
64com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/SceneViewDrawMode.cs
|
|||
using System; |
|||
using UnityEngine; |
|||
public struct ZonalHarmonicsL2 |
|||
{ |
|||
public float[] coeffs; // Must have the size of 3
|
|||
public static ZonalHarmonicsL2 GetHenyeyGreensteinPhaseFunction(float anisotropy) |
|||
namespace UnityEngine.Experimental.Rendering.HDPipeline |
|||
{ |
|||
public struct ZonalHarmonicsL2 |
|||
float g = anisotropy; |
|||
public float[] coeffs; // Must have the size of 3
|
|||
var zh = new ZonalHarmonicsL2(); |
|||
zh.coeffs = new float[3]; |
|||
public static ZonalHarmonicsL2 GetHenyeyGreensteinPhaseFunction(float anisotropy) |
|||
{ |
|||
float g = anisotropy; |
|||
zh.coeffs[0] = 0.5f * Mathf.Sqrt(1.0f / Mathf.PI); |
|||
zh.coeffs[1] = 0.5f * Mathf.Sqrt(3.0f / Mathf.PI) * g; |
|||
zh.coeffs[2] = 0.5f * Mathf.Sqrt(5.0f / Mathf.PI) * g * g; |
|||
var zh = new ZonalHarmonicsL2(); |
|||
zh.coeffs = new float[3]; |
|||
return zh; |
|||
} |
|||
zh.coeffs[0] = 0.5f * Mathf.Sqrt(1.0f / Mathf.PI); |
|||
zh.coeffs[1] = 0.5f * Mathf.Sqrt(3.0f / Mathf.PI) * g; |
|||
zh.coeffs[2] = 0.5f * Mathf.Sqrt(5.0f / Mathf.PI) * g * g; |
|||
public static ZonalHarmonicsL2 GetCornetteShanksPhaseFunction(float anisotropy) |
|||
{ |
|||
float g = anisotropy; |
|||
return zh; |
|||
} |
|||
var zh = new ZonalHarmonicsL2(); |
|||
zh.coeffs = new float[3]; |
|||
public static ZonalHarmonicsL2 GetCornetteShanksPhaseFunction(float anisotropy) |
|||
{ |
|||
float g = anisotropy; |
|||
|
|||
var zh = new ZonalHarmonicsL2(); |
|||
zh.coeffs = new float[3]; |
|||
zh.coeffs[0] = 0.282095f; |
|||
zh.coeffs[1] = 0.293162f * g * (4.0f + (g * g)) / (2.0f + (g * g)); |
|||
zh.coeffs[2] = (0.126157f + 1.44179f * (g * g) + 0.324403f * (g * g) * (g * g)) / (2.0f + (g * g)); |
|||
zh.coeffs[0] = 0.282095f; |
|||
zh.coeffs[1] = 0.293162f * g * (4.0f + (g * g)) / (2.0f + (g * g)); |
|||
zh.coeffs[2] = (0.126157f + 1.44179f * (g * g) + 0.324403f * (g * g) * (g * g)) / (2.0f + (g * g)); |
|||
return zh; |
|||
return zh; |
|||
} |
|||
} |
|||
public class SphericalHarmonicMath |
|||
{ |
|||
// Ref: "Stupid Spherical Harmonics Tricks", p. 6.
|
|||
public static SphericalHarmonicsL2 Convolve(SphericalHarmonicsL2 sh, ZonalHarmonicsL2 zh) |
|||
public class SphericalHarmonicMath |
|||
for (int l = 0; l <= 2; l++) |
|||
// Ref: "Stupid Spherical Harmonics Tricks", p. 6.
|
|||
public static SphericalHarmonicsL2 Convolve(SphericalHarmonicsL2 sh, ZonalHarmonicsL2 zh) |
|||
float n = Mathf.Sqrt((4.0f * Mathf.PI) / (2 * l + 1)); |
|||
float k = zh.coeffs[l]; |
|||
float p = n * k; |
|||
|
|||
for (int m = -l; m <= l; m++) |
|||
for (int l = 0; l <= 2; l++) |
|||
int i = l * (l + 1) + m; |
|||
float n = Mathf.Sqrt((4.0f * Mathf.PI) / (2 * l + 1)); |
|||
float k = zh.coeffs[l]; |
|||
float p = n * k; |
|||
for (int c = 0; c < 3; c++) |
|||
for (int m = -l; m <= l; m++) |
|||
sh[c, i] *= p; |
|||
int i = l * (l + 1) + m; |
|||
|
|||
for (int c = 0; c < 3; c++) |
|||
{ |
|||
sh[c, i] *= p; |
|||
} |
|||
|
|||
return sh; |
|||
return sh; |
|||
} |
|||
// Undoes coefficient rescaling due to the convolution with the clamped cosine kernel
|
|||
// to obtain the canonical values of SH.
|
|||
public static SphericalHarmonicsL2 UndoCosineRescaling(SphericalHarmonicsL2 sh) |
|||
{ |
|||
const float c0 = 0.28209479177387814347f; // 1/2 * sqrt(1/Pi)
|
|||
const float c1 = 0.32573500793527994772f; // 1/3 * sqrt(3/Pi)
|
|||
const float c2 = 0.27313710764801976764f; // 1/8 * sqrt(15/Pi)
|
|||
const float c3 = 0.07884789131313000151f; // 1/16 * sqrt(5/Pi)
|
|||
const float c4 = 0.13656855382400988382f; // 1/16 * sqrt(15/Pi)
|
|||
// Undoes coefficient rescaling due to the convolution with the clamped cosine kernel
|
|||
// to obtain the canonical values of SH.
|
|||
public static SphericalHarmonicsL2 UndoCosineRescaling(SphericalHarmonicsL2 sh) |
|||
{ |
|||
const float c0 = 0.28209479177387814347f; // 1/2 * sqrt(1/Pi)
|
|||
const float c1 = 0.32573500793527994772f; // 1/3 * sqrt(3/Pi)
|
|||
const float c2 = 0.27313710764801976764f; // 1/8 * sqrt(15/Pi)
|
|||
const float c3 = 0.07884789131313000151f; // 1/16 * sqrt(5/Pi)
|
|||
const float c4 = 0.13656855382400988382f; // 1/16 * sqrt(15/Pi)
|
|||
|
|||
// Compute the inverse of SphericalHarmonicsL2::kNormalizationConstants.
|
|||
// See SetSHEMapConstants() in "Stupid Spherical Harmonics Tricks".
|
|||
float[] invNormConsts = { 1 / c0, -1 / c1, 1 / c1, -1 / c1, 1 / c2, -1 / c2, 1 / c3, -1 / c2, 1 / c4 }; |
|||
// Compute the inverse of SphericalHarmonicsL2::kNormalizationConstants.
|
|||
// See SetSHEMapConstants() in "Stupid Spherical Harmonics Tricks".
|
|||
float[] invNormConsts = { 1 / c0, -1 / c1, 1 / c1, -1 / c1, 1 / c2, -1 / c2, 1 / c3, -1 / c2, 1 / c4 }; |
|||
for (int c = 0; c < 3; c++) |
|||
{ |
|||
for (int i = 0; i < 9; i++) |
|||
for (int c = 0; c < 3; c++) |
|||
sh[c, i] *= invNormConsts[i]; |
|||
for (int i = 0; i < 9; i++) |
|||
{ |
|||
sh[c, i] *= invNormConsts[i]; |
|||
} |
|||
} |
|||
return sh; |
|||
} |
|||
return sh; |
|||
} |
|||
// Premultiplies the SH with the polynomial coefficients of SH basis functions,
|
|||
// which avoids using any constants during SH evaluation.
|
|||
// The resulting evaluation takes the form:
|
|||
// (c_0 - c_6) + c_1 y + c_2 z + c_3 x + c_4 x y + c_5 y z + c_6 (3 z^2) + c_7 x z + c_8 (x^2 - y^2)
|
|||
public static SphericalHarmonicsL2 PremultiplyCoefficients(SphericalHarmonicsL2 sh) |
|||
{ |
|||
const float k0 = 0.28209479177387814347f; // {0, 0} : 1/2 * sqrt(1/Pi)
|
|||
const float k1 = 0.48860251190291992159f; // {1, 0} : 1/2 * sqrt(3/Pi)
|
|||
const float k2 = 1.09254843059207907054f; // {2,-2} : 1/2 * sqrt(15/Pi)
|
|||
const float k3 = 0.31539156525252000603f; // {2, 0} : 1/4 * sqrt(5/Pi)
|
|||
const float k4 = 0.54627421529603953527f; // {2, 2} : 1/4 * sqrt(15/Pi)
|
|||
// Premultiplies the SH with the polynomial coefficients of SH basis functions,
|
|||
// which avoids using any constants during SH evaluation.
|
|||
// The resulting evaluation takes the form:
|
|||
// (c_0 - c_6) + c_1 y + c_2 z + c_3 x + c_4 x y + c_5 y z + c_6 (3 z^2) + c_7 x z + c_8 (x^2 - y^2)
|
|||
public static SphericalHarmonicsL2 PremultiplyCoefficients(SphericalHarmonicsL2 sh) |
|||
{ |
|||
const float k0 = 0.28209479177387814347f; // {0, 0} : 1/2 * sqrt(1/Pi)
|
|||
const float k1 = 0.48860251190291992159f; // {1, 0} : 1/2 * sqrt(3/Pi)
|
|||
const float k2 = 1.09254843059207907054f; // {2,-2} : 1/2 * sqrt(15/Pi)
|
|||
const float k3 = 0.31539156525252000603f; // {2, 0} : 1/4 * sqrt(5/Pi)
|
|||
const float k4 = 0.54627421529603953527f; // {2, 2} : 1/4 * sqrt(15/Pi)
|
|||
float[] ks = { k0, -k1, k1, -k1, k2, -k2, k3, -k2, k4 }; |
|||
float[] ks = { k0, -k1, k1, -k1, k2, -k2, k3, -k2, k4 }; |
|||
for (int c = 0; c < 3; c++) |
|||
{ |
|||
for (int i = 0; i < 9; i++) |
|||
for (int c = 0; c < 3; c++) |
|||
sh[c, i] *= ks[i]; |
|||
for (int i = 0; i < 9; i++) |
|||
{ |
|||
sh[c, i] *= ks[i]; |
|||
} |
|||
|
|||
return sh; |
|||
return sh; |
|||
} |
|||
|
|||
public static SphericalHarmonicsL2 RescaleCoefficients(SphericalHarmonicsL2 sh, float scalar) |
|||
{ |
|||
for (int c = 0; c < 3; c++) |
|||
public static SphericalHarmonicsL2 RescaleCoefficients(SphericalHarmonicsL2 sh, float scalar) |
|||
for (int i = 0; i < 9; i++) |
|||
for (int c = 0; c < 3; c++) |
|||
sh[c, i] *= scalar; |
|||
for (int i = 0; i < 9; i++) |
|||
{ |
|||
sh[c, i] *= scalar; |
|||
} |
|||
|
|||
return sh; |
|||
return sh; |
|||
} |
|||
// Packs coefficients so that we can use Peter-Pike Sloan's shader code.
|
|||
// Does not perform premultiplication with coefficients of SH basis functions.
|
|||
// See SetSHEMapConstants() in "Stupid Spherical Harmonics Tricks".
|
|||
public static Vector4[] PackCoefficients(SphericalHarmonicsL2 sh) |
|||
{ |
|||
Vector4[] coeffs = new Vector4[7]; |
|||
|
|||
// Constant + linear
|
|||
for (int c = 0; c < 3; c++) |
|||
{ |
|||
coeffs[c].x = sh[c, 3]; |
|||
coeffs[c].y = sh[c, 1]; |
|||
coeffs[c].z = sh[c, 2]; |
|||
coeffs[c].w = sh[c, 0] - sh[c, 6]; |
|||
} |
|||
// Packs coefficients so that we can use Peter-Pike Sloan's shader code.
|
|||
// Does not perform premultiplication with coefficients of SH basis functions.
|
|||
// See SetSHEMapConstants() in "Stupid Spherical Harmonics Tricks".
|
|||
public static Vector4[] PackCoefficients(SphericalHarmonicsL2 sh) |
|||
{ |
|||
Vector4[] coeffs = new Vector4[7]; |
|||
// Quadratic (4/5)
|
|||
for (int c = 0; c < 3; c++) |
|||
{ |
|||
coeffs[3 + c].x = sh[c, 4]; |
|||
coeffs[3 + c].y = sh[c, 5]; |
|||
coeffs[3 + c].z = sh[c, 6] * 3.0f; |
|||
coeffs[3 + c].w = sh[c, 7]; |
|||
} |
|||
// Constant + linear
|
|||
for (int c = 0; c < 3; c++) |
|||
{ |
|||
coeffs[c].x = sh[c, 3]; |
|||
coeffs[c].y = sh[c, 1]; |
|||
coeffs[c].z = sh[c, 2]; |
|||
coeffs[c].w = sh[c, 0] - sh[c, 6]; |
|||
} |
|||
// Quadratic (5)
|
|||
coeffs[6].x = sh[0, 8]; |
|||
coeffs[6].y = sh[1, 8]; |
|||
coeffs[6].z = sh[2, 8]; |
|||
coeffs[6].w = 1.0f; |
|||
// Quadratic (4/5)
|
|||
for (int c = 0; c < 3; c++) |
|||
{ |
|||
coeffs[3 + c].x = sh[c, 4]; |
|||
coeffs[3 + c].y = sh[c, 5]; |
|||
coeffs[3 + c].z = sh[c, 6] * 3.0f; |
|||
coeffs[3 + c].w = sh[c, 7]; |
|||
return coeffs; |
|||
|
|||
// Quadratic (5)
|
|||
coeffs[6].x = sh[0, 8]; |
|||
coeffs[6].y = sh[1, 8]; |
|||
coeffs[6].z = sh[2, 8]; |
|||
coeffs[6].w = 1.0f; |
|||
|
|||
return coeffs; |
|||
} |
|||
} |
|
|||
#if UNITY_EDITOR
|
|||
using System.Collections; |
|||
using UnityEditor; |
|||
using UnityEditor.Experimental.Rendering; |
|||
public class SceneViewDrawMode |
|||
namespace UnityEngine.Experimental.Rendering.HDPipeline |
|||
static private bool RejectDrawMode(SceneView.CameraMode cameraMode) |
|||
public class SceneViewDrawMode |
|||
if (cameraMode.drawMode == DrawCameraMode.TexturedWire || |
|||
cameraMode.drawMode == DrawCameraMode.ShadowCascades || |
|||
cameraMode.drawMode == DrawCameraMode.RenderPaths || |
|||
cameraMode.drawMode == DrawCameraMode.AlphaChannel || |
|||
cameraMode.drawMode == DrawCameraMode.Overdraw || |
|||
cameraMode.drawMode == DrawCameraMode.Mipmaps || |
|||
cameraMode.drawMode == DrawCameraMode.DeferredDiffuse || |
|||
cameraMode.drawMode == DrawCameraMode.DeferredSpecular || |
|||
cameraMode.drawMode == DrawCameraMode.DeferredSmoothness || |
|||
cameraMode.drawMode == DrawCameraMode.DeferredNormal || |
|||
cameraMode.drawMode == DrawCameraMode.ValidateAlbedo || |
|||
cameraMode.drawMode == DrawCameraMode.ValidateMetalSpecular || |
|||
cameraMode.drawMode == DrawCameraMode.LightOverlap |
|||
) |
|||
return false; |
|||
static private bool RejectDrawMode(SceneView.CameraMode cameraMode) |
|||
{ |
|||
if (cameraMode.drawMode == DrawCameraMode.TexturedWire || |
|||
cameraMode.drawMode == DrawCameraMode.ShadowCascades || |
|||
cameraMode.drawMode == DrawCameraMode.RenderPaths || |
|||
cameraMode.drawMode == DrawCameraMode.AlphaChannel || |
|||
cameraMode.drawMode == DrawCameraMode.Overdraw || |
|||
cameraMode.drawMode == DrawCameraMode.Mipmaps || |
|||
cameraMode.drawMode == DrawCameraMode.DeferredDiffuse || |
|||
cameraMode.drawMode == DrawCameraMode.DeferredSpecular || |
|||
cameraMode.drawMode == DrawCameraMode.DeferredSmoothness || |
|||
cameraMode.drawMode == DrawCameraMode.DeferredNormal || |
|||
cameraMode.drawMode == DrawCameraMode.ValidateAlbedo || |
|||
cameraMode.drawMode == DrawCameraMode.ValidateMetalSpecular || |
|||
cameraMode.drawMode == DrawCameraMode.LightOverlap |
|||
) |
|||
return false; |
|||
return true; |
|||
} |
|||
return true; |
|||
} |
|||
static public void SetupDrawMode() |
|||
{ |
|||
ArrayList sceneViewArray = SceneView.sceneViews; |
|||
foreach (SceneView sceneView in sceneViewArray) |
|||
sceneView.onValidateCameraMode += RejectDrawMode; |
|||
} |
|||
static public void SetupDrawMode() |
|||
{ |
|||
ArrayList sceneViewArray = SceneView.sceneViews; |
|||
foreach (SceneView sceneView in sceneViewArray) |
|||
sceneView.onValidateCameraMode += RejectDrawMode; |
|||
} |
|||
static public void ResetDrawMode() |
|||
{ |
|||
ArrayList sceneViewArray = SceneView.sceneViews; |
|||
foreach (SceneView sceneView in sceneViewArray) |
|||
sceneView.onValidateCameraMode -= RejectDrawMode; |
|||
static public void ResetDrawMode() |
|||
{ |
|||
ArrayList sceneViewArray = SceneView.sceneViews; |
|||
foreach (SceneView sceneView in sceneViewArray) |
|||
sceneView.onValidateCameraMode -= RejectDrawMode; |
|||
} |
|||
} |
|||
} |
|||
#endif
|
撰写
预览
正在加载...
取消
保存
Reference in new issue