fix flash frame offset, swf animation refactor

This commit is contained in:
2016-08-28 14:26:19 +07:00
parent 684409fe08
commit cf7e5b4bac
8 changed files with 227 additions and 110 deletions
@@ -64,10 +64,7 @@ namespace FlashTools.Internal {
var anim_go = new GameObject(_asset.name);
anim_go.AddComponent<MeshFilter>();
anim_go.AddComponent<MeshRenderer>();
anim_go.AddComponent<SwfAnimation>();
anim_go.AddComponent<SwfAnimationController>();
anim_go.GetComponent<SwfAnimation>().InitWithAsset(_asset);
anim_go.AddComponent<SwfAnimation>().asset = _asset;
return anim_go;
}
return null;
@@ -186,19 +186,28 @@ namespace FlashTools.Internal {
? FindBitmapFromAnimationData(asset.Data, inst.Bitmap)
: null;
if ( bitmap != null ) {
var width = bitmap.RealSize.x / 20.0f; //TODO: twips?
var height = bitmap.RealSize.y / 20.0f; //TODO: twips?
var width = bitmap.RealSize.x / 20.0f;
var height = bitmap.RealSize.y / 20.0f;
var v0 = new Vector3( 0, 0, 0);
var v1 = new Vector3(width, 0, 0);
var v2 = new Vector3(width, height, 0);
var v3 = new Vector3( 0, height, 0);
var frame_offset = Matrix4x4.TRS(
new Vector2(
-asset.Data.FrameSize.x * 0.5f / asset.Settings.PixelsPerUnit,
+asset.Data.FrameSize.y * 0.5f / asset.Settings.PixelsPerUnit),
Quaternion.identity,
Vector3.one);
var matrix =
frame_offset *
Matrix4x4.Scale(new Vector3(
+1.0f / asset.Settings.PixelsPerUnit,
-1.0f / asset.Settings.PixelsPerUnit,
+1.0f / asset.Settings.PixelsPerUnit)) * inst.Matrix;
+1.0f / asset.Settings.PixelsPerUnit)) *
inst.Matrix;
baked_vertices.Add(matrix.MultiplyPoint3x4(v0));
baked_vertices.Add(matrix.MultiplyPoint3x4(v1));
@@ -8,7 +8,7 @@ namespace FlashTools.Internal {
SwfAnimation _animation = null;
void DrawCurrentFrame() {
if ( _animation.Asset && _animation.frameCount > 1 ) {
if ( _animation.frameCount > 1 ) {
var new_current_frame = EditorGUILayout.IntSlider(
"Frame", _animation.currentFrame,
0, _animation.frameCount - 1);
@@ -18,6 +18,31 @@ namespace FlashTools.Internal {
}
}
void DrawAnimationControls() {
EditorGUILayout.Space();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
{
if ( GUILayout.Button(new GUIContent("<<", "to begin frame")) ) {
_animation.ToBeginFrame();
}
if ( GUILayout.Button(new GUIContent("<", "to prev frame")) ) {
_animation.ToPrevFrame();
}
GUILayout.Label(string.Format(
"{0}/{1}",
_animation.currentFrame, _animation.frameCount));
if ( GUILayout.Button(new GUIContent(">", "to next frame")) ) {
_animation.ToNextFrame();
}
if ( GUILayout.Button(new GUIContent(">>", "to end frame")) ) {
_animation.ToEndFrame();
}
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
// ------------------------------------------------------------------------
//
// Messages
@@ -29,8 +54,13 @@ namespace FlashTools.Internal {
}
public override void OnInspectorGUI() {
serializedObject.Update();
DrawDefaultInspector();
DrawCurrentFrame();
DrawAnimationControls();
if ( GUI.changed ) {
serializedObject.ApplyModifiedProperties();
}
}
}
}
@@ -56,7 +56,8 @@ namespace FlashTools.Internal {
string swf_asset, SwfAnimationAsset asset, SwfDecoder decoder)
{
var animation_data = new SwfAnimationData{
FrameRate = decoder.UncompressedHeader.FrameRate
FrameRate = decoder.UncompressedHeader.FrameRate,
FrameSize = decoder.UncompressedHeader.FrameSize.ToUnityVectorSize()
};
var context = new SwfContext();
var executer = new SwfContextExecuter(context, 0);
@@ -39,7 +39,15 @@ namespace FlashTools.Internal.SwfTools.SwfTypes {
}
public Rect ToUnityRect() {
return Rect.MinMaxRect(XMin, YMin, XMax, YMax);
return Rect.MinMaxRect(
XMin, YMin,
XMax, YMax);
}
public Vector2 ToUnityVectorSize() {
return new Vector2(
Mathf.Abs(XMax - XMin),
Mathf.Abs(YMax - YMin));
}
}
}
+118 -50
View File
@@ -1,91 +1,147 @@
using UnityEngine;
using System.Collections.Generic;
using FlashTools.Internal;
namespace FlashTools {
[ExecuteInEditMode, DisallowMultipleComponent]
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class SwfAnimation : MonoBehaviour {
[SwfReadOnly]
public SwfAnimationAsset Asset = null;
[SwfSortingLayer]
public string SortingLayer = "Default";
public int SortingOrder = 0;
//
//
//
MeshFilter _meshFilter = null;
MeshRenderer _meshRenderer = null;
MeshFilter _meshFilter = null;
MeshRenderer _meshRenderer = null;
MaterialPropertyBlock _matPropBlock = null;
// ------------------------------------------------------------------------
//
// Properties
//
// ------------------------------------------------------------------------
//
//
//
[Header("Sorting")]
[SerializeField][SwfSortingLayer]
public string _sortingLayer = "Default";
public string sortingLayer {
get { return _sortingLayer; }
set {
_sortingLayer = value;
ChangeSortingProperties();
}
}
[SerializeField]
public int _sortingOrder = 0;
public int sortingOrder {
get { return _sortingOrder; }
set {
_sortingOrder = value;
ChangeSortingProperties();
}
}
[Header("Animation")]
[SerializeField]
SwfAnimationAsset _asset = null;
public SwfAnimationAsset asset {
get { return _asset; }
set {
_asset = value;
ChangeAsset();
}
}
[SerializeField][HideInInspector]
int _currentFrame = 0;
public int currentFrame {
get { return _currentFrame; }
set {
_currentFrame = frameCount > 0
? Mathf.Clamp(value, 0, frameCount - 1)
: 0;
UpdateCurrentMesh();
_currentFrame = value;
ChangeCurrentFrame();
}
}
public int frameCount {
get {
return Asset ? Asset.Data.Frames.Count : 0;
return asset && asset.Data != null && asset.Data.Frames != null
? asset.Data.Frames.Count
: 0;
}
}
public float frameRate {
get {
return Asset ? Asset.Data.FrameRate : 1.0f;
return asset && asset.Data != null
? asset.Data.FrameRate
: 1.0f;
}
}
// ------------------------------------------------------------------------
//
// Functions
//
// ------------------------------------------------------------------------
public void ToBeginFrame() {
currentFrame = 0;
}
public void ToEndFrame() {
currentFrame = frameCount - 1;
}
public bool ToPrevFrame() {
if ( currentFrame > 0 ) {
--currentFrame;
return true;
}
return false;
}
public bool ToNextFrame() {
if ( currentFrame < frameCount - 1 ) {
++currentFrame;
return true;
}
return false;
}
// ------------------------------------------------------------------------
//
// Private
//
// ------------------------------------------------------------------------
SwfAnimationAsset.Frame GetCurrentBakedFrame() {
var frames = Asset ? Asset.Frames : null;
return frames != null && frames.Count > 0
? frames[Mathf.Clamp(currentFrame, 0, frames.Count)]
: null;
void ChangeSortingProperties() {
_meshRenderer.sortingOrder = sortingOrder;
_meshRenderer.sortingLayerName = sortingLayer;
}
void ChangeAsset() {
_meshRenderer.enabled = !!asset;
if ( asset ) {
var prop_block = new MaterialPropertyBlock();
prop_block.SetTexture("_MainTex", asset.Atlas);
_meshRenderer.SetPropertyBlock(prop_block);
}
UpdateCurrentMesh();
}
void ChangeCurrentFrame() {
_currentFrame = frameCount > 0
? Mathf.Clamp(currentFrame, 0, frameCount - 1)
: 0;
UpdateCurrentMesh();
}
void UpdateCurrentMesh() {
var baked_frame = GetCurrentBakedFrame();
_meshFilter.sharedMesh = baked_frame != null ? baked_frame.Mesh : null;
_meshRenderer.sharedMaterials = baked_frame != null ? baked_frame.Materials : new Material[0];
_meshRenderer.sortingOrder = SortingOrder;
_meshRenderer.sortingLayerName = SortingLayer;
_meshRenderer.SetPropertyBlock(_matPropBlock);
var baked_frame = GetCurrentBakedFrame();
_meshFilter.sharedMesh = baked_frame.Mesh;
_meshRenderer.sharedMaterials = baked_frame.Materials;
}
// ---------------------------------------------------------------------
//
// Internal
//
// ---------------------------------------------------------------------
public void InitWithAsset(SwfAnimationAsset asset) {
Asset = asset;
if ( asset ) {
_matPropBlock = new MaterialPropertyBlock();
_matPropBlock.SetTexture("_MainTex", asset.Atlas);
}
var controller = GetComponent<SwfAnimationController>();
if ( controller ) {
controller.InitWithAsset();
}
UpdateCurrentMesh();
SwfAnimationAsset.Frame GetCurrentBakedFrame() {
return currentFrame >= 0 && currentFrame < frameCount
? asset.Frames[currentFrame]
: SwfAnimationAsset.Frame.identity;
}
// ---------------------------------------------------------------------
@@ -97,7 +153,6 @@ namespace FlashTools {
void Awake() {
_meshFilter = GetComponent<MeshFilter>();
_meshRenderer = GetComponent<MeshRenderer>();
InitWithAsset(Asset);
}
void OnEnable() {
@@ -113,5 +168,18 @@ namespace FlashTools {
swf_manager.RemoveSwfAnimation(this);
}
}
#if UNITY_EDITOR
void Reset() {
OnValidate();
}
void OnValidate() {
asset = _asset;
currentFrame = _currentFrame;
sortingLayer = _sortingLayer;
sortingOrder = _sortingOrder;
}
#endif
}
}
@@ -65,6 +65,7 @@ namespace FlashTools {
[System.Serializable]
public class SwfAnimationData {
public float FrameRate = 0.0f;
public Vector2 FrameSize = Vector2.zero;
public List<SwfAnimationFrameData> Frames = new List<SwfAnimationFrameData>();
public List<SwfAnimationBitmapData> Bitmaps = new List<SwfAnimationBitmapData>();
}
@@ -74,6 +75,13 @@ namespace FlashTools {
public class Frame {
public Mesh Mesh;
public Material[] Materials;
public static Frame identity {
get {
return new Frame{
Mesh = new Mesh(),
Materials = new Material[0]};
}
}
}
public SwfAnimationData Data;
public Texture2D Atlas;
@@ -15,10 +15,6 @@ namespace FlashTools {
Playing
}
//
//
//
public bool AutoPlay = false;
public LoopModes LoopMode = LoopModes.Once;
@@ -30,6 +26,52 @@ namespace FlashTools {
float _frameTimer = 0.0f;
States _currentState = States.Stopped;
// ---------------------------------------------------------------------
//
// Functions
//
// ---------------------------------------------------------------------
public void Stop() {
_frameTimer = 0.0f;
_animation.currentFrame = 0;
_currentState = States.Stopped;
}
public void Pause() {
if ( currentState == States.Playing ) {
_currentState = States.Paused;
}
}
public void Resume() {
if ( currentState == States.Paused ) {
_currentState = States.Playing;
}
}
public void Play() {
_frameTimer = 0.0f;
_animation.currentFrame = 0;
_currentState = States.Playing;
}
public bool isStopped {
get { return currentState == States.Stopped; }
}
public bool isPaused {
get { return currentState == States.Paused; }
}
public bool isPlaying {
get { return currentState == States.Playing; }
}
public States currentState {
get { return _currentState; }
}
// ---------------------------------------------------------------------
//
// Internal
@@ -77,52 +119,6 @@ namespace FlashTools {
}
}
// ---------------------------------------------------------------------
//
// Functions
//
// ---------------------------------------------------------------------
public void Stop() {
_frameTimer = 0.0f;
_animation.currentFrame = 0;
_currentState = States.Stopped;
}
public void Pause() {
if ( currentState == States.Playing ) {
_currentState = States.Paused;
}
}
public void Resume() {
if ( currentState == States.Paused ) {
_currentState = States.Playing;
}
}
public void Play() {
_frameTimer = 0.0f;
_animation.currentFrame = 0;
_currentState = States.Playing;
}
public bool isStopped {
get { return currentState == States.Stopped; }
}
public bool isPaused {
get { return currentState == States.Paused; }
}
public bool isPlaying {
get { return currentState == States.Playing; }
}
public States currentState {
get { return _currentState; }
}
// ---------------------------------------------------------------------
//
// Messages