浏览代码

Merge branch 'image_updates' into 'master'

add image resolution.

See merge request upm-packages/ui-widgets/com.unity.uiwidgets!4
/main
Fan Zhang 6 年前
当前提交
6dad37a9
共有 11 个文件被更改,包括 230 次插入24 次删除
  1. 4
      Runtime/painting/decoration_image.cs
  2. 33
      Runtime/painting/image_provider.cs
  3. 2
      Runtime/ui/painting/GifDecoder.cs
  4. 2
      Runtime/ui/painting/canvas_impl.cs
  5. 4
      Runtime/ui/painting/codec.cs
  6. 41
      Runtime/ui/painting/image.cs
  7. 2
      Runtime/ui/painting/painting.cs
  8. 36
      Runtime/widgets/image.cs
  9. 18
      Tests/Editor/Widgets.cs
  10. 101
      Runtime/painting/image_resolution.cs
  11. 11
      Runtime/painting/image_resolution.cs.meta

4
Runtime/painting/decoration_image.cs


if (colorFilter != null) {
paint.colorFilter = colorFilter;
}
paint.invertColors = invertColors;
double halfWidthDelta = (outputSize.width - destinationSize.width) / 2.0;
double halfHeightDelta = (outputSize.height - destinationSize.height) / 2.0;

if (centerSlice == null) {
Rect sourceRect = alignment.inscribe(
fittedSizes.source, Offset.zero & inputSize
sourceSize, Offset.zero & inputSize
);
foreach (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) {
canvas.drawImageRect(image, sourceRect, tileRect, paint);

33
Runtime/painting/image_provider.cs


string name,
double scale
) {
D.assert(bundle != null);
D.assert(name != null);
D.assert(scale >= 0.0);

);
}
protected virtual IPromise<Codec> _loadAsync(AssetBundleImageKey key) {
IPromise<Codec> _loadAsync(AssetBundleImageKey key) {
var coroutine = Window.instance.startCoroutine(this._loadAssetAsync(key));
return coroutine.promise.Then(result => {
if (result == null) {

throw new Exception($"Unable to find asset \"{key.name}\" from asset bundle \"{key.bundle}\"");
}
return CodecUtils.getCodec((Texture2D) result);
if (result is Texture2D texture) {
return CodecUtils.getCodec(new Image(texture, isAsset: true, bundle: key.bundle));
} else if (result is TextAsset text) {
var bytes = text.bytes;
if (key.bundle == null) {
Resources.UnloadAsset(text);
} else {
key.bundle.Unload(text);
}
return CodecUtils.getCodec(bytes);
} else {
throw new Exception($"Unknown type for asset \"{key.name}\": \"{result.GetType()}\"");
}
yield return Resources.LoadAsync<Texture2D>(key.name);
yield break;
ResourceRequest request = Resources.LoadAsync(key.name);
yield return request;
yield return request.asset;
} else {
AssetBundleRequest request = key.bundle.LoadAssetAsync(key.name);
yield return request;
yield return request.asset;
yield return key.bundle.LoadAssetAsync(key.name);
}
}

return CodecUtils.getCodec(bytes);
}
return CodecUtils.getCodec((Texture2D) obj);
return CodecUtils.getCodec(new Image((Texture2D) obj));
});
}

return CodecUtils.getCodec(bytes);
}
return CodecUtils.getCodec((Texture2D) obj);
return CodecUtils.getCodec(new Image((Texture2D) obj));
});
}

}
public class ExactAssetImage : AssetBundleImageProvider, IEquatable<ExactAssetImage> {
public ExactAssetImage(
string assetName,
double scale = 1.0,

2
Runtime/ui/painting/GifDecoder.cs


int r = c[j++] & 0xff;
int g = c[j++] & 0xff;
int b = c[j++] & 0xff;
tab[i++] = (int) (0xff000000 | (r << 16) | (g << 8) | b);
tab[i++] = (int) (0xff000000 | ((uint) r << 16) | ((uint) g << 8) | (uint) b);
}
}

2
Runtime/ui/painting/canvas_impl.cs


var mat = this._getMat(paint);
var properties = this._getMatPropsForImage(image.texture, paint);
var isRT = image is RenderTexture;
var isRT = image.texture is RenderTexture;
this._getLayer().draws.Add(new RenderDraw {
mesh = mesh,
pass = isRT ? CanvasShaderPass.texrtPass0 : CanvasShaderPass.texPass0,

4
Runtime/ui/painting/codec.cs


return Promise<Codec>.Resolved(new ImageCodec(new Image(texture)));
}
public static IPromise<Codec> getCodec(Texture2D texture) {
return Promise<Codec>.Resolved(new ImageCodec(new Image(texture)));
public static IPromise<Codec> getCodec(Image image) {
return Promise<Codec>.Resolved(new ImageCodec(image));
}
}
}

41
Runtime/ui/painting/image.cs


