baked materials wip

This commit is contained in:
2016-08-25 19:18:48 +07:00
parent 6190dd60f6
commit d043c0b973
36 changed files with 2210 additions and 219 deletions

View File

@@ -65,7 +65,7 @@ namespace FlashTools.Internal {
anim_go.AddComponent<MeshFilter>();
anim_go.AddComponent<MeshRenderer>();
anim_go.AddComponent<SwfAnimation>().Asset = _asset;
anim_go.GetComponent<SwfAnimation>().BakeFrameMeshes();
anim_go.GetComponent<SwfAnimation>().InitStuff(); //FIXME
return anim_go;
}
return null;
@@ -126,6 +126,10 @@ namespace FlashTools.Internal {
if ( atlas_prop != null ) {
EditorGUILayout.PropertyField(atlas_prop, true);
}
var baked_frames_prop = serializedObject.FindProperty("BakedFrames");
if ( baked_frames_prop != null && baked_frames_prop.isArray ) {
EditorGUILayout.IntField("Frame count", baked_frames_prop.arraySize);
}
GUI.enabled = true;
_settingsFoldout = EditorGUILayout.Foldout(_settingsFoldout, "Settings");
if ( _settingsFoldout ) {

View File

@@ -4,6 +4,7 @@ using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
namespace FlashTools.Internal {
public class SwfAnimationAssetPostprocessor : AssetPostprocessor {
@@ -29,6 +30,7 @@ namespace FlashTools.Internal {
if ( atlas_asset != asset.Atlas ) {
asset.Atlas = atlas_asset;
ConfigureAtlas(asset_path, asset);
ConfigureBakedFrames(asset_path, asset);
EditorUtility.SetDirty(asset);
AssetDatabase.SaveAssets();
}
@@ -48,6 +50,13 @@ namespace FlashTools.Internal {
return Path.ChangeExtension(asset_path, ".png");
}
// ------------------------------------------------------------------------
//
// ConfigureAtlas
//
// ------------------------------------------------------------------------
static void ConfigureAtlas(string asset_path, SwfAnimationAsset asset) {
var atlas_importer = GetBitmapsAtlasImporter(asset_path);
var atlas_importer_size = GetSizeFromTextureImporter(atlas_importer);
@@ -125,5 +134,150 @@ namespace FlashTools.Internal {
format));
}
}
// ------------------------------------------------------------------------
//
// ConfigureBakedFrames
//
// ------------------------------------------------------------------------
class BakeGroup {
public SwfAnimationInstanceType Type;
public int ClipDepth;
public List<int> Triangles;
public Material Material;
}
static void ConfigureBakedFrames(string asset_path, SwfAnimationAsset asset) {
var baked_frames = new List<SwfAnimationAsset.BakedFrame>();
if ( asset && asset.Atlas && asset.Data != null && asset.Data.Frames.Count > 0 ) {
for ( var i = 0; i < asset.Data.Frames.Count; ++i ) {
var frame = asset.Data.Frames[i];
var baked_frame = BakeFrameFromAnimationFrame(asset, frame);
baked_frames.Add(baked_frame);
}
}
asset.BakedFrames = baked_frames;
}
static SwfAnimationAsset.BakedFrame BakeFrameFromAnimationFrame(SwfAnimationAsset asset, SwfAnimationFrameData frame) {
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<BakeGroup> _groups = new List<BakeGroup>();
List<Material> _materials = new List<Material>();
for ( var i = 0; i < frame.Instances.Count; ++i ) {
var inst = frame.Instances[i];
var bitmap = inst != null ? FindBitmapFromAnimationData(asset.Data, 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 BakeGroup{
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);
}
}
var swf_manager = SwfManager.GetInstance(true);
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();
AssetDatabase.AddObjectToAsset(mesh, asset);
return new SwfAnimationAsset.BakedFrame{
Mesh = mesh,
Materials = _materials.ToArray()};
}
static SwfAnimationBitmapData FindBitmapFromAnimationData(SwfAnimationData data, int bitmap_id) {
for ( var i = 0; i < data.Bitmaps.Count; ++i ) {
var bitmap = data.Bitmaps[i];
if ( bitmap.Id == bitmap_id ) {
return bitmap;
}
}
return null;
}
}
}

View File

@@ -5,23 +5,6 @@ using System.Collections.Generic;
namespace FlashTools.Internal.SwfEditorTools {
//
// SwfReadOnlyDrawer
//
[CustomPropertyDrawer(typeof(SwfReadOnlyAttribute))]
public class SwfReadOnlyDrawer : PropertyDrawer {
public override void OnGUI(
Rect position, SerializedProperty property, GUIContent label)
{
var last_gui_enabled = GUI.enabled;
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = last_gui_enabled;
}
}
//
// SwfSortingLayerDrawer
//

View File

@@ -1,9 +1,6 @@
using UnityEngine;
namespace FlashTools.Internal {
public class SwfReadOnlyAttribute : PropertyAttribute {
}
public class SwfSortingLayerAttribute : PropertyAttribute {
}