baked materials and animations wip

This commit is contained in:
2016-08-25 23:04:27 +07:00
parent d043c0b973
commit 4d0a94dc4d
13 changed files with 284 additions and 220 deletions

View File

@@ -20,3 +20,17 @@ MonoBehaviour:
AtlasForceSquare: 0
AtlasTextureFilter: 1
AtlasTextureFormat: 2
SimpleMaterial: {fileID: 2100000, guid: e92fbe00d5b6d4a3aa9355a4b23f107d, type: 2}
IncrMaskMaterial: {fileID: 2100000, guid: e62e8efca5cac42c9a323961b58d05d6, type: 2}
DecrMaskMaterial: {fileID: 2100000, guid: d63a68532164d470d887fd2603e79ffa, type: 2}
MaskedMaterials:
- {fileID: 2100000, guid: 3902e0561ba6046079e975f4b8340dbd, type: 2}
- {fileID: 2100000, guid: 362db403211244f70830b07296318936, type: 2}
- {fileID: 2100000, guid: 43234b460d10f41088cbd327c045074b, type: 2}
- {fileID: 2100000, guid: f9e8b8b157ffa4866812a11237c48758, type: 2}
- {fileID: 2100000, guid: 04597a59523be4b939c0a5dfd05b973e, type: 2}
- {fileID: 2100000, guid: 502586963b8c243d48808238fb1210ef, type: 2}
- {fileID: 2100000, guid: dfbc0da8152b047b58270d40b138b36c, type: 2}
- {fileID: 2100000, guid: efeaba40d10a4417eb27036131ba80b1, type: 2}
- {fileID: 2100000, guid: 0f131b0532a4e4feaba67826153a42b7, type: 2}
- {fileID: 2100000, guid: ccd8fc1b2c96543ff9dc43ed384704a2, type: 2}

View File

@@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: ea1dd488b8903439b90ce209ff82574c
timeCreated: 1472013183
timeCreated: 1472140695
licenseType: Free
NativeFormatImporter:
userData:

View File

@@ -64,8 +64,7 @@ namespace FlashTools.Internal {
var anim_go = new GameObject(_asset.name);
anim_go.AddComponent<MeshFilter>();
anim_go.AddComponent<MeshRenderer>();
anim_go.AddComponent<SwfAnimation>().Asset = _asset;
anim_go.GetComponent<SwfAnimation>().InitStuff(); //FIXME
anim_go.AddComponent<SwfAnimation>().InitWithAsset(_asset);
return anim_go;
}
return null;

View File

