Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 25e71d7

Browse files
committed
fast shadow fix some artifacts
1 parent 456e2b6 commit 25e71d7

File tree

7 files changed

+65
-14
lines changed

7 files changed

+65
-14
lines changed

Runtime/Resources/UIWidgets_canvas_shadowRBox.shader

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ Shader "UIWidgets/ShadowRBox"
55
{
66
_SrcBlend("_SrcBlend", Int) = 1 // One
77
_DstBlend("_DstBlend", Int) = 10 // OneMinusSrcAlpha
8+
_StencilComp("_StencilComp", Float) = 8 // - Equal, 8 - Always
89
}
910
SubShader
1011
{
12+
ZTest Always
13+
ZWrite Off
1114
Blend [_SrcBlend] [_DstBlend]
15+
16+
Stencil {
17+
Ref 128
18+
Comp [_StencilComp]
19+
}
20+
1221
Pass {
1322
CGPROGRAM
1423

1524
float4 box;
16-
float2 window;
25+
float4 _viewport;
1726
float sigma;
1827
float4 color;
1928
float corner;
29+
float _mat[9];
2030

2131
struct appdata
2232
{
@@ -81,7 +91,14 @@ Shader "UIWidgets/ShadowRBox"
8191
v2f o;
8292
float padding = 3.0 * sigma;
8393
o.coord = lerp(box.xy - padding, box.zw + padding, v.vertex.xy);
84-
o.vertex = float4(o.coord.x * 2.0 /window.x - 1.0, o.coord.y * 2.0/window.y - 1.0, 0, 1);
94+
float3x3 mat = float3x3(_mat[0], _mat[1], _mat[2], _mat[3], _mat[4], _mat[5], 0, 0, 1);
95+
float2 p = mul(mat, float3(o.coord.xy, 1.0)).xy - _viewport.xy;
96+
97+
#if UNITY_UV_STARTS_AT_TOP
98+
o.vertex = float4(2.0 * p.x / _viewport.z - 1.0, 2.0 * p.y / _viewport.w - 1.0, 0, 1);
99+
#else
100+
o.vertex = float4(2.0 * p.x / _viewport.z - 1.0, 1.0 - 2.0 * p.y / _viewport.w, 0, 1);
101+
#endif
85102
return o;
86103
}
87104

Runtime/ui/painting/shadow_utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static class ShadowUtils {
1212

1313
const float kMaxAmbientRadius = 300 * kAmbientHeightFactor * kAmbientGeomFactor;
1414

15-
const bool debugShadow = true;
15+
const bool debugShadow = false;
1616

1717
static float divideAndPin(float numer, float denom, float min, float max) {
1818
return (numer / denom).clamp(min, max);

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,9 @@ void _drawLayer(RenderLayer layer, CommandBuffer cmdBuf) {
10441044

10451045
break;
10461046
case CmdDraw cmd:
1047+
if (cmd.material != CanvasShader.shadowBox) {
1048+
1049+
}
10471050
this._setRenderTarget(cmdBuf, layer.rtID, ref toClear);
10481051

10491052
if (cmd.layerId != null) {

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shader.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23
using UnityEngine;
34
using UnityEngine.Rendering;
45

@@ -188,6 +189,8 @@ static CanvasShader() {
188189
_shadowRBox = new Material(shadowRBoxShader) {hideFlags = HideFlags.HideAndDontSave};
189190
}
190191

192+
public static Material shadowBox => _shadowBox;
193+
191194
static readonly int _viewportId = Shader.PropertyToID("_viewport");
192195
static readonly int _alphaId = Shader.PropertyToID("_alpha");
193196
static readonly int _colorId = Shader.PropertyToID("_color");
@@ -206,8 +209,9 @@ static CanvasShader() {
206209

207210

208211
static readonly int _shadowBoxId = Shader.PropertyToID("box");
209-
static readonly int _shadowSigma = Shader.PropertyToID("sigma");
210-
static readonly int _shadowColor = Shader.PropertyToID("color");
212+
static readonly int _shadowSigmaId = Shader.PropertyToID("sigma");
213+
static readonly int _shadowColorId = Shader.PropertyToID("color");
214+
static readonly int _shadowCornerId = Shader.PropertyToID("corner");
211215

212216
static Vector4 _colorToVector4(uiColor c) {
213217
return new Vector4(
@@ -501,19 +505,22 @@ public static PictureFlusher.CmdDraw maskFilter(PictureFlusher.RenderLayer layer
501505
}
502506

503507
public static PictureFlusher.CmdDraw fastShadow(PictureFlusher.RenderLayer layer, uiMeshMesh mesh,
504-
bool isRect, Vector4 bound) {
508+
bool isRect, Vector4 bound, uiColor color) {
505509
Vector4 viewport = layer.viewport;
506510
var mat = _shadowBox;
507-
//if (isRect) {
508-
// mat = _shadowBox;
509-
//}
511+
if (!isRect) {
512+
mat = _shadowRBox;
513+
}
510514

511515
//use props to set all the uniforms !!!!!
512516
var props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
513517
props.SetVector(_viewportId, viewport);
514-
props.SetFloat(_shadowSigma, 3f);
518+
props.SetFloat(_shadowSigmaId, 3f);
515519
props.SetVector(_shadowBoxId, bound);
516-
props.SetVector(_shadowColor, new Vector4(0, 0, 0, 1));
520+
props.SetVector(_shadowColorId, _colorToVector4(color));
521+
if (!isRect) {
522+
props.SetFloat(_shadowCornerId, 30f);
523+
}
517524

518525
return PictureFlusher.CmdDraw.create(
519526
mesh: mesh,

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shadow_utils.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ public partial class PictureFlusher {
77
void _drawRRectShadow(uiPath path, uiPaint paint) {
88
D.assert(path.isRRect, () => "Cannot draw Shadow for non-RRect shapes");
99
D.assert(paint.style == PaintingStyle.fill, () => "Cannot draw Shadow for stroke lines");
10+
var bound = path.getBounds();
11+
if (!this._applyClip(bound)) {
12+
return;
13+
}
1014

1115
var layer = this._currentLayer;
1216
var state = layer.currentState;
@@ -28,9 +32,7 @@ void _drawRRectShadow(uiPath path, uiPaint paint) {
2832
_triangles.Add(3);
2933

3034
var mesh = uiMeshMesh.create(state.matrix, vertices, _triangles);
31-
var bound = path.getBounds();
32-
Debug.Log("Draw shadow>>> " + bound.top);
33-
layer.draws.Add(CanvasShader.fastShadow(layer, mesh, path.isRect, new Vector4(bound.left, bound.top, bound.right, bound.bottom)));
35+
layer.draws.Add(CanvasShader.fastShadow(layer, mesh, path.isRect, new Vector4(bound.left, bound.top, bound.right, bound.bottom), paint.color));
3436
}
3537

3638
}

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shadow_utils.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/ui/renderer/common/geometry/path/path_extension.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)