MeshSequencePlayer.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace VertexAnimationTools_30{
  6. [ExecuteInEditMode]
  7. [RequireComponent(typeof(MeshRenderer))]
  8. [RequireComponent(typeof(MeshFilter))]
  9. [DisallowMultipleComponent]
  10. [DefaultExecutionOrder(100)]
  11. [HelpURL("https://polyflow.studio/VertexAnimationToolsDocumentation/VertexAnimationTools30_Documentation.html#MeshSequencePlayer")]
  12. public class MeshSequencePlayer : MonoBehaviour {
  13. [SerializeField]
  14. public MeshFilter MF;
  15. [SerializeField]
  16. public MeshCollider MC;
  17. [SerializeField]
  18. public MeshRenderer MR;
  19. public PlayerUpdateMode UpdateMode = PlayerUpdateMode.LateUpdate;
  20. public AutoPlaybackTypeEnum PlaybackMode = AutoPlaybackTypeEnum.PingPong;
  21. public float FramesPerSecond = 30;
  22. public float Length{
  23. get{
  24. if(meshSequence == null){
  25. return 0;
  26. }
  27. return (float)meshSequence.FramesCount/FramesPerSecond;
  28. }
  29. set{
  30. if(meshSequence != null){
  31. value = Mathf.Abs(value);
  32. FramesPerSecond = (float)meshSequence.FramesCount/value;
  33. }
  34. }
  35. }
  36. public bool UseTimescale = true;
  37. public float NormalizedTime;
  38. float timeDirection = 1;
  39. MeshSequence prevMeshSequence;
  40. public MeshSequence meshSequence;
  41. MeshSequence.Frame currentFrame;
  42. public int TabChoise = 1;
  43. void OnEnable(){
  44. Init();
  45. }
  46. public void Init(){
  47. MF = GetComponent<MeshFilter>();
  48. MR = GetComponent<MeshRenderer>();
  49. MC = GetComponent<MeshCollider>();
  50. LateUpdate();
  51. }
  52. void Update() {
  53. if (UpdateMode == PlayerUpdateMode.Update) {
  54. UpdatePlayback();
  55. }
  56. }
  57. void LateUpdate() {
  58. if (UpdateMode == PlayerUpdateMode.LateUpdate) {
  59. UpdatePlayback();
  60. }
  61. }
  62. void UpdatePlayback(){
  63. if(meshSequence == null){
  64. return;
  65. }
  66. currentFrame = meshSequence[NormalizedTime];
  67. if (currentFrame != null){
  68. if(MF != null){
  69. MF.sharedMesh = currentFrame.FrameMesh;
  70. }
  71. if( MR != null ){
  72. if( meshSequence.PostImport.GenerateMaterials ){
  73. MR.materials = currentFrame.Materials;
  74. }
  75. }
  76. if( MC!=null ){
  77. MC.sharedMesh = currentFrame.FrameMesh;
  78. }
  79. }
  80. if(Application.isPlaying){
  81. float dt = UseTimescale? Time.deltaTime : Time.unscaledDeltaTime;
  82. if(PlaybackMode == AutoPlaybackTypeEnum.Repeat){
  83. NormalizedTime += dt / Length;
  84. NormalizedTime = Mathf.Repeat(NormalizedTime, 1f);
  85. } else if(PlaybackMode == AutoPlaybackTypeEnum.Once){
  86. NormalizedTime += dt / Length;
  87. if(NormalizedTime>1f){
  88. NormalizedTime = 1f;
  89. PlaybackMode = AutoPlaybackTypeEnum.None;
  90. }
  91. } else if (PlaybackMode == AutoPlaybackTypeEnum.PingPong){
  92. NormalizedTime += (dt * timeDirection) / Length;
  93. if(NormalizedTime>1f){
  94. NormalizedTime = 1f - (NormalizedTime - Mathf.Floor(NormalizedTime)); ;
  95. timeDirection = -1;
  96. } else if(NormalizedTime<0){
  97. NormalizedTime = -NormalizedTime;
  98. timeDirection = 1;
  99. }
  100. }
  101. }
  102. }
  103. public IEnumerable<TaskInfo> ImportIE( MeshSequence t ) {
  104. yield return new TaskInfo("Prepare importing",0);
  105. if (ProjectionSamples.Get == null) {
  106. yield return new TaskInfo("Missing Projection Samples asset. Reinstal Vertex Animation Tools package", -1);
  107. yield break;
  108. }
  109. t.PreImport.MSI = new MeshSequenceInfo(t.PreImport.PathToObj, t.PreImport.FilesSortMode );
  110. if (t.PreImport.MSI.State != MeshSequenceInfo.StateEnum.Ready) {
  111. yield return new TaskInfo(t.PreImport.MSI.ShortInfo, -1);
  112. }
  113. t.PreImport.VColorSettings.go = gameObject;
  114. int importFrom = 0;
  115. int importTo = t.PreImport.MSI.Count;
  116. int totalFramesCount = t.PreImport.MSI.Count;
  117. if (t.PreImport.ImportCustomRange) {
  118. importFrom = Mathf.Clamp( t.PreImport.ImportFromFrame, 0, t.PreImport.MSI.Count);
  119. importTo = Mathf.Clamp(t.PreImport.ImportToFrame, 0, t.PreImport.MSI.Count);
  120. totalFramesCount = Mathf.Clamp(importTo - importFrom, 0, int.MaxValue);
  121. }
  122. t.SequenceVerticesCount = new NumbersRange(true);
  123. t.SequenceObjVerticesCount = new NumbersRange(true);
  124. t.SequenceTrianglesCount = new NumbersRange(true);
  125. t.SequenceObjPolygonsCount = new NumbersRange(true);
  126. t.SequenceSubmeshesCount = new NumbersRange(true);
  127. t.SetMaterialsUnused();
  128. List<MeshSequence.Frame> nframes = new List<MeshSequence.Frame>();
  129. int counter = 0;
  130. for (int f = importFrom; f < importTo; f++) {
  131. string objName = Path.GetFileNameWithoutExtension(t.PreImport.MSI[f].fi.FullName);
  132. MeshSequence.Frame frame = t.GetFrameByName(objName );
  133. ObjData od = new ObjData(t.PreImport.MSI[f].fi.FullName);
  134. od.NormalsRecalculationMode = t.PreImport.NormalRecalculationMode;
  135. od.FlipNormals = t.PreImport.FlipNormals;
  136. od.SmoothingGroupsMode = t.PreImport.SmoothingGroupImportMode;
  137. od.ImportUV = t.PreImport.ImportUV;
  138. od.CalculateNormals = t.PreImport.CalculateNormals;
  139. od.CalculateTangents = t.PreImport.CalculateTangents;
  140. #if UNITY_2017_3_OR_NEWER
  141. od.IndexFormat = t.PreImport.IndexFormat;
  142. #endif
  143. od.Build();
  144. od.Apply(t.PreImport.SwapYZAxis, t.PreImport.ScaleFactor);
  145. od.CopyTo(frame.FrameMesh);
  146. if (t.PreImport.GenerateMaterials) {
  147. frame.Materials = t.GetPolygonalMeshMaterials(od.SubMeshes.GetNames());
  148. }
  149. if (od.CalcVertexColor(t.PreImport.VColorSettings)) {
  150. frame.FrameMesh.colors = od.UM_colors;
  151. }
  152. frame.MeshTrisCount = frame.FrameMesh.triangles.Length / 3;
  153. frame.MeshVertsCount = frame.FrameMesh.vertexCount;
  154. frame.ObjVerticesCount = od.Vertices.Count;
  155. frame.ObjPolygonsCount = od.AllPolygons.Count;
  156. t.SequenceSubmeshesCount.Set(od.SubMeshes.Count);
  157. t.SequenceVerticesCount.Set(frame.FrameMesh.vertexCount);
  158. t.SequenceObjVerticesCount.Set(frame.FrameMesh.triangles.Length / 3);
  159. t.SequenceTrianglesCount.Set(od.Vertices.Count);
  160. t.SequenceObjPolygonsCount.Set(od.AllPolygons.Count);
  161. nframes.Add(frame);
  162. TaskInfo ti = new TaskInfo(string.Format("Importing frame #{0}", f), counter/ (float)totalFramesCount);
  163. counter++;
  164. yield return ti;
  165. }
  166. t.PreImport.NormalizedPerFrame = 1f / (float)nframes.Count;
  167. t.PreImport.ImportDate = System.DateTime.Now.ToString();
  168. t.PostImport = t.PreImport;
  169. t.Frames = nframes;
  170. t.ClearUnusedMaterials();
  171. }
  172. }
  173. }