@@ -100,14 +100,14 @@ namespace FlashTools.Internal {
}
static FilterMode SwfAtlasFilterToImporterFilter(
SwfConverterSettings.SwfAtlasFilter filter)
SwfSettings.AtlasFilter filter)
{
switch ( filter ) {
case SwfConverterSettings.SwfAtlasFilter.Point:
case SwfSettings.AtlasFilter.Point:
return FilterMode.Point;
case SwfConverterSettings.SwfAtlasFilter.Bilinear:
case SwfSettings.AtlasFilter.Bilinear:
return FilterMode.Bilinear;
case SwfConverterSettings.SwfAtlasFilter.Trilinear:
case SwfSettings.AtlasFilter.Trilinear:
return FilterMode.Trilinear;
default:
throw new UnityException(string.Format(
@@ -117,16 +117,16 @@ namespace FlashTools.Internal {
}
static TextureImporterFormat SwfAtlasFormatToImporterFormat(
SwfConverterSettings.SwfAtlasFormat format)
SwfSettings.AtlasFormat format)
{
switch ( format ) {
case SwfConverterSettings.SwfAtlasFormat.AutomaticCompressed:
case SwfSettings.AtlasFormat.AutomaticCompressed:
return TextureImporterFormat.AutomaticCompressed;
case SwfConverterSettings.SwfAtlasFormat.Automatic16bit:
case SwfSettings.AtlasFormat.Automatic16bit:
return TextureImporterFormat.Automatic16bit;
case SwfConverterSettings.SwfAtlasFormat.AutomaticTruecolor:
case SwfSettings.AtlasFormat.AutomaticTruecolor:
return TextureImporterFormat.AutomaticTruecolor;
case SwfConverterSettings.SwfAtlasFormat.AutomaticCrunched:
case SwfSettings.AtlasFormat.AutomaticCrunched:
return TextureImporterFormat.AutomaticCrunched;
default:
throw new UnityException(string.Format(
@@ -228,21 +228,21 @@ namespace FlashTools.Internal {
}
}
var swf_manager = SwfManager.GetInstance(true);
var default_converter = SwfConverterSettings.GetDefaultConverter();
for ( var i = 0; i < _groups.Count; ++i ) {
var group = _groups[i];
switch ( group.Type ) {
case SwfAnimationInstanceType.Mask:
group.Material = swf_manager.GetIncrMaskMaterial();
group.Material = default_converter.GetIncrMaskMaterial();
break;
case SwfAnimationInstanceType.Group:
group.Material = swf_manager.GetSimpleMaterial();
group.Material = default_converter.GetSimpleMaterial();
break;
case SwfAnimationInstanceType.Masked:
group.Material = swf_manager.GetMaskedMaterial(group.ClipDepth);
group.Material = default_converter.GetMaskedMaterial(group.ClipDepth);
break;
case SwfAnimationInstanceType.MaskReset:
group.Material = swf_manager.GetDecrMaskMaterial();
group.Material = default_converter.GetDecrMaskMaterial();
break;
}
}

View File

@@ -238,9 +238,9 @@ namespace FlashTools.Internal {
}
static Rect[] PackAndSaveBitmapsAtlas(
string swf_asset,
Texture2D[] textures,
SwfConverterSettings.Settings settings)
string swf_asset,
Texture2D[] textures,
SwfSettings settings)
{
var atlas_info = PackBitmapsAtlas(textures, settings);
File.WriteAllBytes(
@@ -254,8 +254,8 @@ namespace FlashTools.Internal {
}
static BitmapsAtlasInfo PackBitmapsAtlas(
Texture2D[] textures,
SwfConverterSettings.Settings settings)
Texture2D[] textures,
SwfSettings settings)
{
var atlas_padding = Mathf.Max(0, settings.AtlasPadding);
var max_atlas_size = Mathf.Max(32, settings.AtlasPowerOfTwo

View File

@@ -5,6 +5,54 @@ using System.Collections.Generic;
namespace FlashTools.Internal.SwfEditorTools {
//
// SwfIntRange
//
[CustomPropertyDrawer(typeof(SwfIntRangeAttribute))]
public class SwfIntRangeDrawer : PropertyDrawer {
static void ValidateProperty(SerializedProperty property, int min, int max) {
if ( property.propertyType == SerializedPropertyType.Integer ) {
var clamp = Mathf.Clamp(property.intValue, min, max);
if ( clamp != property.intValue ) {
property.intValue = clamp;
property.serializedObject.ApplyModifiedProperties();
}
}
}
public override void OnGUI(
Rect position, SerializedProperty property, GUIContent label)
{
var attr = attribute as SwfIntRangeAttribute;
ValidateProperty(property, attr.Min, attr.Max);
EditorGUI.PropertyField(position, property, label, true);
}
}
//
// SwfFloatRange
//
[CustomPropertyDrawer(typeof(SwfFloatRangeAttribute))]
public class SwfFloatRangeDrawer : PropertyDrawer {
static void ValidateProperty(SerializedProperty property, float min, float max) {
if ( property.propertyType == SerializedPropertyType.Float ) {
var clamp = Mathf.Clamp(property.floatValue, min, max);
if ( clamp != property.floatValue ) {
property.floatValue = clamp;
property.serializedObject.ApplyModifiedProperties();
}
}
}
public override void OnGUI(
Rect position, SerializedProperty property, GUIContent label)
{
var attr = attribute as SwfFloatRangeAttribute;
ValidateProperty(property, attr.Min, attr.Max);
EditorGUI.PropertyField(position, property, label, true);
}
}
//
// SwfSortingLayerDrawer
//

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
@@ -7,65 +8,107 @@ using System.IO;
namespace FlashTools.Internal {
public class SwfConverterSettings : ScriptableObject {
public enum SwfAtlasFilter {
Point,
Bilinear,
Trilinear
}
public enum SwfAtlasFormat {
AutomaticCompressed,
Automatic16bit,
AutomaticTruecolor,
AutomaticCrunched
}
// ---------------------------------------------------------------------
//
// Constants
//
// ---------------------------------------------------------------------
[System.Serializable]
public struct Settings {
[SwfPowerOfTwoIfAttribute(5, 13, "AtlasPowerOfTwo")]
public int MaxAtlasSize;
public int AtlasPadding;
public int PixelsPerUnit;
public bool GenerateMipMaps;
public bool AtlasPowerOfTwo;
public bool AtlasForceSquare;
public SwfAtlasFilter AtlasTextureFilter;
public SwfAtlasFormat AtlasTextureFormat;
const string DefaultBasePath = "Assets/FlashTools/Resources/";
const string DefaultSettingsPath = DefaultBasePath + "SwfConverterSettings.asset";
const string SwfSimpleMatPath = DefaultBasePath + "Materials/SwfSimpleMat.mat";
const string SwfIncrMaskMatPath = DefaultBasePath + "Materials/SwfIncrMaskMat.mat";
const string SwfDecrMaskMatPath = DefaultBasePath + "Materials/SwfDecrMaskMat.mat";
const string SwfMaskedMatPathFmt = DefaultBasePath + "Materials/SwfMaskedMat_{0}.mat";
public static Settings identity {
get {
return new Settings{
MaxAtlasSize = 1024,
AtlasPadding = 1,
PixelsPerUnit = 100,
GenerateMipMaps = true,
AtlasPowerOfTwo = true,
AtlasForceSquare = false,
AtlasTextureFilter = SwfAtlasFilter.Bilinear,
AtlasTextureFormat = SwfAtlasFormat.AutomaticTruecolor};
}
}
// ---------------------------------------------------------------------
//
// Properties
//
// ---------------------------------------------------------------------
public bool CheckEquals(Settings other) {
return
MaxAtlasSize == other.MaxAtlasSize &&
AtlasPadding == other.AtlasPadding &&
PixelsPerUnit == other.PixelsPerUnit &&
GenerateMipMaps == other.GenerateMipMaps &&
AtlasPowerOfTwo == other.AtlasPowerOfTwo &&
AtlasForceSquare == other.AtlasForceSquare &&
AtlasTextureFilter == other.AtlasTextureFilter &&
AtlasTextureFormat == other.AtlasTextureFormat;
}
}
public Settings DefaultSettings;
[Header("Settings")]
public SwfSettings DefaultSettings;
[Header("Materials cache")]
public Material SimpleMaterial;
public Material IncrMaskMaterial;
public Material DecrMaskMaterial;
public List<Material> MaskedMaterials;
#if UNITY_EDITOR
void Reset() {
DefaultSettings = Settings.identity;
// ---------------------------------------------------------------------
//
// Private
//
// ---------------------------------------------------------------------
void FillMaterialsCache() {
SimpleMaterial = SafeLoadMaterial(SwfSimpleMatPath, true);
IncrMaskMaterial = SafeLoadMaterial(SwfIncrMaskMatPath, true);
DecrMaskMaterial = SafeLoadMaterial(SwfDecrMaskMatPath, true);
MaskedMaterials = new List<Material>();
for ( var i = 0; i < int.MaxValue; ++i ) {
var mat = SafeLoadMaterial(string.Format(SwfMaskedMatPathFmt, i), false);
if ( mat ) {
MaskedMaterials.Add(mat);
} else {
break;
}
}
}
public static Settings GetDefaultSettings() {
Material SafeLoadMaterial(string path, bool exception) {
var material = AssetDatabase.LoadAssetAtPath<Material>(path);
if ( !material && exception ) {
throw new UnityException(string.Format(
"SwfManager. Material not found: {0}",
path));
}
return material;
}
// ---------------------------------------------------------------------
//
// Functions
//
// ---------------------------------------------------------------------
public Material GetMaskedMaterial(int stencil_id) {
if ( stencil_id < 0 || stencil_id >= MaskedMaterials.Count ) {
throw new UnityException(string.Format(
"SwfConverterSettings. Unsupported stencil id: {0}",
stencil_id));
}
return MaskedMaterials[stencil_id];
}
public Material GetSimpleMaterial() {
return SimpleMaterial;
}
public Material GetIncrMaskMaterial() {
return IncrMaskMaterial;
}
public Material GetDecrMaskMaterial() {
return DecrMaskMaterial;
}
// ---------------------------------------------------------------------
//
// Messages
//
// ---------------------------------------------------------------------
void Reset() {
DefaultSettings = SwfSettings.identity;
FillMaterialsCache();
}
public static SwfConverterSettings GetDefaultConverter() {
var settings_path = DefaultSettingsPath;
var settings = AssetDatabase.LoadAssetAtPath<SwfConverterSettings>(settings_path);
if ( !settings ) {
@@ -74,13 +117,11 @@ namespace FlashTools.Internal {
AssetDatabase.CreateAsset(settings, settings_path);
AssetDatabase.SaveAssets();
}
return settings.DefaultSettings;
return settings;
}
static string DefaultSettingsPath {
get {
return "Assets/FlashTools/Resources/SwfConverterSettings.asset";
}
public static SwfSettings GetDefaultSettings() {
return GetDefaultConverter().DefaultSettings;
}
static void CreateAssetDatabaseFolders(string path) {

View File

@@ -1,6 +1,24 @@
using UnityEngine;
namespace FlashTools.Internal {
public class SwfIntRangeAttribute : PropertyAttribute {
public int Min;
public int Max;
public SwfIntRangeAttribute(int min, int max) {
Min = min;
Max = max;
}
}
public class SwfFloatRangeAttribute : PropertyAttribute {
public float Min;
public float Max;
public SwfFloatRangeAttribute(float min, float max) {
Min = min;
Max = max;
}
}
public class SwfSortingLayerAttribute : PropertyAttribute {
}

View File

@@ -0,0 +1,57 @@
using UnityEngine;
namespace FlashTools.Internal {
[System.Serializable]
public struct SwfSettings {
public enum AtlasFilter {
Point,
Bilinear,
Trilinear
}
public enum AtlasFormat {
AutomaticCompressed,
Automatic16bit,
AutomaticTruecolor,
AutomaticCrunched
}
[SwfPowerOfTwoIfAttribute(5, 13, "AtlasPowerOfTwo")]
public int MaxAtlasSize;
[SwfIntRange(0, int.MaxValue)]
public int AtlasPadding;
[SwfFloatRange(float.Epsilon, float.MaxValue)]
public float PixelsPerUnit;
public bool GenerateMipMaps;
public bool AtlasPowerOfTwo;
public bool AtlasForceSquare;
public AtlasFilter AtlasTextureFilter;
public AtlasFormat AtlasTextureFormat;
public static SwfSettings identity {
get {
return new SwfSettings{
MaxAtlasSize = 1024,
AtlasPadding = 1,
PixelsPerUnit = 100.0f,
GenerateMipMaps = true,
AtlasPowerOfTwo = true,
AtlasForceSquare = false,
AtlasTextureFilter = AtlasFilter.Bilinear,
AtlasTextureFormat = AtlasFormat.AutomaticTruecolor};
}
}
public bool CheckEquals(SwfSettings other) {
return
MaxAtlasSize == other.MaxAtlasSize &&
AtlasPadding == other.AtlasPadding &&
Mathf.Approximately(PixelsPerUnit, other.PixelsPerUnit) &&
GenerateMipMaps == other.GenerateMipMaps &&
AtlasPowerOfTwo == other.AtlasPowerOfTwo &&
AtlasForceSquare == other.AtlasForceSquare &&
AtlasTextureFilter == other.AtlasTextureFilter &&
AtlasTextureFormat == other.AtlasTextureFormat;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b1fb47265b4e347af9d738537b50fe18
timeCreated: 1472132899
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -6,16 +6,16 @@ namespace FlashTools {
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class SwfAnimation : MonoBehaviour {
public SwfAnimationAsset Asset = null;
public SwfAnimationAsset Asset = null;
[SwfSortingLayer]
public string SortingLayer = "Default";
public int SortingOrder = 0;
public string SortingLayer = "Default";
public int SortingOrder = 0;
MeshFilter _meshFilter = null;
MeshRenderer _meshRenderer = null;
bool _inited = false;
MaterialPropertyBlock _matPropBlock = null;
bool _inited = false;
MaterialPropertyBlock _matPropBlock = null;
MeshFilter _meshFilter = null;
MeshRenderer _meshRenderer = null;
int _current_frame = 0;
float _frame_timer = 0.0f;
@@ -38,7 +38,8 @@ namespace FlashTools {
//
// ------------------------------------------------------------------------
public void InitStuff() {
public void InitWithAsset(SwfAnimationAsset asset) {
Asset = asset;
if ( Asset && !_inited ) {
_inited = true;
_matPropBlock = new MaterialPropertyBlock();
@@ -91,7 +92,7 @@ namespace FlashTools {
void Awake() {
_meshFilter = GetComponent<MeshFilter>();
_meshRenderer = GetComponent<MeshRenderer>();
InitStuff();
InitWithAsset(Asset);
}
void OnEnable() {

View File

@@ -72,16 +72,14 @@ namespace FlashTools {
public class SwfAnimationAsset : ScriptableObject {
[System.Serializable]
public class BakedFrame {
public Mesh Mesh = null;
public Material[] Materials = new Material[0];
public Mesh Mesh;
public Material[] Materials;
}
public SwfAnimationData Data;
public Texture2D Atlas;
public List<BakedFrame> BakedFrames;
#if UNITY_EDITOR
public SwfConverterSettings.Settings Settings;
public SwfConverterSettings.Settings Overridden;
#endif
public SwfAnimationData Data;
public Texture2D Atlas;
public List<BakedFrame> BakedFrames;
public SwfSettings Settings;
public SwfSettings Overridden;
#if UNITY_EDITOR
void Reset() {

View File

@@ -1,35 +1,15 @@
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace FlashTools {
[ExecuteInEditMode]
public class SwfManager : MonoBehaviour {
// ---------------------------------------------------------------------
//
// Constants
//
// ---------------------------------------------------------------------
const string SwfSimpleMatPath = "Assets/FlashTools/Resources/Materials/SwfSimpleMat.mat";
const string SwfIncrMaskMatPath = "Assets/FlashTools/Resources/Materials/SwfIncrMaskMat.mat";
const string SwfDecrMaskMatPath = "Assets/FlashTools/Resources/Materials/SwfDecrMaskMat.mat";
const string SwfMaskedMatPathFmt = "Assets/FlashTools/Resources/Materials/SwfMaskedMat_{0}.mat";
// ---------------------------------------------------------------------
//
// Properties
//
// ---------------------------------------------------------------------
[SerializeField] [HideInInspector] Material _simpleMaterial = null;
[SerializeField] [HideInInspector] Material _incrMaskMaterial = null;
[SerializeField] [HideInInspector] Material _decrMaskMaterial = null;
[SerializeField] [HideInInspector] List<Material> _maskedMaterials = null;
HashSet<SwfAnimation> _animations = new HashSet<SwfAnimation>();
// ---------------------------------------------------------------------
@@ -70,79 +50,6 @@ namespace FlashTools {
//
// ---------------------------------------------------------------------
void FillMaterialsCache() {
/*
if ( !_maskedShader ) {
_maskedShader = SafeLoadShader(SwfMaskedShaderPath);
}
if ( _maskedMaterials == null ) {
_maskedMaterials = new List<Material>(SwfStencilIdCount);
for ( var i = 0; i < _maskedMaterials.Capacity; ++i ) {
var material = new Material(_maskedShader);
material.SetInt("_StencilID", i);
_maskedMaterials.Add(material);
}
}
if ( !_simpleMaterial ) {
_simpleMaterial = new Material(SafeLoadShader(SwfSimpleShaderPath));
}
if ( !_incrMaskMaterial ) {
_incrMaskMaterial = new Material(SafeLoadShader(SwfIncrMaskShaderPath));
}
if ( !_decrMaskMaterial ) {
_decrMaskMaterial = new Material(SafeLoadShader(SwfDecrMaskShaderPath));
}*/
if ( !_simpleMaterial ) {
_simpleMaterial = SafeLoadMaterial(SwfSimpleMatPath, true);
}
if ( !_incrMaskMaterial ) {
_incrMaskMaterial = SafeLoadMaterial(SwfIncrMaskMatPath, true);
}
if ( !_decrMaskMaterial ) {
_decrMaskMaterial = SafeLoadMaterial(SwfDecrMaskMatPath, true);
}
if ( _maskedMaterials == null ) {
_maskedMaterials = new List<Material>();
for ( var i = 0; i < 100; ++i ) {
var mat = SafeLoadMaterial(string.Format(SwfMaskedMatPathFmt, i), false);
if ( mat ) {
_maskedMaterials.Add(mat);
} else {
break;
}
}
}
}
Material SafeLoadMaterial(string path, bool exception) {
#if UNITY_EDITOR
var material = AssetDatabase.LoadAssetAtPath<Material>(path);
if ( !material && exception ) {
throw new UnityException(string.Format(
"SwfManager. Material not found: {0}",
path));
}
return material;
#else
throw new UnityException("IMPLME!");
#endif
}
/*
Shader SafeLoadShader(string path) {
var shader = Shader.Find(path);
if ( !shader ) {
throw new UnityException(string.Format(
"SwfManager. Shader not found: {0}",
path));
}
return shader;
}*/
void GrabEnabledAnimations() {
var all_animations = FindObjectsOfType<SwfAnimation>();
for ( int i = 0, e = all_animations.Length; i < e; ++i ) {
@@ -164,43 +71,12 @@ namespace FlashTools {
}
}
// ---------------------------------------------------------------------
//
// Functions
//
// ---------------------------------------------------------------------
public Material GetMaskedMaterial(int stencil_id) {
if ( stencil_id < 0 || stencil_id >= _maskedMaterials.Count ) {
throw new UnityException(string.Format(
"SwfManager. Unsupported stencil id: {0}",
stencil_id));
}
return _maskedMaterials[stencil_id];
}
public Material GetSimpleMaterial() {
return _simpleMaterial;
}
public Material GetIncrMaskMaterial() {
return _incrMaskMaterial;
}
public Material GetDecrMaskMaterial() {
return _decrMaskMaterial;
}
// ---------------------------------------------------------------------
//
// Messages
//
// ---------------------------------------------------------------------
void Awake() {
FillMaterialsCache();
}
void OnEnable() {
GrabEnabledAnimations();
}