浏览代码

Merge branch 'develop_matrix3' into 'master'

fix issue2 matrix4x4 -> matrix3

See merge request upm-packages/ui-widgets/com.unity.uiwidgets!5
/main
Shenhua Gu 6 年前
当前提交
c6729b4f
共有 18 个文件被更改,包括 142 次插入172 次删除
  1. 1
      Runtime/flow/container_layer.cs
  2. 82
      Runtime/painting/matrix_utils.cs
  3. 15
      Runtime/rendering/box.cs
  4. 19
      Runtime/rendering/layer.cs
  5. 24
      Runtime/rendering/object.cs
  6. 49
      Runtime/rendering/proxy_box.cs
  7. 2
      Runtime/rendering/proxy_box.mixin.gen.cs
  8. 2
      Runtime/rendering/proxy_box.mixin.njk
  9. 14
      Runtime/rendering/sliver.cs
  10. 2
      Runtime/rendering/sliver_multi_box_adaptor.cs
  11. 2
      Runtime/rendering/sliver_padding.cs
  12. 10
      Runtime/rendering/view.cs
  13. 14
      Runtime/rendering/viewport.cs
  14. 4
      Runtime/ui/compositing.cs
  15. 31
      Runtime/ui/matrix.cs
  16. 15
      Runtime/widgets/basic.cs
  17. 9
      Runtime/widgets/container.cs
  18. 19
      Runtime/widgets/widget_inspector.cs

1
Runtime/flow/container_layer.cs


