浏览代码

optimize drawShadow

/main
xingwei.zhu 6 年前
当前提交
48d7fde1
共有 2 个文件被更改,包括 52 次插入32 次删除
  1. 68
      Runtime/ui/painting/shadow_utils.cs
  2. 16
      Runtime/ui/utils/renderer/compositeCanvas/flow/physical_shape_layer.cs

68
Runtime/ui/painting/shadow_utils.cs


namespace Unity.UIWidgets.ui {
static class ShadowUtils {
public const bool kUseFastShadow = true;
public const bool kUseFastShadow = false;
public const float kAmbientHeightFactor = 1.0f / 128.0f;
public const float kAmbientGeomFactor = 64.0f;

return radius > 0 ? kBlurSigmaScale * radius + 0.5f : 0.0f;
}
public static void computeTonalColors(Color inAmbientColor, Color inSpotColor,
ref Color outAmbientColor, ref Color outSpotColor) {
outAmbientColor = Color.fromARGB(inAmbientColor.alpha, 0, 0, 0);
public static void computeTonalColors(uiColor inAmbientColor, uiColor inSpotColor,
ref uiColor? outAmbientColor, ref uiColor? outSpotColor) {
outAmbientColor = uiColor.fromARGB(inAmbientColor.alpha, 0, 0, 0);
outSpotColor = inSpotColor;
}

}
public static void drawShadow(Canvas canvas, Path path, Vector3 zPlaneParams, Vector3 devLightPos,
float lightRadius, Color ambientColor, Color spotColor, int flags) {
float lightRadius, uiColor ambientColor, uiColor spotColor, int flags) {
if (kUseFastShadow) {
drawShadowFast(canvas, path, zPlaneParams, devLightPos, lightRadius, ambientColor, spotColor, flags);
}

}
//cached variables
static readonly Paint _shadowPaint = new Paint();
static readonly Matrix3 _shadowMatrix = Matrix3.I();
float lightRadius, Color ambientColor, Color spotColor, int flags) {
float lightRadius, uiColor ambientColor, uiColor spotColor, int flags) {
Matrix3 viewMatrix = canvas.getTotalMatrix();
//ambient light

float blurRadius = 0.5f * devSpaceOutset * oneOverA;
float strokeWidth = 0.5f * (devSpaceOutset - blurRadius);
Paint paint = new Paint {color = ambientColor, strokeWidth = strokeWidth, style = PaintingStyle.fill};
//Paint paint = new Paint {color = ambientColor, strokeWidth = strokeWidth, style = PaintingStyle.fill};
_shadowPaint.color = new Color(ambientColor.value);
_shadowPaint.strokeWidth = strokeWidth;
_shadowPaint.style = PaintingStyle.fill;
canvas.setMatrix(Matrix3.I());
_shadowMatrix.reset();
canvas.setMatrix(_shadowMatrix);
paint.maskFilter = MaskFilter.blur(BlurStyle.normal, sigma);
canvas.drawPath(devSpacePath, paint);
_shadowPaint.maskFilter = MaskFilter.blur(BlurStyle.normal, sigma);
canvas.drawPath(devSpacePath, _shadowPaint);
Matrix3 shadowMatrix = Matrix3.I();
//Matrix3 shadowMatrix = Matrix3.I();
shadowMatrix, ref radius)) {
_shadowMatrix, ref radius)) {
canvas.setMatrix(shadowMatrix);
Paint paint2 = new Paint {color = spotColor};
canvas.setMatrix(_shadowMatrix);
_shadowPaint.color = new Color(spotColor.value);
_shadowPaint.strokeWidth = 0;
_shadowPaint.style = PaintingStyle.fill;
paint2.maskFilter = MaskFilter.blur(BlurStyle.normal, sigma2);
canvas.drawPath(path, paint2);
_shadowPaint.maskFilter = MaskFilter.blur(BlurStyle.normal, sigma2);
canvas.drawPath(path, _shadowPaint);
_shadowPaint.maskFilter = null;
float lightRadius, Color ambientColor, Color spotColor, int flags) {
float lightRadius, uiColor ambientColor, uiColor spotColor, int flags) {
Matrix3 viewMatrix = canvas.getTotalMatrix();
//ambient light

float strokeWidth = 0.5f * (devSpaceOutset - blurRadius);
Paint paint = new Paint {color = ambientColor, strokeWidth = strokeWidth, style = PaintingStyle.fill};
canvas.drawPath(path, paint);
//Paint paint = new Paint {color = ambientColor, strokeWidth = strokeWidth, style = PaintingStyle.fill};
_shadowPaint.color = new Color(ambientColor.value);
_shadowPaint.strokeWidth = strokeWidth;
_shadowPaint.style = PaintingStyle.fill;
canvas.drawPath(path, _shadowPaint);
Matrix3 shadowMatrix = Matrix3.I();
shadowMatrix, ref radius)) {
_shadowMatrix, ref radius)) {
canvas.setMatrix(shadowMatrix);
Paint paint2 = new Paint {color = spotColor};
canvas.drawPath(path, paint2);
canvas.setMatrix(_shadowMatrix);
//Paint paint2 = new Paint {color = spotColor};
_shadowPaint.color = new Color(spotColor.value);
_shadowPaint.strokeWidth = 0;
_shadowPaint.style = PaintingStyle.fill;
canvas.drawPath(path, _shadowPaint);
canvas.restore();
}

16
Runtime/ui/utils/renderer/compositeCanvas/flow/physical_shape_layer.cs


public static void drawShadow(Canvas canvas, Path path, Color color, float elevation, bool transparentOccluder,
float dpr) {
Color inAmbient = color.withAlpha((int) (kAmbientAlpha * color.alpha));
Color inSpot = color.withAlpha((int) (kSpotAlpha * color.alpha));
Color ambientColor = null;
Color spotColor = null;
uiColor uicolor = uiColor.fromColor(color);
uiColor inAmbient = uicolor.withAlpha((int) (kAmbientAlpha * uicolor.alpha));
uiColor inSpot = uicolor.withAlpha((int) (kSpotAlpha * uicolor.alpha));
uiColor? ambientColor = null;
uiColor? spotColor = null;
ShadowUtils.computeTonalColors(inAmbient, inSpot, ref ambientColor, ref spotColor);
ShadowUtils.drawShadow(
canvas,

dpr * kLightRadius,
ambientColor,
spotColor,
ambientColor.Value,
spotColor.Value,
0
);
}
正在加载...
取消
保存