浏览代码

fix textBlob maskFilter and blurFilter.

/main
kg 6 年前
当前提交
ba2503d3
共有 4 个文件被更改,包括 46 次插入37 次删除
  1. 8
      Runtime/ui/painting/canvas_impl.cs
  2. 23
      Runtime/ui/painting/canvas_shader.cs
  3. 43
      Runtime/ui/painting/txt/mesh_generator.cs
  4. 9
      Tests/Editor/CanvasAndLayers.cs

8
Runtime/ui/painting/canvas_impl.cs


var scale = state.scale * this._devicePixelRatio;
var matrix = new Matrix3(state.matrix);
matrix.preTranslate(offset.dx, offset.dy);
matrix.preTranslate(offset.dx, offset.dy);
var textBlobBounds = matrix.mapRect(textBlob.boundsInText);
// request font texture so text mesh could be generated correctly
var style = textBlob.style;

var tex = font.material.mainTexture;
Action<Paint> drawMesh = (Paint p) => {
if (!this._applyClip(matrix.mapRect(textBlob.bounds))) {
if (!this._applyClip(textBlobBounds)) {
return;
}

if (paint.maskFilter != null && paint.maskFilter.sigma != 0) {
this._drawWithMaskFilter(textBlob.bounds, drawMesh, paint, paint.maskFilter);
this._drawWithMaskFilter(textBlobBounds, drawMesh, paint, paint.maskFilter);
return;
}

23
Runtime/ui/painting/canvas_shader.cs


}
static void _getShaderPassAndProps(
PictureFlusher.RenderLayer layer, Paint paint, MeshMesh mesh, float alpha,
PictureFlusher.RenderLayer layer, Paint paint, Matrix3 meshMatrix, float alpha,
out int pass, out MaterialPropertyBlock props) {
Vector4 viewport = layer.viewport;

case _LinearGradient linear:
pass = 1;
props.SetMatrix("_shaderMat", linear.getGradientMat(
_getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
_getShaderMatBase(layer.currentState, meshMatrix)).toMatrix4x4());
props.SetTexture("_shaderTex", linear.gradientTex.texture);
props.SetVector("_leftColor", _colorToVector4(linear.leftColor));
props.SetVector("_rightColor", _colorToVector4(linear.rightColor));

pass = 2;
props.SetMatrix("_shaderMat", radial.getGradientMat(
_getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
_getShaderMatBase(layer.currentState, meshMatrix)).toMatrix4x4());
props.SetTexture("_shaderTex", radial.gradientTex.texture);
props.SetVector("_leftColor", _colorToVector4(radial.leftColor));
props.SetVector("_rightColor", _colorToVector4(radial.rightColor));

pass = 3;
props.SetMatrix("_shaderMat", sweep.getGradientMat(
_getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
_getShaderMatBase(layer.currentState, meshMatrix)).toMatrix4x4());
props.SetTexture("_shaderTex", sweep.gradientTex.texture);
props.SetVector("_leftColor", _colorToVector4(sweep.leftColor));
props.SetVector("_rightColor", _colorToVector4(sweep.rightColor));

case ImageShader image:
pass = 4;
props.SetMatrix("_shaderMat", image.getShaderMat(
_getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
_getShaderMatBase(layer.currentState, meshMatrix)).toMatrix4x4());
props.SetTexture("_shaderTex", image.image.texture);
props.SetInt("_tileMode", (int) image.tileMode);
return;

public static PictureFlusher.CmdDraw convexFill(PictureFlusher.RenderLayer layer, Paint paint,
MeshMesh mesh) {
var mat = _convexFillMat.getMaterial(paint.blendMode, layer.ignoreClip);
_getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);
_getShaderPassAndProps(layer, paint, mesh.matrix, 1.0f, out var pass, out var props);
return new PictureFlusher.CmdDraw {
mesh = mesh,

public static PictureFlusher.CmdDraw fill1(PictureFlusher.RenderLayer layer, Paint paint,
MeshMesh mesh) {
var mat = _fill1Mat.getMaterial(paint.blendMode);
_getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);
_getShaderPassAndProps(layer, paint, mesh.matrix, 1.0f, out var pass, out var props);
return new PictureFlusher.CmdDraw {
mesh = mesh.boundsMesh,

public static PictureFlusher.CmdDraw stroke0(PictureFlusher.RenderLayer layer, Paint paint,
float alpha, MeshMesh mesh) {
var mat = _stroke0Mat.getMaterial(paint.blendMode, layer.ignoreClip);
_getShaderPassAndProps(layer, paint, mesh, alpha, out var pass, out var props);
_getShaderPassAndProps(layer, paint, mesh.matrix, alpha, out var pass, out var props);
return new PictureFlusher.CmdDraw {
mesh = mesh,

public static PictureFlusher.CmdDraw tex(PictureFlusher.RenderLayer layer, Paint paint,
MeshMesh mesh, Image image) {
var mat = _texMat.getMaterial(paint.blendMode, layer.ignoreClip);
_getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);
_getShaderPassAndProps(layer, paint, mesh.matrix, 1.0f, out var pass, out var props);
image.texture.filterMode = paint.filterMode;
props.SetTexture("_tex", image.texture);

public static PictureFlusher.CmdDraw texRT(PictureFlusher.RenderLayer layer, Paint paint,
MeshMesh mesh, PictureFlusher.RenderLayer renderLayer) {
var mat = _texMat.getMaterial(paint.blendMode, layer.ignoreClip);
_getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);
_getShaderPassAndProps(layer, paint, mesh.matrix, 1.0f, out var pass, out var props);
props.SetInt("_texMode", 1); // pre alpha
return new PictureFlusher.CmdDraw {

MeshMesh mesh, TextBlobMesh textMesh, Texture tex) {
var mat = _texMat.getMaterial(paint.blendMode, layer.ignoreClip);
_getShaderPassAndProps(layer, paint, mesh, 1.0f, out var pass, out var props);
var meshMatrix = mesh != null ? mesh.matrix : textMesh.matrix;
_getShaderPassAndProps(layer, paint, meshMatrix, 1.0f, out var pass, out var props);
tex.filterMode = paint.filterMode;
props.SetTexture("_tex", tex);
props.SetInt("_texMode", 2); // alpha only

43
Runtime/ui/painting/txt/mesh_generator.cs


class TextBlobMesh {
static readonly Dictionary<MeshKey, MeshInfo> _meshes = new Dictionary<MeshKey, MeshInfo>();
readonly TextBlob _textBlob;
readonly float _scale;
readonly Matrix3 _transform;
public readonly TextBlob textBlob;
public readonly float scale;
public readonly Matrix3 matrix;
public TextBlobMesh(TextBlob textBlob, float scale, Matrix3 transform) {
this._textBlob = textBlob;
this._scale = scale;
this._transform = transform;
public TextBlobMesh(TextBlob textBlob, float scale, Matrix3 matrix) {
this.textBlob = textBlob;
this.scale = scale;
this.matrix = matrix;
}
public static long frameCount {

this._resolved = true;
var style = this._textBlob.style;
var style = this.textBlob.style;
var key = new MeshKey(this._textBlob.instanceId, this._scale);
var key = new MeshKey(this.textBlob.instanceId, this.scale);
this._mesh = meshInfo.mesh.transform(this._transform);
this._mesh = meshInfo.mesh.transform(this.matrix);
var length = this._textBlob.textSize;
var text = this._textBlob.text;
var fontSizeToLoad = Mathf.CeilToInt(style.UnityFontSize * this._scale);
var length = this.textBlob.textSize;
var text = this.textBlob.text;
var fontSizeToLoad = Mathf.CeilToInt(style.UnityFontSize * this.scale);
var ch = text[charIndex + this._textBlob.textOffset];
var ch = text[charIndex + this.textBlob.textOffset];
var position = this._textBlob.positions[charIndex];
var position = this.textBlob.positions[charIndex];
if (LayoutUtils.isWordSpace(ch) || LayoutUtils.isLineEndSpace(ch) || ch == '\t') {
continue;
}

}
var glyphInfo = font.getGlyphInfo(ch, fontSizeToLoad, style.UnityFontStyle);
var minX = glyphInfo.rect.left / this._scale;
var maxX = glyphInfo.rect.right / this._scale;
var minY = glyphInfo.rect.top / this._scale;
var maxY = glyphInfo.rect.bottom / this._scale;
var minX = glyphInfo.rect.left / this.scale;
var maxX = glyphInfo.rect.right / this.scale;
var minY = glyphInfo.rect.top / this.scale;
var maxY = glyphInfo.rect.bottom / this.scale;
var baseIndex = vertices.Count;

MeshMesh mesh = vertices.Count > 0 ? new MeshMesh(null, vertices, triangles, uv) : null;
_meshes[key] = new MeshInfo(key, mesh, fontInfo.textureVersion);
this._mesh = mesh.transform(this._transform);
this._mesh = mesh.transform(this.matrix);
return this._mesh;
}
}

9
Tests/Editor/CanvasAndLayers.cs


TextBlobBuilder builder = new TextBlobBuilder();
string text = "This is a text blob";
builder.allocRunPos(new TextStyle(), text, 0, text.Length);
builder.setBounds(Unity.UIWidgets.ui.Rect.fromLTWH(0, 0, 200, 50));
builder.setBounds(Unity.UIWidgets.ui.Rect.fromLTWH(-10, -20, 200, 50));
builder.positions = new Vector2d[] {
new Vector2d(10, 0),
new Vector2d(20, 0),

new Vector2d(190, 0),
};
canvas.drawTextBlob(builder.make(), new Offset(100, 100), paint);
var textBlob = builder.make();
canvas.drawTextBlob(textBlob, new Offset(100, 100), new Paint {
color = Colors.black,
maskFilter = MaskFilter.blur(BlurStyle.normal, 5),
});
canvas.drawTextBlob(textBlob, new Offset(100, 100), paint);
canvas.drawLine(
new Offset(10, 30),

正在加载...
取消
保存