浏览代码

Merge pull request #95 from UnityTech/kgdev

some fixes.
/main
GitHub 6 年前
当前提交
dbb685b0
共有 15 个文件被更改,包括 677 次插入138 次删除
  1. 10
      Runtime/editor/editor_window.cs
  2. 3
      Runtime/editor/rasterizer.cs
  3. 39
      Runtime/editor/surface.cs
  4. 4
      Runtime/engine/UIWidgetsPanel.cs
  5. 23
      Runtime/flow/compositor_context.cs
  6. 1
      Runtime/flow/layer.cs
  7. 8
      Runtime/flow/layer_tree.cs
  8. 2
      Runtime/flow/picture_layer.cs
  9. 29
      Runtime/flow/raster_cache.cs
  10. 20
      Runtime/ui/painting/canvas_impl.cs
  11. 113
      Runtime/ui/painting/canvas_shader.cs
  12. 43
      Runtime/ui/painting/txt/mesh_generator.cs
  13. 9
      Tests/Editor/CanvasAndLayers.cs
  14. 504
      Samples/UIWidgetsGallery/gallery.unity
  15. 7
      Samples/UIWidgetsGallery/gallery.unity.meta

10
Runtime/editor/editor_window.cs


public void OnEnable() {
this._devicePixelRatio = this.queryDevicePixelRatio();
this._antiAliasing = this.queryAntiAliasing();
var size = this.queryWindowSize();
this._lastWindowWidth = size.x;
this._lastWindowHeight = size.y;

return true;
}
if (this._antiAliasing != this.queryAntiAliasing()) {
return true;
}
var size = this.queryWindowSize();
if (this._lastWindowWidth != size.x
|| this._lastWindowHeight != size.y) {

using (this.getScope()) {
if (this.displayMetricsChanged()) {
this._devicePixelRatio = this.queryDevicePixelRatio();
this._antiAliasing = this.queryAntiAliasing();
var size = this.queryWindowSize();
this._lastWindowWidth = size.x;
this._lastWindowHeight = size.y;

protected abstract Vector2 queryWindowSize();
protected virtual Surface createSurface() {
return new EditorWindowSurface(antiAliasing: this.antiAliasing);
return new WindowSurfaceImpl();
}
void _beginFrame() {

layerTree.frameSize = this._physicalSize;
layerTree.devicePixelRatio = this._devicePixelRatio;
layerTree.antiAliasing = this._antiAliasing;
this._rasterizer.draw(layerTree);
}

3
Runtime/editor/rasterizer.cs


bool _drawToSurface(LayerTree layerTree) {
D.assert(this._surface != null);
var frame = this._surface.acquireFrame(layerTree.frameSize, layerTree.devicePixelRatio);
var frame = this._surface.acquireFrame(
layerTree.frameSize, layerTree.devicePixelRatio, layerTree.antiAliasing);
if (frame == null) {
return false;
}

39
Runtime/editor/surface.cs


}
public interface Surface : IDisposable {
SurfaceFrame acquireFrame(Size size, float devicePixelRatio);
SurfaceFrame acquireFrame(Size size, float devicePixelRatio, int antiAliasing);
int getAntiAliasing();
public class EditorWindowSurface : Surface {
public class WindowSurfaceImpl : Surface {
static Material _guiTextureMat;
static Material _uiDefaultMat;

GrSurface _surface;
readonly DrawToTargetFunc _drawToTargetFunc;
MeshPool _meshPool = new MeshPool();
int _antiAliasing;
public int getAntiAliasing() {
return this._antiAliasing;
}
public EditorWindowSurface(DrawToTargetFunc drawToTargetFunc = null, int antiAliasing = Window.defaultAntiAliasing) {
public WindowSurfaceImpl(DrawToTargetFunc drawToTargetFunc = null) {
this._antiAliasing = antiAliasing;
public SurfaceFrame acquireFrame(Size size, float devicePixelRatio) {
this._createOrUpdateRenderTexture(size, devicePixelRatio);
public SurfaceFrame acquireFrame(Size size, float devicePixelRatio, int antiAliasing) {
this._createOrUpdateRenderTexture(size, devicePixelRatio, antiAliasing);
return new SurfaceFrame(this._surface,
(frame, canvas) => this._presentSurface(canvas));

return true;
}
void _createOrUpdateRenderTexture(Size size, float devicePixelRatio) {
void _createOrUpdateRenderTexture(Size size, float devicePixelRatio, int antiAliasing) {
&& this._surface.getRenderTexture() != null) {
&& this._surface.antiAliasing == antiAliasing
&& this._surface.getRenderTexture() != null) {
return;
}

}
this._surface = new GrSurface(size, devicePixelRatio, this._meshPool, this._antiAliasing);
this._surface = new GrSurface(size, devicePixelRatio, antiAliasing, this._meshPool);
}
}

public readonly float devicePixelRatio;
public readonly int antiAliasing;
readonly MeshPool _meshPool;

int _antiAliasing;
public int getAntiAliasing() {
return this._antiAliasing;
}
public RenderTexture getRenderTexture() {
return this._renderTexture;
}

this._canvas = new CommandBufferCanvas(
this._renderTexture, this.devicePixelRatio, this._meshPool, this._antiAliasing);
this._renderTexture, this.devicePixelRatio, this._meshPool);
public GrSurface(Size size, float devicePixelRatio, MeshPool meshPool, int antiAliasing = Window.defaultAntiAliasing) {
public GrSurface(Size size, float devicePixelRatio, int antiAliasing, MeshPool meshPool) {
this._antiAliasing = antiAliasing;
this.antiAliasing = antiAliasing;
var desc = new RenderTextureDescriptor(
(int) this.size.width, (int) this.size.height,

4
Runtime/engine/UIWidgetsPanel.cs


}
protected override Surface createSurface() {
return new EditorWindowSurface(this._uiWidgetsPanel.applyRenderTexture, this._antiAliasing);
return new WindowSurfaceImpl(this._uiWidgetsPanel.applyRenderTexture);
}
public override GUIContent titleContent {

public int antiAliasing {
get {
return this.antiAliasingOverride >= 0 ? this.antiAliasingOverride : Window.defaultAntiAliasing;
return this.antiAliasingOverride >= 0 ? this.antiAliasingOverride : QualitySettings.antiAliasing;
}
}

23
Runtime/flow/compositor_context.cs


public class ScopedFrame : IDisposable {
readonly CompositorContext _context;
readonly Canvas _canvas;
readonly bool _instrumentation_enabled;
readonly bool _instrumentationEnabled;
public ScopedFrame(CompositorContext context, Canvas canvas, bool instrumentation_enabled) {
public ScopedFrame(CompositorContext context, Canvas canvas, bool instrumentationEnabled) {
this._instrumentation_enabled = instrumentation_enabled;
this._context._beginFrame(this, this._instrumentation_enabled);
this._instrumentationEnabled = instrumentationEnabled;
this._context._beginFrame(this, this._instrumentationEnabled);
}
public CompositorContext context() {

}
public void Dispose() {
this._context._endFrame(this, this._instrumentation_enabled);
this._context._endFrame(this, this._instrumentationEnabled);
}
}

this._frameTime = new Stopwatch();
}
public ScopedFrame acquireFrame(Canvas canvas, bool instrumentation_enabled) {
return new ScopedFrame(this, canvas, instrumentation_enabled);
public ScopedFrame acquireFrame(Canvas canvas, bool instrumentationEnabled) {
return new ScopedFrame(this, canvas, instrumentationEnabled);
this._rasterCache.antiAliasing = surface.getAntiAliasing();
}
public void onGrContextDestroyed() {

return this._frameTime;
}
void _beginFrame(ScopedFrame frame, bool enable_instrumentation) {
if (enable_instrumentation) {
void _beginFrame(ScopedFrame frame, bool enableInstrumentation) {
if (enableInstrumentation) {
void _endFrame(ScopedFrame frame, bool enable_instrumentation) {
void _endFrame(ScopedFrame frame, bool enableInstrumentation) {
if (enable_instrumentation) {
if (enableInstrumentation) {
this._frameTime.stop();
}
}

1
Runtime/flow/layer.cs


public class PrerollContext {
public RasterCache rasterCache;
public float devicePixelRatio;
public int antiAliasing;
public Rect cullRect;
public Stopwatch frameTime;
}

8
Runtime/flow/layer_tree.cs


get { return this._devicePixelRatio; }
set { this._devicePixelRatio = value; }
}
int _antiAliasing;
public int antiAliasing {
get { return this._antiAliasing; }
set { this._antiAliasing = value; }
}
static readonly Matrix3 _identityMatrix = Matrix3.I();

devicePixelRatio = frame.canvas().getDevicePixelRatio(),
antiAliasing = this.antiAliasing,
cullRect = Rect.largest,
frameTime = frame.context().frameTime()
};

2
Runtime/flow/picture_layer.cs


ctm[5] = ctm[5].alignToPixel(context.devicePixelRatio);
this._rasterCacheResult = context.rasterCache.getPrerolledImage(
this._picture, ctm, context.devicePixelRatio, this._isComplex, this._willChange);
this._picture, ctm, context.devicePixelRatio, context.antiAliasing, this._isComplex, this._willChange);
} else {
this._rasterCacheResult = null;
}

29
Runtime/flow/raster_cache.cs


}
class _RasterCacheKey : IEquatable<_RasterCacheKey> {
internal _RasterCacheKey(Picture picture, Matrix3 matrix, float devicePixelRatio) {
internal _RasterCacheKey(Picture picture, Matrix3 matrix, float devicePixelRatio, int antiAliasing) {
D.assert(picture != null);
D.assert(matrix != null);
this.picture = picture;

this.matrix[2] = 0.0f;
this.matrix[5] = 0.0f;
this.devicePixelRatio = devicePixelRatio;
this.antiAliasing = antiAliasing;
}
public readonly Picture picture;

public readonly float devicePixelRatio;
public readonly int antiAliasing;
public bool Equals(_RasterCacheKey other) {
if (ReferenceEquals(null, other)) {
return false;

return Equals(this.picture, other.picture) &&
Equals(this.matrix, other.matrix) &&
this.devicePixelRatio.Equals(other.devicePixelRatio);
this.devicePixelRatio.Equals(other.devicePixelRatio) &&
this.antiAliasing.Equals(other.antiAliasing);
}
public override bool Equals(object obj) {

var hashCode = (this.picture != null ? this.picture.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (this.matrix != null ? this.matrix.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ this.devicePixelRatio.GetHashCode();
hashCode = (hashCode * 397) ^ this.antiAliasing.GetHashCode();
return hashCode;
}
}

readonly Dictionary<_RasterCacheKey, _RasterCacheEntry> _cache;
MeshPool _meshPool;
public int antiAliasing {
set { this._antiAliasing = value; }
}
int _antiAliasing = Window.defaultAntiAliasing;
Picture picture, Matrix3 transform, float devicePixelRatio, bool isComplex, bool willChange) {
Picture picture, Matrix3 transform, float devicePixelRatio, int antiAliasing, bool isComplex, bool willChange) {
if (this.threshold == 0) {
return null;
}

return null;
}
_RasterCacheKey cacheKey = new _RasterCacheKey(picture, transform, devicePixelRatio);
_RasterCacheKey cacheKey = new _RasterCacheKey(picture, transform, devicePixelRatio, antiAliasing);
var entry = this._cache.putIfAbsent(cacheKey, () => new _RasterCacheEntry());

if (entry.image == null) {
D.assert(this._meshPool != null);
entry.image = this._rasterizePicture(picture, transform, devicePixelRatio, this._meshPool);
entry.image = this._rasterizePicture(picture, transform, devicePixelRatio, antiAliasing, this._meshPool);
}
return entry.image;

}
RasterCacheResult _rasterizePicture(Picture picture, Matrix3 transform, float devicePixelRatio,
MeshPool meshPool) {
int antiAliasing, MeshPool meshPool) {
var bounds = transform.mapRect(picture.paintBounds).roundOut(devicePixelRatio);
var desc = new RenderTextureDescriptor(

autoGenerateMips = false,
};
if (this._antiAliasing != 0) {
desc.msaaSamples = this._antiAliasing;
if (antiAliasing != 0) {
desc.msaaSamples = antiAliasing;
var canvas = new CommandBufferCanvas(renderTexture, devicePixelRatio, meshPool, this._antiAliasing);
var canvas = new CommandBufferCanvas(renderTexture, devicePixelRatio, meshPool);
canvas.translate(-bounds.left, -bounds.top);
canvas.concat(transform);
canvas.drawPicture(picture);

20
Runtime/ui/painting/canvas_impl.cs


readonly float _fringeWidth;
readonly float _devicePixelRatio;
readonly MeshPool _meshPool;
readonly int _antiAliasing;
public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool, int antiAliasing) {
public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool) {
D.assert(renderTexture);
D.assert(devicePixelRatio > 0);
D.assert(meshPool != null);

this._devicePixelRatio = devicePixelRatio;
this._meshPool = meshPool;
this._antiAliasing = antiAliasing;
}
public float getDevicePixelRatio() {

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;
}

autoGenerateMips = false,
};
if (this._antiAliasing != 0) {
desc.msaaSamples = this._antiAliasing;
if (this._renderTexture.antiAliasing != 0) {
desc.msaaSamples = this._renderTexture.antiAliasing;
}
cmdBuf.GetTemporaryRT(subLayer.rtID, desc, subLayer.filterMode);

public class CommandBufferCanvas : RecorderCanvas {
readonly PictureFlusher _flusher;
public CommandBufferCanvas(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool, int antiAliasing = Window.defaultAntiAliasing)
public CommandBufferCanvas(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool)
this._flusher = new PictureFlusher(renderTexture, devicePixelRatio, meshPool, antiAliasing);
this._flusher = new PictureFlusher(renderTexture, devicePixelRatio, meshPool);
}
public override float getDevicePixelRatio() {

113
Runtime/ui/painting/canvas_shader.cs


_stencilMat = new Material(stencilShader) {hideFlags = HideFlags.HideAndDontSave};
_filterMat = new Material(filterShader) {hideFlags = HideFlags.HideAndDontSave};
}
static readonly int _viewportId = Shader.PropertyToID("_viewport");
static readonly int _alphaId = Shader.PropertyToID("_alpha");
static readonly int _colorId = Shader.PropertyToID("_color");
static readonly int _shaderMatId = Shader.PropertyToID("_shaderMat");
static readonly int _shaderTexId = Shader.PropertyToID("_shaderTex");
static readonly int _leftColorId = Shader.PropertyToID("_leftColor");
static readonly int _rightColorId = Shader.PropertyToID("_rightColor");
static readonly int _tileModeId = Shader.PropertyToID("_tileMode");
static readonly int _biasId = Shader.PropertyToID("_bias");
static readonly int _scaleId = Shader.PropertyToID("_scale");
static readonly int _texId = Shader.PropertyToID("_tex");
static readonly int _texModeId = Shader.PropertyToID("_texMode");
static readonly int _mfRadiusId = Shader.PropertyToID("_mf_radius");
static readonly int _mfImgIncId = Shader.PropertyToID("_mf_imgInc");
static readonly int _mfKernelId = Shader.PropertyToID("_mf_kernel");
static Vector4 _colorToVector4(Color c) {
return new Vector4(

}
static void _getShaderPassAndProps(
PictureFlusher.RenderLayer layer, Paint paint, MeshMesh mesh, float alpha,
PictureFlusher.RenderLayer layer, Paint paint, Matrix3 meshMatrix, float alpha,
props.SetVector("_viewport", viewport);
props.SetFloat("_alpha", alpha);
props.SetVector(_viewportId, viewport);
props.SetFloat(_alphaId, alpha);
props.SetVector("_color", _colorToVector4(paint.color));
props.SetVector(_colorId, _colorToVector4(paint.color));
props.SetMatrix("_shaderMat", linear.getGradientMat(
_getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
props.SetTexture("_shaderTex", linear.gradientTex.texture);
props.SetVector("_leftColor", _colorToVector4(linear.leftColor));
props.SetVector("_rightColor", _colorToVector4(linear.rightColor));
props.SetInt("_tileMode", (int) linear.tileMode);
props.SetMatrix(_shaderMatId, linear.getGradientMat(
_getShaderMatBase(layer.currentState, meshMatrix)).toMatrix4x4());
props.SetTexture(_shaderTexId, linear.gradientTex.texture);
props.SetVector(_leftColorId, _colorToVector4(linear.leftColor));
props.SetVector(_rightColorId, _colorToVector4(linear.rightColor));
props.SetInt(_tileModeId, (int) linear.tileMode);
props.SetMatrix("_shaderMat", radial.getGradientMat(
_getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
props.SetTexture("_shaderTex", radial.gradientTex.texture);
props.SetVector("_leftColor", _colorToVector4(radial.leftColor));
props.SetVector("_rightColor", _colorToVector4(radial.rightColor));
props.SetInt("_tileMode", (int) radial.tileMode);
props.SetMatrix(_shaderMatId, radial.getGradientMat(
_getShaderMatBase(layer.currentState, meshMatrix)).toMatrix4x4());
props.SetTexture(_shaderTexId, radial.gradientTex.texture);
props.SetVector(_leftColorId, _colorToVector4(radial.leftColor));
props.SetVector(_rightColorId, _colorToVector4(radial.rightColor));
props.SetInt(_tileModeId, (int) radial.tileMode);
props.SetMatrix("_shaderMat", sweep.getGradientMat(
_getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
props.SetTexture("_shaderTex", sweep.gradientTex.texture);
props.SetVector("_leftColor", _colorToVector4(sweep.leftColor));
props.SetVector("_rightColor", _colorToVector4(sweep.rightColor));
props.SetInt("_tileMode", (int) sweep.tileMode);
props.SetFloat("_bias", sweep.bias);
props.SetFloat("_scale", sweep.scale);
props.SetMatrix(_shaderMatId, sweep.getGradientMat(
_getShaderMatBase(layer.currentState, meshMatrix)).toMatrix4x4());
props.SetTexture(_shaderTexId, sweep.gradientTex.texture);
props.SetVector(_leftColorId, _colorToVector4(sweep.leftColor));
props.SetVector(_rightColorId, _colorToVector4(sweep.rightColor));
props.SetInt(_tileModeId, (int) sweep.tileMode);
props.SetFloat(_biasId, sweep.bias);
props.SetFloat(_scaleId, sweep.scale);
props.SetMatrix("_shaderMat", image.getShaderMat(
_getShaderMatBase(layer.currentState, mesh.matrix)).toMatrix4x4());
props.SetTexture("_shaderTex", image.image.texture);
props.SetInt("_tileMode", (int) image.tileMode);
props.SetMatrix(_shaderMatId, image.getShaderMat(
_getShaderMatBase(layer.currentState, meshMatrix)).toMatrix4x4());
props.SetTexture(_shaderTexId, image.image.texture);
props.SetInt(_tileModeId, (int) image.tileMode);
return;
default:
throw new Exception("Unknown paint.shader: " + paint.shader);

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,

var pass = 0;
var props = new MaterialPropertyBlock();
props.SetVector("_viewport", viewport);
props.SetVector(_viewportId, viewport);
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,

var pass = 0;
var props = new MaterialPropertyBlock();
props.SetVector("_viewport", viewport);
props.SetVector(_viewportId, viewport);
return new PictureFlusher.CmdDraw {
mesh = mesh,

var pass = 0;
var props = new MaterialPropertyBlock();
props.SetVector("_viewport", viewport);
props.SetVector(_viewportId, viewport);
return new PictureFlusher.CmdDraw {
mesh = mesh,

var pass = 1;
var props = new MaterialPropertyBlock();
props.SetVector("_viewport", viewport);
props.SetVector(_viewportId, viewport);
return new PictureFlusher.CmdDraw {
mesh = mesh,

var pass = 2;
var props = new MaterialPropertyBlock();
props.SetVector("_viewport", viewport);
props.SetVector(_viewportId, viewport);
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);
props.SetTexture("_tex", image.texture);
props.SetInt("_texMode", image.texture is RenderTexture ? 1 : 0); // pre alpha if RT else post alpha
props.SetTexture(_texId, image.texture);
props.SetInt(_texModeId, image.texture is RenderTexture ? 1 : 0); // pre alpha if RT else post alpha
return new PictureFlusher.CmdDraw {
mesh = mesh,

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);
props.SetInt("_texMode", 1); // pre alpha
_getShaderPassAndProps(layer, paint, mesh.matrix, 1.0f, out var pass, out var props);
props.SetInt(_texModeId, 1); // pre alpha
return new PictureFlusher.CmdDraw {
mesh = mesh,

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);
props.SetTexture("_tex", tex);
props.SetInt("_texMode", 2); // alpha only
props.SetTexture(_texId, tex);
props.SetInt(_texModeId, 2); // alpha only
return new PictureFlusher.CmdDraw {
mesh = mesh,

var pass = 0;
var props = new MaterialPropertyBlock();
props.SetVector("_viewport", viewport);
props.SetVector(_viewportId, viewport);
props.SetFloat("_mf_radius", radius);
props.SetVector("_mf_imgInc", imgInc);
props.SetFloatArray("_mf_kernel", kernel);
props.SetFloat(_mfRadiusId, radius);
props.SetVector(_mfImgIncId, imgInc);
props.SetFloatArray(_mfKernelId, kernel);
return new PictureFlusher.CmdDraw {
mesh = mesh,

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),

504
Samples/UIWidgetsGallery/gallery.unity


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!29 &1
OcclusionCullingSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_OcclusionBakeSettings:
smallestOccluder: 5
smallestHole: 0.25
backfaceThreshold: 100
m_SceneGUID: 00000000000000000000000000000000
m_OcclusionCullingData: {fileID: 0}
--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 9
m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3
m_FogDensity: 0.01
m_LinearFogStart: 0
m_LinearFogEnd: 300
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
m_AmbientIntensity: 1
m_AmbientMode: 0
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0}
m_HaloStrength: 0.5
m_FlareStrength: 1
m_FlareFadeSpeed: 3
m_HaloTexture: {fileID: 0}
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0}
m_DefaultReflectionMode: 0
m_DefaultReflectionResolution: 128
m_ReflectionBounces: 1
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.44657892, g: 0.4964127, b: 0.5748172, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 11
m_GIWorkflowMode: 0
m_GISettings:
serializedVersion: 2
m_BounceScale: 1
m_IndirectOutputScale: 1
m_AlbedoBoost: 1
m_EnvironmentLightingMode: 0
m_EnableBakedLightmaps: 1
m_EnableRealtimeLightmaps: 1
m_LightmapEditorSettings:
serializedVersion: 10
m_Resolution: 2
m_BakeResolution: 40
m_AtlasSize: 1024
m_AO: 0
m_AOMaxDistance: 1
m_CompAOExponent: 1
m_CompAOExponentDirect: 0
m_Padding: 2
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
m_TextureCompression: 1
m_FinalGather: 0
m_FinalGatherFiltering: 1
m_FinalGatherRayCount: 256
m_ReflectionCompression: 2
m_MixedBakeMode: 2
m_BakeBackend: 1
m_PVRSampling: 1
m_PVRDirectSampleCount: 32
m_PVRSampleCount: 500
m_PVRBounces: 2
m_PVRFilterTypeDirect: 0
m_PVRFilterTypeIndirect: 0
m_PVRFilterTypeAO: 0
m_PVRFilteringMode: 1
m_PVRCulling: 1
m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 5
m_PVRFilteringGaussRadiusAO: 2
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_ShowResolutionOverlay: 1
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 1
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
agentSlope: 45
agentClimb: 0.4
ledgeDropHeight: 0
maxJumpAcrossDistance: 0
minRegionArea: 2
manualCellSize: 0
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &242452675
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 242452676}
- component: {fileID: 242452678}
- component: {fileID: 242452677}
m_Layer: 5
m_Name: gallery
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &242452676
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 242452675}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1248027221}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &242452677
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 242452675}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9c5c86936ca864ae684720ddcd50d759, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_Texture: {fileID: 0}
m_UVRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
devicePixelRatioOverride: 0
antiAliasingOverride: 4
--- !u!222 &242452678
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 242452675}
m_CullTransparentMesh: 0
--- !u!1 &1152820164
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1152820167}
- component: {fileID: 1152820166}
- component: {fileID: 1152820165}
m_Layer: 0
m_Name: EventSystem
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1152820165
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1152820164}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 1077351063, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_HorizontalAxis: Horizontal
m_VerticalAxis: Vertical
m_SubmitButton: Submit
m_CancelButton: Cancel
m_InputActionsPerSecond: 10
m_RepeatDelay: 0.5
m_ForceModuleActive: 0
--- !u!114 &1152820166
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1152820164}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: -619905303, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_FirstSelected: {fileID: 0}
m_sendNavigationEvents: 1
m_DragThreshold: 10
--- !u!4 &1152820167
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1152820164}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1248027217
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1248027221}
- component: {fileID: 1248027220}
- component: {fileID: 1248027219}
- component: {fileID: 1248027218}
m_Layer: 5
m_Name: Canvas
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1248027218
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1248027217}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 1301386320, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_IgnoreReversedGraphics: 1
m_BlockingObjects: 0
m_BlockingMask:
serializedVersion: 2
m_Bits: 4294967295
--- !u!114 &1248027219
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1248027217}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 1980459831, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_UiScaleMode: 0
m_ReferencePixelsPerUnit: 100
m_ScaleFactor: 1
m_ReferenceResolution: {x: 800, y: 600}
m_ScreenMatchMode: 0
m_MatchWidthOrHeight: 0
m_PhysicalUnit: 3
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
--- !u!223 &1248027220
Canvas:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1248027217}
m_Enabled: 1
serializedVersion: 3
m_RenderMode: 0
m_Camera: {fileID: 0}
m_PlaneDistance: 100
m_PixelPerfect: 0
m_ReceivesEvents: 1
m_OverrideSorting: 0
m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0
m_AdditionalShaderChannelsFlag: 0
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
--- !u!224 &1248027221
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1248027217}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_Children:
- {fileID: 242452676}
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!1 &1304413924
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1304413926}
- component: {fileID: 1304413925}
m_Layer: 0
m_Name: Directional Light
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!108 &1304413925
Light:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1304413924}
m_Enabled: 1
serializedVersion: 8
m_Type: 1
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
m_Intensity: 1
m_Range: 10
m_SpotAngle: 30
m_CookieSize: 10
m_Shadows:
m_Type: 2
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1
m_Bias: 0.05
m_NormalBias: 0.4
m_NearPlane: 0.2
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 4
m_LightShadowCasterMode: 0
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!4 &1304413926
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1304413924}
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
m_LocalPosition: {x: 0, y: 3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &1396321320
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1396321323}
- component: {fileID: 1396321322}
- component: {fileID: 1396321321}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!81 &1396321321
AudioListener:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1396321320}
m_Enabled: 1
--- !u!20 &1396321322
Camera:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1396321320}
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 1
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
m_projectionMatrixMode: 1
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_GateFitMode: 2
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: 0.3
far clip plane: 1000
field of view: 60
orthographic: 0
orthographic size: 5
m_Depth: -1
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 1
m_AllowMSAA: 1
m_AllowDynamicResolution: 0
m_ForceIntoRT: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: 0.022
--- !u!4 &1396321323
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1396321320}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

7
Samples/UIWidgetsGallery/gallery.unity.meta


fileFormatVersion: 2
guid: 97d82ba0e79874aee90750ac8d1661bc
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存