using System.Collections.Generic;
using Unity.UIWidgets.ui;
using Matrix4x4 = UnityEngine.Matrix4x4;
namespace Unity.UIWidgets.flow {
public abstract class ContainerLayer : Layer {

82
Runtime/painting/matrix_utils.cs


namespace Unity.UIWidgets.painting {
public static class MatrixUtils {
public static Offset transformPoint(this Matrix4x4 transform, Offset point) {
var position3 = new Vector3((float) point.dx, (float) point.dy, 0);
var transformed3 = transform.MultiplyPoint(position3);
return new Offset(transformed3.x, transformed3.y);
}
public static Rect transformRect(this Matrix4x4 transform, Offset[] points, out bool isRect) {
D.assert(points != null && points.Length == 4, "expected 4 points");
var topLeft = transform.transformPoint(points[0]);
var topRight = transform.transformPoint(points[1]);
var bottomLeft = transform.transformPoint(points[2]);
var bottomRight = transform.transformPoint(points[3]);
isRect = topLeft.dy == topRight.dy
&& topRight.dx == bottomRight.dx
&& bottomRight.dy == bottomLeft.dy
&& bottomLeft.dx == topLeft.dx;
var left = Math.Min(Math.Min(Math.Min(topLeft.dx, topRight.dx), bottomLeft.dx), bottomRight.dx);
var right = Math.Max(Math.Max(Math.Max(topLeft.dx, topRight.dx), bottomLeft.dx), bottomRight.dx);
var top = Math.Min(Math.Min(Math.Min(topLeft.dy, topRight.dy), bottomLeft.dy), bottomRight.dy);
var bottom = Math.Max(Math.Max(Math.Max(topLeft.dy, topRight.dy), bottomLeft.dy), bottomRight.dy);
return Rect.fromLTRB(left, top, right, bottom);
}
public static Rect transformRect(this Matrix4x4 transform, Rect rect, out bool isRect) {
return transform.transformRect(
new[] {rect.topLeft, rect.topRight, rect.bottomLeft, rect.bottomRight},
out isRect);
}
public static Rect transformRect(this Matrix4x4 transform, Offset[] points) {
bool isRect;
return transform.transformRect(points, out isRect);
}
public static Rect transformRect(this Matrix4x4 transform, Rect rect) {
bool isRect;
return transform.transformRect(rect, out isRect);
}
public static Rect inverseTransformRect(this Matrix4x4 transform, Rect rect) {
D.assert(rect != null);
D.assert(transform.determinant != 0.0);
if (transform.isIdentity) {
return rect;
}
var inverse = transform.inverse;
return inverse.transformRect(rect);
}
public static Offset getAsTranslation(this Matrix4x4 transform) {
if (transform.m00 == 1.0 &&
transform.m10 == 0.0 &&
transform.m20 == 0.0 &&
transform.m30 == 0.0 &&
transform.m01 == 0.0 &&
transform.m11 == 1.0 &&
transform.m21 == 0.0 &&
transform.m31 == 0.0 &&
transform.m02 == 0.0 &&
transform.m12 == 0.0 &&
transform.m22 == 1.0 &&
transform.m32 == 0.0 &&
transform.m23 == 0.0 &&
transform.m33 == 1.0) {
return new Offset(transform.m03, transform.m13);
}
return null;
}
public static bool isPerspective(this Matrix4x4 transform) {
return transform[3, 0] != 0 || transform[3, 1] != 0 || transform[3, 2] != 0 || transform[3, 3] != 1;
public static Offset getAsTranslation(this Matrix3 matrix3)
{
return matrix3.isTranslate() ? new Offset(matrix3[Matrix3.kMTransX], matrix3[Matrix3.kMTransY]) : null;
}
}
}

15
Runtime/rendering/box.cs


return false;
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
D.assert(child != null);
D.assert(child.parent == this);
D.assert(() => {

var childParentData = (BoxParentData) child.parentData;
var offset = childParentData.offset;
transform = Matrix4x4.Translate(offset.toVector()) * transform;
transform = Matrix3.makeTrans(offset) * transform;
if (transform.determinant == 0) {
return Offset.zero;
}
var inverse = Matrix3.I();
var invertible = transform.invert(inverse);
return invertible ? inverse.mapPoint(point) : Offset.zero;
transform = transform.inverse;
return transform.transformPoint(point);
return this.getTransformTo(ancestor).transformPoint(point);
return this.getTransformTo(ancestor).mapPoint(point);
}
public override Rect paintBounds {

19
Runtime/rendering/layer.cs


using System.Collections.Generic;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.painting;
using UnityEngine;
using Rect = Unity.UIWidgets.ui.Rect;

}
}
public virtual void applyTransform(Layer child, ref Matrix4x4 transform) {
public virtual void applyTransform(Layer child, ref Matrix3 transform) {
D.assert(child != null);
}

}
public class TransformLayer : OffsetLayer {
public TransformLayer(Matrix4x4? transform = null, Offset offset = null) : base(offset) {
this._transform = transform ?? Matrix4x4.identity;
public TransformLayer(Matrix3 transform = null, Offset offset = null) : base(offset) {
this._transform = transform ?? Matrix3.I();
public Matrix4x4 transform {
public Matrix3 transform {
Matrix4x4 _transform;
Matrix4x4 _lastEffectiveTransform;
Matrix3 _transform;
Matrix3 _lastEffectiveTransform;
public override void addToScene(SceneBuilder builder, Offset layerOffset) {
this._lastEffectiveTransform = this.transform;

this._lastEffectiveTransform =
Matrix4x4.Translate(totalOffset.toVector()) * this._lastEffectiveTransform;
Matrix3.makeTrans(totalOffset) * this._lastEffectiveTransform;
}
builder.pushTransform(this._lastEffectiveTransform);

public override void applyTransform(Layer child, ref Matrix4x4 transform) {
public override void applyTransform(Layer child, ref Matrix3 transform) {
D.assert(child != null);
transform = transform * this._lastEffectiveTransform;
}

properties.add(new DiagnosticsProperty<Matrix4x4>("transform", this.transform));
properties.add(new DiagnosticsProperty<Matrix3>("transform", this.transform));
}
}

24
Runtime/rendering/object.cs


}
}
public void pushTransform(bool needsCompositing, Offset offset, Matrix4x4 transform,
public void pushTransform(bool needsCompositing, Offset offset, Matrix3 transform,
var effectiveTransform = Matrix4x4.Translate(offset.toVector())
* transform * Matrix4x4.Translate(-offset.toVector());
var effectiveTransform = Matrix3.makeTrans(offset)
* transform * Matrix3.makeTrans(-offset);
if (needsCompositing) {
if (needsCompositing)
{
var inverse = Matrix3.I();
var invertible = effectiveTransform.invert(inverse);
D.assert(invertible);
childPaintBounds: effectiveTransform.inverseTransformRect(this.estimatedBounds)
childPaintBounds: inverse.mapRect(this.estimatedBounds)
this.canvas.concat(effectiveTransform.toMatrix3());
this.canvas.concat(effectiveTransform);
painter(this, offset);
this.canvas.restore();
}

public virtual void paint(PaintingContext context, Offset offset) {
}
public virtual void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public virtual void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
public Matrix4x4 getTransformTo(RenderObject ancestor) {
public Matrix3 getTransformTo(RenderObject ancestor) {
D.assert(this.attached);
if (ancestor == null) {

renderers.Add(renderer);
}
var transform = Matrix4x4.identity;
var transform = Matrix3.I();
return transform;
}

49
Runtime/rendering/proxy_box.cs


public class RenderTransform : RenderProxyBox {
public RenderTransform(
Matrix4x4 transform,
Matrix3 transform,
Offset origin = null,
Alignment alignment = null,
bool transformHitTests = true,

public bool transformHitTests;
public Matrix4x4 transform {
public Matrix3 transform {
set {
if (this._transform == value) {
return;

}
}
Matrix4x4 _transform;
Matrix3 _transform;
this._transform = Matrix4x4.identity;
this._transform = Matrix3.I();
this._transform = Matrix4x4.Rotate(Quaternion.Euler((float) degrees, 0, 0)) * this._transform;
this.markNeedsPaint();
//2D, do nothing
this._transform = Matrix4x4.Rotate(Quaternion.Euler(0, (float) degrees, 0)) * this._transform;
this.markNeedsPaint();
//2D, do nothing
this._transform = Matrix4x4.Rotate(Quaternion.Euler(0, 0, (float) degrees)) * this._transform;
this._transform = Matrix3.makeRotate((float) degrees) * this._transform;
this._transform = Matrix4x4.Translate(new Vector3((float) x, (float) y, (float) z)) * this._transform;
this._transform = Matrix3.makeTrans((float)x, (float)y) * this._transform;
this._transform = Matrix4x4.Scale(new Vector3((float) x, (float) y, (float) z)) * this._transform;
this._transform = Matrix3.makeScale((float) x, (float) y) * this._transform;
Matrix4x4 _effectiveTransform {
Matrix3 _effectiveTransform {
get {
Alignment resolvedAlignment = this.alignment;
if (this._origin == null && resolvedAlignment == null) {

var result = Matrix4x4.identity;
var result = Matrix3.I();
result = Matrix4x4.Translate(new Vector2((float) this._origin.dx, (float) this._origin.dy)) *
result = Matrix3.makeTrans((float) this._origin.dx, (float) this._origin.dy) *
result;
}

result = Matrix4x4.Translate(new Vector2((float) translation.dx, (float) translation.dy)) * result;
result = Matrix3.makeTrans((float) translation.dx, (float) translation.dy) * result;
result = Matrix4x4.Translate(new Vector2((float) -translation.dx, (float) -translation.dy)) *
result = Matrix3.makeTrans((float) -translation.dx, (float) -translation.dy) *
result = Matrix4x4.Translate(new Vector2((float) -this._origin.dx, (float) -this._origin.dy)) *
result = Matrix3.makeTrans((float) -this._origin.dx, (float) -this._origin.dy) *
result;
}

public override bool hitTest(HitTestResult result, Offset position = null) {
return this.hitTestChildren(result, position: position);
}
if (transform.determinant == 0) {
var inverse = Matrix3.I();
var invertible = transform.invert(inverse);
if (!invertible) {
position = transform.inverse.transformPoint(position);
position = inverse.mapPoint(position);
}
return base.hitTestChildren(result, position: position);

if (this.child != null) {
if (this.child != null)
{
if (childOffset == null) {
context.pushTransform(this.needsCompositing, offset, transform, base.paint);
} else {

}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
properties.add(new DiagnosticsProperty<Matrix4x4>("transform matrix", this._transform));
properties.add(new DiagnosticsProperty<Matrix3>("transform matrix", this._transform));
properties.add(new DiagnosticsProperty<Offset>("origin", this.origin));
properties.add(new DiagnosticsProperty<Alignment>("alignment", this.alignment));
properties.add(new DiagnosticsProperty<bool>("transformHitTests", this.transformHitTests));

2
Runtime/rendering/proxy_box.mixin.gen.cs


return false;
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
}
public override void paint(PaintingContext context, Offset offset) {

2
Runtime/rendering/proxy_box.mixin.njk


return false;
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
}
public override void paint(PaintingContext context, Offset offset) {

14
Runtime/rendering/sliver.cs


public class SliverPhysicalParentData : ParentData {
public Offset paintOffset = Offset.zero;
public void applyPaintTransform(ref Matrix4x4 transform) {
transform = Matrix4x4.Translate(this.paintOffset.toVector()) * transform;
public void applyPaintTransform(ref Matrix3 transform) {
transform = Matrix3.makeTrans(this.paintOffset) * transform;
}
public override string ToString() {

return 0.0;
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
D.assert(() => { throw new UIWidgetsError(this.GetType() + " does not implement applyPaintTransform."); });
}

}
public static void applyPaintTransformForBoxChild(this RenderSliver it, RenderBox child,
ref Matrix4x4 transform) {
ref Matrix3 transform) {
bool rightWayUp = _getRightWayUp(it.constraints);
double delta = it.childMainAxisPosition(child);
double crossAxisDelta = it.childCrossAxisPosition(child);

delta = it.geometry.paintExtent - child.size.width - delta;
}
transform = Matrix4x4.Translate(new Vector2((float) delta, (float) crossAxisDelta)) * transform;
transform = Matrix3.makeTrans((float) delta, (float) crossAxisDelta) * transform;
break;
case Axis.vertical:
if (!rightWayUp) {

transform = Matrix4x4.Translate(new Vector2((float) crossAxisDelta, (float) delta)) * transform;
transform = Matrix3.makeTrans((float) crossAxisDelta, (float) delta) * transform;
break;
}
}

return -this.constraints.scrollOffset;
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
D.assert(child != null);
D.assert(child == this.child);

2
Runtime/rendering/sliver_multi_box_adaptor.cs


return childParentData.layoutOffset;
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
this.applyPaintTransformForBoxChild((RenderBox) child, ref transform);
}

2
Runtime/rendering/sliver_padding.cs


return this.beforePadding;
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
D.assert(child != null);
D.assert(child == this.child);

10
Runtime/rendering/view.cs


public readonly double devicePixelRatio;
public Matrix4x4 toMatrix() {
return Matrix4x4.identity;
public Matrix3 toMatrix() {
return Matrix3.I();
}
public override string ToString() {

this.owner.requestVisualUpdate();
}
Matrix4x4 _rootTransform;
Matrix3 _rootTransform;
public Layer _updateMatricesAndCreateNewRootLayer() {
this._rootTransform = this.configuration.toMatrix();

}
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
transform *= this._rootTransform;
base.applyPaintTransform(child, ref transform);
}

get
{
D.assert(_rootTransform != null);
return MatrixUtils.transformRect(_rootTransform, Offset.zero & size);
return _rootTransform.mapRect(Offset.zero & size);
}
}

14
Runtime/rendering/viewport.cs


RenderObject descendant;
rect = rect ?? target.paintBounds;
Matrix4x4 transform;
Matrix3 transform;
if (target is RenderBox) {
RenderBox targetBox = (RenderBox) target;

RenderSliver pivotParent = (RenderSliver) pivot.parent;
transform = targetBox.getTransformTo(pivot);
Rect bounds = transform.transformRect(rect);
Rect bounds = transform.mapRect(rect);
double offset = 0.0;

transform = target.getTransformTo(this);
this.applyPaintTransform(child, ref transform);
Rect targetRect = transform.transformRect(rect);
Rect targetRect = transform.mapRect(rect);
switch (this.axisDirection) {
case AxisDirection.down:

targetOffset = trailingEdgeOffset;
} else {
var transform = descendant.getTransformTo(viewport.parent);
return transform.transformRect(rect ?? descendant.paintBounds);
return transform.mapRect(rect ?? descendant.paintBounds);
}
D.assert(targetOffset != null);

return 0.0;
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
D.assert(child != null);
var childParentData = (SliverPhysicalParentData) child.parentData;

return pinnedExtent;
}
public override void applyPaintTransform(RenderObject child, ref Matrix4x4 transform) {
public override void applyPaintTransform(RenderObject child, ref Matrix3 transform) {
transform = Matrix4x4.Translate(offset.toVector()) * transform;
transform = Matrix3.makeTrans(offset) * transform;
}
protected override double computeChildMainAxisPosition(RenderSliver child, double parentMainAxisPosition) {

4
Runtime/ui/compositing.cs


this._currentLayer = layer;
}
public void pushTransform(Matrix4x4 matrix) {
public void pushTransform(Matrix3 matrix) {
layer.transform = matrix.toMatrix3();
layer.transform = matrix;
this._pushLayer(layer);
}

31
Runtime/ui/matrix.cs


using System;
using System.Collections.Generic;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting;
using UnityEngine;
namespace Unity.UIWidgets.ui {

public static Matrix3 makeTrans(float dx, float dy) {
var m = new Matrix3();
m.setTranslate(dx, dy);
return m;
}
public static Matrix3 makeRotate(float degree)
{
var m = new Matrix3();
m.setRotate(degree);
return m;
}
public static Matrix3 makeTrans(Offset offset)
{
var m = new Matrix3();
m.setTranslate((float)offset.dx, (float)offset.dy);
return m;
}

return hash;
}
}
public static Matrix3 operator *(Matrix3 a, Matrix3 b)
{
return concat(a, b);
}
public override string ToString()
{
return "Matrix3(" + string.Join(",", Array.ConvertAll(fMat, i => i.ToString())) + ")";
}
public Offset mapPoint(Offset point)
{
return this.mapXY((float)point.dx, (float)point.dy);
}
}

15
Runtime/widgets/basic.cs


public class Transform : SingleChildRenderObjectWidget {
public Transform(
Key key = null,
Matrix4x4? transform = null,
Matrix3 transform = null,
Offset origin = null,
Alignment alignment = null,
bool transformHitTests = true,

this.transform = transform.Value;
this.transform = new Matrix3(transform);
this.origin = origin;
this.alignment = alignment;
this.transformHitTests = transformHitTests;

Widget child = null,
double degree = 0.0
) : base(key: key, child: child) {
this.transform = Matrix4x4.Rotate(Quaternion.Euler(0, 0, (float) degree));
this.transform = Matrix3.makeRotate((float) degree);
this.origin = origin;
this.alignment = alignment;
this.transformHitTests = transformHitTests;

Widget child = null
) : base(key: key, child: child) {
D.assert(offset != null);
this.transform = Matrix4x4.Translate(new Vector3((float) offset.dx, (float) offset.dy, 0.0f));
this.transform = Matrix3.makeTrans((float) offset.dx, (float) offset.dy);
this.origin = null;
this.alignment = null;
this.transformHitTests = transformHitTests;

bool transformHitTests = true,
Widget child = null
) : base(key: key, child: child) {
this.transform = Matrix4x4.Scale(new Vector3((float) scale, (float) scale, 1.0f));
this.transform = Matrix3.makeScale((float) scale, (float) scale);
this.origin = origin;
this.alignment = alignment;
this.transformHitTests = transformHitTests;

return new Transform(key, scale, origin, alignment, transformHitTests, child);
}
public readonly Matrix4x4 transform;
public readonly Matrix3 transform;
public override RenderObject createRenderObject(BuildContext context) {
public override RenderObject createRenderObject(BuildContext context)
{
return new RenderTransform(
transform: this.transform,
origin: this.origin,

9
Runtime/widgets/container.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;

double? height = null,
BoxConstraints constraints = null,
EdgeInsets margin = null,
Matrix4x4? transfrom = null,
Matrix3 transfrom = null,
Widget child = null
) : base(key) {
D.assert(margin == null || margin.isNonNegative);

public readonly Decoration foregroundDecoration;
public readonly BoxConstraints constraints;
public readonly EdgeInsets margin;
public readonly Matrix4x4? transform;
public readonly Matrix3 transform;
EdgeInsets _paddingIncludingDecoration {
get {

}
if (this.transform != null) {
current = new Transform(transform: this.transform.Value, child: current);
current = new Transform(transform: new Matrix3(this.transform), child: current);
}
return current;

this.constraints, defaultValue: Diagnostics.kNullDefaultValue));
properties.add(new DiagnosticsProperty<EdgeInsets>("margin",
this.margin, defaultValue: Diagnostics.kNullDefaultValue));
properties.add(ObjectFlagProperty<Matrix4x4?>.has("transform",
properties.add(ObjectFlagProperty<Matrix3>.has("transform",
this.transform));
}
}

19
Runtime/widgets/widget_inspector.cs


List<RenderObject> edgeHits,
Offset position,
RenderObject renderObject,
Matrix4x4 transform
Matrix3 transform
Matrix4x4 inverse = transform.inverse;
var localPosition = MatrixUtils.transformPoint(inverse, position);
var inverse = Matrix3.I();
var invertible = transform.invert(inverse);
if (!invertible)
{
return false;
}
var localPosition = inverse.mapPoint(position);
List<DiagnosticsNode> children = renderObject.debugDescribeChildren();
for (int i = children.Count - 1; i >= 0; --i)

continue;
}
Matrix4x4 childTransform = transform;
var childTransform = transform;
renderObject.applyPaintTransform(child, ref childTransform);
if (_hitTestHelper(hits, edgeHits, position, child, childTransform))
{

class _TransformedRect:IEquatable<_TransformedRect>
{
public readonly Rect rect;
public readonly Matrix4x4 transform;
public readonly Matrix3 transform;
public _TransformedRect(RenderObject obj)
{

var borderPaint = new Paint(){color = _kHighlightedRenderObjectBorderColor, style = PaintingStyle.stroke, strokeWidth = 1};
Rect selectedPaintRect = state.selected.rect.deflate(0.5);
canvas.save();
canvas.setMatrix(state.selected.transform.toMatrix3());
canvas.setMatrix(state.selected.transform);
canvas.drawRect(selectedPaintRect, fillPaint);
canvas.drawRect(selectedPaintRect, borderPaint);
canvas.restore();

canvas.save();
canvas.setMatrix(transformedRect.transform.toMatrix3());
canvas.setMatrix(transformedRect.transform);
canvas.drawRect(transformedRect.rect.deflate(0.5), borderPaint);
canvas.restore();
}

正在加载...
取消
保存