XftSprite.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class XftSprite : RenderObject
  6. {
  7. public XftSprite(VertexPool.VertexSegment segment, float width, float height, STYPE type, ORIPOINT oripoint, float maxFps, bool simple)
  8. {
  9. this.UVChanged = (this.ColorChanged = false);
  10. this.MyTransform.position = Vector3.zero;
  11. this.MyTransform.rotation = Quaternion.identity;
  12. this.LocalMat = (this.WorldMat = Matrix4x4.identity);
  13. this.Vertexsegment = segment;
  14. this.LastMat = Matrix4x4.identity;
  15. this.ElapsedTime = 0f;
  16. this.Simple = simple;
  17. this.OriPoint = oripoint;
  18. this.RotateAxis = Vector3.zero;
  19. this.SetSizeXZ(width, height);
  20. this.RotateAxis.y = 1f;
  21. this.Type = type;
  22. this.ResetSegment();
  23. }
  24. public override void ApplyShaderParam(float x, float y)
  25. {
  26. VertexPool pool = this.Vertexsegment.Pool;
  27. int vertStart = this.Vertexsegment.VertStart;
  28. Vector2 one = Vector2.one;
  29. one.x = x;
  30. one.y = y;
  31. pool.UVs2[vertStart] = one;
  32. pool.UVs2[vertStart + 1] = one;
  33. pool.UVs2[vertStart + 2] = one;
  34. pool.UVs2[vertStart + 3] = one;
  35. this.Vertexsegment.Pool.UV2Changed = true;
  36. }
  37. public override void Initialize(EffectNode node)
  38. {
  39. base.Initialize(node);
  40. this.SetUVCoord(node.LowerLeftUV, node.UVDimensions);
  41. this.SetColor(this.Node.Color);
  42. if (this.Simple)
  43. {
  44. node.Update(0f);
  45. this.Transform();
  46. }
  47. if (node.Owner.DirType != DIRECTION_TYPE.Sphere)
  48. {
  49. this.SetRotationTo(node.Owner.ClientTransform.rotation * node.OriDirection);
  50. }
  51. else
  52. {
  53. this.SetRotationTo(node.OriDirection);
  54. }
  55. }
  56. public override void Reset()
  57. {
  58. this.SetRotation((float)this.Node.OriRotateAngle);
  59. this.SetPosition(this.Node.Position);
  60. this.SetColor(Color.clear);
  61. this.Update(true, 0f);
  62. }
  63. public override void Update(float deltaTime)
  64. {
  65. if (this.Node.Owner.AlwaysSyncRotation && this.Node.Owner.DirType != DIRECTION_TYPE.Sphere)
  66. {
  67. this.SetRotationTo(this.Node.Owner.ClientTransform.rotation * this.Node.OriDirection);
  68. }
  69. this.SetScale(this.Node.Scale.x * this.Node.OriScaleX, this.Node.Scale.y * this.Node.OriScaleY);
  70. this.SetColor(this.Node.Color);
  71. if (this.Node.Owner.UVAffectorEnable || this.Node.Owner.UVRotAffectorEnable || this.Node.Owner.UVScaleAffectorEnable)
  72. {
  73. this.SetUVCoord(this.Node.LowerLeftUV, this.Node.UVDimensions);
  74. }
  75. this.SetRotation((float)this.Node.OriRotateAngle + this.Node.RotateAngle);
  76. this.SetPosition(this.Node.CurWorldPos);
  77. this.Update(false, deltaTime);
  78. }
  79. public void ResetSegment()
  80. {
  81. VertexPool pool = this.Vertexsegment.Pool;
  82. int indexStart = this.Vertexsegment.IndexStart;
  83. int vertStart = this.Vertexsegment.VertStart;
  84. pool.Indices[indexStart] = vertStart;
  85. pool.Indices[indexStart + 1] = vertStart + 3;
  86. pool.Indices[indexStart + 2] = vertStart + 1;
  87. pool.Indices[indexStart + 3] = vertStart + 3;
  88. pool.Indices[indexStart + 4] = vertStart + 2;
  89. pool.Indices[indexStart + 5] = vertStart + 1;
  90. pool.Vertices[vertStart] = Vector3.zero;
  91. pool.Vertices[vertStart + 1] = Vector3.zero;
  92. pool.Vertices[vertStart + 2] = Vector3.zero;
  93. pool.Vertices[vertStart + 3] = Vector3.zero;
  94. pool.Colors[vertStart] = Color.white;
  95. pool.Colors[vertStart + 1] = Color.white;
  96. pool.Colors[vertStart + 2] = Color.white;
  97. pool.Colors[vertStart + 3] = Color.white;
  98. pool.UVs[vertStart] = Vector2.zero;
  99. pool.UVs[vertStart + 1] = Vector2.zero;
  100. pool.UVs[vertStart + 2] = Vector2.zero;
  101. pool.UVs[vertStart + 3] = Vector2.zero;
  102. pool.UVChanged = (pool.IndiceChanged = (pool.ColorChanged = (pool.VertChanged = true)));
  103. }
  104. public void SetUVCoord(Vector2 lowerleft, Vector2 dimensions)
  105. {
  106. this.LowerLeftUV = lowerleft;
  107. this.UVDimensions = dimensions;
  108. XftTools.TopLeftUVToLowerLeft(ref this.LowerLeftUV, ref this.UVDimensions);
  109. this.UVChanged = true;
  110. }
  111. public void SetPosition(Vector3 pos)
  112. {
  113. this.MyTransform.position = pos;
  114. }
  115. public void SetRotation(Quaternion q)
  116. {
  117. this.MyTransform.rotation = q;
  118. }
  119. public void SetRotationFaceTo(Vector3 dir)
  120. {
  121. this.MyTransform.rotation = Quaternion.FromToRotation(Vector3.up, dir);
  122. }
  123. public void SetRotationTo(Vector3 dir)
  124. {
  125. if (dir == Vector3.zero)
  126. {
  127. return;
  128. }
  129. Quaternion rotation = Quaternion.identity;
  130. Vector3 vector = dir;
  131. vector.y = 0f;
  132. if (vector == Vector3.zero)
  133. {
  134. vector = Vector3.up;
  135. }
  136. if (this.OriPoint == ORIPOINT.CENTER)
  137. {
  138. Quaternion rhs = Quaternion.FromToRotation(new Vector3(0f, 0f, 1f), vector);
  139. Quaternion lhs = Quaternion.FromToRotation(vector, dir);
  140. rotation = lhs * rhs;
  141. }
  142. else if (this.OriPoint == ORIPOINT.LEFT_UP)
  143. {
  144. Quaternion rhs2 = Quaternion.FromToRotation(this.LocalMat.MultiplyPoint3x4(this.v3), vector);
  145. Quaternion lhs2 = Quaternion.FromToRotation(vector, dir);
  146. rotation = lhs2 * rhs2;
  147. }
  148. else if (this.OriPoint == ORIPOINT.LEFT_BOTTOM)
  149. {
  150. Quaternion rhs3 = Quaternion.FromToRotation(this.LocalMat.MultiplyPoint3x4(this.v4), vector);
  151. Quaternion lhs3 = Quaternion.FromToRotation(vector, dir);
  152. rotation = lhs3 * rhs3;
  153. }
  154. else if (this.OriPoint == ORIPOINT.RIGHT_BOTTOM)
  155. {
  156. Quaternion rhs4 = Quaternion.FromToRotation(this.LocalMat.MultiplyPoint3x4(this.v1), vector);
  157. Quaternion lhs4 = Quaternion.FromToRotation(vector, dir);
  158. rotation = lhs4 * rhs4;
  159. }
  160. else if (this.OriPoint == ORIPOINT.RIGHT_UP)
  161. {
  162. Quaternion rhs5 = Quaternion.FromToRotation(this.LocalMat.MultiplyPoint3x4(this.v2), vector);
  163. Quaternion lhs5 = Quaternion.FromToRotation(vector, dir);
  164. rotation = lhs5 * rhs5;
  165. }
  166. else if (this.OriPoint == ORIPOINT.BOTTOM_CENTER)
  167. {
  168. Quaternion rhs6 = Quaternion.FromToRotation(new Vector3(0f, 0f, 1f), vector);
  169. Quaternion lhs6 = Quaternion.FromToRotation(vector, dir);
  170. rotation = lhs6 * rhs6;
  171. }
  172. else if (this.OriPoint == ORIPOINT.TOP_CENTER)
  173. {
  174. Quaternion rhs7 = Quaternion.FromToRotation(new Vector3(0f, 0f, -1f), vector);
  175. Quaternion lhs7 = Quaternion.FromToRotation(vector, dir);
  176. rotation = lhs7 * rhs7;
  177. }
  178. else if (this.OriPoint == ORIPOINT.RIGHT_CENTER)
  179. {
  180. Quaternion rhs8 = Quaternion.FromToRotation(new Vector3(-1f, 0f, 0f), vector);
  181. Quaternion lhs8 = Quaternion.FromToRotation(vector, dir);
  182. rotation = lhs8 * rhs8;
  183. }
  184. else if (this.OriPoint == ORIPOINT.LEFT_CENTER)
  185. {
  186. Quaternion rhs9 = Quaternion.FromToRotation(new Vector3(1f, 0f, 0f), vector);
  187. Quaternion lhs9 = Quaternion.FromToRotation(vector, dir);
  188. rotation = lhs9 * rhs9;
  189. }
  190. this.MyTransform.rotation = rotation;
  191. }
  192. public void SetSizeXZ(float width, float height)
  193. {
  194. this.v1 = new Vector3(-width / 2f, 0f, height / 2f);
  195. this.v2 = new Vector3(-width / 2f, 0f, -height / 2f);
  196. this.v3 = new Vector3(width / 2f, 0f, -height / 2f);
  197. this.v4 = new Vector3(width / 2f, 0f, height / 2f);
  198. Vector3 zero = Vector3.zero;
  199. if (this.OriPoint == ORIPOINT.LEFT_UP)
  200. {
  201. zero = this.v3;
  202. }
  203. else if (this.OriPoint == ORIPOINT.LEFT_BOTTOM)
  204. {
  205. zero = this.v4;
  206. }
  207. else if (this.OriPoint == ORIPOINT.RIGHT_BOTTOM)
  208. {
  209. zero = this.v1;
  210. }
  211. else if (this.OriPoint == ORIPOINT.RIGHT_UP)
  212. {
  213. zero = this.v2;
  214. }
  215. else if (this.OriPoint == ORIPOINT.BOTTOM_CENTER)
  216. {
  217. zero = new Vector3(0f, 0f, height / 2f);
  218. }
  219. else if (this.OriPoint == ORIPOINT.TOP_CENTER)
  220. {
  221. zero = new Vector3(0f, 0f, -height / 2f);
  222. }
  223. else if (this.OriPoint == ORIPOINT.LEFT_CENTER)
  224. {
  225. zero = new Vector3(width / 2f, 0f, 0f);
  226. }
  227. else if (this.OriPoint == ORIPOINT.RIGHT_CENTER)
  228. {
  229. zero = new Vector3(-width / 2f, 0f, 0f);
  230. }
  231. this.v1 += zero;
  232. this.v2 += zero;
  233. this.v3 += zero;
  234. this.v4 += zero;
  235. }
  236. public void UpdateUV()
  237. {
  238. VertexPool pool = this.Vertexsegment.Pool;
  239. int vertStart = this.Vertexsegment.VertStart;
  240. if (this.UVDimensions.y > 0f)
  241. {
  242. pool.UVs[vertStart] = this.LowerLeftUV + Vector2.up * this.UVDimensions.y;
  243. pool.UVs[vertStart + 1] = this.LowerLeftUV;
  244. pool.UVs[vertStart + 2] = this.LowerLeftUV + Vector2.right * this.UVDimensions.x;
  245. pool.UVs[vertStart + 3] = this.LowerLeftUV + this.UVDimensions;
  246. }
  247. else
  248. {
  249. pool.UVs[vertStart] = this.LowerLeftUV;
  250. pool.UVs[vertStart + 1] = this.LowerLeftUV + Vector2.up * this.UVDimensions.y;
  251. pool.UVs[vertStart + 2] = this.LowerLeftUV + this.UVDimensions;
  252. pool.UVs[vertStart + 3] = this.LowerLeftUV + Vector2.right * this.UVDimensions.x;
  253. }
  254. this.Vertexsegment.Pool.UVChanged = true;
  255. }
  256. public void UpdateColor()
  257. {
  258. VertexPool pool = this.Vertexsegment.Pool;
  259. int vertStart = this.Vertexsegment.VertStart;
  260. pool.Colors[vertStart] = this.Color;
  261. pool.Colors[vertStart + 1] = this.Color;
  262. pool.Colors[vertStart + 2] = this.Color;
  263. pool.Colors[vertStart + 3] = this.Color;
  264. this.Vertexsegment.Pool.ColorChanged = true;
  265. }
  266. public void SetCustomHeight(float[] h)
  267. {
  268. this.UseCustomHeight = true;
  269. this.h1 = h[0];
  270. this.h2 = h[1];
  271. this.h3 = h[2];
  272. this.h4 = h[3];
  273. this.Transform();
  274. }
  275. public void Transform()
  276. {
  277. this.LocalMat.SetTRS(Vector3.zero, this.Rotation, this.ScaleVector);
  278. Transform transform = this.Node.MyCamera.transform;
  279. if (this.Type == STYPE.BILLBOARD)
  280. {
  281. this.MyTransform.LookAt(transform.rotation * Vector3.up, transform.rotation * Vector3.back);
  282. }
  283. else if (this.Type == STYPE.BILLBOARD_Y)
  284. {
  285. Vector3 up = transform.position - this.MyTransform.position;
  286. up.y = 0f;
  287. this.MyTransform.LookAt(Vector3.up, up);
  288. }
  289. this.WorldMat.SetTRS(this.MyTransform.position, this.MyTransform.rotation, Vector3.one);
  290. Matrix4x4 matrix4x = this.WorldMat * this.LocalMat;
  291. VertexPool pool = this.Vertexsegment.Pool;
  292. int vertStart = this.Vertexsegment.VertStart;
  293. Vector3 vector = matrix4x.MultiplyPoint3x4(this.v1);
  294. Vector3 vector2 = matrix4x.MultiplyPoint3x4(this.v2);
  295. Vector3 vector3 = matrix4x.MultiplyPoint3x4(this.v3);
  296. Vector3 vector4 = matrix4x.MultiplyPoint3x4(this.v4);
  297. if (this.Type == STYPE.BILLBOARD_SELF)
  298. {
  299. Vector3 a = Vector3.zero;
  300. Vector3 vector5 = Vector3.zero;
  301. Vector3 b = Vector3.one * this.Node.Owner.Owner.Scale;
  302. float magnitude;
  303. if (this.Node.Owner.SpriteUVStretch == UV_STRETCH.Vertical)
  304. {
  305. a = (vector + vector4) / 2f;
  306. vector5 = (vector2 + vector3) / 2f;
  307. magnitude = (vector4 - vector).magnitude;
  308. }
  309. else
  310. {
  311. a = (vector + vector2) / 2f;
  312. vector5 = (vector4 + vector3) / 2f;
  313. magnitude = (vector2 - vector).magnitude;
  314. }
  315. Vector3 lhs = a - vector5;
  316. Vector3 rhs = this.Node.MyCamera.transform.position - Vector3.Scale(a, b);
  317. Vector3 vector6 = Vector3.Cross(lhs, rhs);
  318. vector6.Normalize();
  319. vector6 *= magnitude * 0.5f;
  320. Vector3 rhs2 = this.Node.MyCamera.transform.position - Vector3.Scale(vector5, b);
  321. Vector3 vector7 = Vector3.Cross(lhs, rhs2);
  322. vector7.Normalize();
  323. vector7 *= magnitude * 0.5f;
  324. if (this.Node.Owner.SpriteUVStretch == UV_STRETCH.Vertical)
  325. {
  326. vector = a - vector6;
  327. vector4 = a + vector6;
  328. vector2 = vector5 - vector7;
  329. vector3 = vector5 + vector7;
  330. }
  331. else
  332. {
  333. vector = a - vector6;
  334. vector2 = a + vector6;
  335. vector4 = vector5 - vector7;
  336. vector3 = vector5 + vector7;
  337. }
  338. }
  339. pool.Vertices[vertStart] = vector;
  340. pool.Vertices[vertStart + 1] = vector2;
  341. pool.Vertices[vertStart + 2] = vector3;
  342. pool.Vertices[vertStart + 3] = vector4;
  343. if (this.UseCustomHeight)
  344. {
  345. pool.Vertices[vertStart].y = this.h1;
  346. pool.Vertices[vertStart + 1].y = this.h2;
  347. pool.Vertices[vertStart + 2].y = this.h3;
  348. pool.Vertices[vertStart + 3].y = this.h4;
  349. }
  350. }
  351. public void SetRotation(float angle)
  352. {
  353. this.Rotation = Quaternion.AngleAxis(angle, this.RotateAxis);
  354. }
  355. public void SetScale(float width, float height)
  356. {
  357. this.ScaleVector.x = width;
  358. this.ScaleVector.z = height;
  359. }
  360. public void Update(bool force, float deltaTime)
  361. {
  362. this.ElapsedTime += deltaTime;
  363. if (this.ElapsedTime > base.Fps || force)
  364. {
  365. if (!this.Simple || force)
  366. {
  367. this.Transform();
  368. }
  369. if (this.UVChanged)
  370. {
  371. this.UpdateUV();
  372. }
  373. if (this.ColorChanged)
  374. {
  375. this.UpdateColor();
  376. }
  377. this.UVChanged = (this.ColorChanged = false);
  378. if (!force)
  379. {
  380. this.ElapsedTime -= base.Fps;
  381. }
  382. }
  383. }
  384. public void SetColor(Color c)
  385. {
  386. this.Color = c;
  387. this.ColorChanged = true;
  388. }
  389. protected Vector2 LowerLeftUV;
  390. protected Vector2 UVDimensions;
  391. public XTransform MyTransform;
  392. public Vector3 v1 = Vector3.zero;
  393. public Vector3 v2 = Vector3.zero;
  394. public Vector3 v3 = Vector3.zero;
  395. public Vector3 v4 = Vector3.zero;
  396. protected VertexPool.VertexSegment Vertexsegment;
  397. public Color Color;
  398. private Vector3 ScaleVector;
  399. private Quaternion Rotation;
  400. private Matrix4x4 LocalMat;
  401. private Matrix4x4 WorldMat;
  402. protected float ElapsedTime;
  403. protected bool UVChanged;
  404. protected bool ColorChanged;
  405. protected Matrix4x4 LastMat;
  406. protected Vector3 RotateAxis;
  407. private STYPE Type;
  408. private ORIPOINT OriPoint;
  409. private bool Simple;
  410. public bool UseCustomHeight;
  411. public float h1;
  412. public float h2;
  413. public float h3;
  414. public float h4;
  415. }
  416. }