CustomMesh.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class CustomMesh : RenderObject
  6. {
  7. public CustomMesh(VertexPool.VertexSegment segment, Mesh mesh, Vector3 dir, float maxFps)
  8. {
  9. this.MyMesh = mesh;
  10. this.MeshVerts = new Vector3[mesh.vertices.Length];
  11. mesh.vertices.CopyTo(this.MeshVerts, 0);
  12. this.Vertexsegment = segment;
  13. this.MyDirection = dir;
  14. this.SetPosition(Vector3.zero);
  15. this.InitVerts();
  16. }
  17. public override void ApplyShaderParam(float x, float y)
  18. {
  19. Vector2 one = Vector2.one;
  20. one.x = x;
  21. one.y = y;
  22. VertexPool pool = this.Vertexsegment.Pool;
  23. int vertStart = this.Vertexsegment.VertStart;
  24. for (int i = 0; i < this.Vertexsegment.VertCount; i++)
  25. {
  26. pool.UVs2[vertStart + i] = one;
  27. }
  28. this.Vertexsegment.Pool.UV2Changed = true;
  29. }
  30. public override void Initialize(EffectNode node)
  31. {
  32. base.Initialize(node);
  33. this.SetColor(this.Node.Color);
  34. this.SetRotation((float)this.Node.OriRotateAngle);
  35. this.SetScale(this.Node.OriScaleX, this.Node.OriScaleY);
  36. this.SetUVCoord(this.Node.LowerLeftUV, this.Node.UVDimensions);
  37. this.SetDirection(this.Node.OriDirection);
  38. }
  39. public override void Reset()
  40. {
  41. this.SetColor(Color.clear);
  42. this.SetRotation((float)this.Node.OriRotateAngle);
  43. this.Update(true, 0f);
  44. }
  45. public override void Update(float deltaTime)
  46. {
  47. this.SetScale(this.Node.Scale.x * this.Node.OriScaleX, this.Node.Scale.y * this.Node.OriScaleY);
  48. this.SetColor(this.Node.Color);
  49. if (this.Node.Owner.UVAffectorEnable || this.Node.Owner.UVRotAffectorEnable || this.Node.Owner.UVScaleAffectorEnable)
  50. {
  51. this.SetUVCoord(this.Node.LowerLeftUV, this.Node.UVDimensions);
  52. }
  53. this.SetRotation((float)this.Node.OriRotateAngle + this.Node.RotateAngle);
  54. this.SetPosition(this.Node.CurWorldPos);
  55. this.Update(false, deltaTime);
  56. }
  57. public void SetDirection(Vector3 dir)
  58. {
  59. this.MyDirection = dir;
  60. }
  61. public void SetUVCoord(Vector2 lowerleft, Vector2 dimensions)
  62. {
  63. this.LowerLeftUV = lowerleft;
  64. this.UVDimensions = dimensions;
  65. XftTools.TopLeftUVToLowerLeft(ref this.LowerLeftUV, ref this.UVDimensions);
  66. this.UVChanged = true;
  67. }
  68. public void SetColor(Color c)
  69. {
  70. this.MyColor = c;
  71. this.ColorChanged = true;
  72. }
  73. public void SetPosition(Vector3 pos)
  74. {
  75. this.MyPosition = pos;
  76. }
  77. public void SetScale(float width, float height)
  78. {
  79. this.MyScale.x = width;
  80. this.MyScale.y = height;
  81. }
  82. public void SetRotation(float angle)
  83. {
  84. this.MyRotation = Quaternion.AngleAxis(angle, this.Node.Owner.MeshRotateAxis);
  85. }
  86. public void InitVerts()
  87. {
  88. VertexPool pool = this.Vertexsegment.Pool;
  89. int indexStart = this.Vertexsegment.IndexStart;
  90. int vertStart = this.Vertexsegment.VertStart;
  91. for (int i = 0; i < this.MeshVerts.Length; i++)
  92. {
  93. pool.Vertices[vertStart + i] = Vector3.zero;
  94. }
  95. int[] triangles = this.MyMesh.triangles;
  96. for (int j = 0; j < this.Vertexsegment.IndexCount; j++)
  97. {
  98. pool.Indices[j + indexStart] = triangles[j] + vertStart;
  99. }
  100. this.m_oriUvs = this.MyMesh.uv;
  101. for (int k = 0; k < this.m_oriUvs.Length; k++)
  102. {
  103. pool.UVs[k + vertStart] = this.m_oriUvs[k];
  104. }
  105. Color[] colors = this.MyMesh.colors;
  106. for (int l = 0; l < colors.Length; l++)
  107. {
  108. pool.Colors[l + vertStart] = Color.clear;
  109. }
  110. }
  111. public void UpdateUV()
  112. {
  113. VertexPool pool = this.Vertexsegment.Pool;
  114. int vertStart = this.Vertexsegment.VertStart;
  115. for (int i = 0; i < this.m_oriUvs.Length; i++)
  116. {
  117. Vector2 vector = this.LowerLeftUV + Vector2.Scale(this.m_oriUvs[i], this.UVDimensions);
  118. if (vector.x > this.UVDimensions.x + this.LowerLeftUV.x)
  119. {
  120. float num = vector.x - this.UVDimensions.x - this.LowerLeftUV.x;
  121. num = Mathf.Repeat(num, this.UVDimensions.x + this.LowerLeftUV.x);
  122. vector.x = this.LowerLeftUV.x + num * this.UVDimensions.x;
  123. }
  124. pool.UVs[i + vertStart] = vector;
  125. }
  126. this.Vertexsegment.Pool.UVChanged = true;
  127. }
  128. public void UpdateColor()
  129. {
  130. VertexPool pool = this.Vertexsegment.Pool;
  131. int vertStart = this.Vertexsegment.VertStart;
  132. for (int i = 0; i < this.Vertexsegment.VertCount; i++)
  133. {
  134. pool.Colors[vertStart + i] = this.MyColor;
  135. }
  136. this.Vertexsegment.Pool.ColorChanged = true;
  137. }
  138. public void Transform()
  139. {
  140. Quaternion lhs;
  141. if (this.Node.Owner.AlwaysSyncRotation)
  142. {
  143. lhs = Quaternion.FromToRotation(Vector3.up, this.Node.Owner.transform.rotation * this.MyDirection);
  144. }
  145. else
  146. {
  147. lhs = Quaternion.FromToRotation(Vector3.up, this.MyDirection);
  148. }
  149. Vector3 one = Vector3.one;
  150. one.x = (one.z = this.MyScale.x);
  151. one.y = this.MyScale.y;
  152. this.LocalMat.SetTRS(Vector3.zero, lhs * this.MyRotation, one);
  153. this.WorldMat.SetTRS(this.MyPosition, Quaternion.identity, Vector3.one);
  154. Matrix4x4 matrix4x = this.WorldMat * this.LocalMat;
  155. VertexPool pool = this.Vertexsegment.Pool;
  156. for (int i = this.Vertexsegment.VertStart; i < this.Vertexsegment.VertStart + this.Vertexsegment.VertCount; i++)
  157. {
  158. pool.Vertices[i] = matrix4x.MultiplyPoint3x4(this.MeshVerts[i - this.Vertexsegment.VertStart]);
  159. }
  160. }
  161. public void Update(bool force, float deltaTime)
  162. {
  163. this.ElapsedTime += deltaTime;
  164. if (this.ElapsedTime > base.Fps || force)
  165. {
  166. this.Transform();
  167. if (this.ColorChanged)
  168. {
  169. this.UpdateColor();
  170. }
  171. if (this.UVChanged)
  172. {
  173. this.UpdateUV();
  174. }
  175. this.ColorChanged = (this.UVChanged = false);
  176. if (!force)
  177. {
  178. this.ElapsedTime -= base.Fps;
  179. }
  180. }
  181. }
  182. protected VertexPool.VertexSegment Vertexsegment;
  183. public Mesh MyMesh;
  184. public Vector3[] MeshVerts;
  185. public Color MyColor;
  186. public Vector3 MyPosition = Vector3.zero;
  187. public Vector2 MyScale = Vector2.one;
  188. public Quaternion MyRotation = Quaternion.identity;
  189. public Vector3 MyDirection;
  190. private Matrix4x4 LocalMat;
  191. private Matrix4x4 WorldMat;
  192. private float ElapsedTime;
  193. public bool ColorChanged;
  194. public bool UVChanged;
  195. protected Vector2 LowerLeftUV;
  196. protected Vector2 UVDimensions;
  197. protected Vector2[] m_oriUvs;
  198. }
  199. }