RopeData.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Xft
  5. {
  6. public class RopeData
  7. {
  8. public void Init(EffectLayer owner)
  9. {
  10. this.Owner = owner;
  11. this.Vertexsegment = owner.GetVertexPool().GetRopeVertexSeg(owner.MaxENodes);
  12. this.dummyNode = new EffectNode(0, owner.ClientTransform, false, owner);
  13. List<Affector> affectorList = owner.InitAffectors(this.dummyNode);
  14. this.dummyNode.SetAffectorList(affectorList);
  15. this.dummyNode.SetRenderType(4);
  16. this.dummyNode.Init(Vector3.zero, 0f, -1f, 0, 1f, 1f, Color.clear, Vector2.zero, Vector2.one);
  17. }
  18. protected void RefreshData()
  19. {
  20. this.NodeList.Clear();
  21. for (int i = 0; i < this.Owner.MaxENodes; i++)
  22. {
  23. EffectNode effectNode = this.Owner.ActiveENodes[i];
  24. if (effectNode != null)
  25. {
  26. this.NodeList.Add(effectNode);
  27. }
  28. }
  29. this.NodeList.Sort();
  30. }
  31. public void Update(float deltaTime)
  32. {
  33. this.RefreshData();
  34. if (this.NodeList.Count < 2)
  35. {
  36. return;
  37. }
  38. this.dummyNode.Update(deltaTime);
  39. this.ClearDeadVerts();
  40. this.UpdateVertices();
  41. this.UpdateIndices();
  42. }
  43. protected void ClearNodeVert(EffectNode node)
  44. {
  45. int num = this.Vertexsegment.VertStart + node.Index * 2;
  46. VertexPool pool = this.Vertexsegment.Pool;
  47. pool.Vertices[num] = this.Owner.ClientTransform.position;
  48. pool.Colors[num] = Color.clear;
  49. pool.Vertices[num + 1] = this.Owner.ClientTransform.position;
  50. pool.Colors[num + 1] = Color.clear;
  51. pool.VertChanged = true;
  52. pool.ColorChanged = true;
  53. }
  54. public void ClearDeadVerts()
  55. {
  56. for (int i = 0; i < this.Owner.MaxENodes; i++)
  57. {
  58. EffectNode effectNode = this.Owner.AvailableENodes[i];
  59. if (effectNode != null)
  60. {
  61. this.ClearNodeVert(effectNode);
  62. }
  63. }
  64. this.Vertexsegment.ClearIndices();
  65. }
  66. public void UpdateIndices()
  67. {
  68. int num = 0;
  69. VertexPool pool = this.Vertexsegment.Pool;
  70. for (int i = this.NodeList.Count - 1; i >= 0; i--)
  71. {
  72. EffectNode effectNode = this.NodeList[i];
  73. EffectNode effectNode2 = (i - 1 < 0) ? null : this.NodeList[i - 1];
  74. if (effectNode2 == null)
  75. {
  76. break;
  77. }
  78. int num2 = this.Vertexsegment.VertStart + effectNode.Index * 2;
  79. int num3 = this.Vertexsegment.VertStart + effectNode2.Index * 2;
  80. int num4 = this.Vertexsegment.IndexStart + num * 6;
  81. pool.Indices[num4] = num2;
  82. pool.Indices[num4 + 1] = num2 + 1;
  83. pool.Indices[num4 + 2] = num3;
  84. pool.Indices[num4 + 3] = num2 + 1;
  85. pool.Indices[num4 + 4] = num3 + 1;
  86. pool.Indices[num4 + 5] = num3;
  87. num++;
  88. }
  89. pool.IndiceChanged = true;
  90. }
  91. public void UpdateVertices()
  92. {
  93. float num = 0f;
  94. Vector2 lowerLeftUV = this.dummyNode.LowerLeftUV;
  95. Vector2 uvdimensions = this.dummyNode.UVDimensions;
  96. uvdimensions.y = -uvdimensions.y;
  97. lowerLeftUV.y = 1f - lowerLeftUV.y;
  98. float num2 = this.Owner.RopeUVLen;
  99. if (this.Owner.RopeFixUVLen)
  100. {
  101. float num3 = 0f;
  102. for (int i = 0; i < this.NodeList.Count - 1; i++)
  103. {
  104. num3 += (this.NodeList[i + 1].GetWorldPos() - this.NodeList[i].GetWorldPos()).magnitude;
  105. }
  106. num2 = num3;
  107. }
  108. for (int j = this.NodeList.Count - 1; j >= 0; j--)
  109. {
  110. EffectNode effectNode = this.NodeList[j];
  111. EffectNode effectNode2 = (j + 1 >= this.NodeList.Count) ? null : this.NodeList[j + 1];
  112. EffectNode effectNode3 = (j - 1 < 0) ? null : this.NodeList[j - 1];
  113. Vector3 lhs;
  114. if (effectNode3 == null)
  115. {
  116. lhs = effectNode.GetWorldPos() - effectNode2.GetWorldPos();
  117. }
  118. else if (effectNode2 == null)
  119. {
  120. lhs = effectNode3.GetWorldPos() - effectNode.GetWorldPos();
  121. }
  122. else
  123. {
  124. lhs = effectNode3.GetWorldPos() - effectNode2.GetWorldPos();
  125. }
  126. Vector3 position = this.Owner.MyCamera.transform.position;
  127. Vector3 rhs = position - effectNode.GetWorldPos();
  128. Vector3 vector = Vector3.Cross(lhs, rhs);
  129. vector.Normalize();
  130. vector *= this.Owner.RopeWidth * 0.5f * effectNode.Scale.x;
  131. Vector3 vector2 = effectNode.GetWorldPos() - vector;
  132. Vector3 vector3 = effectNode.GetWorldPos() + vector;
  133. VertexPool pool = this.Vertexsegment.Pool;
  134. float num4 = num / num2 * Mathf.Abs(uvdimensions.y);
  135. Vector2 zero = Vector2.zero;
  136. int num5 = this.Vertexsegment.VertStart + effectNode.Index * 2;
  137. pool.Vertices[num5] = vector2;
  138. pool.Colors[num5] = effectNode.Color;
  139. zero.x = lowerLeftUV.x + uvdimensions.x;
  140. zero.y = lowerLeftUV.y - num4;
  141. pool.UVs[num5] = zero;
  142. pool.Vertices[num5 + 1] = vector3;
  143. pool.Colors[num5 + 1] = effectNode.Color;
  144. zero.x = lowerLeftUV.x;
  145. zero.y = lowerLeftUV.y - num4;
  146. pool.UVs[num5 + 1] = zero;
  147. if (effectNode3 != null)
  148. {
  149. num += (effectNode3.GetWorldPos() - effectNode.GetWorldPos()).magnitude;
  150. }
  151. else
  152. {
  153. num += (effectNode.GetWorldPos() - effectNode2.GetWorldPos()).magnitude;
  154. }
  155. }
  156. this.Vertexsegment.Pool.UVChanged = true;
  157. this.Vertexsegment.Pool.VertChanged = true;
  158. this.Vertexsegment.Pool.ColorChanged = true;
  159. }
  160. public List<EffectNode> NodeList = new List<EffectNode>();
  161. public VertexPool.VertexSegment Vertexsegment;
  162. public EffectLayer Owner;
  163. public EffectNode dummyNode;
  164. }
  165. }