mirror of
https://github.com/BlackMATov/unity-flash-tools.git
synced 2025-12-16 22:19:31 +07:00
refactor color effect reader
This commit is contained in:
@@ -49,6 +49,7 @@
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\FlashAnimAssetPostprocessor.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\FlashAnimEditor.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\FlashAnimFtaPostprocessor.cs" />
|
||||
<None Include="Assets\FlashTools\Shaders\FlashAnim.shader" />
|
||||
<Reference Include="UnityEditor.Advertisements">
|
||||
<HintPath>/Applications/Unity/Unity.app/Contents/UnityExtensions/Unity/Advertisements/Editor/UnityEditor.Advertisements.dll</HintPath>
|
||||
</Reference>
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Assets\FlashTools\Scripts\FlashAnim.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\FlashAnimAsset.cs" />
|
||||
<None Include="Assets\FlashTools\Shaders\FlashAnim.shader" />
|
||||
<Reference Include="UnityEngine.UI">
|
||||
<HintPath>/Applications/Unity/Unity.app/Contents/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
|
||||
</Reference>
|
||||
|
||||
Binary file not shown.
@@ -134,7 +134,7 @@ Camera:
|
||||
m_Enabled: 1
|
||||
serializedVersion: 2
|
||||
m_ClearFlags: 1
|
||||
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0.019607844}
|
||||
m_BackGroundColor: {r: 1, g: 1, b: 1, a: 0.019607844}
|
||||
m_NormalizedViewPortRect:
|
||||
serializedVersion: 2
|
||||
x: 0
|
||||
@@ -145,7 +145,7 @@ Camera:
|
||||
far clip plane: 1000
|
||||
field of view: 60
|
||||
orthographic: 1
|
||||
orthographic size: 5
|
||||
orthographic size: 1.5
|
||||
m_Depth: -1
|
||||
m_CullingMask:
|
||||
serializedVersion: 2
|
||||
|
||||
@@ -17,6 +17,8 @@ namespace FlashTools {
|
||||
float _current_z = 0.0f;
|
||||
|
||||
List<Vector2> _uvs = new List<Vector2>();
|
||||
List<Color> _mulcolors = new List<Color>();
|
||||
List<Vector4> _addcolors = new List<Vector4>();
|
||||
List<Vector3> _vertices = new List<Vector3>();
|
||||
List<int> _triangles = new List<int>();
|
||||
|
||||
@@ -100,7 +102,7 @@ namespace FlashTools {
|
||||
return null;
|
||||
}
|
||||
|
||||
void RenderInstance(FlashAnimInstData elem_data, int frame_num, Matrix4x4 matrix) {
|
||||
void RenderInstance(FlashAnimInstData elem_data, int frame_num, Matrix4x4 matrix, FlashAnimColorTransform color_trans) {
|
||||
if ( elem_data.Type == FlashAnimInstType.Bitmap ) {
|
||||
var bitmap = Asset ? FindBitmap(Asset.Data.Library, elem_data.Asset) : null;
|
||||
if ( bitmap != null ) {
|
||||
@@ -130,16 +132,26 @@ namespace FlashTools {
|
||||
_uvs.Add(new Vector2(source_rect.xMax, source_rect.yMax));
|
||||
_uvs.Add(new Vector2(source_rect.xMax, source_rect.yMin));
|
||||
_uvs.Add(new Vector2(source_rect.xMin, source_rect.yMin));
|
||||
|
||||
_mulcolors.Add(color_trans.Mul);
|
||||
_mulcolors.Add(color_trans.Mul);
|
||||
_mulcolors.Add(color_trans.Mul);
|
||||
_mulcolors.Add(color_trans.Mul);
|
||||
|
||||
_addcolors.Add(color_trans.Add);
|
||||
_addcolors.Add(color_trans.Add);
|
||||
_addcolors.Add(color_trans.Add);
|
||||
_addcolors.Add(color_trans.Add);
|
||||
}
|
||||
} else if ( elem_data.Type == FlashAnimInstType.Symbol ) {
|
||||
var symbol = Asset ? FindSymbol(Asset.Data.Library, elem_data.Asset) : null;
|
||||
if ( symbol != null ) {
|
||||
RenderSymbol(symbol, frame_num, matrix);
|
||||
RenderSymbol(symbol, frame_num, matrix, color_trans);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RenderSymbol(FlashAnimSymbolData symbol, int frame_num, Matrix4x4 matix) {
|
||||
void RenderSymbol(FlashAnimSymbolData symbol, int frame_num, Matrix4x4 matix, FlashAnimColorTransform color_trans) {
|
||||
for ( var i = 0; i < symbol.Layers.Count; ++i ) {
|
||||
var layer = symbol.Layers[i];
|
||||
if ( layer.LayerType != FlashAnimLayerType.Guide &&
|
||||
@@ -154,7 +166,8 @@ namespace FlashTools {
|
||||
RenderInstance(
|
||||
elem.Instance,
|
||||
elem.Instance.FirstFrame,
|
||||
matix * elem.Matrix);
|
||||
matix * elem.Matrix,
|
||||
color_trans * elem.Instance.ColorTransform);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -176,7 +189,7 @@ namespace FlashTools {
|
||||
|
||||
void Start() {
|
||||
if ( Asset && Asset.Atlas ) {
|
||||
var material = new Material(Shader.Find("Sprites/Default"));
|
||||
var material = new Material(Shader.Find("FlashTools/FlashAnim"));
|
||||
material.SetTexture("_MainTex", Asset.Atlas);
|
||||
GetComponent<MeshRenderer>().sharedMaterial = material;
|
||||
}
|
||||
@@ -201,14 +214,18 @@ namespace FlashTools {
|
||||
_vertices.Clear();
|
||||
_triangles.Clear();
|
||||
_uvs.Clear();
|
||||
_mulcolors.Clear();
|
||||
_addcolors.Clear();
|
||||
_current_z = 0.0f;
|
||||
|
||||
RenderSymbol(
|
||||
GetCurrentSymbol(),
|
||||
_current_frame,
|
||||
Matrix4x4.Scale(new Vector3(
|
||||
1.0f / Asset.PixelsPerUnit,
|
||||
-1.0f / Asset.PixelsPerUnit,
|
||||
1.0f / Asset.PixelsPerUnit)));
|
||||
1.0f / Asset.PixelsPerUnit)),
|
||||
FlashAnimColorTransform.identity);
|
||||
var mesh_filter = GetComponent<MeshFilter>();
|
||||
if ( mesh_filter ) {
|
||||
var mesh = mesh_filter.sharedMesh
|
||||
@@ -218,6 +235,8 @@ namespace FlashTools {
|
||||
mesh.SetVertices(_vertices);
|
||||
mesh.SetTriangles(_triangles, 0);
|
||||
mesh.SetUVs(0, _uvs);
|
||||
mesh.SetUVs(1, _addcolors);
|
||||
mesh.SetColors(_mulcolors);
|
||||
mesh.RecalculateNormals();
|
||||
mesh_filter.sharedMesh = mesh;
|
||||
}
|
||||
|
||||
@@ -39,6 +39,40 @@ namespace FlashTools {
|
||||
Symbol
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public struct FlashAnimColorTransform {
|
||||
public Vector4 Mul;
|
||||
public Vector4 Add;
|
||||
|
||||
public FlashAnimColorTransform(Vector4 Mul, Vector4 Add) {
|
||||
this.Mul = Mul;
|
||||
this.Add = Add;
|
||||
}
|
||||
|
||||
public static FlashAnimColorTransform identity {
|
||||
get {
|
||||
return new FlashAnimColorTransform(
|
||||
new Vector4(1,1,1,1),
|
||||
new Vector4(0,0,0,0));
|
||||
}
|
||||
}
|
||||
|
||||
public static FlashAnimColorTransform operator*(
|
||||
FlashAnimColorTransform a, FlashAnimColorTransform b)
|
||||
{
|
||||
var res = new FlashAnimColorTransform();
|
||||
res.Mul.x = a.Mul.x * b.Mul.x;
|
||||
res.Mul.y = a.Mul.y * b.Mul.y;
|
||||
res.Mul.z = a.Mul.z * b.Mul.z;
|
||||
res.Mul.w = a.Mul.w * b.Mul.w;
|
||||
res.Add.x = a.Add.x * b.Mul.x + b.Add.x;
|
||||
res.Add.y = a.Add.y * b.Mul.y + b.Add.y;
|
||||
res.Add.z = a.Add.z * b.Mul.z + b.Add.z;
|
||||
res.Add.w = a.Add.w * b.Mul.w + b.Add.w;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class FlashAnimBitmapData {
|
||||
public string Id = string.Empty;
|
||||
@@ -54,12 +88,13 @@ namespace FlashTools {
|
||||
|
||||
[System.Serializable]
|
||||
public class FlashAnimInstData {
|
||||
public FlashAnimInstType Type = FlashAnimInstType.Bitmap;
|
||||
public FlashAnimBlendMode BlendMode = FlashAnimBlendMode.Normal;
|
||||
public string Asset = string.Empty;
|
||||
public bool Visible = true;
|
||||
public int FirstFrame = 0;
|
||||
public FlashAnimLoopingMode LoopingMode = FlashAnimLoopingMode.SingleFrame;
|
||||
public FlashAnimInstType Type = FlashAnimInstType.Bitmap;
|
||||
public FlashAnimBlendMode BlendMode = FlashAnimBlendMode.Normal;
|
||||
public string Asset = string.Empty;
|
||||
public bool Visible = true;
|
||||
public int FirstFrame = 0;
|
||||
public FlashAnimLoopingMode LoopingMode = FlashAnimLoopingMode.SingleFrame;
|
||||
public FlashAnimColorTransform ColorTransform = FlashAnimColorTransform.identity;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
|
||||
@@ -134,6 +134,10 @@ namespace FlashTools.Internal {
|
||||
instance.Visible = SafeLoadBoolFromElemAttr(inst_elem, "visible" , instance.Visible);
|
||||
instance.FirstFrame = SafeLoadIntFromElemAttr (inst_elem, "first_frame" , instance.FirstFrame);
|
||||
instance.LoopingMode = SafeLoadEnumFromElemAttr(inst_elem, "looping_mode", instance.LoopingMode);
|
||||
var color_transform_elem = inst_elem.Element("color_effect");
|
||||
if ( color_transform_elem != null ) {
|
||||
instance.ColorTransform = SafeLoadColFromElemAttr(color_transform_elem, "transform", instance.ColorTransform);
|
||||
}
|
||||
data.Instance = instance;
|
||||
}
|
||||
|
||||
@@ -166,7 +170,17 @@ namespace FlashTools.Internal {
|
||||
|
||||
static int SafeLoadIntFromElemAttr(XElement elem, string attr_name, int def_value) {
|
||||
int value;
|
||||
if ( elem != null && int.TryParse(SafeLoadStrFromElemAttr(elem, attr_name, string.Empty), out value) ) {
|
||||
var int_str = SafeLoadStrFromElemAttr(elem, attr_name, string.Empty);
|
||||
if ( elem != null && int.TryParse(int_str, out value) ) {
|
||||
return value;
|
||||
}
|
||||
return def_value;
|
||||
}
|
||||
|
||||
static float SafeLoadFloatFromElemAttr(XElement elem, string attr_name, float def_value) {
|
||||
float value;
|
||||
var float_str = SafeLoadStrFromElemAttr(elem, attr_name, string.Empty);
|
||||
if ( elem != null && float.TryParse(float_str, NumberStyles.Any, CultureInfo.InvariantCulture, out value) ) {
|
||||
return value;
|
||||
}
|
||||
return def_value;
|
||||
@@ -174,7 +188,8 @@ namespace FlashTools.Internal {
|
||||
|
||||
static bool SafeLoadBoolFromElemAttr(XElement elem, string attr_name, bool def_value) {
|
||||
bool value;
|
||||
if ( elem != null && bool.TryParse(SafeLoadStrFromElemAttr(elem, attr_name, string.Empty), out value) ) {
|
||||
var bool_str = SafeLoadStrFromElemAttr(elem, attr_name, string.Empty);
|
||||
if ( elem != null && bool.TryParse(bool_str, out value) ) {
|
||||
return value;
|
||||
}
|
||||
return def_value;
|
||||
@@ -206,6 +221,30 @@ namespace FlashTools.Internal {
|
||||
return def_value;
|
||||
}
|
||||
|
||||
static FlashAnimColorTransform SafeLoadColFromElemAttr(XElement elem, string attr_name, FlashAnimColorTransform def_value) {
|
||||
var col_str = SafeLoadStrFromElemAttr(elem, attr_name, string.Empty);
|
||||
var col_strs = col_str.Split(';');
|
||||
if ( col_strs.Length == 8 ) {
|
||||
float rp, gp, bp, ap, ra, ga, ba, aa;
|
||||
if (
|
||||
float.TryParse(col_strs[0], NumberStyles.Any, CultureInfo.InvariantCulture, out rp) &&
|
||||
float.TryParse(col_strs[1], NumberStyles.Any, CultureInfo.InvariantCulture, out gp) &&
|
||||
float.TryParse(col_strs[2], NumberStyles.Any, CultureInfo.InvariantCulture, out bp) &&
|
||||
float.TryParse(col_strs[3], NumberStyles.Any, CultureInfo.InvariantCulture, out ap) &&
|
||||
float.TryParse(col_strs[4], NumberStyles.Any, CultureInfo.InvariantCulture, out ra) &&
|
||||
float.TryParse(col_strs[5], NumberStyles.Any, CultureInfo.InvariantCulture, out ga) &&
|
||||
float.TryParse(col_strs[6], NumberStyles.Any, CultureInfo.InvariantCulture, out ba) &&
|
||||
float.TryParse(col_strs[7], NumberStyles.Any, CultureInfo.InvariantCulture, out aa))
|
||||
{
|
||||
return new FlashAnimColorTransform(
|
||||
new Vector4(rp, gp, bp, ap),
|
||||
new Vector4(ra, ga, ba, aa)
|
||||
);
|
||||
}
|
||||
}
|
||||
return def_value;
|
||||
}
|
||||
|
||||
static T SafeLoadEnumFromElemAttr<T>(XElement elem, string attr_name, T def_value) {
|
||||
try {
|
||||
return (T)Enum.Parse(typeof(T), SafeLoadStrFromElemAttr(elem, attr_name, string.Empty), true);
|
||||
|
||||
9
Assets/FlashTools/Shaders.meta
Normal file
9
Assets/FlashTools/Shaders.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ee7c6466110fb4e278e247d3d334450f
|
||||
folderAsset: yes
|
||||
timeCreated: 1456948832
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
75
Assets/FlashTools/Shaders/FlashAnim.shader
Normal file
75
Assets/FlashTools/Shaders/FlashAnim.shader
Normal file
@@ -0,0 +1,75 @@
|
||||
Shader "FlashTools/FlashAnim" {
|
||||
Properties {
|
||||
[PerRendererData] _MainTex ("Sprite Texture", 2D ) = "white" {}
|
||||
[MaterialToggle] PixelSnap ("Pixel snap" , Float) = 0
|
||||
}
|
||||
|
||||
SubShader {
|
||||
Tags {
|
||||
"Queue" = "Transparent"
|
||||
"IgnoreProjector" = "True"
|
||||
"RenderType" = "Transparent"
|
||||
"PreviewType" = "Plane"
|
||||
"CanUseSpriteAtlas" = "True"
|
||||
}
|
||||
|
||||
Cull Off
|
||||
Lighting Off
|
||||
ZWrite Off
|
||||
Blend One OneMinusSrcAlpha
|
||||
|
||||
Pass {
|
||||
CGPROGRAM
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
#pragma multi_compile _ PIXELSNAP_ON
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
struct appdata_t {
|
||||
float4 vertex : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
float4 mulcolor : COLOR;
|
||||
float4 addcolor : TEXCOORD1;
|
||||
};
|
||||
|
||||
struct v2f {
|
||||
float4 vertex : SV_POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
fixed4 mulcolor : COLOR;
|
||||
fixed4 addcolor : TEXCOORD1;
|
||||
};
|
||||
|
||||
v2f vert(appdata_t IN) {
|
||||
v2f OUT;
|
||||
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
|
||||
OUT.uv = IN.uv;
|
||||
OUT.mulcolor = IN.mulcolor;
|
||||
OUT.addcolor = IN.addcolor;
|
||||
#ifdef PIXELSNAP_ON
|
||||
OUT.vertex = UnityPixelSnap(OUT.vertex);
|
||||
#endif
|
||||
return OUT;
|
||||
}
|
||||
|
||||
sampler2D _MainTex;
|
||||
sampler2D _AlphaTex;
|
||||
float _AlphaSplitEnabled;
|
||||
|
||||
fixed4 SampleSpriteTexture(float2 uv) {
|
||||
fixed4 color = tex2D(_MainTex, uv);
|
||||
#if UNITY_TEXTURE_ALPHASPLIT_ALLOWED
|
||||
if (_AlphaSplitEnabled)
|
||||
color.a = tex2D(_AlphaTex, uv).r;
|
||||
#endif //UNITY_TEXTURE_ALPHASPLIT_ALLOWED
|
||||
return color;
|
||||
}
|
||||
|
||||
fixed4 frag(v2f IN) : SV_Target {
|
||||
fixed4 c = SampleSpriteTexture(IN.uv) * IN.mulcolor + IN.addcolor;
|
||||
c.rgb *= c.a;
|
||||
return c;
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Assets/FlashTools/Shaders/FlashAnim.shader.meta
Normal file
9
Assets/FlashTools/Shaders/FlashAnim.shader.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f1e8cd2344e824b6a9b12efcacde262c
|
||||
timeCreated: 1456948894
|
||||
licenseType: Free
|
||||
ShaderImporter:
|
||||
defaultTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -871,51 +871,51 @@ if (typeof Object.create != 'function') {
|
||||
.attr("first_frame" , this.get_first_frame())
|
||||
.attr("looping_mode", this.get_looping_mode());
|
||||
if (this.inst.colorMode !== "none") {
|
||||
var color_effect_node = instance_node.child("color_transform");
|
||||
var color_effect_node = instance_node.child("color_effect");
|
||||
if (this.inst.colorMode == "brightness") {
|
||||
var brightness = this.inst.brightness / 100.0;
|
||||
color_effect_node
|
||||
.attr("ap", 1)
|
||||
.attr("rp", 1 - Math.abs(brightness))
|
||||
.attr("gp", 1 - Math.abs(brightness))
|
||||
.attr("bp", 1 - Math.abs(brightness))
|
||||
.attr("aa", 0)
|
||||
.attr("ra", brightness < 0 ? 0 : brightness)
|
||||
.attr("ga", brightness < 0 ? 0 : brightness)
|
||||
.attr("ba", brightness < 0 ? 0 : brightness);
|
||||
color_effect_node.attr("transform", "{0};{1};{2};{3};{4};{5};{6};{7}".format(
|
||||
1 - Math.abs(brightness),
|
||||
1 - Math.abs(brightness),
|
||||
1 - Math.abs(brightness),
|
||||
1,
|
||||
brightness < 0 ? 0 : brightness,
|
||||
brightness < 0 ? 0 : brightness,
|
||||
brightness < 0 ? 0 : brightness,
|
||||
0));
|
||||
} else if (this.inst.colorMode == "tint") {
|
||||
var tint_color = ft.hex_str_to_color32(this.inst.tintColor);
|
||||
var tint_percent = this.inst.tintPercent / 100.0;
|
||||
color_effect_node
|
||||
.attr("ap", 1)
|
||||
.attr("rp", 1 - tint_percent)
|
||||
.attr("gp", 1 - tint_percent)
|
||||
.attr("bp", 1 - tint_percent)
|
||||
.attr("aa", 0)
|
||||
.attr("ra", tint_color[0] / 255.0 * tint_percent)
|
||||
.attr("ga", tint_color[1] / 255.0 * tint_percent)
|
||||
.attr("ba", tint_color[2] / 255.0 * tint_percent);
|
||||
color_effect_node.attr("transform", "{0};{1};{2};{3};{4};{5};{6};{7}".format(
|
||||
1 - tint_percent,
|
||||
1 - tint_percent,
|
||||
1 - tint_percent,
|
||||
1,
|
||||
tint_color[0] / 255.0 * tint_percent,
|
||||
tint_color[1] / 255.0 * tint_percent,
|
||||
tint_color[2] / 255.0 * tint_percent,
|
||||
0));
|
||||
} else if (this.inst.colorMode == "alpha") {
|
||||
var alpha = this.inst.colorAlphaPercent / 100.0;
|
||||
color_effect_node
|
||||
.attr("ap", alpha)
|
||||
.attr("rp", 1)
|
||||
.attr("gp", 1)
|
||||
.attr("bp", 1)
|
||||
.attr("aa", 0)
|
||||
.attr("ra", 0)
|
||||
.attr("ga", 0)
|
||||
.attr("ba", 0);
|
||||
color_effect_node.attr("transform", "{0};{1};{2};{3};{4};{5};{6};{7}".format(
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
alpha,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0));
|
||||
} else if (this.inst.colorMode == "advanced") {
|
||||
color_effect_node
|
||||
.attr("aa", this.inst.colorAlphaAmount / 255.0)
|
||||
.attr("ra", this.inst.colorRedAmount / 255.0)
|
||||
.attr("ga", this.inst.colorGreenAmount / 255.0)
|
||||
.attr("ba", this.inst.colorBlueAmount / 255.0)
|
||||
.attr("ap", this.inst.colorAlphaPercent / 100.0)
|
||||
.attr("rp", this.inst.colorRedPercent / 100.0)
|
||||
.attr("gp", this.inst.colorGreenPercent / 100.0)
|
||||
.attr("bp", this.inst.colorBluePercent / 100.0);
|
||||
color_effect_node.attr("transform", "{0};{1};{2};{3};{4};{5};{6};{7}".format(
|
||||
this.inst.colorRedPercent / 100.0,
|
||||
this.inst.colorGreenPercent / 100.0,
|
||||
this.inst.colorBluePercent / 100.0,
|
||||
this.inst.colorAlphaPercent / 100.0,
|
||||
this.inst.colorRedAmount / 255.0,
|
||||
this.inst.colorGreenAmount / 255.0,
|
||||
this.inst.colorBlueAmount / 255.0,
|
||||
this.inst.colorAlphaAmount / 255.0));
|
||||
} else {
|
||||
ft.assert(false,
|
||||
"Unsupported color mode ({0})!",
|
||||
|
||||
Reference in New Issue
Block a user