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

Commit 35a3a16

Browse files
committed
fast shadow version 1.0
1 parent 25e71d7 commit 35a3a16

File tree

7 files changed

+47
-34
lines changed

7 files changed

+47
-34
lines changed

Runtime/ui/geometry.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,21 @@ public float brRadiusY {
10891089
get { return this.brRadius.y; }
10901090
}
10911091

1092+
public bool isNaiveRRect {
1093+
get {
1094+
var radius = this.tlRadiusX;
1095+
return this.tlRadiusY == radius &&
1096+
this.trRadiusX == radius &&
1097+
this.trRadiusY == radius &&
1098+
this.blRadiusX == radius &&
1099+
this.blRadiusY == radius &&
1100+
this.brRadiusX == radius &&
1101+
this.brRadiusY == radius &&
1102+
radius <= this.width / 2 &&
1103+
radius <= this.height / 2;
1104+
}
1105+
}
1106+
10921107
public static readonly RRect zero = new RRect(0, 0, 0, 0, (Radius) null);
10931108

10941109
public RRect shift(Offset offset) {

Runtime/ui/painting/path.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class Path {
2828
PathShapeHint _shapeHint = PathShapeHint.Other;
2929
public PathShapeHint shapeHint => this._shapeHint;
3030

31+
float _rCorner;
32+
public float rCorner => this._rCorner;
33+
3134
public uint pathKey {
3235
get {
3336
return this._pathKey;
@@ -41,12 +44,15 @@ public Path(int capacity = 128) {
4144

4245
public List<float> commands => this._commands;
4346

44-
void _updateRRectFlag(bool isRRect, PathShapeHint shapeHint = PathShapeHint.Other) {
47+
void _updateRRectFlag(bool isRRect, PathShapeHint shapeHint = PathShapeHint.Other, float corner = 0) {
4548
if (this._commands.Count > 0 && !this._isRRect) {
4649
return;
4750
}
4851
this._isRRect = isRRect && this._hasOnlyMoveTos();
49-
this._shapeHint = shapeHint;
52+
if (this._isRRect) {
53+
this._shapeHint = shapeHint;
54+
this._rCorner = corner;
55+
}
5056
}
5157

5258
bool _hasOnlyMoveTos() {
@@ -514,7 +520,7 @@ public void addRect(Rect rect) {
514520
}
515521

516522
public void addRRect(RRect rrect) {
517-
this._updateRRectFlag(true, PathShapeHint.RRect);
523+
this._updateRRectFlag(rrect.isNaiveRRect, PathShapeHint.NaiveRRect, rrect.blRadiusX);
518524
float w = rrect.width;
519525
float h = rrect.height;
520526
float halfw = Mathf.Abs(w) * 0.5f;
@@ -550,7 +556,7 @@ public void addRRect(RRect rrect) {
550556
}
551557

552558
public void addEllipse(float cx, float cy, float rx, float ry) {
553-
this._updateRRectFlag(true, PathShapeHint.Oval);
559+
this._updateRRectFlag(rx == ry, PathShapeHint.Circle, rx);
554560
this._appendMoveTo(cx - rx, cy);
555561
this._appendBezierTo(cx - rx, cy + ry * _KAPPA90,
556562
cx - rx * _KAPPA90, cy + ry, cx, cy + ry);
@@ -564,13 +570,11 @@ public void addEllipse(float cx, float cy, float rx, float ry) {
564570
}
565571

566572
public void addCircle(float cx, float cy, float r) {
567-
this._updateRRectFlag(true, PathShapeHint.Oval);
568573
this.addEllipse(cx, cy, r, r);
569574
}
570575

571576
public void addOval(Rect oval) {
572577
D.assert(oval != null);
573-
this._updateRRectFlag(true, PathShapeHint.Oval);
574578
var center = oval.center;
575579
this.addEllipse(center.dx, center.dy, oval.width / 2, oval.height / 2);
576580
}
@@ -763,7 +767,7 @@ public void addPath(Path path, Offset offset) {
763767
public void addPath(Path path, Matrix3 transform = null) {
764768
D.assert(path != null);
765769

766-
this._updateRRectFlag(path.isRRect, path.shapeHint);
770+
this._updateRRectFlag(path.isRRect, path.shapeHint, path.rCorner);
767771
var i = 0;
768772
while (i < path._commands.Count) {
769773
var cmd = (PathCommand) path._commands[i];
@@ -1405,8 +1409,8 @@ void _chop(out _Conic c1, out _Conic c2) {
14051409

14061410
public enum PathShapeHint {
14071411
Rect,
1408-
Oval,
1409-
RRect,
1412+
Circle,
1413+
NaiveRRect,
14101414
Other
14111415
}
14121416

Runtime/ui/painting/shadow_utils.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,6 @@ static void drawShadowFull2(Canvas canvas, Path path, Vector3 zPlaneParams, Vect
222222
}
223223

224224
//ambient light
225-
_devSpacePath.resetAll();
226-
_devSpacePath.addPath(path, viewMatrix);
227225
float devSpaceOutset = ambientBlurRadius(zPlaneParams.z);
228226
float oneOverA = ambientRecipAlpha(zPlaneParams.z);
229227
float blurRadius = 0.5f * devSpaceOutset * oneOverA;
@@ -232,18 +230,10 @@ static void drawShadowFull2(Canvas canvas, Path path, Vector3 zPlaneParams, Vect
232230
_shadowPaint.color = new Color(ambientColor.value);
233231
_shadowPaint.strokeWidth = strokeWidth;
234232
_shadowPaint.style = PaintingStyle.fill;
235-
236-
canvas.save();
237-
_shadowMatrix.reset();
238-
canvas.setMatrix(_shadowMatrix);
239-
float sigma = convertRadiusToSigma(blurRadius);
240-
_shadowPaint.maskFilter = _devSpacePath.isRRect ? MaskFilter.fastShadow(sigma) : MaskFilter.blur(BlurStyle.normal, sigma);
241-
//canvas.drawPath(_devSpacePath, _shadowPaint);
242-
canvas.restore();
233+
canvas.drawPath(path, _shadowPaint);
243234

244235
//spot light
245236
float radius = 0.0f;
246-
247237
if (!getSpotShadowTransform(devLightPos, lightRadius, viewMatrix, zPlaneParams, path.getBounds(),
248238
_shadowMatrix, ref radius)) {
249239
return;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ public static PictureFlusher.CmdDraw maskFilter(PictureFlusher.RenderLayer layer
504504
);
505505
}
506506

507-
public static PictureFlusher.CmdDraw fastShadow(PictureFlusher.RenderLayer layer, uiMeshMesh mesh,
508-
bool isRect, Vector4 bound, uiColor color) {
507+
public static PictureFlusher.CmdDraw fastShadow(PictureFlusher.RenderLayer layer, uiMeshMesh mesh, float sigma,
508+
bool isRect, bool isCircle, float corner, Vector4 bound, uiColor color) {
509509
Vector4 viewport = layer.viewport;
510510
var mat = _shadowBox;
511511
if (!isRect) {
@@ -515,11 +515,11 @@ public static PictureFlusher.CmdDraw fastShadow(PictureFlusher.RenderLayer layer
515515
//use props to set all the uniforms !!!!!
516516
var props = ObjectPool<MaterialPropertyBlockWrapper>.alloc();
517517
props.SetVector(_viewportId, viewport);
518-
props.SetFloat(_shadowSigmaId, 3f);
518+
props.SetFloat(_shadowSigmaId, sigma);
519519
props.SetVector(_shadowBoxId, bound);
520520
props.SetVector(_shadowColorId, _colorToVector4(color));
521521
if (!isRect) {
522-
props.SetFloat(_shadowCornerId, 30f);
522+
props.SetFloat(_shadowCornerId, corner);
523523
}
524524

525525
return PictureFlusher.CmdDraw.create(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void _drawRRectShadow(uiPath path, uiPaint paint) {
1414

1515
var layer = this._currentLayer;
1616
var state = layer.currentState;
17+
float sigma = state.scale * paint.maskFilter.Value.sigma;
1718

1819
var vertices = ObjectPool<uiList<Vector3>>.alloc();
1920
vertices.SetCapacity(4);
@@ -32,7 +33,7 @@ void _drawRRectShadow(uiPath path, uiPaint paint) {
3233
_triangles.Add(3);
3334

3435
var mesh = uiMeshMesh.create(state.matrix, vertices, _triangles);
35-
layer.draws.Add(CanvasShader.fastShadow(layer, mesh, path.isRect, new Vector4(bound.left, bound.top, bound.right, bound.bottom), paint.color));
36+
layer.draws.Add(CanvasShader.fastShadow(layer, mesh, sigma, path.isRect, path.isCircle, path.rCorner, new Vector4(bound.left, bound.top, bound.right, bound.bottom), paint.color));
3637
}
3738

3839
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ public partial class uiPath : PoolObject {
2222
uiPathShapeHint _shapeHint = uiPathShapeHint.Other;
2323
public uiPathShapeHint shapeHint => this._shapeHint;
2424

25-
void _updateRRectFlag(bool isRRect, uiPathShapeHint shapeHint = uiPathShapeHint.Other) {
25+
float _rCorner;
26+
public float rCorner => this._rCorner;
27+
28+
void _updateRRectFlag(bool isRRect, uiPathShapeHint shapeHint = uiPathShapeHint.Other, float corner = 0) {
2629
if (this._commands.Count > 0 && !this._isRRect) {
2730
return;
2831
}
2932
this._isRRect = isRRect && this._hasOnlyMoveTos();
3033
if (this._isRRect) {
3134
this._shapeHint = shapeHint;
35+
this._rCorner = corner;
3236
}
3337
}
3438

@@ -248,7 +252,7 @@ public void addRect(Rect rect) {
248252
}
249253

250254
public void addRRect(RRect rrect) {
251-
this._updateRRectFlag(true, uiPathShapeHint.RRect);
255+
this._updateRRectFlag(rrect.isNaiveRRect, uiPathShapeHint.NaiveRRect, rrect.blRadiusX);
252256
float w = rrect.width;
253257
float h = rrect.height;
254258
float halfw = Mathf.Abs(w) * 0.5f;
@@ -297,7 +301,7 @@ public void winding(PathWinding dir) {
297301
}
298302

299303
public void addEllipse(float cx, float cy, float rx, float ry) {
300-
this._updateRRectFlag(true, uiPathShapeHint.Oval);
304+
this._updateRRectFlag(rx == ry, uiPathShapeHint.Circle, rx);
301305
this._appendMoveTo(cx - rx, cy);
302306
this._appendBezierTo(cx - rx, cy + ry * _KAPPA90,
303307
cx - rx * _KAPPA90, cy + ry, cx, cy + ry);
@@ -311,7 +315,6 @@ public void addEllipse(float cx, float cy, float rx, float ry) {
311315
}
312316

313317
public void addCircle(float cx, float cy, float r) {
314-
this._updateRRectFlag(true, uiPathShapeHint.Oval);
315318
this.addEllipse(cx, cy, r, r);
316319
}
317320

@@ -429,7 +432,7 @@ public static uiPath fromPath(Path path) {
429432
return uipath;
430433
}
431434

432-
uipath._updateRRectFlag(path.isRRect, (uiPathShapeHint)path.shapeHint);
435+
uipath._updateRRectFlag(path.isRRect, (uiPathShapeHint)path.shapeHint, path.rCorner);
433436

434437
var i = 0;
435438
var _commands = path.commands;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ public partial class uiPath {
33

44
public enum uiPathShapeHint {
55
Rect,
6-
Oval,
7-
RRect,
6+
Circle,
7+
NaiveRRect,
88
Other
99
}
1010

1111
public bool isRect {
1212
get { return this._shapeHint == uiPathShapeHint.Rect; }
1313
}
1414

15-
public bool isOval {
16-
get { return this._shapeHint == uiPathShapeHint.Oval; }
15+
public bool isCircle {
16+
get { return this._shapeHint == uiPathShapeHint.Circle; }
1717
}
1818
}
1919
}

0 commit comments

Comments
 (0)