kg
6 年前
当前提交
1b1a743c
共有 30 个文件被更改,包括 663 次插入 和 589 次删除
-
92Assets/UIWidgets/Tests/CanvasAndLayers.cs
-
7Assets/UIWidgets/Tests/Menu.cs
-
45Assets/UIWidgets/editor/editor_window.cs
-
8Assets/UIWidgets/flow/clip_rect_layer.cs
-
2Assets/UIWidgets/flow/layer.cs
-
16Assets/UIWidgets/flow/layer_builder.cs
-
4Assets/UIWidgets/flow/opacity_layer.cs
-
6Assets/UIWidgets/flow/picture_layer.cs
-
2Assets/UIWidgets/flow/transform_layer.cs
-
34Assets/UIWidgets/rendering/binding.cs
-
66Assets/UIWidgets/rendering/layer.cs
-
110Assets/UIWidgets/rendering/object.cs
-
10Assets/UIWidgets/rendering/view.cs
-
19Assets/UIWidgets/scheduler/binding.cs
-
9Assets/UIWidgets/ui/compositing.cs
-
4Assets/UIWidgets/ui/geometry.cs
-
37Assets/UIWidgets/ui/painting/painting.cs
-
2Assets/UIWidgets/ui/painting/picture.cs
-
2scripts/cmds/codegen.js
-
67Assets/UIWidgets/Tests/RenderBoxes.cs
-
3Assets/UIWidgets/Tests/RenderBoxes.cs.meta
-
40Assets/UIWidgets/flow/clip_rrect_layer.cs
-
3Assets/UIWidgets/flow/clip_rrect_layer.cs.meta
-
322Assets/UIWidgets/ui/painting/canvas_impl.cs
-
11Assets/UIWidgets/rendering/binding.gen.cs.meta
-
3Assets/UIWidgets/rendering/binding.njk
-
3Assets/UIWidgets/rendering/binding.njk.meta
-
3Assets/UIWidgets/rendering/binding.gen.cs
-
322Assets/UIWidgets/ui/painting/editor_canvas.cs
-
0/Assets/UIWidgets/ui/painting/canvas_impl.cs.meta
|
|||
using UnityEditor; |
|||
|
|||
namespace Editor.Tests { |
|||
namespace UIWidgets.Tests { |
|||
} |
|||
|
|||
[MenuItem("UIWidgetsTests/RenderBoxes")] |
|||
public static void renderBoxes() { |
|||
EditorWindow.GetWindow(typeof(RenderBoxes)); |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
using System.Linq; |
|||
using UIWidgets.editor; |
|||
using UIWidgets.rendering; |
|||
using UnityEditor; |
|||
using UnityEngine; |
|||
|
|||
namespace UIWidgets.Tests { |
|||
public class RenderBoxes : EditorWindow { |
|||
private readonly Func<RenderBox>[] _options; |
|||
|
|||
private readonly string[] _optionStrings; |
|||
|
|||
private int _selected; |
|||
|
|||
RenderBoxes() { |
|||
this._options = new Func<RenderBox>[] { |
|||
this.test1, |
|||
}; |
|||
this._optionStrings = this._options.Select(x => x.Method.Name).ToArray(); |
|||
this._selected = 0; |
|||
|
|||
this.titleContent = new GUIContent("RenderBoxes"); |
|||
} |
|||
|
|||
private WindowAdapter windowAdapter; |
|||
|
|||
private RendererBindings rendererBindings; |
|||
|
|||
[NonSerialized] private bool hasInvoked = false; |
|||
|
|||
void OnGUI() { |
|||
if (this.windowAdapter != null) { |
|||
this.windowAdapter.OnGUI(); |
|||
} |
|||
|
|||
var selected = EditorGUILayout.Popup("test case", this._selected, this._optionStrings); |
|||
if (selected != this._selected || !this.hasInvoked) { |
|||
this._selected = selected; |
|||
this.hasInvoked = true; |
|||
|
|||
var renderBox = this._options[this._selected](); |
|||
this.rendererBindings.setRoot(renderBox); |
|||
} |
|||
} |
|||
|
|||
void Update() { |
|||
if (this.windowAdapter != null) { |
|||
this.windowAdapter.Update(); |
|||
} |
|||
} |
|||
|
|||
private void OnEnable() { |
|||
this.windowAdapter = new WindowAdapter(this); |
|||
this.rendererBindings = new RendererBindings(this.windowAdapter); |
|||
} |
|||
|
|||
void OnDestroy() { |
|||
this.windowAdapter = null; |
|||
this.rendererBindings = null; |
|||
} |
|||
|
|||
RenderBox test1() { |
|||
return null; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: e773b48a6f94416d89e1f690b34c1379 |
|||
timeCreated: 1534920245 |
|
|||
using UIWidgets.ui; |
|||
using UnityEngine; |
|||
using Rect = UIWidgets.ui.Rect; |
|||
using Color = UIWidgets.ui.Color; |
|||
|
|||
namespace UIWidgets.flow { |
|||
public class ClipRRectLayer : ContainerLayer { |
|||
private RRect _clipRRect; |
|||
|
|||
public RRect clipRRect { |
|||
set { this._clipRRect = value; } |
|||
} |
|||
|
|||
public override void preroll(PrerollContext context, Matrix4x4 matrix) { |
|||
var childPaintBounds = Rect.zero; |
|||
this.prerollChildren(context, matrix, ref childPaintBounds); |
|||
childPaintBounds = childPaintBounds.intersect(this._clipRRect.outerRect); |
|||
|
|||
if (!childPaintBounds.isEmpty) { |
|||
this.paintBounds = childPaintBounds; |
|||
} |
|||
} |
|||
|
|||
public override void paint(PaintContext context) { |
|||
var canvas = context.canvas; |
|||
|
|||
canvas.save(); |
|||
canvas.clipRRect(this._clipRRect); |
|||
var paint = new Paint {color = new Color(0xFFFFFFFF)}; |
|||
canvas.saveLayer(this.paintBounds, paint); |
|||
try { |
|||
this.paintChildren(context); |
|||
} |
|||
finally { |
|||
canvas.restore(); |
|||
canvas.restore(); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 8c1ecaedbb1c4868bd17a21e7466284c |
|||
timeCreated: 1534912541 |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UIWidgets.painting; |
|||
using UnityEditor; |
|||
using UnityEngine; |
|||
|
|||
namespace UIWidgets.ui { |
|||
public class CanvasImpl : Canvas { |
|||
static CanvasImpl() { |
|||
var shader = Shader.Find("UIWidgets/2D Handles Lines"); |
|||
if (shader == null) { |
|||
throw new Exception("UIWidgets/2D Handles Lines not found"); |
|||
} |
|||
|
|||
CanvasImpl.linesMat = new Material(shader); |
|||
CanvasImpl.linesMat.hideFlags = HideFlags.HideAndDontSave; |
|||
|
|||
shader = Shader.Find("UIWidgets/GUIRoundedRect"); |
|||
if (shader == null) { |
|||
throw new Exception("UIWidgets/GUIRoundedRect not found"); |
|||
} |
|||
|
|||
CanvasImpl.guiRoundedRectMat = new Material(shader); |
|||
CanvasImpl.guiRoundedRectMat.hideFlags = HideFlags.HideAndDontSave; |
|||
|
|||
shader = Shader.Find("UIWidgets/GUITextureClip"); |
|||
if (shader == null) { |
|||
throw new Exception("UIWidgets/GUITextureClip not found"); |
|||
} |
|||
|
|||
CanvasImpl.guiTextureClipMat = new Material(shader); |
|||
CanvasImpl.guiTextureClipMat.hideFlags = HideFlags.HideAndDontSave; |
|||
|
|||
shader = Shader.Find("UIWidgets/ShadowRect"); |
|||
if (shader == null) { |
|||
throw new Exception("UIWidgets/ShadowRect not found"); |
|||
} |
|||
|
|||
CanvasImpl.shadowRectMat = new Material(shader); |
|||
CanvasImpl.shadowRectMat.hideFlags = HideFlags.HideAndDontSave; |
|||
} |
|||
|
|||
private static readonly Material linesMat; |
|||
private static readonly Material guiRoundedRectMat; |
|||
private static readonly Material guiTextureClipMat; |
|||
private static readonly Material shadowRectMat; |
|||
|
|||
private Matrix4x4 _transform; |
|||
private ClipRec _clipRec; |
|||
private LayerRec _layerRec; |
|||
private Stack<CanvasRec> _stack; |
|||
|
|||
private Stack<CanvasRec> stack { |
|||
get { return this._stack ?? (this._stack = new Stack<CanvasRec>()); } |
|||
} |
|||
|
|||
public CanvasImpl() { |
|||
this._transform = Matrix4x4.identity; |
|||
} |
|||
|
|||
public void drawPloygon4(Offset[] points, Paint paint) { |
|||
var color = paint.color; |
|||
if (color.alpha > 0) { |
|||
Vector3[] verts = new Vector3 [points.Length]; |
|||
for (int i = 0; i < points.Length; i++) { |
|||
verts[i] = points[i].toVector(); |
|||
} |
|||
|
|||
this.prepareGL(CanvasImpl.linesMat); |
|||
CanvasImpl.linesMat.SetPass(0); |
|||
|
|||
GL.Begin(GL.TRIANGLES); |
|||
GL.Color(color.toColor()); |
|||
for (int index = 0; index < 2; ++index) { |
|||
GL.Vertex(verts[index * 2]); |
|||
GL.Vertex(verts[index * 2 + 1]); |
|||
GL.Vertex(verts[(index * 2 + 2) % 4]); |
|||
GL.Vertex(verts[index * 2]); |
|||
GL.Vertex(verts[(index * 2 + 2) % 4]); |
|||
GL.Vertex(verts[index * 2 + 1]); |
|||
} |
|||
|
|||
GL.End(); |
|||
} |
|||
} |
|||
|
|||
public void drawRect(Rect rect, BorderWidth borderWidth, BorderRadius borderRadius, Paint paint) { |
|||
this.prepareGL(CanvasImpl.guiRoundedRectMat); |
|||
|
|||
CanvasImpl.guiRoundedRectMat.SetFloatArray("UIWidgets_BorderWidth", |
|||
borderWidth == null ? new[] {0f, 0f, 0f, 0f} : borderWidth.toFloatArray()); |
|||
CanvasImpl.guiRoundedRectMat.SetFloatArray("UIWidgets_CornerRadius", |
|||
borderRadius == null ? new[] {0f, 0f, 0f, 0f} : borderRadius.toFloatArray()); |
|||
|
|||
Graphics.DrawTexture(rect.toRect(), EditorGUIUtility.whiteTexture, |
|||
new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0, |
|||
paint.color.toColor(), CanvasImpl.guiRoundedRectMat); |
|||
} |
|||
|
|||
public void drawRectShadow(Rect rect, Paint paint) { |
|||
this.prepareGL(CanvasImpl.shadowRectMat); |
|||
|
|||
CanvasImpl.shadowRectMat.SetFloat("UIWidgets_sigma", (float) paint.blurSigma); |
|||
|
|||
Graphics.DrawTexture(rect.toRect(), EditorGUIUtility.whiteTexture, |
|||
new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0, |
|||
paint.color.toColor(), CanvasImpl.shadowRectMat); |
|||
} |
|||
|
|||
public void drawPicture(Picture picture) { |
|||
this.save(); |
|||
|
|||
int saveCount = 0; |
|||
|
|||
var drawCmds = picture.drawCmds; |
|||
foreach (var drawCmd in drawCmds) { |
|||
if (drawCmd is DrawPloygon4) { |
|||
var drawPloygon4 = (DrawPloygon4) drawCmd; |
|||
this.drawPloygon4(drawPloygon4.points, drawPloygon4.paint); |
|||
} else if (drawCmd is DrawRect) { |
|||
var drawRect = (DrawRect) drawCmd; |
|||
this.drawRect(drawRect.rect, drawRect.borderWidth, drawRect.borderRadius, drawRect.paint); |
|||
} else if (drawCmd is DrawRectShadow) { |
|||
var drawRectShadow = (DrawRectShadow) drawCmd; |
|||
this.drawRectShadow(drawRectShadow.rect, drawRectShadow.paint); |
|||
} else if (drawCmd is DrawPicture) { |
|||
var drawPicture = (DrawPicture) drawCmd; |
|||
this.drawPicture(drawPicture.picture); |
|||
} else if (drawCmd is DrawConcat) { |
|||
this.concat(((DrawConcat) drawCmd).transform); |
|||
} else if (drawCmd is DrawSave) { |
|||
saveCount++; |
|||
this.save(); |
|||
} else if (drawCmd is DrawSaveLayer) { |
|||
saveCount++; |
|||
var drawSaveLayer = (DrawSaveLayer) drawCmd; |
|||
this.saveLayer(drawSaveLayer.rect, drawSaveLayer.paint); |
|||
} else if (drawCmd is DrawRestore) { |
|||
saveCount--; |
|||
if (saveCount < 0) { |
|||
throw new Exception("unmatched save/restore in picture"); |
|||
} |
|||
|
|||
this.restore(); |
|||
} else if (drawCmd is DrawClipRect) { |
|||
var drawClipRect = (DrawClipRect) drawCmd; |
|||
this.clipRect(drawClipRect.rect); |
|||
} else if (drawCmd is DrawClipRRect) { |
|||
var drawClipRRect = (DrawClipRRect) drawCmd; |
|||
this.clipRRect(drawClipRRect.rrect); |
|||
} else { |
|||
throw new Exception("unknown drawCmd: " + drawCmd); |
|||
} |
|||
} |
|||
|
|||
if (saveCount != 0) { |
|||
throw new Exception("unmatched save/restore in picture"); |
|||
} |
|||
|
|||
this.restore(); |
|||
} |
|||
|
|||
public void concat(Matrix4x4 transform) { |
|||
this._transform = transform * this._transform; |
|||
} |
|||
|
|||
public void save() { |
|||
var state = new CanvasRec { |
|||
transform = this._transform, |
|||
clipRect = this._clipRec, |
|||
layerRec = this._layerRec, |
|||
}; |
|||
this.stack.Push(state); |
|||
} |
|||
|
|||
public void saveLayer(Rect bounds, Paint paint) { |
|||
this.save(); |
|||
|
|||
var textureWidth = (int) Math.Round(bounds.width * EditorGUIUtility.pixelsPerPoint); |
|||
var textureHeight = (int) Math.Round(bounds.height * EditorGUIUtility.pixelsPerPoint); |
|||
|
|||
var texture = RenderTexture.GetTemporary( |
|||
textureWidth, textureHeight, 32, |
|||
RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); |
|||
|
|||
RenderTexture.active = texture; |
|||
GL.PushMatrix(); |
|||
GL.LoadPixelMatrix((float) bounds.left, (float) bounds.right, (float) bounds.bottom, (float) bounds.top); |
|||
GL.Clear(true, true, new UnityEngine.Color(0, 0, 0, 0)); |
|||
|
|||
this._layerRec = new LayerRec { |
|||
bounds = bounds, |
|||
paint = paint, |
|||
texture = texture, |
|||
}; |
|||
|
|||
this._transform = Matrix4x4.identity; |
|||
this._clipRec = null; |
|||
} |
|||
|
|||
public void restore() { |
|||
var layerRec = this._layerRec; |
|||
|
|||
var state = this._stack.Pop(); |
|||
this._transform = state.transform; |
|||
this._clipRec = state.clipRect; |
|||
this._layerRec = state.layerRec; |
|||
|
|||
if (layerRec != this._layerRec) { |
|||
RenderTexture.active = this._layerRec != null ? this._layerRec.texture : null; |
|||
GL.PopMatrix(); |
|||
|
|||
this.prepareGL(CanvasImpl.guiTextureClipMat); |
|||
|
|||
Graphics.DrawTexture(layerRec.bounds.toRect(), layerRec.texture, |
|||
new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0, |
|||
layerRec.paint.color.toColor(), CanvasImpl.guiTextureClipMat); |
|||
|
|||
RenderTexture.ReleaseTemporary(layerRec.texture); |
|||
layerRec.texture = null; |
|||
} |
|||
} |
|||
|
|||
public void clipRect(Rect rect) { |
|||
if (rect.isInfinite) { |
|||
return; |
|||
} |
|||
|
|||
this.pushClipRect(rect, this._transform); |
|||
} |
|||
|
|||
public void clipRRect(RRect rect) { |
|||
if (rect.isInfinite) { |
|||
return; |
|||
} |
|||
|
|||
this.pushClipRRect(rect, this._transform); |
|||
} |
|||
|
|||
private void pushClipRect(Rect clipRect, Matrix4x4 transform) { |
|||
if (this._clipRec != null) { |
|||
throw new Exception("already a clipRec, considering using saveLayer."); |
|||
} |
|||
|
|||
this._clipRec = new ClipRec(transform, rect: clipRect); |
|||
} |
|||
|
|||
private void pushClipRRect(RRect clipRRect, Matrix4x4 transform) { |
|||
if (this._clipRec != null) { |
|||
throw new Exception("already a clipRec, considering using saveLayer."); |
|||
} |
|||
|
|||
this._clipRec = new ClipRec(transform, rrect: clipRRect); |
|||
} |
|||
|
|||
private void prepareGL(Material mat) { |
|||
if (this._clipRec != null) { |
|||
mat.SetMatrix("UIWidgets_GUIClipMatrix", this._clipRec.transform.inverse); |
|||
if (this._clipRec.rect != null) { |
|||
var rect = this._clipRec.rect; |
|||
mat.SetVector("UIWidgets_GUIClipRect", new Vector4( |
|||
(float) rect.left, |
|||
(float) rect.top, |
|||
(float) rect.width, |
|||
(float) rect.height)); |
|||
mat.SetVector("UIWidgets_GUIClipRectRadius", new Vector4(0, 0, 0, 0)); |
|||
} else { |
|||
var rrect = this._clipRec.rrect; |
|||
var rect = rrect.outerRect; |
|||
mat.SetVector("UIWidgets_GUIClipRect", new Vector4( |
|||
(float) rect.left, |
|||
(float) rect.top, |
|||
(float) rect.width, |
|||
(float) rect.height)); |
|||
mat.SetVector("UIWidgets_GUIClipRectRadius", |
|||
new Vector4( |
|||
(float) rrect.tlRadius, |
|||
(float) rrect.trRadius, |
|||
(float) rrect.brRadius, |
|||
(float) rrect.blRadius)); |
|||
} |
|||
} else { |
|||
mat.SetMatrix("UIWidgets_GUIClipMatrix", Matrix4x4.identity); |
|||
var rect = Rect.largest; |
|||
mat.SetVector("UIWidgets_GUIClipRect", new Vector4( |
|||
(float) rect.left, |
|||
(float) rect.top, |
|||
(float) rect.width, |
|||
(float) rect.height)); |
|||
mat.SetVector("UIWidgets_GUIClipRectRadius", new Vector4(0, 0, 0, 0)); |
|||
} |
|||
|
|||
GL.MultMatrix(this._transform); |
|||
} |
|||
|
|||
private class ClipRec { |
|||
public ClipRec(Matrix4x4 transform, Rect rect = null, RRect rrect = null) { |
|||
this.transform = transform; |
|||
this.rect = rect; |
|||
this.rrect = rrect; |
|||
} |
|||
|
|||
public readonly Matrix4x4 transform; |
|||
|
|||
public readonly Rect rect; |
|||
|
|||
public readonly RRect rrect; |
|||
} |
|||
|
|||
private class LayerRec { |
|||
public Rect bounds; |
|||
public Paint paint; |
|||
public RenderTexture texture; |
|||
} |
|||
|
|||
private class CanvasRec { |
|||
public Matrix4x4 transform; |
|||
public ClipRec clipRect; |
|||
public LayerRec layerRec; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: b8e36c63511f04dbf8d5ed86f144274d |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
namespace UIWidgets.rendering { |
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 3c1a210c4c4b4354bba182d6903742cd |
|||
timeCreated: 1534222730 |
|
|||
namespace UIWidgets.rendering { |
|||
|
|||
} |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UIWidgets.painting; |
|||
using UnityEditor; |
|||
using UnityEngine; |
|||
|
|||
namespace UIWidgets.ui { |
|||
public class EditorCanvas : Canvas { |
|||
static EditorCanvas() { |
|||
var shader = Shader.Find("UIWidgets/2D Handles Lines"); |
|||
if (shader == null) { |
|||
throw new Exception("UIWidgets/2D Handles Lines not found"); |
|||
} |
|||
|
|||
EditorCanvas.linesMat = new Material(shader); |
|||
EditorCanvas.linesMat.hideFlags = HideFlags.HideAndDontSave; |
|||
|
|||
shader = Shader.Find("UIWidgets/GUIRoundedRect"); |
|||
if (shader == null) { |
|||
throw new Exception("UIWidgets/GUIRoundedRect not found"); |
|||
} |
|||
|
|||
EditorCanvas.guiRoundedRectMat = new Material(shader); |
|||
EditorCanvas.guiRoundedRectMat.hideFlags = HideFlags.HideAndDontSave; |
|||
|
|||
shader = Shader.Find("UIWidgets/GUITextureClip"); |
|||
if (shader == null) { |
|||
throw new Exception("UIWidgets/GUITextureClip not found"); |
|||
} |
|||
|
|||
EditorCanvas.guiTextureClipMat = new Material(shader); |
|||
EditorCanvas.guiTextureClipMat.hideFlags = HideFlags.HideAndDontSave; |
|||
|
|||
shader = Shader.Find("UIWidgets/ShadowRect"); |
|||
if (shader == null) { |
|||
throw new Exception("UIWidgets/ShadowRect not found"); |
|||
} |
|||
|
|||
EditorCanvas.shadowRectMat = new Material(shader); |
|||
EditorCanvas.shadowRectMat.hideFlags = HideFlags.HideAndDontSave; |
|||
} |
|||
|
|||
private static readonly Material linesMat; |
|||
private static readonly Material guiRoundedRectMat; |
|||
private static readonly Material guiTextureClipMat; |
|||
private static readonly Material shadowRectMat; |
|||
|
|||
private Matrix4x4 _transform; |
|||
private ClipRec _clipRec; |
|||
private LayerRec _layerRec; |
|||
private Stack<CanvasRec> _stack; |
|||
|
|||
private Stack<CanvasRec> stack { |
|||
get { return this._stack ?? (this._stack = new Stack<CanvasRec>()); } |
|||
} |
|||
|
|||
public EditorCanvas() { |
|||
this._transform = Matrix4x4.identity; |
|||
} |
|||
|
|||
public void drawPloygon4(Offset[] points, Paint paint) { |
|||
var color = paint.color; |
|||
if (color.alpha > 0) { |
|||
Vector3[] verts = new Vector3 [points.Length]; |
|||
for (int i = 0; i < points.Length; i++) { |
|||
verts[i] = points[i].toVector(); |
|||
} |
|||
|
|||
this.prepareGL(EditorCanvas.linesMat); |
|||
EditorCanvas.linesMat.SetPass(0); |
|||
|
|||
GL.Begin(GL.TRIANGLES); |
|||
GL.Color(color.toColor()); |
|||
for (int index = 0; index < 2; ++index) { |
|||
GL.Vertex(verts[index * 2]); |
|||
GL.Vertex(verts[index * 2 + 1]); |
|||
GL.Vertex(verts[(index * 2 + 2) % 4]); |
|||
GL.Vertex(verts[index * 2]); |
|||
GL.Vertex(verts[(index * 2 + 2) % 4]); |
|||
GL.Vertex(verts[index * 2 + 1]); |
|||
} |
|||
|
|||
GL.End(); |
|||
} |
|||
} |
|||
|
|||
public void drawRect(Rect rect, BorderWidth borderWidth, BorderRadius borderRadius, Paint paint) { |
|||
this.prepareGL(EditorCanvas.guiRoundedRectMat); |
|||
|
|||
EditorCanvas.guiRoundedRectMat.SetFloatArray("UIWidgets_BorderWidth", |
|||
borderWidth == null ? new[] {0f, 0f, 0f, 0f} : borderWidth.toFloatArray()); |
|||
EditorCanvas.guiRoundedRectMat.SetFloatArray("UIWidgets_CornerRadius", |
|||
borderRadius == null ? new[] {0f, 0f, 0f, 0f} : borderRadius.toFloatArray()); |
|||
|
|||
Graphics.DrawTexture(rect.toRect(), EditorGUIUtility.whiteTexture, |
|||
new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0, |
|||
paint.color.toColor(), EditorCanvas.guiRoundedRectMat); |
|||
} |
|||
|
|||
public void drawRectShadow(Rect rect, Paint paint) { |
|||
this.prepareGL(EditorCanvas.shadowRectMat); |
|||
|
|||
EditorCanvas.shadowRectMat.SetFloat("UIWidgets_sigma", (float) paint.blurSigma); |
|||
|
|||
Graphics.DrawTexture(rect.toRect(), EditorGUIUtility.whiteTexture, |
|||
new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0, |
|||
paint.color.toColor(), EditorCanvas.shadowRectMat); |
|||
} |
|||
|
|||
public void drawPicture(Picture picture) { |
|||
this.save(); |
|||
|
|||
int saveCount = 0; |
|||
|
|||
var drawCmds = picture.drawCmds; |
|||
foreach (var drawCmd in drawCmds) { |
|||
if (drawCmd is DrawPloygon4) { |
|||
var drawPloygon4 = (DrawPloygon4) drawCmd; |
|||
this.drawPloygon4(drawPloygon4.points, drawPloygon4.paint); |
|||
} else if (drawCmd is DrawRect) { |
|||
var drawRect = (DrawRect) drawCmd; |
|||
this.drawRect(drawRect.rect, drawRect.borderWidth, drawRect.borderRadius, drawRect.paint); |
|||
} else if (drawCmd is DrawRectShadow) { |
|||
var drawRectShadow = (DrawRectShadow) drawCmd; |
|||
this.drawRectShadow(drawRectShadow.rect, drawRectShadow.paint); |
|||
} else if (drawCmd is DrawPicture) { |
|||
var drawPicture = (DrawPicture) drawCmd; |
|||
this.drawPicture(drawPicture.picture); |
|||
} else if (drawCmd is DrawConcat) { |
|||
this.concat(((DrawConcat) drawCmd).transform); |
|||
} else if (drawCmd is DrawSave) { |
|||
saveCount++; |
|||
this.save(); |
|||
} else if (drawCmd is DrawSaveLayer) { |
|||
saveCount++; |
|||
var drawSaveLayer = (DrawSaveLayer) drawCmd; |
|||
this.saveLayer(drawSaveLayer.rect, drawSaveLayer.paint); |
|||
} else if (drawCmd is DrawRestore) { |
|||
saveCount--; |
|||
if (saveCount < 0) { |
|||
throw new Exception("unmatched save/restore in picture"); |
|||
} |
|||
|
|||
this.restore(); |
|||
} else if (drawCmd is DrawClipRect) { |
|||
var drawClipRect = (DrawClipRect) drawCmd; |
|||
this.clipRect(drawClipRect.rect); |
|||
} else if (drawCmd is DrawClipRRect) { |
|||
var drawClipRRect = (DrawClipRRect) drawCmd; |
|||
this.clipRRect(drawClipRRect.rrect); |
|||
} else { |
|||
throw new Exception("unknown drawCmd: " + drawCmd); |
|||
} |
|||
} |
|||
|
|||
if (saveCount != 0) { |
|||
throw new Exception("unmatched save/restore in picture"); |
|||
} |
|||
|
|||
this.restore(); |
|||
} |
|||
|
|||
public void concat(Matrix4x4 transform) { |
|||
this._transform = transform * this._transform; |
|||
} |
|||
|
|||
public void save() { |
|||
var state = new CanvasRec { |
|||
transform = this._transform, |
|||
clipRect = this._clipRec, |
|||
layerRec = this._layerRec, |
|||
}; |
|||
this.stack.Push(state); |
|||
} |
|||
|
|||
public void saveLayer(Rect bounds, Paint paint) { |
|||
this.save(); |
|||
|
|||
var textureWidth = (int) Math.Round(bounds.width * EditorGUIUtility.pixelsPerPoint); |
|||
var textureHeight = (int) Math.Round(bounds.height * EditorGUIUtility.pixelsPerPoint); |
|||
|
|||
var texture = RenderTexture.GetTemporary( |
|||
textureWidth, textureHeight, 32, |
|||
RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); |
|||
|
|||
RenderTexture.active = texture; |
|||
GL.PushMatrix(); |
|||
GL.LoadPixelMatrix((float) bounds.left, (float) bounds.right, (float) bounds.bottom, (float) bounds.top); |
|||
GL.Clear(true, true, new UnityEngine.Color(0, 0, 0, 0)); |
|||
|
|||
this._layerRec = new LayerRec { |
|||
bounds = bounds, |
|||
paint = paint, |
|||
texture = texture, |
|||
}; |
|||
|
|||
this._transform = Matrix4x4.identity; |
|||
this._clipRec = null; |
|||
} |
|||
|
|||
public void restore() { |
|||
var layerRec = this._layerRec; |
|||
|
|||
var state = this._stack.Pop(); |
|||
this._transform = state.transform; |
|||
this._clipRec = state.clipRect; |
|||
this._layerRec = state.layerRec; |
|||
|
|||
if (layerRec != this._layerRec) { |
|||
RenderTexture.active = this._layerRec != null ? this._layerRec.texture : null; |
|||
GL.PopMatrix(); |
|||
|
|||
this.prepareGL(EditorCanvas.guiTextureClipMat); |
|||
|
|||
Graphics.DrawTexture(layerRec.bounds.toRect(), layerRec.texture, |
|||
new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0, |
|||
layerRec.paint.color.toColor(), EditorCanvas.guiTextureClipMat); |
|||
|
|||
RenderTexture.ReleaseTemporary(layerRec.texture); |
|||
layerRec.texture = null; |
|||
} |
|||
} |
|||
|
|||
public void clipRect(Rect rect) { |
|||
if (rect.isInfinite) { |
|||
return; |
|||
} |
|||
|
|||
this.pushClipRect(rect, this._transform); |
|||
} |
|||
|
|||
public void clipRRect(RRect rect) { |
|||
if (rect.isInfinite) { |
|||
return; |
|||
} |
|||
|
|||
this.pushClipRRect(rect, this._transform); |
|||
} |
|||
|
|||
private void pushClipRect(Rect clipRect, Matrix4x4 transform) { |
|||
if (this._clipRec != null) { |
|||
throw new Exception("already a clipRec, considering using saveLayer."); |
|||
} |
|||
|
|||
this._clipRec = new ClipRec(transform, rect: clipRect); |
|||
} |
|||
|
|||
private void pushClipRRect(RRect clipRRect, Matrix4x4 transform) { |
|||
if (this._clipRec != null) { |
|||
throw new Exception("already a clipRec, considering using saveLayer."); |
|||
} |
|||
|
|||
this._clipRec = new ClipRec(transform, rrect: clipRRect); |
|||
} |
|||
|
|||
private void prepareGL(Material mat) { |
|||
if (this._clipRec != null) { |
|||
mat.SetMatrix("UIWidgets_GUIClipMatrix", this._clipRec.transform.inverse); |
|||
if (this._clipRec.rect != null) { |
|||
var rect = this._clipRec.rect; |
|||
mat.SetVector("UIWidgets_GUIClipRect", new Vector4( |
|||
(float) rect.left, |
|||
(float) rect.top, |
|||
(float) rect.width, |
|||
(float) rect.height)); |
|||
mat.SetVector("UIWidgets_GUIClipRectRadius", new Vector4(0, 0, 0, 0)); |
|||
} else { |
|||
var rrect = this._clipRec.rrect; |
|||
var rect = rrect.outerRect; |
|||
mat.SetVector("UIWidgets_GUIClipRect", new Vector4( |
|||
(float) rect.left, |
|||
(float) rect.top, |
|||
(float) rect.width, |
|||
(float) rect.height)); |
|||
mat.SetVector("UIWidgets_GUIClipRectRadius", |
|||
new Vector4( |
|||
(float) rrect.tlRadius, |
|||
(float) rrect.trRadius, |
|||
(float) rrect.brRadius, |
|||
(float) rrect.blRadius)); |
|||
} |
|||
} else { |
|||
mat.SetMatrix("UIWidgets_GUIClipMatrix", Matrix4x4.identity); |
|||
var rect = Rect.largest; |
|||
mat.SetVector("UIWidgets_GUIClipRect", new Vector4( |
|||
(float) rect.left, |
|||
(float) rect.top, |
|||
(float) rect.width, |
|||
(float) rect.height)); |
|||
mat.SetVector("UIWidgets_GUIClipRectRadius", new Vector4(0, 0, 0, 0)); |
|||
} |
|||
|
|||
GL.MultMatrix(this._transform); |
|||
} |
|||
|
|||
private class ClipRec { |
|||
public ClipRec(Matrix4x4 transform, Rect rect = null, RRect rrect = null) { |
|||
this.transform = transform; |
|||
this.rect = rect; |
|||
this.rrect = rrect; |
|||
} |
|||
|
|||
public readonly Matrix4x4 transform; |
|||
|
|||
public readonly Rect rect; |
|||
|
|||
public readonly RRect rrect; |
|||
} |
|||
|
|||
private class LayerRec { |
|||
public Rect bounds; |
|||
public Paint paint; |
|||
public RenderTexture texture; |
|||
} |
|||
|
|||
private class CanvasRec { |
|||
public Matrix4x4 transform; |
|||
public ClipRec clipRect; |
|||
public LayerRec layerRec; |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue