浏览代码

cache shadowUtil's path

replace delegates
/main
xingwei.zhu 5 年前
当前提交
bcd38152
共有 4 个文件被更改,包括 67 次插入38 次删除
  1. 4
      Runtime/ui/painting/path.cs
  2. 13
      Runtime/ui/painting/shadow_utils.cs
  3. 14
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
  4. 74
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_utils.cs

4
Runtime/ui/painting/path.cs


return sb.ToString();
}
public void resetAll() {
this._reset();
}
void _reset() {
this._commands.Clear();
this._commandx = 0;

13
Runtime/ui/painting/shadow_utils.cs


outSpotColor = inSpotColor;
}
static Matrix3 _toHomogeneous = Matrix3.I();
public static bool getSpotShadowTransform(Vector3 lightPos, float lightRadius, Matrix3 ctm,
Vector3 zPlaneParams, Rect pathBounds, Matrix3 shadowTransform, ref float radius) {
float heightFunc(float x, float y) {

h0.y / h2.z, h1.y / h2.z, h2.y / h2.z,
h0.z / h2.z, h1.z / h2.z, 1);
Matrix3 toHomogeneous = Matrix3.I();
_toHomogeneous.reset();
toHomogeneous.setAll(xScale, 0, -xScale * pathBounds.left - 1,
_toHomogeneous.setAll(xScale, 0, -xScale * pathBounds.left - 1,
shadowTransform.preConcat(toHomogeneous);
shadowTransform.preConcat(_toHomogeneous);
radius = spotBlurRadius(occluderHeight, lightPos.z, lightRadius);
}

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

Matrix3 viewMatrix = canvas.getTotalMatrix();
//ambient light
Path devSpacePath = path.transform(viewMatrix);
_devSpacePath.resetAll();
_devSpacePath.addPath(path, viewMatrix);
float devSpaceOutset = ambientBlurRadius(zPlaneParams.z);
float oneOverA = ambientRecipAlpha(zPlaneParams.z);
float blurRadius = 0.5f * devSpaceOutset * oneOverA;

canvas.setMatrix(_shadowMatrix);
float sigma = convertRadiusToSigma(blurRadius);
_shadowPaint.maskFilter = MaskFilter.blur(BlurStyle.normal, sigma);
canvas.drawPath(devSpacePath, _shadowPaint);
canvas.drawPath(_devSpacePath, _shadowPaint);
canvas.restore();
//spot light

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


this._fringeWidth = 1.0f / devicePixelRatio;
this._devicePixelRatio = devicePixelRatio;
this._meshPool = meshPool;
this.___drawTextDrawMeshCallback = this._drawTextDrawMeshCallback;
this.___drawPathDrawMeshCallback2 = this._drawPathDrawMeshCallback2;
this.___drawPathDrawMeshCallback = this._drawPathDrawMeshCallback;
readonly _drawPathDrawMeshCallbackDelegate ___drawTextDrawMeshCallback;
readonly _drawPathDrawMeshCallbackDelegate ___drawPathDrawMeshCallback2;
readonly _drawPathDrawMeshCallbackDelegate ___drawPathDrawMeshCallback;
public float getDevicePixelRatio() {
return this._devicePixelRatio;

var mesh = fillMesh.transform(state.matrix);
if (paint.maskFilter != null && paint.maskFilter.Value.sigma != 0) {
this._drawWithMaskFilter(mesh.bounds, paint, paint.maskFilter.Value, mesh, convex, 0, null, uiRectHelper.zero, null, this._drawPathDrawMeshCallback);
this._drawWithMaskFilter(mesh.bounds, paint, paint.maskFilter.Value, mesh, convex, 0, null, uiRectHelper.zero, null, this.___drawPathDrawMeshCallback);
return;
}

var mesh = strokenMesh.transform(state.matrix);
if (paint.maskFilter != null && paint.maskFilter.Value.sigma != 0) {
this._drawWithMaskFilter(mesh.bounds, paint, paint.maskFilter.Value, mesh, false, alpha, null, uiRectHelper.zero, null, this._drawPathDrawMeshCallback2);
this._drawWithMaskFilter(mesh.bounds, paint, paint.maskFilter.Value, mesh, false, alpha, null, uiRectHelper.zero, null, this.___drawPathDrawMeshCallback2);
return;
}

var tex = font.material.mainTexture;
if (paint.maskFilter != null && paint.maskFilter.Value.sigma != 0) {
this._drawWithMaskFilter(textBlobBounds, paint, paint.maskFilter.Value, null, false, 0, tex, textBlobBounds, mesh, this._drawTextDrawMeshCallback);
this._drawWithMaskFilter(textBlobBounds, paint, paint.maskFilter.Value, null, false, 0, tex, textBlobBounds, mesh, this.___drawTextDrawMeshCallback);
return;
}

74
Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_utils.cs


using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.UIWidgets.foundation;
using UnityEngine;

static readonly Dictionary<int, float[]> _gaussianKernels
= new Dictionary<int, float[]>();
static float[] calculateKernel(float _cur_gaussian_sigma, int _cur_width, int _cur_radius) {
var kernel = new float[25];
float twoSigmaSqrd = 2.0f * _cur_gaussian_sigma * _cur_gaussian_sigma;
if (ScalarUtils.ScalarNearlyZero(twoSigmaSqrd)) {
for (int i = 0; i < _cur_width; ++i) {
kernel[i] = 0.0f;
}
return kernel;
}
float denom = 1.0f / twoSigmaSqrd;
float sum = 0.0f;
for (int i = 0; i < _cur_width; ++i) {
float x = i - _cur_radius;
// Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
// is dropped here, since we renormalize the kernel below.
kernel[i] = Mathf.Exp(-x * x * denom);
sum += kernel[i];
}
// Normalize the kernel
float scale = 1.0f / sum;
for (int i = 0; i < _cur_width; ++i) {
kernel[i] *= scale;
}
_cur_gaussian_sigma = -1;
_cur_radius = -1;
return kernel;
}
public static float[] get1DGaussianKernel(float gaussianSigma, int radius) {
var width = 2 * radius + 1;
D.assert(width <= 25);

D.assert(radius < 10000);
int key = (int)(gaussianSigma * 1000000) + radius;
return _gaussianKernels.putIfAbsent(key, () => {
var kernel = new float[25];
float twoSigmaSqrd = 2.0f * gaussianSigma * gaussianSigma;
float[] value;
if (_gaussianKernels.TryGetValue(key, out value)) {
return value;
}
if (ScalarUtils.ScalarNearlyZero(twoSigmaSqrd)) {
for (int i = 0; i < width; ++i) {
kernel[i] = 0.0f;
}
return kernel;
}
float denom = 1.0f / twoSigmaSqrd;
float sum = 0.0f;
for (int i = 0; i < width; ++i) {
float x = i - radius;
// Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
// is dropped here, since we renormalize the kernel below.
kernel[i] = Mathf.Exp(-x * x * denom);
sum += kernel[i];
}
// Normalize the kernel
float scale = 1.0f / sum;
for (int i = 0; i < width; ++i) {
kernel[i] *= scale;
}
return kernel;
});
value = calculateKernel(gaussianSigma, width, radius);
_gaussianKernels[key] = value;
return value;
}
public static float adjustSigma(float sigma, out int scaleFactor, out int radius) {

正在加载...
取消
保存