chached meshes wip

This commit is contained in:
2016-09-11 22:55:02 +07:00
parent f068463454
commit e98071f471
6 changed files with 107 additions and 105 deletions

View File

@@ -40,6 +40,19 @@ namespace FlashTools.Internal {
}
}
bool isTargetValidForPreview {
get {
var atlas = targetAtlas;
var frame = targetFrame;
var sequence = targetSequence;
return
atlas &&
frame != null &&
sequence != null &&
frame.CachedMesh && frame.CachedMesh.vertexCount > 0;
}
}
static SwfClipAsset.Frame GetFrameForClip(SwfClipAsset clip, int sequence_index) {
var sequence = GetSequenceForClip(clip, sequence_index);
var frames = sequence != null && sequence.Frames != null && sequence.Frames.Count > 0
@@ -60,8 +73,8 @@ namespace FlashTools.Internal {
static Bounds CalculateBoundsForSequence(SwfClipAsset.Sequence sequence) {
var bounds = sequence != null && sequence.Frames != null && sequence.Frames.Count > 0
? sequence.Frames
.Where (p => !!p.Mesh)
.Select(p => p.Mesh.bounds)
.Where (p => !!p.CachedMesh)
.Select(p => p.CachedMesh.bounds)
: new Bounds[0];
var result = bounds.Any() ? bounds.First() : new Bounds();
foreach ( var bound in bounds ) {
@@ -142,17 +155,15 @@ namespace FlashTools.Internal {
public override void OnPreviewGUI(Rect r, GUIStyle background) {
if ( Event.current.type == EventType.Repaint ) {
var atlas = targetAtlas;
var frame = targetFrame;
var sequence = targetSequence;
if ( atlas && frame != null && sequence != null ) {
if ( isTargetValidForPreview ) {
_previewUtils.BeginPreview(r, background);
{
_matPropBlock.SetTexture("_MainTex", atlas);
ConfigureCameraForSequence(_previewUtils.m_Camera, sequence);
_matPropBlock.SetTexture("_MainTex", targetAtlas);
ConfigureCameraForSequence(_previewUtils.m_Camera, targetSequence);
var frame = targetFrame;
for ( var i = 0; i < frame.Materials.Length; ++i ) {
_previewUtils.DrawMesh(
frame.Mesh,
frame.CachedMesh,
Matrix4x4.identity,
frame.Materials[i],
i,

View File

@@ -19,15 +19,12 @@ namespace FlashTools.Internal {
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
{
if ( GUILayout.Button("Rewind") ) {
AllControllersForeach(p => p.Rewind());
}
if ( GUILayout.Button("Stop") ) {
AllControllersForeach(p => p.Stop());
}
if ( GUILayout.Button("Pause") ) {
AllControllersForeach(p => p.Pause());
}
if ( GUILayout.Button("Resume") ) {
AllControllersForeach(p => p.Resume());
}
if ( GUILayout.Button("Play") ) {
AllControllersForeach(p => p.Play());
}

View File

@@ -151,23 +151,10 @@ namespace FlashTools.Internal {
clip_asset.Container = AssetDatabase.AssetPathToGUID(asset_path);
clip_asset.FrameRate = asset.Data.FrameRate;
clip_asset.Sequences = LoadClipSequences(asset, symbol);
ConfigureClipSubAssets(clip_asset);
EditorUtility.SetDirty(clip_asset);
asset.Clips.Add(clip_asset);
}
static void ConfigureClipSubAssets(SwfClipAsset clip_asset) {
SwfEditorUtils.RemoveAllSubAssets(
AssetDatabase.GetAssetPath(clip_asset));
foreach ( var sequence in clip_asset.Sequences ) {
for ( var i = 0; i < sequence.Frames.Count; ++i ) {
var mesh = sequence.Frames[i].Mesh;
mesh.name = string.Format("{0}_{1}", sequence.Name, i);
AssetDatabase.AddObjectToAsset(mesh, clip_asset);
}
}
}
static List<SwfClipAsset.Sequence> LoadClipSequences(
SwfAsset asset, SwfSymbolData symbol)
{
@@ -306,21 +293,18 @@ namespace FlashTools.Internal {
}
}
var mesh = new Mesh();
mesh.subMeshCount = baked_groups.Count;
mesh.SetVertices(baked_vertices);
for ( var i = 0; i < baked_groups.Count; ++i ) {
mesh.SetTriangles(baked_groups[i].Triangles, i);
}
mesh.SetUVs(0, baked_uvs);
mesh.SetUVs(1, baked_addcolors);
mesh.SetColors(baked_mulcolors);
mesh.RecalculateNormals();
mesh.Optimize();
var mesh_data = new SwfClipAsset.MeshData{
SubMeshes = baked_groups
.Select(p => new SwfClipAsset.SubMeshData{Triangles = p.Triangles})
.ToList(),
Vertices = baked_vertices,
UVs = baked_uvs,
AddColors = baked_addcolors,
MulColors = baked_mulcolors};
return new SwfClipAsset.Frame{
Mesh = mesh,
Materials = baked_materials.ToArray()};
return new SwfClipAsset.Frame(
mesh_data,
baked_materials.ToArray());
}
static SwfBitmapData FindBitmapFromAssetData(SwfAssetData data, int bitmap_id) {

View File

@@ -198,7 +198,7 @@ namespace FlashTools {
void UpdateCurrentMesh() {
if ( _meshFilter && _meshRenderer ) {
var baked_frame = GetCurrentBakedFrame();
_meshFilter.sharedMesh = baked_frame.Mesh;
_meshFilter.sharedMesh = baked_frame.CachedMesh;
_meshRenderer.sharedMaterials = baked_frame.Materials;
}
}

View File

@@ -4,15 +4,63 @@ using System.Collections.Generic;
namespace FlashTools {
public class SwfClipAsset : ScriptableObject {
[System.Serializable]
public class SubMeshData {
public List<int> Triangles = new List<int>();
}
[System.Serializable]
public class MeshData {
public List<SubMeshData> SubMeshes = new List<SubMeshData>();
public List<Vector3> Vertices = new List<Vector3>();
public List<Vector2> UVs = new List<Vector2>();
public List<Vector4> AddColors = new List<Vector4>();
public List<Color> MulColors = new List<Color>();
public void FillMesh(Mesh mesh) {
if ( SubMeshes.Count > 0 ) {
mesh.subMeshCount = SubMeshes.Count;
mesh.SetVertices(Vertices);
for ( var i = 0; i < SubMeshes.Count; ++i ) {
mesh.SetTriangles(SubMeshes[i].Triangles, i);
}
mesh.SetUVs(0, UVs);
mesh.SetUVs(1, AddColors);
mesh.SetColors(MulColors);
}
}
}
[System.Serializable]
public class Frame {
public Mesh Mesh = null;
public Material[] Materials = new Material[0];
public MeshData MeshData = new MeshData();
public Material[] Materials = new Material[0];
public Frame() {
MeshData = new MeshData();
Materials = new Material[0];
}
public Frame(MeshData mesh_data, Material[] materials) {
MeshData = mesh_data;
Materials = materials;
}
Mesh _cachedMesh = null;
public Mesh CachedMesh {
get {
if ( !_cachedMesh ) {
_cachedMesh = new Mesh();
MeshData.FillMesh(_cachedMesh);
}
return _cachedMesh;
}
}
}
[System.Serializable]
public class Sequence {
public string Name = string.Empty;
public List<Frame> Frames = new List<Frame>();
public string Name = string.Empty;
public List<Frame> Frames = new List<Frame>();
}
[SwfReadOnly]

View File

@@ -6,8 +6,9 @@ namespace FlashTools {
[RequireComponent(typeof(SwfClip))]
public class SwfClipController : MonoBehaviour {
SwfClip _clip = null;
float _timer = 0.0f;
SwfClip _clip = null;
bool _isPlaying = false;
float _tickTimer = 0.0f;
// ---------------------------------------------------------------------
//
@@ -16,11 +17,9 @@ namespace FlashTools {
// ---------------------------------------------------------------------
public event System.Action<SwfClipController> OnStopPlayingEvent;
public event System.Action<SwfClipController> OnPlayStoppedEvent;
public event System.Action<SwfClipController> OnRewindPlayingEvent;
public event System.Action<SwfClipController> OnPausePlayingEvent;
public event System.Action<SwfClipController> OnResumePausedEvent;
// ---------------------------------------------------------------------
//
// Properties
@@ -37,12 +36,6 @@ namespace FlashTools {
Loop
}
public enum States {
Stopped,
Paused,
Playing
}
[SerializeField]
bool _autoPlay = false;
public bool autoPlay {
@@ -72,21 +65,12 @@ namespace FlashTools {
set { _loopMode = value; }
}
States _state = States.Stopped;
public States state {
get { return _state; }
public bool isPlaying {
get { return _isPlaying; }
}
public bool isStopped {
get { return state == States.Stopped; }
}
public bool isPaused {
get { return state == States.Paused; }
}
public bool isPlaying {
get { return state == States.Playing; }
get { return !_isPlaying; }
}
// ---------------------------------------------------------------------
@@ -96,43 +80,21 @@ namespace FlashTools {
// ---------------------------------------------------------------------
public void Stop() {
Stop(true);
}
public void Stop(bool rewind) {
var is_playing = isPlaying;
_timer = 0.0f;
_state = States.Stopped;
if ( rewind ) {
Rewind();
}
_isPlaying = false;
_tickTimer = 0.0f;
if ( is_playing && OnStopPlayingEvent != null ) {
OnStopPlayingEvent(this);
}
}
public void Pause() {
if ( isPlaying ) {
_state = States.Paused;
if ( OnPausePlayingEvent != null ) {
OnPausePlayingEvent(this);
}
}
}
public void Resume() {
if ( isPaused ) {
_state = States.Playing;
if ( OnResumePausedEvent != null ) {
OnResumePausedEvent(this);
}
}
}
public void Play() {
Rewind();
_timer = 0.0f;
_state = States.Playing;
var is_stopped = isStopped;
_isPlaying = true;
_tickTimer = 0.0f;
if ( is_stopped && OnPlayStoppedEvent != null ) {
OnPlayStoppedEvent(this);
}
}
public void Rewind() {
@@ -171,9 +133,9 @@ namespace FlashTools {
void UpdateTimer(float dt) {
var frame_rate = _clip ? _clip.frameRate : 1.0f;
_timer += frame_rate * rateScale * dt;
while ( _timer > 1.0f ) {
_timer -= 1.0f;
_tickTimer += frame_rate * rateScale * dt;
while ( _tickTimer > 1.0f ) {
_tickTimer -= 1.0f;
TimerTick();
}
}
@@ -182,7 +144,7 @@ namespace FlashTools {
if ( !NextClipFrame() ) {
switch ( loopMode ) {
case LoopModes.Once:
Stop(false);
Stop();
break;
case LoopModes.Loop:
Rewind();
@@ -216,7 +178,7 @@ namespace FlashTools {
void Awake() {
_clip = GetComponent<SwfClip>();
if ( autoPlay ) {
if ( autoPlay && Application.isPlaying ) {
Play();
}
}