Extension.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. namespace VertexAnimationTools_30{
  6. public enum InterpolateModeEnum {
  7. Linear,
  8. Hermite
  9. }
  10. public enum TransitionModeEnum {
  11. None,
  12. Begin,
  13. End
  14. }
  15. public enum PlayerUpdateMode {
  16. Update,
  17. LateUpdate,
  18. Manually
  19. }
  20. public enum AutoPlaybackTypeEnum {
  21. Repeat,
  22. PingPong,
  23. Once,
  24. None
  25. }
  26. [System.Serializable]
  27. public class MaterialInfo {
  28. public Material Mat;
  29. public bool Used;
  30. public string Name;
  31. public MaterialInfo(string name, Material mat) {
  32. Mat = mat;
  33. Name = name;
  34. }
  35. }
  36. public struct TaskInfo {
  37. public string Name;
  38. public float Persentage;
  39. public TaskInfo(string name, float persentage) {
  40. Name = name;
  41. Persentage = persentage;
  42. }
  43. }
  44. public class NormalizedSpline {
  45. public Vector3[] Vertices;
  46. public float[] Distances;
  47. public float Length;
  48. public NormalizedSpline(Vector3[] vertices ) {
  49. Vertices = new Vector3[vertices.Length];
  50. vertices.CopyTo(Vertices, 0);
  51. Distances = new float[Vertices.Length];
  52. Length = 0;
  53. for (int v = 1; v<Vertices.Length; v++) {
  54. Length += Vector3.Distance(Vertices[v], Vertices[v - 1]);
  55. Distances[v] = Length;
  56. }
  57. }
  58. public Vector3 GetPoint(float persentage) {
  59. float targetDist = persentage * Length;
  60. if (targetDist <= 0) {
  61. return Vertices[0];
  62. }
  63. if (targetDist >= Length) {
  64. return Vertices[Vertices.Length - 1];
  65. }
  66. for (int v = 1; v<Distances.Length; v++) {
  67. float a = Distances[v-1];
  68. float b = Distances[v];
  69. if (a <= targetDist && b > targetDist) {
  70. float lv = (targetDist - a)/(b-a);
  71. return Vector3.LerpUnclamped(Vertices[v - 1], Vertices[v], lv);
  72. }
  73. }
  74. return Vertices[0];
  75. }
  76. }
  77. public class SinAnimator {
  78. public float Speed;
  79. public float MinValue;
  80. public float MaxValue;
  81. float timer;
  82. float targetTime;
  83. float valueFrom;
  84. float valueTo;
  85. public float Result;
  86. public SinAnimator(float minValue, float maxValue, float speed ) {
  87. Speed = speed;
  88. MinValue = minValue;
  89. MaxValue = maxValue;
  90. valueTo = Random.Range(MinValue, MaxValue);
  91. Reset();
  92. }
  93. public SinAnimator(float minValue, float maxValue, float speed, float value) {
  94. Speed = speed;
  95. MinValue = minValue;
  96. MaxValue = maxValue;
  97. valueTo = Random.Range(MinValue, MaxValue);
  98. Result = value;
  99. valueFrom = value;
  100. valueTo = Random.Range(MinValue, MaxValue);
  101. targetTime = Mathf.Abs(valueTo - valueFrom) / Speed;
  102. }
  103. void Reset() {
  104. float prevSign = Mathf.Sign(valueTo-valueFrom);
  105. valueFrom = valueTo;
  106. if (prevSign > 0) {
  107. valueTo = Random.Range(MinValue, Mathf.Lerp(Result, MinValue, 0.5f));
  108. } else {
  109. valueTo = Random.Range(MaxValue, Mathf.Lerp(Result, MaxValue, 0.5f));
  110. }
  111. targetTime = Mathf.Abs(valueTo - valueFrom) / Speed;
  112. }
  113. public void Update() {
  114. if (timer > targetTime) {
  115. timer = 0;
  116. Reset();
  117. }
  118. float t = timer / targetTime;
  119. t = Extension.LinearToSin(t);
  120. Result = Mathf.LerpUnclamped(valueFrom, valueTo, t);
  121. timer += Time.deltaTime;
  122. }
  123. }
  124. public struct BindInfo {
  125. public int VidxA;
  126. public int VidxB;
  127. public int VidxC;
  128. public Vector3 Bary;
  129. public PFU TrisSpace;
  130. public BindInfo(int vidxa, int vidxb, int vidxc, Vector3 bary, PFU tspace ) {
  131. VidxA = vidxa;
  132. VidxB = vidxb;
  133. VidxC = vidxc;
  134. Bary = bary;
  135. TrisSpace = tspace;
  136. }
  137. }
  138. [System.Serializable]
  139. public struct PFU {
  140. public Vector3 P;
  141. public Vector3 F;
  142. public Vector3 U;
  143. public PFU(Matrix4x4 tm) {
  144. P = tm.GetColumn(3);
  145. F = tm.GetColumn(2);
  146. U = tm.GetColumn(1);
  147. }
  148. public static PFU operator - ( PFU a, PFU b ) {
  149. a.P -= b.P;
  150. a.F -= b.F;
  151. a.U -= b.U;
  152. return a;
  153. }
  154. public static PFU operator + (PFU a, PFU b) {
  155. a.P += b.P;
  156. a.F += b.F;
  157. a.U += b.U;
  158. return a;
  159. }
  160. public static PFU operator * (PFU a, float t) {
  161. a.P *= t;
  162. a.F *= t;
  163. a.U *= t;
  164. return a;
  165. }
  166. public static PFU operator * (PFU a, Matrix4x4 matrix) {
  167. a.P = matrix.MultiplyPoint3x4( a.P ) ;
  168. a.F = matrix.MultiplyVector( a.F ) ;
  169. a.U = matrix.MultiplyVector( a.U );
  170. return a;
  171. }
  172. public Matrix4x4 Matrix {
  173. get {
  174. return Matrix4x4.TRS(P, Quaternion.LookRotation(F, U), Vector3.one);
  175. }
  176. set {
  177. P = value.GetColumn(3);
  178. F = value.GetColumn(2);
  179. U = value.GetColumn(1);
  180. }
  181. }
  182. }
  183. [System.Serializable]
  184. public struct NumbersRange {
  185. public int Min;
  186. public int Max;
  187. public NumbersRange(bool isWhole) {
  188. Max = isWhole?0:int.MinValue;
  189. Min = int.MaxValue;
  190. }
  191. public void Set(int number) {
  192. Min = Mathf.Min(Min, number);
  193. Max = Mathf.Max(Max, number);
  194. }
  195. }
  196. [System.Serializable]
  197. public class ConstraintClip {
  198. public PFU[] Frames;
  199. public ConstraintClip(int framesCount) {
  200. Frames = new PFU[framesCount];
  201. }
  202. public ConstraintClip GetCopy() {
  203. ConstraintClip r = new ConstraintClip(Frames.Length);
  204. for (int i = 0; i<r.Frames.Length; i++) {
  205. r.Frames[i] = Frames[i];
  206. }
  207. return r;
  208. }
  209. }
  210. public class TasksStack {
  211. public class Task {
  212. public string Name;
  213. public float Iterations;
  214. public float NormalizedFrom;
  215. public float NormalizedTo;
  216. public float Weight = 1;
  217. public Task(string name ) {
  218. Name = name;
  219. }
  220. public TaskInfo GetInfo( int iteration ) {
  221. float persentage = (float)iteration / (float)Iterations;
  222. float totalPersentage = Mathf.Lerp(NormalizedFrom, NormalizedTo, persentage);
  223. return new TaskInfo(string.Format("{0} {1}%", Name, (persentage * 100).ToString("F0")), totalPersentage);
  224. }
  225. public TaskInfo GetInfo(float localPersentage ) {
  226. float totalPersentage = Mathf.Lerp(NormalizedFrom, NormalizedTo, localPersentage);
  227. return new TaskInfo(string.Format("{0} {1}%", Name, (localPersentage * 100).ToString("F0")), totalPersentage);
  228. }
  229. }
  230. public string Name;
  231. List<Task> items = new List<Task>();
  232. Dictionary<string, Task> itemsDict = new Dictionary<string, Task>();
  233. public int CurrentIdx = 0;
  234. public Task this[string name] {
  235. get {
  236. return itemsDict[name];
  237. }
  238. }
  239. public Task this[int idx] {
  240. get {
  241. return items[idx];
  242. }
  243. }
  244. public TasksStack(string name) {
  245. Name = name;
  246. itemsDict = new Dictionary<string, Task>();
  247. }
  248. public void Add( string name, float iterations ) {
  249. Task existing = null;
  250. if (itemsDict.TryGetValue(name, out existing) == false) {
  251. Task t = new Task( name );
  252. t.Iterations = iterations;
  253. itemsDict.Add(name, t);
  254. items.Add(t);
  255. }
  256. }
  257. public void Add(string name, float iterations, float weight) {
  258. Task existing = null;
  259. if (itemsDict.TryGetValue(name, out existing) == false) {
  260. Task t = new Task(name);
  261. t.Iterations = iterations;
  262. t.Weight = weight;
  263. itemsDict.Add(name, t);
  264. items.Add(t);
  265. }
  266. }
  267. public void Normalize() {
  268. float summ = 0;
  269. for (int i = 0; i<items.Count; i++) {
  270. items[i].NormalizedFrom = summ;
  271. summ += items[i].Weight;
  272. items[i].NormalizedTo = summ;
  273. }
  274. for (int i = 0; i < items.Count; i++) {
  275. items[i].NormalizedFrom /= summ;
  276. items[i].NormalizedTo /= summ;
  277. }
  278. }
  279. public TaskInfo Done {
  280. get {
  281. return new TaskInfo(string.Format("{0} is done", Name), 1f);
  282. }
  283. }
  284. public void PrintDebugInfo() {
  285. for (int i = 0; i<items.Count; i++) {
  286. Debug.LogFormat("{0} name: [{1}] {2}% ", i, items[i].Name, ((items[i].NormalizedTo - items[i].NormalizedFrom)*100f).ToString("F2") );
  287. //Debug.LogFormat("{0} name: [{1}] from:{2} to:{3} weight:{4}", i, items[i].Name, items[i].NormalizedFrom, items[i].NormalizedTo, items[i].Weight);
  288. }
  289. }
  290. }
  291. public static class Extension {
  292. public static float LinearToSin(float t) {
  293. return 1f - (Mathf.Sin((t * 3.141592f) + 1.5708f) * 0.49999f + 0.5f);
  294. }
  295. public static float InverseLerpUnclamped( float a, float b, float value){
  296. float result;
  297. if (a != b) {
  298. result = (value - a) / (b - a) ;
  299. }
  300. else {
  301. result = 0;
  302. }
  303. return result;
  304. }
  305. public static float SmoothLoopCurve(float t, float minAmount, float maxAmount, float easeOffset, float easeLength){
  306. t = Mathf.Clamp01(t);
  307. if(t>=0.5f){
  308. if( Mathf.Approximately(easeOffset, easeLength )){
  309. return t<(1f-easeOffset)? minAmount : maxAmount;
  310. }
  311. t = 0.5f+ Mathf.InverseLerp(1f - easeLength, 1f - easeOffset, t)*0.5f;
  312. } else {
  313. if( Mathf.Approximately(easeOffset, easeLength )){
  314. return t>( easeOffset)? minAmount : maxAmount;
  315. }
  316. t = Mathf.InverseLerp(easeOffset, easeLength, t) * 0.5f;
  317. }
  318. float result = Mathf.Sin( (t*6.28318f)+1.5708f )*0.49999f+0.5f;
  319. return Mathf.LerpUnclamped( minAmount, maxAmount, result );
  320. }
  321. public static void SpawnMesh(Mesh m, string name){
  322. GameObject go = new GameObject( name );
  323. go.AddComponent<MeshFilter>().mesh = GameObject.Instantiate(m) as Mesh;
  324. go.AddComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Standard"));
  325. }
  326. public static Material GetRandomMaterial( string name ) {
  327. Material mat = new Material( (Material)Resources.Load("VertexAnimationToolsDefaultMaterial"));
  328. mat.SetColor("_Color", Color.Lerp(Color.red, Color.Lerp(Color.green, Color.blue, Random.value), Random.value));
  329. mat.name = name;
  330. return mat;
  331. }
  332. public static Vector3[] Copy(Vector3[] source) {
  333. Vector3[] result = new Vector3[source.Length];
  334. System.Array.Copy(source, result, source.Length);
  335. return result;
  336. }
  337. public static Vector3[] Delta(Vector3[] a, Vector3[] b) {
  338. Vector3[] result = new Vector3[a.Length];
  339. for (int i = 0; i < a.Length; i++) {
  340. result[i] = b[i] - a[i];
  341. }
  342. return result;
  343. }
  344. public static void CopyDataTo(this Mesh m, Mesh other) {
  345. other.Clear();
  346. other.ClearBlendShapes();
  347. other.vertices = m.vertices;
  348. other.normals = m.normals;
  349. other.tangents = m.tangents;
  350. other.uv = m.uv;
  351. other.triangles = m.triangles;
  352. for (int i = 0; i<m.blendShapeCount; i++) {
  353. Vector3[] bsPos = new Vector3[m.vertexCount];
  354. Vector3[] bsNorms = new Vector3[m.vertexCount];
  355. Vector3[] bsTangents = new Vector3[m.vertexCount];
  356. m.GetBlendShapeFrameVertices(i, 0, bsPos, bsNorms, bsTangents);
  357. string shapeName = m.GetBlendShapeName(i);
  358. other.AddBlendShapeFrame(shapeName, 100, bsPos, bsNorms, bsTangents);
  359. }
  360. }
  361. }
  362. }