FRendererTrack.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace Flux
  4. {
  5. public class FRendererTrack : FTrack {
  6. private static Dictionary<int,MaterialPropertyBlockInfo> _materialPropertyBlocks = null;
  7. private static MaterialPropertyBlockInfo GetMaterialPropertyBlockInfo( int objInstanceId )
  8. {
  9. if( _materialPropertyBlocks == null )
  10. _materialPropertyBlocks = new Dictionary<int, MaterialPropertyBlockInfo>();
  11. MaterialPropertyBlockInfo matPropertyBlockInfo = null;
  12. if( _materialPropertyBlocks.TryGetValue( objInstanceId, out matPropertyBlockInfo ) )
  13. return matPropertyBlockInfo;
  14. matPropertyBlockInfo = new MaterialPropertyBlockInfo();
  15. _materialPropertyBlocks.Add( objInstanceId, matPropertyBlockInfo );
  16. return matPropertyBlockInfo;
  17. }
  18. private class MaterialPropertyBlockInfo
  19. {
  20. public MaterialPropertyBlock _materialPropertyBlock = new MaterialPropertyBlock();
  21. public int _frameGotCleared = 0;
  22. public void Clear( int frame )
  23. {
  24. _materialPropertyBlock.Clear();
  25. _frameGotCleared = frame;
  26. }
  27. }
  28. private MaterialPropertyBlockInfo _matPropertyBlockInfo = null;
  29. public MaterialPropertyBlock GetMaterialPropertyBlock() { return _matPropertyBlockInfo != null ? _matPropertyBlockInfo._materialPropertyBlock : null; }
  30. private Renderer _renderer = null;
  31. public Renderer Renderer { get { return _renderer; } }
  32. public override void Init()
  33. {
  34. _renderer = Owner != null ? Owner.GetComponent<Renderer>() : null;
  35. if( !(_renderer is SpriteRenderer) )
  36. _matPropertyBlockInfo = GetMaterialPropertyBlockInfo( Owner != null ? Owner.GetInstanceID() : -1 );
  37. base.Init();
  38. }
  39. public override void UpdateEvents (int frame, float time)
  40. {
  41. if( _matPropertyBlockInfo != null && _matPropertyBlockInfo._frameGotCleared != frame )
  42. _matPropertyBlockInfo.Clear( frame );
  43. base.UpdateEvents(frame, time);
  44. if( _matPropertyBlockInfo != null )
  45. _renderer.SetPropertyBlock( _matPropertyBlockInfo._materialPropertyBlock );
  46. }
  47. public override void UpdateEventsEditor (int currentFrame, float currentTime)
  48. {
  49. if( _matPropertyBlockInfo != null && _matPropertyBlockInfo._frameGotCleared != currentFrame )
  50. _matPropertyBlockInfo.Clear( currentFrame );
  51. base.UpdateEventsEditor (currentFrame, currentTime);
  52. if( _matPropertyBlockInfo != null )
  53. _renderer.SetPropertyBlock( _matPropertyBlockInfo._materialPropertyBlock );
  54. }
  55. public override void Stop ()
  56. {
  57. base.Stop();
  58. if( _matPropertyBlockInfo != null )
  59. {
  60. // Init();
  61. _matPropertyBlockInfo.Clear( _matPropertyBlockInfo._frameGotCleared );
  62. if( _renderer != null )
  63. _renderer.SetPropertyBlock( _matPropertyBlockInfo._materialPropertyBlock );
  64. }
  65. }
  66. }
  67. }