mirror of
https://github.com/BlackMATov/unity-flash-tools.git
synced 2025-12-17 06:29:20 +07:00
SwfBakedAnimation to SwfAnimation
This commit is contained in:
@@ -48,7 +48,6 @@
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfAnimationAssetEditor.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfAnimationAssetPostprocessor.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfAnimationEditor.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfBakedAnimationEditor.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfPostprocessor.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfPropertyDrawers.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfTools\SwfContext.cs" />
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
<Compile Include="Assets\FlashTools\Scripts\Internal\SwfPropertyAttributes.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\SwfAnimation.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\SwfAnimationAsset.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\SwfBakedAnimation.cs" />
|
||||
<Compile Include="Assets\FlashTools\Scripts\SwfManager.cs" />
|
||||
<None Include="Assets\FlashTools\Resources\Shaders\SwfDecrMask.shader" />
|
||||
<None Include="Assets\FlashTools\Resources\Shaders\SwfSimple.shader" />
|
||||
<None Include="Assets\FlashTools\Resources\Shaders\SwfIncrMask.shader" />
|
||||
|
||||
@@ -59,24 +59,20 @@ namespace FlashTools.Internal {
|
||||
}
|
||||
}
|
||||
|
||||
GameObject CreateAnimationGO(bool baked) {
|
||||
GameObject CreateAnimationGO() {
|
||||
if ( _asset ) {
|
||||
var anim_go = new GameObject(_asset.name);
|
||||
anim_go.AddComponent<MeshFilter>();
|
||||
anim_go.AddComponent<MeshRenderer>();
|
||||
if ( baked ) {
|
||||
anim_go.AddComponent<SwfBakedAnimation>().Asset = _asset;
|
||||
anim_go.GetComponent<SwfBakedAnimation>().BakeFrameMeshes();
|
||||
} else {
|
||||
anim_go.AddComponent<SwfAnimation>().Asset = _asset;
|
||||
}
|
||||
anim_go.GetComponent<SwfAnimation>().BakeFrameMeshes();
|
||||
return anim_go;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void CreateAnimationPrefab(bool baked) {
|
||||
var anim_go = CreateAnimationGO(baked);
|
||||
void CreateAnimationPrefab() {
|
||||
var anim_go = CreateAnimationGO();
|
||||
if ( anim_go ) {
|
||||
var prefab_path = GetPrefabPath();
|
||||
if ( !string.IsNullOrEmpty(prefab_path) ) {
|
||||
@@ -93,8 +89,8 @@ namespace FlashTools.Internal {
|
||||
}
|
||||
}
|
||||
|
||||
void CreateAnimationOnScene(bool baked) {
|
||||
var anim_go = CreateAnimationGO(baked);
|
||||
void CreateAnimationOnScene() {
|
||||
var anim_go = CreateAnimationGO();
|
||||
if ( anim_go ) {
|
||||
Undo.RegisterCreatedObjectUndo(anim_go, "Create SwfAnimation");
|
||||
}
|
||||
@@ -167,20 +163,10 @@ namespace FlashTools.Internal {
|
||||
GUILayout.BeginHorizontal();
|
||||
{
|
||||
if ( GUILayout.Button("Create animation prefab") ) {
|
||||
CreateAnimationPrefab(false);
|
||||
CreateAnimationPrefab();
|
||||
}
|
||||
if ( GUILayout.Button("Create animation on scene") ) {
|
||||
CreateAnimationOnScene(false);
|
||||
}
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
GUILayout.BeginHorizontal();
|
||||
{
|
||||
if ( GUILayout.Button("Create baked animation prefab") ) {
|
||||
CreateAnimationPrefab(true);
|
||||
}
|
||||
if ( GUILayout.Button("Create baked animation on scene") ) {
|
||||
CreateAnimationOnScene(true);
|
||||
CreateAnimationOnScene();
|
||||
}
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 40e69fd373e75451283f0389b624e18e
|
||||
timeCreated: 1458054572
|
||||
licenseType: Free
|
||||
guid: 7de32773fb5894d2a8e5b1f15f3b6549
|
||||
timeCreated: 1472043063
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace FlashTools.Internal {
|
||||
[CustomEditor(typeof(SwfBakedAnimation))]
|
||||
public class SwfBakedAnimationEditor : Editor {
|
||||
SwfBakedAnimation _animation = null;
|
||||
|
||||
void OnEnable() {
|
||||
_animation = target as SwfBakedAnimation;
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
DrawDefaultInspector();
|
||||
|
||||
if ( _animation.Asset && _animation.frameCount > 1 ) {
|
||||
var new_current_frame = EditorGUILayout.IntSlider(
|
||||
"Frame", _animation.currentFrame,
|
||||
0, _animation.frameCount - 1);
|
||||
if ( new_current_frame != _animation.currentFrame ) {
|
||||
_animation.currentFrame = new_current_frame;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7de32773fb5894d2a8e5b1f15f3b6549
|
||||
timeCreated: 1472043063
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,31 +1,49 @@
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using FlashTools.Internal;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace FlashTools {
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
|
||||
public class SwfAnimation : MonoBehaviour {
|
||||
public SwfAnimationAsset Asset = null;
|
||||
public int GroupCount = 0;
|
||||
|
||||
public int SortingOrder = 0;
|
||||
[SwfSortingLayer]
|
||||
public string SortingLayer = "Default";
|
||||
public int SortingOrder = 0;
|
||||
|
||||
MeshFilter _meshFilter = null;
|
||||
MeshRenderer _meshRenderer = null;
|
||||
|
||||
int _current_frame = 0;
|
||||
float _frame_timer = 0.0f;
|
||||
string _last_asset_path = string.Empty;
|
||||
|
||||
List<Vector2> _uvs = new List<Vector2>();
|
||||
List<Color> _mulcolors = new List<Color>();
|
||||
List<Vector4> _addcolors = new List<Vector4>();
|
||||
List<Vector3> _vertices = new List<Vector3>();
|
||||
public int frameCount {
|
||||
get { return Asset ? Asset.Data.Frames.Count : 0; }
|
||||
}
|
||||
|
||||
public int currentFrame {
|
||||
get { return _current_frame; }
|
||||
set {
|
||||
_current_frame = Mathf.Clamp(value, 0, frameCount - 1);
|
||||
FixCurrentMesh();
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
//
|
||||
// Stuff
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
MaterialPropertyBlock MatPropBlock;
|
||||
|
||||
class Frame {
|
||||
public Mesh Mesh;
|
||||
public Material[] Materials;
|
||||
}
|
||||
|
||||
List<Frame> _frames = new List<Frame>();
|
||||
|
||||
class Group {
|
||||
public SwfAnimationInstanceType Type;
|
||||
@@ -34,76 +52,39 @@ namespace FlashTools {
|
||||
public Material Material;
|
||||
}
|
||||
|
||||
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<Group> _groups = new List<Group>();
|
||||
List<Material> _materials = new List<Material>();
|
||||
|
||||
public int frameCount {
|
||||
get { return Asset ? Asset.Data.Frames.Count : 0; }
|
||||
}
|
||||
|
||||
public int currentFrame {
|
||||
get { return _current_frame; }
|
||||
set { _current_frame = Mathf.Clamp(value, 0, frameCount - 1); }
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
//
|
||||
// Messages
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
void Update() {
|
||||
if ( Asset ) {
|
||||
_frame_timer += Asset.Data.FrameRate * Time.deltaTime;
|
||||
while ( _frame_timer > 1.0f ) {
|
||||
_frame_timer -= 1.0f;
|
||||
++_current_frame;
|
||||
if ( _current_frame > frameCount - 1 ) {
|
||||
_current_frame = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
#if UNITY_EDITOR
|
||||
OnValidate();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnValidate() {
|
||||
if ( Asset ) {
|
||||
_last_asset_path = AssetDatabase.GetAssetPath(Asset);
|
||||
} else {
|
||||
if ( !string.IsNullOrEmpty(_last_asset_path) ) {
|
||||
Asset = AssetDatabase.LoadAssetAtPath<SwfAnimationAsset>(_last_asset_path);
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
SwfAnimationBitmapData FindBitmap(int bitmap_id) {
|
||||
if ( Asset ) {
|
||||
for ( var i = 0; i < Asset.Data.Bitmaps.Count; ++i ) {
|
||||
var bitmap = Asset.Data.Bitmaps[i];
|
||||
if ( bitmap.Id == bitmap_id ) {
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void OnRenderObject() {
|
||||
if ( Asset ) {
|
||||
_vertices.Clear();
|
||||
void ClearTempBakeData() {
|
||||
_uvs.Clear();
|
||||
_mulcolors.Clear();
|
||||
_addcolors.Clear();
|
||||
_vertices.Clear();
|
||||
_groups.Clear();
|
||||
_materials.Clear();
|
||||
}
|
||||
|
||||
var frame = Asset.Data.Frames[currentFrame];
|
||||
foreach ( var inst in frame.Instances ) {
|
||||
var bitmap = FindBitmap(inst.Bitmap);
|
||||
public void BakeFrameMeshes() {
|
||||
if ( Asset && Asset.Atlas && Asset.Data != null && Asset.Data.Frames.Count > 0 ) {
|
||||
MatPropBlock = new MaterialPropertyBlock();
|
||||
MatPropBlock.SetTexture("_MainTex", Asset.Atlas);
|
||||
for ( var i = 0; i < Asset.Data.Frames.Count; ++i ) {
|
||||
var frame = Asset.Data.Frames[i];
|
||||
BakeFrameMesh(frame);
|
||||
}
|
||||
}
|
||||
FixCurrentMesh();
|
||||
}
|
||||
|
||||
void BakeFrameMesh(SwfAnimationFrameData frame) {
|
||||
var swf_manager = SwfManager.Instance;
|
||||
for ( var i = 0; i < frame.Instances.Count; ++i ) {
|
||||
var inst = frame.Instances[i];
|
||||
var bitmap = inst != null ? FindBitmap(inst.Bitmap) : null;
|
||||
if ( bitmap != null ) {
|
||||
var width = bitmap.RealSize.x / 20.0f;
|
||||
var height = bitmap.RealSize.y / 20.0f;
|
||||
@@ -115,9 +96,9 @@ namespace FlashTools {
|
||||
|
||||
var matrix =
|
||||
Matrix4x4.Scale(new Vector3(
|
||||
1.0f / Asset.Settings.PixelsPerUnit,
|
||||
+1.0f / Asset.Settings.PixelsPerUnit,
|
||||
-1.0f / Asset.Settings.PixelsPerUnit,
|
||||
1.0f / Asset.Settings.PixelsPerUnit)) * inst.Matrix;
|
||||
+1.0f / Asset.Settings.PixelsPerUnit)) * inst.Matrix;
|
||||
|
||||
_vertices.Add(matrix.MultiplyPoint3x4(v0));
|
||||
_vertices.Add(matrix.MultiplyPoint3x4(v1));
|
||||
@@ -140,12 +121,16 @@ namespace FlashTools {
|
||||
_addcolors.Add(inst.ColorTransform.Add);
|
||||
_addcolors.Add(inst.ColorTransform.Add);
|
||||
|
||||
if ( _groups.Count == 0 || _groups[_groups.Count - 1].Type != inst.Type || _groups[_groups.Count - 1].ClipDepth != inst.ClipDepth) {
|
||||
var gr = new Group();
|
||||
gr.Type = inst.Type;
|
||||
gr.ClipDepth = inst.ClipDepth;
|
||||
gr.Triangles = new List<int>();
|
||||
_groups.Add(gr);
|
||||
if ( _groups.Count == 0 ||
|
||||
_groups[_groups.Count - 1].Type != inst.Type ||
|
||||
_groups[_groups.Count - 1].ClipDepth != inst.ClipDepth )
|
||||
{
|
||||
_groups.Add(new Group{
|
||||
Type = inst.Type,
|
||||
ClipDepth = inst.ClipDepth,
|
||||
Triangles = new List<int>(),
|
||||
Material = null
|
||||
});
|
||||
}
|
||||
|
||||
_groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2);
|
||||
@@ -157,53 +142,119 @@ namespace FlashTools {
|
||||
}
|
||||
}
|
||||
|
||||
var full_groups = _groups.Where(p => p.Triangles.Count > 0).ToArray();
|
||||
for ( var i = 0; i < full_groups.Length; ++i ) {
|
||||
var gr = full_groups[i];
|
||||
switch ( gr.Type ) {
|
||||
for ( var i = 0; i < _groups.Count; ++i ) {
|
||||
var group = _groups[i];
|
||||
switch ( group.Type ) {
|
||||
case SwfAnimationInstanceType.Mask:
|
||||
gr.Material = new Material(Shader.Find("FlashTools/SwfIncrMask"));
|
||||
gr.Material.SetTexture("_MainTex", Asset.Atlas);
|
||||
group.Material = swf_manager.GetIncrMaskMaterial();
|
||||
break;
|
||||
case SwfAnimationInstanceType.Group:
|
||||
gr.Material = new Material(Shader.Find("FlashTools/SwfSimple"));
|
||||
gr.Material.SetTexture("_MainTex", Asset.Atlas);
|
||||
group.Material = swf_manager.GetSimpleMaterial();
|
||||
break;
|
||||
case SwfAnimationInstanceType.Masked:
|
||||
gr.Material = new Material(Shader.Find("FlashTools/SwfMasked"));
|
||||
gr.Material.SetTexture("_MainTex", Asset.Atlas);
|
||||
gr.Material.SetInt("_StencilID", gr.ClipDepth);
|
||||
group.Material = swf_manager.GetMaskedMaterial(group.ClipDepth);
|
||||
break;
|
||||
case SwfAnimationInstanceType.MaskReset:
|
||||
gr.Material = new Material(Shader.Find("FlashTools/SwfDecrMask"));
|
||||
gr.Material.SetTexture("_MainTex", Asset.Atlas);
|
||||
group.Material = swf_manager.GetDecrMaskMaterial();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var mesh_renderer = GetComponent<MeshRenderer>();
|
||||
mesh_renderer.sharedMaterials = full_groups.Select(p => p.Material).ToArray();
|
||||
mesh_renderer.sortingOrder = SortingOrder;
|
||||
mesh_renderer.sortingLayerName = SortingLayer;
|
||||
for ( var i = 0; i < _groups.Count; ++i ) {
|
||||
var group = _groups[i];
|
||||
_materials.Add(group.Material);
|
||||
}
|
||||
|
||||
var mesh_filter = GetComponent<MeshFilter>();
|
||||
if ( mesh_filter ) {
|
||||
var mesh = mesh_filter.sharedMesh
|
||||
? mesh_filter.sharedMesh
|
||||
: new Mesh();
|
||||
mesh.Clear();
|
||||
mesh.subMeshCount = full_groups.Length;
|
||||
GroupCount = full_groups.Length;
|
||||
var mesh = new Mesh();
|
||||
mesh.subMeshCount = _groups.Count;
|
||||
mesh.SetVertices(_vertices);
|
||||
for ( var i = 0; i < full_groups.Length; ++i ) {
|
||||
mesh.SetTriangles(full_groups[i].Triangles, i);
|
||||
for ( var i = 0; i < _groups.Count; ++i ) {
|
||||
mesh.SetTriangles(_groups[i].Triangles, i);
|
||||
}
|
||||
mesh.SetUVs(0, _uvs);
|
||||
mesh.SetUVs(1, _addcolors);
|
||||
mesh.SetColors(_mulcolors);
|
||||
mesh.RecalculateNormals();
|
||||
mesh_filter.sharedMesh = mesh;
|
||||
|
||||
_frames.Add(new Frame{
|
||||
Mesh = mesh,
|
||||
Materials = _materials.ToArray()});
|
||||
ClearTempBakeData();
|
||||
}
|
||||
|
||||
SwfAnimationBitmapData FindBitmap(int bitmap_id) {
|
||||
if ( Asset ) {
|
||||
for ( var i = 0; i < Asset.Data.Bitmaps.Count; ++i ) {
|
||||
var bitmap = Asset.Data.Bitmaps[i];
|
||||
if ( bitmap.Id == bitmap_id ) {
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void UpdateFrameTimer() {
|
||||
if ( Asset ) {
|
||||
if ( _frames.Count == 0 ) {
|
||||
BakeFrameMeshes();
|
||||
FixCurrentMesh();
|
||||
}
|
||||
_frame_timer += Asset.Data.FrameRate * Time.deltaTime;
|
||||
while ( _frame_timer > 1.0f ) {
|
||||
_frame_timer -= 1.0f;
|
||||
++_current_frame;
|
||||
if ( _current_frame > frameCount - 1 ) {
|
||||
_current_frame = 0;
|
||||
}
|
||||
FixCurrentMesh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FixCurrentMesh() {
|
||||
if ( _frames.Count > 0 ) {
|
||||
var frame = _frames[Mathf.Clamp(currentFrame, 0, _frames.Count)];
|
||||
_meshFilter.sharedMesh = frame.Mesh;
|
||||
_meshRenderer.sharedMaterials = frame.Materials;
|
||||
_meshRenderer.sortingOrder = SortingOrder;
|
||||
_meshRenderer.sortingLayerName = SortingLayer;
|
||||
_meshRenderer.SetPropertyBlock(MatPropBlock);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Internal
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public void InternalUpdate() {
|
||||
UpdateFrameTimer();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Messages
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
void Awake() {
|
||||
_meshFilter = GetComponent<MeshFilter>();
|
||||
_meshRenderer = GetComponent<MeshRenderer>();
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
var swf_manager = SwfManager.Instance;
|
||||
if ( swf_manager ) {
|
||||
swf_manager.AddSwfAnimation(this);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable() {
|
||||
var swf_manager = SwfManager.Instance;
|
||||
if ( swf_manager ) {
|
||||
swf_manager.RemoveSwfAnimation(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2baa05cb4bda40e5ac10a5f833e104e
|
||||
timeCreated: 1458054531
|
||||
licenseType: Free
|
||||
guid: 950d548c7e22f4e25a47de474b49e86e
|
||||
timeCreated: 1472040299
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
|
||||
@@ -1,261 +0,0 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using FlashTools.Internal;
|
||||
|
||||
namespace FlashTools {
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
|
||||
public class SwfBakedAnimation : MonoBehaviour {
|
||||
public SwfAnimationAsset Asset = null;
|
||||
|
||||
[SwfSortingLayer]
|
||||
public string SortingLayer = "Default";
|
||||
public int SortingOrder = 0;
|
||||
|
||||
MeshFilter _meshFilter = null;
|
||||
MeshRenderer _meshRenderer = null;
|
||||
|
||||
int _current_frame = 0;
|
||||
float _frame_timer = 0.0f;
|
||||
|
||||
public int frameCount {
|
||||
get { return Asset ? Asset.Data.Frames.Count : 0; }
|
||||
}
|
||||
|
||||
public int currentFrame {
|
||||
get { return _current_frame; }
|
||||
set {
|
||||
_current_frame = Mathf.Clamp(value, 0, frameCount - 1);
|
||||
FixCurrentMesh();
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
//
|
||||
// Stuff
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
MaterialPropertyBlock MatPropBlock;
|
||||
|
||||
class Frame {
|
||||
public Mesh Mesh;
|
||||
public Material[] Materials;
|
||||
}
|
||||
|
||||
List<Frame> _frames = new List<Frame>();
|
||||
|
||||
class Group {
|
||||
public SwfAnimationInstanceType Type;
|
||||
public int ClipDepth;
|
||||
public List<int> Triangles;
|
||||
public Material Material;
|
||||
}
|
||||
|
||||
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<Group> _groups = new List<Group>();
|
||||
List<Material> _materials = new List<Material>();
|
||||
|
||||
void ClearTempBakeData() {
|
||||
_uvs.Clear();
|
||||
_mulcolors.Clear();
|
||||
_addcolors.Clear();
|
||||
_vertices.Clear();
|
||||
_groups.Clear();
|
||||
_materials.Clear();
|
||||
}
|
||||
|
||||
public void BakeFrameMeshes() {
|
||||
if ( Asset && Asset.Atlas && Asset.Data != null && Asset.Data.Frames.Count > 0 ) {
|
||||
MatPropBlock = new MaterialPropertyBlock();
|
||||
MatPropBlock.SetTexture("_MainTex", Asset.Atlas);
|
||||
for ( var i = 0; i < Asset.Data.Frames.Count; ++i ) {
|
||||
var frame = Asset.Data.Frames[i];
|
||||
BakeFrameMesh(frame);
|
||||
}
|
||||
}
|
||||
FixCurrentMesh();
|
||||
}
|
||||
|
||||
void BakeFrameMesh(SwfAnimationFrameData frame) {
|
||||
var swf_manager = SwfManager.Instance;
|
||||
for ( var i = 0; i < frame.Instances.Count; ++i ) {
|
||||
var inst = frame.Instances[i];
|
||||
var bitmap = inst != null ? FindBitmap(inst.Bitmap) : null;
|
||||
if ( bitmap != null ) {
|
||||
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 matrix =
|
||||
Matrix4x4.Scale(new Vector3(
|
||||
+1.0f / Asset.Settings.PixelsPerUnit,
|
||||
-1.0f / Asset.Settings.PixelsPerUnit,
|
||||
+1.0f / Asset.Settings.PixelsPerUnit)) * inst.Matrix;
|
||||
|
||||
_vertices.Add(matrix.MultiplyPoint3x4(v0));
|
||||
_vertices.Add(matrix.MultiplyPoint3x4(v1));
|
||||
_vertices.Add(matrix.MultiplyPoint3x4(v2));
|
||||
_vertices.Add(matrix.MultiplyPoint3x4(v3));
|
||||
|
||||
var source_rect = bitmap.SourceRect;
|
||||
_uvs.Add(new Vector2(source_rect.xMin, source_rect.yMin));
|
||||
_uvs.Add(new Vector2(source_rect.xMax, source_rect.yMin));
|
||||
_uvs.Add(new Vector2(source_rect.xMax, source_rect.yMax));
|
||||
_uvs.Add(new Vector2(source_rect.xMin, source_rect.yMax));
|
||||
|
||||
_mulcolors.Add(inst.ColorTransform.Mul);
|
||||
_mulcolors.Add(inst.ColorTransform.Mul);
|
||||
_mulcolors.Add(inst.ColorTransform.Mul);
|
||||
_mulcolors.Add(inst.ColorTransform.Mul);
|
||||
|
||||
_addcolors.Add(inst.ColorTransform.Add);
|
||||
_addcolors.Add(inst.ColorTransform.Add);
|
||||
_addcolors.Add(inst.ColorTransform.Add);
|
||||
_addcolors.Add(inst.ColorTransform.Add);
|
||||
|
||||
if ( _groups.Count == 0 ||
|
||||
_groups[_groups.Count - 1].Type != inst.Type ||
|
||||
_groups[_groups.Count - 1].ClipDepth != inst.ClipDepth )
|
||||
{
|
||||
_groups.Add(new Group{
|
||||
Type = inst.Type,
|
||||
ClipDepth = inst.ClipDepth,
|
||||
Triangles = new List<int>(),
|
||||
Material = null
|
||||
});
|
||||
}
|
||||
|
||||
_groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2);
|
||||
_groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 1);
|
||||
_groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 0);
|
||||
_groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 0);
|
||||
_groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 3);
|
||||
_groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2);
|
||||
}
|
||||
}
|
||||
|
||||
for ( var i = 0; i < _groups.Count; ++i ) {
|
||||
var group = _groups[i];
|
||||
switch ( group.Type ) {
|
||||
case SwfAnimationInstanceType.Mask:
|
||||
group.Material = swf_manager.GetIncrMaskMaterial();
|
||||
break;
|
||||
case SwfAnimationInstanceType.Group:
|
||||
group.Material = swf_manager.GetSimpleMaterial();
|
||||
break;
|
||||
case SwfAnimationInstanceType.Masked:
|
||||
group.Material = swf_manager.GetMaskedMaterial(group.ClipDepth);
|
||||
break;
|
||||
case SwfAnimationInstanceType.MaskReset:
|
||||
group.Material = swf_manager.GetDecrMaskMaterial();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for ( var i = 0; i < _groups.Count; ++i ) {
|
||||
var group = _groups[i];
|
||||
_materials.Add(group.Material);
|
||||
}
|
||||
|
||||
var mesh = new Mesh();
|
||||
mesh.subMeshCount = _groups.Count;
|
||||
mesh.SetVertices(_vertices);
|
||||
for ( var i = 0; i < _groups.Count; ++i ) {
|
||||
mesh.SetTriangles(_groups[i].Triangles, i);
|
||||
}
|
||||
mesh.SetUVs(0, _uvs);
|
||||
mesh.SetUVs(1, _addcolors);
|
||||
mesh.SetColors(_mulcolors);
|
||||
mesh.RecalculateNormals();
|
||||
|
||||
_frames.Add(new Frame{
|
||||
Mesh = mesh,
|
||||
Materials = _materials.ToArray()});
|
||||
ClearTempBakeData();
|
||||
}
|
||||
|
||||
SwfAnimationBitmapData FindBitmap(int bitmap_id) {
|
||||
if ( Asset ) {
|
||||
for ( var i = 0; i < Asset.Data.Bitmaps.Count; ++i ) {
|
||||
var bitmap = Asset.Data.Bitmaps[i];
|
||||
if ( bitmap.Id == bitmap_id ) {
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void UpdateFrameTimer() {
|
||||
if ( Asset ) {
|
||||
if ( _frames.Count == 0 ) {
|
||||
BakeFrameMeshes();
|
||||
FixCurrentMesh();
|
||||
}
|
||||
_frame_timer += Asset.Data.FrameRate * Time.deltaTime;
|
||||
while ( _frame_timer > 1.0f ) {
|
||||
_frame_timer -= 1.0f;
|
||||
++_current_frame;
|
||||
if ( _current_frame > frameCount - 1 ) {
|
||||
_current_frame = 0;
|
||||
}
|
||||
FixCurrentMesh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FixCurrentMesh() {
|
||||
if ( _frames.Count > 0 ) {
|
||||
var frame = _frames[Mathf.Clamp(currentFrame, 0, _frames.Count)];
|
||||
_meshFilter.sharedMesh = frame.Mesh;
|
||||
_meshRenderer.sharedMaterials = frame.Materials;
|
||||
_meshRenderer.sortingOrder = SortingOrder;
|
||||
_meshRenderer.sortingLayerName = SortingLayer;
|
||||
_meshRenderer.SetPropertyBlock(MatPropBlock);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Internal
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public void InternalUpdate() {
|
||||
UpdateFrameTimer();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Messages
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
void Awake() {
|
||||
_meshFilter = GetComponent<MeshFilter>();
|
||||
_meshRenderer = GetComponent<MeshRenderer>();
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
var swf_manager = SwfManager.Instance;
|
||||
if ( swf_manager ) {
|
||||
swf_manager.AddSwfBakedAnimation(this);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable() {
|
||||
var swf_manager = SwfManager.Instance;
|
||||
if ( swf_manager ) {
|
||||
swf_manager.RemoveSwfBakedAnimation(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 950d548c7e22f4e25a47de474b49e86e
|
||||
timeCreated: 1472040299
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -29,7 +29,7 @@ namespace FlashTools {
|
||||
[SerializeField] [HideInInspector] Material _incrMaskMaterial = null;
|
||||
[SerializeField] [HideInInspector] Material _decrMaskMaterial = null;
|
||||
|
||||
HashSet<SwfBakedAnimation> _bakedAnimations = new HashSet<SwfBakedAnimation>();
|
||||
HashSet<SwfAnimation> _animations = new HashSet<SwfAnimation>();
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
@@ -53,12 +53,12 @@ namespace FlashTools {
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public void AddSwfBakedAnimation(SwfBakedAnimation animation) {
|
||||
_bakedAnimations.Add(animation);
|
||||
public void AddSwfAnimation(SwfAnimation animation) {
|
||||
_animations.Add(animation);
|
||||
}
|
||||
|
||||
public void RemoveSwfBakedAnimation(SwfBakedAnimation animation) {
|
||||
_bakedAnimations.Remove(animation);
|
||||
public void RemoveSwfAnimation(SwfAnimation animation) {
|
||||
_animations.Remove(animation);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
@@ -142,21 +142,21 @@ namespace FlashTools {
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
var all_baked_animations = FindObjectsOfType<SwfBakedAnimation>();
|
||||
for ( int i = 0, e = all_baked_animations.Length; i < e; ++i ) {
|
||||
var baked_animation = all_baked_animations[i];
|
||||
if ( baked_animation.enabled ) {
|
||||
_bakedAnimations.Add(baked_animation);
|
||||
var all_animations = FindObjectsOfType<SwfAnimation>();
|
||||
for ( int i = 0, e = all_animations.Length; i < e; ++i ) {
|
||||
var animation = all_animations[i];
|
||||
if ( animation.enabled ) {
|
||||
_animations.Add(animation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable() {
|
||||
_bakedAnimations.Clear();
|
||||
_animations.Clear();
|
||||
}
|
||||
|
||||
void Update() {
|
||||
var iter = _bakedAnimations.GetEnumerator();
|
||||
var iter = _animations.GetEnumerator();
|
||||
while ( iter.MoveNext() ) {
|
||||
iter.Current.InternalUpdate();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user