namespace Unity.UIWidgets.ui {
public class Image : IEquatable<Image>, IDisposable {
Texture _texture;
readonly bool _isOwner;
readonly bool _noDispose;
readonly bool _isAsset;
AssetBundle _bundle;
public Image(Texture texture, bool noDispose = false, bool isAsset = false, AssetBundle bundle = null) {
D.assert(!noDispose || !isAsset && bundle == null);
D.assert(isAsset || bundle == null);
public Image(Texture texture, bool isOwner = true) {
this._isOwner = isOwner;
this._noDispose = noDispose;
this._isAsset = isAsset;
this._bundle = bundle;
}
public int width => this._texture != null ? this._texture.width : 0;

public Texture texture => this._texture;
~Image() {
Timer.runInMain(this._dispose);
this._dispose();
if (this._isOwner) {
this._texture = ObjectUtils.SafeDestroy(this._texture);
if (this._noDispose) {
this._texture = null;
return;
}
if (this._isAsset) {
var t = this._texture;
this._texture = null;
var b = this._bundle;
this._bundle = null;
if (b == null) {
// make sure no ref back to this in finalizer
Timer.runInMain(() => { Resources.UnloadAsset(t); });
} else {
// make sure no ref back to this in finalizer
Timer.runInMain(() => { b.Unload(t); });
}
} else {
var t = this._texture;
this._texture = null;
// make sure no ref back to this in finalizer
Timer.runInMain(() => { ObjectUtils.SafeDestroy(t); });
}
}

2
Runtime/ui/painting/painting.cs


public PaintShader shader = null;
public double blurSigma;
public bool invertColors;
}
public static class Conversions {

36
Runtime/widgets/image.cs


filterMode
);
}
public static Image asset(
string name,
Key key = null,
AssetBundle bundle = null,
double? scale = null,
double? width = null,
double? height = null,
Color color = null,
BlendMode colorBlendMode = BlendMode.srcIn,
BoxFit? fit = null,
Alignment alignment = null,
ImageRepeat repeat = ImageRepeat.noRepeat,
Rect centerSlice = null,
bool gaplessPlayback = false,
FilterMode filterMode = FilterMode.Point
) {
var image = scale != null
? (AssetBundleImageProvider) new ExactAssetImage(name, bundle: bundle, scale: scale.Value)
: new AssetImage(name, bundle: bundle);
return new Image(
key,
image,
width,
height,
color,
colorBlendMode,
fit,
alignment,
repeat,
centerSlice,
gaplessPlayback,
filterMode
);
}
public static Image memory(
byte[] bytes,

18
Tests/Editor/Widgets.cs


// if local image test
if (selected == 0) {
localImagePath = EditorGUILayout.TextField(localImagePath);
if (GUILayout.Button("load")) {
if (GUILayout.Button("loadLocal")) {
this.windowAdapter.attachRootWidget(rootWidget);
}
if (GUILayout.Button("loadAsset")) {
var rootWidget = this.loadAsset();
this.windowAdapter.attachRootWidget(rootWidget);
}
}

Widget localImage() {
var image = widgets.Image.file(
localImagePath,
filterMode: FilterMode.Bilinear
);
return image;
}
Widget loadAsset() {
var image = widgets.Image.asset(
localImagePath,
filterMode: FilterMode.Bilinear
);

101
Runtime/painting/image_resolution.cs


using System;
using System.Collections;
using RSG;
using Unity.UIWidgets.async;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
using UnityEngine;
using Object = UnityEngine.Object;
using Path = System.IO.Path;
namespace Unity.UIWidgets.painting {
public class AssetImage : AssetBundleImageProvider, IEquatable<AssetImage> {
public AssetImage(string assetName,
AssetBundle bundle = null) {
D.assert(assetName != null);
this.assetName = assetName;
this.bundle = bundle;
}
public readonly string assetName;
public readonly AssetBundle bundle;
protected override
IPromise<AssetBundleImageKey> obtainKey(ImageConfiguration configuration) {
AssetBundle chosenBundle = this.bundle ? this.bundle : configuration.bundle;
var devicePixelRatio = configuration.devicePixelRatio ?? Window.instance.devicePixelRatio;
var coroutine = Window.instance.startCoroutine(this._loadAssetAsync(chosenBundle, devicePixelRatio));
return coroutine.promise.Then(result => {
D.assert(result != null);
return (AssetBundleImageKey) result;
});
}
IEnumerator _loadAssetAsync(AssetBundle bundle, double devicePixelRatio) {
var extension = Path.GetExtension(this.assetName);
var name = Path.GetFileNameWithoutExtension(this.assetName);
var upper = devicePixelRatio.ceil();
for (var scale = upper; scale >= 1; scale--) {
var assetName = name + "@" + scale + extension;
Object asset;
if (bundle == null) {
ResourceRequest request = Resources.LoadAsync(assetName);
yield return request;
asset = request.asset;
} else {
AssetBundleRequest request = bundle.LoadAssetAsync(assetName);
yield return request;
asset = request.asset;
}
if (asset != null) {
yield return new AssetBundleImageKey(
bundle,
assetName,
scale: scale
);
yield break;
}
}
yield return new AssetBundleImageKey(
bundle,
this.assetName,
scale: 1.0
);
}
public bool Equals(AssetImage other) {
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(this.assetName, other.assetName) && Equals(this.bundle, other.bundle);
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return this.Equals((AssetImage) obj);
}
public override int GetHashCode() {
unchecked {
return ((this.assetName != null ? this.assetName.GetHashCode() : 0) * 397) ^ (this.bundle != null ? this.bundle.GetHashCode() : 0);
}
}
public static bool operator ==(AssetImage left, AssetImage right) {
return Equals(left, right);
}
public static bool operator !=(AssetImage left, AssetImage right) {
return !Equals(left, right);
}
public override string ToString() {
return $"{this.GetType()}(bundle: {this.bundle}, name: \"{this.assetName}\")";
}
}
}

11
Runtime/painting/image_resolution.cs.meta


fileFormatVersion: 2
guid: 10deadb7160304e88ae6468d43429bbe
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存