提交 435f183a 编写于 作者: timchen1002's avatar timchen1002

update

上级 3174debb
......@@ -15,6 +15,7 @@ MonoBehaviour:
useDynamicBatching: 0
useGPUInstancing: 1
useSRPBatcher: 1
useLightsPerObject: 1
shadows:
maxDistance: 30
distanceFade: 0.1
......
......@@ -10,6 +10,7 @@ public class CustomRenderPipelineAsset : RenderPipelineAsset
[SerializeField]
bool useDynamicBatching = true, useGPUInstancing = true, useSRPBatcher = true;
// 是否使用逐对象光源
[SerializeField]
bool useLightsPerObject = true;
[SerializeField]
......
......@@ -117,8 +117,21 @@ public class Shadows
this.cullingResults = cullingResults;
this.shadowSettings = shadowSettings;
shadowedDirectionalLightCount = 0;
useShadowMask = false;
shadowedOtherLightCount = 0;
useShadowMask = false;
}
/// <summary>
/// 释放申请的RT内存
/// </summary>
public void Cleanup()
{
buffer.ReleaseTemporaryRT(dirShadowAtlasId);
if (shadowedOtherLightCount > 0)
{
buffer.ReleaseTemporaryRT(otherShadowAtlasId);
}
ExecuteBuffer();
}
/// <summary>
......@@ -201,7 +214,7 @@ public class Shadows
normalBias = light.shadowNormalBias,
isPoint = isPoint
};
Vector4 data = new Vector4(light.shadowStrength, shadowedOtherLightCount++, isPoint ? 1f : 0f, maskChannel);
Vector4 data = new Vector4(light.shadowStrength, shadowedOtherLightCount, isPoint ? 1f : 0f, maskChannel);
shadowedOtherLightCount = newLightCount;
return data;
}
......@@ -307,7 +320,7 @@ public class Shadows
int split = tiles <= 1 ? 1 : tiles <= 4 ? 2 : 4;
int tileSize = atlasSize / split;
// 遍历所有光源渲染阴影贴图
for (int i = 0; i < shadowedOtherLightCount; i++)
for (int i = 0; i < shadowedOtherLightCount;)
{
if (shadowedOtherLights[i].isPoint)
{
......@@ -427,18 +440,22 @@ public class Shadows
{
ShadowedOtherLight light = shadowedOtherLights[index];
var newShadowSettings = new ShadowDrawingSettings(cullingResults, light.visibleLightIndex);
for (int i = 0; i < 6; i++)
// 计算法线偏差
float texelSize = 2f / tileSize;
float filterSize = texelSize * ((float)shadowSettings.other.filter + 1f);
float bias = light.normalBias * filterSize * 1.4142136f;
float tileScale = 1f / split;
float fovBias = Mathf.Atan(1f + bias + filterSize) * Mathf.Rad2Deg * 2f - 90f;
for (int i = 0; i < 6; i++)
{
cullingResults.ComputePointShadowMatricesAndCullingPrimitives(light.visibleLightIndex, (CubemapFace)i, 0f, out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix, out ShadowSplitData splitData);
newShadowSettings.splitData = splitData;
cullingResults.ComputePointShadowMatricesAndCullingPrimitives(light.visibleLightIndex, (CubemapFace)i, fovBias, out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix, out ShadowSplitData splitData);
viewMatrix.m11 = -viewMatrix.m11;
viewMatrix.m12 = -viewMatrix.m12;
viewMatrix.m13 = -viewMatrix.m13;
newShadowSettings.splitData = splitData;
int tileIndex = index + i;
// 计算法线偏差
float texelSize = 2f / (tileSize * projectionMatrix.m00);
float filterSize = texelSize * ((float)shadowSettings.other.filter + 1f);
float bias = light.normalBias * filterSize * 1.4142136f;
Vector2 offset = SetTileViewport(tileIndex, split, tileSize);
float tileScale = 1f / split;
SetOtherTileData(tileIndex, offset, tileScale, bias);
otherShadowMatrices[tileIndex] = ConvertToAtlasMatrix(projectionMatrix * viewMatrix, offset, tileScale);
buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
......@@ -530,17 +547,4 @@ public class Shadows
m.m23 = 0.5f * (m.m23 + m.m33);
return m;
}
/// <summary>
/// 释放申请的RT内存
/// </summary>
public void Cleanup()
{
buffer.ReleaseTemporaryRT(dirShadowAtlasId);
if (shadowedOtherLightCount > 0)
{
buffer.ReleaseTemporaryRT(otherShadowAtlasId);
}
ExecuteBuffer();
}
}
......@@ -60,7 +60,9 @@ OtherShadowData GetOtherShadowData(int lightIndex)
data.strength = _OtherLightShadowData[lightIndex].x;
data.tileIndex = _OtherLightShadowData[lightIndex].y;
data.shadowMaskChannel = _OtherLightShadowData[lightIndex].w;
data.isPoint = _OtherLightShadowData[lightIndex].z == 1.0;
data.lightPositionWS = 0.0;
data.lightDirectionWS = 0.0;
data.spotDirectionWS = 0.0;
return data;
}
......@@ -97,6 +99,7 @@ Light GetOtherLight(int index, Surface surfaceWS, ShadowData shadowData)
OtherShadowData otherShadowData = GetOtherShadowData(index);
otherShadowData.lightPositionWS = position;
otherShadowData.lightDirectionWS = light.direction;
otherShadowData.spotDirectionWS = spotDirection;
// 光照强度随范围和距离衰减
light.attenuation = GetOtherShadowAttenuation(otherShadowData, shadowData, surfaceWS) * spotAttenuation * rangeAttenuation / distanceSqr;
......
......@@ -79,6 +79,7 @@ float GetBakedShadow(ShadowMask mask, int channel)
return shadow;
}
// 获取烘焙阴影的衰减值
float GetBakedShadow(ShadowMask mask, int channel, float strength)
{
if (mask.always || mask.distance)
......@@ -265,6 +266,7 @@ struct OtherShadowData
{
float strength;
int tileIndex;
bool isPoint;
int shadowMaskChannel;
float3 lightPositionWS;
float3 lightDirectionWS;
......@@ -302,7 +304,8 @@ float FilterOtherShadow(float3 positionSTS, float3 bonuds)
#endif
}
static const float3 pointShadowPlanes[6] = {
static const float3 pointShadowPlanes[6] =
{
float3(-1.0, 0.0, 0.0),
float3(1.0, 0.0, 0.0),
float3(0.0, -1.0, 0.0),
......@@ -315,9 +318,16 @@ static const float3 pointShadowPlanes[6] = {
float GetOtherShadow(OtherShadowData other, ShadowData global, Surface surfaceWS)
{
float tileIndex = other.tileIndex;
float3 lightPlane = other.spotDirectionWS;
if (other.isPoint)
{
float faceOffset = CubeMapFaceID(-other.lightDirectionWS);
tileIndex += faceOffset;
lightPlane = pointShadowPlanes[faceOffset];
}
float4 tileData = _OtherShadowTiles[tileIndex];
float3 surfaceToLight = other.lightPositionWS - surfaceWS.position;
float distanceToLightPlane = dot(surfaceToLight, other.spotDirectionWS);
float distanceToLightPlane = dot(surfaceToLight, lightPlane);
float3 normalBias = surfaceWS.interpolatedNormal * (distanceToLightPlane * tileData.w);
float4 positionSTS = mul(_OtherShadowMatrices[tileIndex], float4(surfaceWS.position + normalBias, 1.0));
return FilterOtherShadow(positionSTS.xyz / positionSTS.w, tileData.xyz);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册