Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
timchen1002
CustomSRP_10_PointAndSpotShadows
提交
435f183a
C
CustomSRP_10_PointAndSpotShadows
项目概览
timchen1002
/
CustomSRP_10_PointAndSpotShadows
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
CustomSRP_10_PointAndSpotShadows
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
435f183a
编写于
9月 22, 2021
作者:
timchen1002
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
3174debb
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
45 addition
and
26 deletion
+45
-26
Assets/Custom RP.asset
Assets/Custom RP.asset
+1
-0
Assets/Custom RP/Runtime/CustomRenderPipelineAsset.cs
Assets/Custom RP/Runtime/CustomRenderPipelineAsset.cs
+1
-0
Assets/Custom RP/Runtime/Shadows.cs
Assets/Custom RP/Runtime/Shadows.cs
+28
-24
Assets/Custom RP/ShaderLibrary/Light.hlsl
Assets/Custom RP/ShaderLibrary/Light.hlsl
+3
-0
Assets/Custom RP/ShaderLibrary/Shadows.hlsl
Assets/Custom RP/ShaderLibrary/Shadows.hlsl
+12
-2
未找到文件。
Assets/Custom RP.asset
浏览文件 @
435f183a
...
...
@@ -15,6 +15,7 @@ MonoBehaviour:
useDynamicBatching
:
0
useGPUInstancing
:
1
useSRPBatcher
:
1
useLightsPerObject
:
1
shadows
:
maxDistance
:
30
distanceFade
:
0.1
...
...
Assets/Custom RP/Runtime/CustomRenderPipelineAsset.cs
浏览文件 @
435f183a
...
...
@@ -10,6 +10,7 @@ public class CustomRenderPipelineAsset : RenderPipelineAsset
[
SerializeField
]
bool
useDynamicBatching
=
true
,
useGPUInstancing
=
true
,
useSRPBatcher
=
true
;
// 是否使用逐对象光源
[
SerializeField
]
bool
useLightsPerObject
=
true
;
[
SerializeField
]
...
...
Assets/Custom RP/Runtime/Shadows.cs
浏览文件 @
435f183a
...
...
@@ -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
();
}
}
Assets/Custom RP/ShaderLibrary/Light.hlsl
浏览文件 @
435f183a
...
...
@@ -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
;
...
...
Assets/Custom RP/ShaderLibrary/Shadows.hlsl
浏览文件 @
435f183a
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录