mirror of
https://github.com/BlackMATov/unity-flash-tools.git
synced 2025-12-13 03:30:14 +07:00
Add optional bitmap trimming
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
###### Version 1.3.15
|
||||
* Add bitmap trimming
|
||||
* Add optional bitmap trimming
|
||||
* Fix preview leaks in the Editor mode
|
||||
* Add warning notes about outdated assets
|
||||
* Add log message about successfully converting
|
||||
|
||||
@@ -15,6 +15,7 @@ MonoBehaviour:
|
||||
MaxAtlasSize: 2048
|
||||
AtlasPadding: 1
|
||||
PixelsPerUnit: 100
|
||||
BitmapTrimming: 1
|
||||
GenerateMipMaps: 0
|
||||
AtlasPowerOfTwo: 1
|
||||
AtlasForceSquare: 1
|
||||
|
||||
@@ -97,7 +97,7 @@ namespace FTEditor.Postprocessors {
|
||||
if ( bitmap.Redirect == 0 ) {
|
||||
textures.Add(new KeyValuePair<ushort, Texture2D>(
|
||||
bitmap.Id,
|
||||
LoadTextureFromData(bitmap)));
|
||||
LoadTextureFromData(bitmap, asset.Settings)));
|
||||
}
|
||||
}
|
||||
var rects = PackAndSaveBitmapsAtlas(
|
||||
@@ -113,14 +113,36 @@ namespace FTEditor.Postprocessors {
|
||||
return data;
|
||||
}
|
||||
|
||||
static Texture2D LoadTextureFromData(SwfBitmapData bitmap) {
|
||||
static Texture2D LoadTextureFromData(SwfBitmapData bitmap, SwfSettingsData settings) {
|
||||
var argb32 = settings.BitmapTrimming
|
||||
? TrimBitmapByRect(bitmap, bitmap.TrimmedRect)
|
||||
: bitmap.ARGB32;
|
||||
var widht = settings.BitmapTrimming
|
||||
? bitmap.TrimmedRect.width
|
||||
: bitmap.RealWidth;
|
||||
var height = settings.BitmapTrimming
|
||||
? bitmap.TrimmedRect.height
|
||||
: bitmap.RealHeight;
|
||||
var texture = new Texture2D(
|
||||
bitmap.TrimmedRect.width, bitmap.TrimmedRect.height,
|
||||
widht, height,
|
||||
TextureFormat.ARGB32, false);
|
||||
texture.LoadRawTextureData(bitmap.ARGB32);
|
||||
texture.LoadRawTextureData(argb32);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static byte[] TrimBitmapByRect(SwfBitmapData bitmap, SwfRectIntData rect) {
|
||||
var argb32 = new byte[rect.area * 4];
|
||||
for ( var i = 0; i < rect.height; ++i ) {
|
||||
var src_index = rect.xMin + (rect.yMin + i) * bitmap.RealWidth;
|
||||
var dst_index = i * rect.width;
|
||||
Array.Copy(
|
||||
bitmap.ARGB32, src_index * 4,
|
||||
argb32, dst_index * 4,
|
||||
rect.width * 4);
|
||||
}
|
||||
return argb32;
|
||||
}
|
||||
|
||||
struct BitmapsAtlasInfo {
|
||||
public Texture2D Atlas;
|
||||
public Rect[] Rects;
|
||||
@@ -365,17 +387,19 @@ namespace FTEditor.Postprocessors {
|
||||
bitmap = FindBitmapFromAssetData(data, bitmap.Redirect);
|
||||
}
|
||||
if ( bitmap != null ) {
|
||||
var tr = bitmap.TrimmedRect;
|
||||
var v0 = new Vector2(tr.xMin, tr.yMin) / 20.0f;
|
||||
var v1 = new Vector2(tr.xMax, tr.yMin) / 20.0f;
|
||||
var v2 = new Vector2(tr.xMax, tr.yMax) / 20.0f;
|
||||
var v3 = new Vector2(tr.xMin, tr.yMax) / 20.0f;
|
||||
var br = asset.Settings.BitmapTrimming
|
||||
? bitmap.TrimmedRect
|
||||
: new SwfRectIntData(bitmap.RealWidth, bitmap.RealHeight);
|
||||
|
||||
var v0 = new Vector2(br.xMin, br.yMin);
|
||||
var v1 = new Vector2(br.xMax, br.yMin);
|
||||
var v2 = new Vector2(br.xMax, br.yMax);
|
||||
var v3 = new Vector2(br.xMin, br.yMax);
|
||||
|
||||
var matrix =
|
||||
Matrix4x4.Scale(
|
||||
new Vector3(1.0f, -1.0f, 1.0f) /
|
||||
asset.Settings.PixelsPerUnit) *
|
||||
inst.Matrix.ToUMatrix();
|
||||
Matrix4x4.Scale(new Vector3(1.0f, -1.0f, 1.0f) / asset.Settings.PixelsPerUnit) *
|
||||
inst.Matrix.ToUMatrix() *
|
||||
Matrix4x4.Scale(new Vector3(1.0f / 20.0f, 1.0f / 20.0f, 1.0f));
|
||||
|
||||
baked_vertices.Add(matrix.MultiplyPoint3x4(v0));
|
||||
baked_vertices.Add(matrix.MultiplyPoint3x4(v1));
|
||||
|
||||
@@ -409,18 +409,17 @@ namespace FTEditor.Postprocessors {
|
||||
static SwfBitmapData ConvertBitmap(ushort id, SwfLibraryBitmapDefine bitmap) {
|
||||
var trimmed_rect = bitmap.Redirect > 0
|
||||
? new SwfRectIntData(bitmap.Width, bitmap.Height)
|
||||
: FindBitmapTrimmedRect(bitmap);
|
||||
var trimmed_argb32 = bitmap.Redirect > 0
|
||||
? bitmap.ARGB32
|
||||
: TrimBitmapByRect(bitmap, trimmed_rect);
|
||||
: CalculateBitmapTrimmedRect(bitmap);
|
||||
return new SwfBitmapData{
|
||||
Id = id,
|
||||
ARGB32 = trimmed_argb32,
|
||||
ARGB32 = bitmap.ARGB32,
|
||||
Redirect = bitmap.Redirect,
|
||||
RealWidth = bitmap.Width,
|
||||
RealHeight = bitmap.Height,
|
||||
TrimmedRect = trimmed_rect};
|
||||
}
|
||||
|
||||
static SwfRectIntData FindBitmapTrimmedRect(SwfLibraryBitmapDefine bitmap) {
|
||||
static SwfRectIntData CalculateBitmapTrimmedRect(SwfLibraryBitmapDefine bitmap) {
|
||||
var rect = new SwfRectIntData{
|
||||
xMin = bitmap.Width,
|
||||
yMin = bitmap.Height,
|
||||
@@ -441,19 +440,6 @@ namespace FTEditor.Postprocessors {
|
||||
? new SwfRectIntData(0, 0, 1, 1)
|
||||
: rect;
|
||||
}
|
||||
|
||||
static byte[] TrimBitmapByRect(SwfLibraryBitmapDefine bitmap, SwfRectIntData rect) {
|
||||
var trimmed_argb32 = new byte[rect.area * 4];
|
||||
for ( var i = 0; i < rect.height; ++i ) {
|
||||
var src_index = rect.xMin + (rect.yMin + i) * bitmap.Width;
|
||||
var dst_index = i * rect.width;
|
||||
Array.Copy(
|
||||
bitmap.ARGB32, src_index * 4,
|
||||
trimmed_argb32, dst_index * 4,
|
||||
rect.width * 4);
|
||||
}
|
||||
return trimmed_argb32;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
@@ -287,6 +287,8 @@ namespace FTEditor {
|
||||
public ushort Id = 0;
|
||||
public byte[] ARGB32 = new byte[0];
|
||||
public ushort Redirect = 0;
|
||||
public int RealWidth = 0;
|
||||
public int RealHeight = 0;
|
||||
public SwfRectData SourceRect = SwfRectData.identity;
|
||||
public SwfRectIntData TrimmedRect = SwfRectIntData.identity;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace FTRuntime {
|
||||
public int AtlasPadding;
|
||||
[SwfFloatRange(float.Epsilon, float.MaxValue)]
|
||||
public float PixelsPerUnit;
|
||||
public bool BitmapTrimming;
|
||||
public bool GenerateMipMaps;
|
||||
public bool AtlasPowerOfTwo;
|
||||
public bool AtlasForceSquare;
|
||||
@@ -33,6 +34,7 @@ namespace FTRuntime {
|
||||
MaxAtlasSize = 2048,
|
||||
AtlasPadding = 1,
|
||||
PixelsPerUnit = 100.0f,
|
||||
BitmapTrimming = true,
|
||||
GenerateMipMaps = false,
|
||||
AtlasPowerOfTwo = true,
|
||||
AtlasForceSquare = true,
|
||||
@@ -46,6 +48,7 @@ namespace FTRuntime {
|
||||
MaxAtlasSize == other.MaxAtlasSize &&
|
||||
AtlasPadding == other.AtlasPadding &&
|
||||
Mathf.Approximately(PixelsPerUnit, other.PixelsPerUnit) &&
|
||||
BitmapTrimming == other.BitmapTrimming &&
|
||||
GenerateMipMaps == other.GenerateMipMaps &&
|
||||
AtlasPowerOfTwo == other.AtlasPowerOfTwo &&
|
||||
AtlasForceSquare == other.AtlasForceSquare &&
|
||||
|
||||
@@ -70,13 +70,13 @@ https://gist.github.com/talecrafter/111ea3345911bd238f4998b4d5a04bf3
|
||||
**** TODO Возможно проблемы с DX9
|
||||
UNITY_UV_STARTS_AT_TOP
|
||||
UNITY_HALF_TEXEL_OFFSET
|
||||
** TODO Версия 1.3.15
|
||||
** DONE Версия 1.3.15
|
||||
*** Баги
|
||||
**** DONE Утечка превью в редакторе
|
||||
*** Улучшения
|
||||
**** DONE Выводить в лог успешную конвертацию с контекстом
|
||||
**** DONE Предупреждения о устаревших ассетах
|
||||
**** TODO Опциональный тримминг
|
||||
**** DONE Опциональный тримминг
|
||||
**** DONE Триммить изображения из swf (adou.fla)
|
||||
** DONE Версия 1.3.14
|
||||
*** Баги
|
||||
|
||||
Reference in New Issue
Block a user