Emitter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class Emitter
  6. {
  7. public Emitter(EffectLayer owner)
  8. {
  9. this.Layer = owner;
  10. this.EmitLoop = (float)this.Layer.EmitLoop;
  11. this.EmitDist = this.Layer.DiffDistance;
  12. this.LastClientPos = this.Layer.ClientTransform.position;
  13. }
  14. public void Reset()
  15. {
  16. this.EmitterElapsedTime = 0f;
  17. this.IsFirstEmit = true;
  18. this.EmitLoop = (float)this.Layer.EmitLoop;
  19. this.EmitDist = this.Layer.DiffDistance;
  20. this.m_emitCount = 0;
  21. this.CurveEmitDone = false;
  22. this.m_curveCountTime = 0f;
  23. }
  24. public void StopEmit()
  25. {
  26. this.EmitLoop = 0f;
  27. this.EmitterElapsedTime = 999999f;
  28. this.EmitDist = 9999999f;
  29. this.mElapsedTime = 0f;
  30. }
  31. protected int EmitByCurve(float deltaTime)
  32. {
  33. XCurveParam emitterCurveX = this.Layer.EmitterCurveX;
  34. if (emitterCurveX == null)
  35. {
  36. UnityEngine.Debug.LogWarning("emitter hasn't set a curve yet!");
  37. return 0;
  38. }
  39. this.EmitterElapsedTime += deltaTime;
  40. int num = (int)emitterCurveX.Evaluate(this.EmitterElapsedTime) - this.m_emitCount;
  41. int num2;
  42. if (num > this.Layer.AvailableNodeCount)
  43. {
  44. num2 = this.Layer.AvailableNodeCount;
  45. }
  46. else
  47. {
  48. num2 = num;
  49. }
  50. if (num2 < 0)
  51. {
  52. num2 = 0;
  53. }
  54. this.m_emitCount += num2;
  55. if (num2 == 0)
  56. {
  57. this.m_curveCountTime += deltaTime;
  58. if (this.m_curveCountTime > 1f)
  59. {
  60. this.CurveEmitDone = true;
  61. }
  62. }
  63. else
  64. {
  65. this.m_curveCountTime = 0f;
  66. }
  67. if (this.CurveEmitDone)
  68. {
  69. return 0;
  70. }
  71. return num2;
  72. }
  73. protected int EmitByDistance()
  74. {
  75. if (this.Layer.mStopped)
  76. {
  77. return 0;
  78. }
  79. if ((this.Layer.ClientTransform.position - this.LastClientPos).magnitude >= this.EmitDist)
  80. {
  81. this.LastClientPos = this.Layer.ClientTransform.position;
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. protected int EmitByBurst(float deltaTime)
  87. {
  88. if (!this.IsFirstEmit)
  89. {
  90. this.EmitLoop = 0f;
  91. return 0;
  92. }
  93. this.IsFirstEmit = false;
  94. int num = (int)this.Layer.EmitRate;
  95. int num2;
  96. if (num > this.Layer.AvailableNodeCount)
  97. {
  98. num2 = this.Layer.AvailableNodeCount;
  99. }
  100. else
  101. {
  102. num2 = num;
  103. }
  104. if (num2 <= 0)
  105. {
  106. return 0;
  107. }
  108. return num2;
  109. }
  110. protected int EmitByRate(float deltaTime)
  111. {
  112. if (this.Layer.IsBurstEmit)
  113. {
  114. return this.EmitByBurst(deltaTime);
  115. }
  116. this.EmitterElapsedTime += deltaTime;
  117. if (this.Layer.EmitDuration > 0f && this.EmitterElapsedTime >= this.Layer.EmitDuration)
  118. {
  119. if (this.EmitLoop > 0f)
  120. {
  121. this.EmitLoop -= 1f;
  122. }
  123. this.m_emitCount = 0;
  124. this.EmitterElapsedTime = 0f;
  125. this.IsFirstEmit = false;
  126. this.mElapsedTime = 0f;
  127. }
  128. if (this.EmitLoop == 0f)
  129. {
  130. return 0;
  131. }
  132. if (this.Layer.AvailableNodeCount == 0)
  133. {
  134. return 0;
  135. }
  136. this.mElapsedTime += deltaTime;
  137. int num = (int)(this.mElapsedTime * this.Layer.EmitRate);
  138. int num2;
  139. if (num > this.Layer.AvailableNodeCount)
  140. {
  141. num2 = this.Layer.AvailableNodeCount;
  142. }
  143. else
  144. {
  145. num2 = num;
  146. }
  147. if (num2 <= 0)
  148. {
  149. return 0;
  150. }
  151. this.mElapsedTime = 0f;
  152. this.m_emitCount += num2;
  153. return num2;
  154. }
  155. public Vector3 GetEmitRotation(EffectNode node)
  156. {
  157. Vector3 vector = Vector3.zero;
  158. if (this.Layer.DirType == DIRECTION_TYPE.Sphere)
  159. {
  160. vector = node.GetOriginalPos() - this.Layer.ClientTransform.position;
  161. if (vector == Vector3.zero)
  162. {
  163. Vector3 up = Vector3.up;
  164. Quaternion rotation = Quaternion.Euler((float)UnityEngine.Random.Range(0, 360), (float)UnityEngine.Random.Range(0, 360), (float)UnityEngine.Random.Range(0, 360));
  165. vector = rotation * up;
  166. }
  167. }
  168. else if (this.Layer.DirType == DIRECTION_TYPE.Planar)
  169. {
  170. vector = this.Layer.OriVelocityAxis;
  171. }
  172. else if (this.Layer.DirType == DIRECTION_TYPE.Cone)
  173. {
  174. if (this.Layer.EmitType == 3 && this.Layer.EmitUniform)
  175. {
  176. Vector3 vector2;
  177. if (!this.Layer.SyncClient)
  178. {
  179. vector2 = node.Position - (node.GetRealClientPos() + this.Layer.EmitPoint);
  180. }
  181. else
  182. {
  183. vector2 = node.Position - this.Layer.EmitPoint;
  184. }
  185. int num = this.Layer.AngleAroundAxis;
  186. if (this.Layer.UseRandomDirAngle)
  187. {
  188. num = UnityEngine.Random.Range(this.Layer.AngleAroundAxis, this.Layer.AngleAroundAxisMax);
  189. }
  190. Vector3 toDirection = Vector3.RotateTowards(vector2, this.Layer.CircleDir, (float)(90 - num) * 0.0174532924f, 1f);
  191. Quaternion rotation2 = Quaternion.FromToRotation(vector2, toDirection);
  192. vector = rotation2 * vector2;
  193. }
  194. else
  195. {
  196. Quaternion rhs = Quaternion.Euler(0f, 0f, (float)this.Layer.AngleAroundAxis);
  197. Quaternion rhs2 = Quaternion.Euler(0f, (float)UnityEngine.Random.Range(0, 360), 0f);
  198. Quaternion lhs = Quaternion.FromToRotation(Vector3.up, this.Layer.OriVelocityAxis);
  199. vector = lhs * rhs2 * rhs * Vector3.up;
  200. }
  201. }
  202. else if (this.Layer.DirType == DIRECTION_TYPE.Cylindrical)
  203. {
  204. Vector3 vector3 = node.GetOriginalPos() - this.Layer.ClientTransform.position;
  205. if (vector3 == Vector3.zero)
  206. {
  207. Vector3 up2 = Vector3.up;
  208. Quaternion rotation3 = Quaternion.Euler((float)UnityEngine.Random.Range(0, 360), (float)UnityEngine.Random.Range(0, 360), (float)UnityEngine.Random.Range(0, 360));
  209. vector3 = rotation3 * up2;
  210. }
  211. float d = Vector3.Dot(this.Layer.OriVelocityAxis, vector3);
  212. vector = vector3 - d * this.Layer.OriVelocityAxis.normalized;
  213. }
  214. return vector;
  215. }
  216. public void SetEmitPosition(EffectNode node)
  217. {
  218. Vector3 vector = Vector3.zero;
  219. Vector3 realClientPos = node.GetRealClientPos();
  220. if (this.Layer.EmitType == 1)
  221. {
  222. Vector3 emitPoint = this.Layer.EmitPoint;
  223. float x = UnityEngine.Random.Range(emitPoint.x - this.Layer.BoxSize.x / 2f, emitPoint.x + this.Layer.BoxSize.x / 2f);
  224. float y = UnityEngine.Random.Range(emitPoint.y - this.Layer.BoxSize.y / 2f, emitPoint.y + this.Layer.BoxSize.y / 2f);
  225. float z = UnityEngine.Random.Range(emitPoint.z - this.Layer.BoxSize.z / 2f, emitPoint.z + this.Layer.BoxSize.z / 2f);
  226. vector.x = x;
  227. vector.y = y;
  228. vector.z = z;
  229. if (!this.Layer.SyncClient)
  230. {
  231. vector = this.Layer.ClientTransform.rotation * vector + realClientPos;
  232. }
  233. else
  234. {
  235. vector = this.Layer.ClientTransform.rotation * vector;
  236. }
  237. }
  238. else if (this.Layer.EmitType == 0)
  239. {
  240. vector = this.Layer.EmitPoint;
  241. if (!this.Layer.SyncClient)
  242. {
  243. vector = realClientPos + this.Layer.EmitPoint;
  244. }
  245. }
  246. else if (this.Layer.EmitType == 2)
  247. {
  248. vector = this.Layer.EmitPoint;
  249. if (!this.Layer.SyncClient)
  250. {
  251. vector = realClientPos + this.Layer.EmitPoint;
  252. }
  253. Vector3 point = Vector3.up * this.Layer.Radius;
  254. Quaternion rotation = Quaternion.Euler((float)UnityEngine.Random.Range(0, 360), (float)UnityEngine.Random.Range(0, 360), (float)UnityEngine.Random.Range(0, 360));
  255. vector = rotation * point + vector;
  256. }
  257. else if (this.Layer.EmitType == 4)
  258. {
  259. Vector3 position = this.Layer.LineStartObj.position;
  260. Vector3 position2 = this.Layer.LineEndObj.position;
  261. Vector3 vector2 = position2 - position;
  262. float d;
  263. if (this.Layer.EmitUniform)
  264. {
  265. float num = (float)(node.Index + 1) / (float)this.Layer.MaxENodes;
  266. d = vector2.magnitude * num;
  267. }
  268. else
  269. {
  270. d = UnityEngine.Random.Range(0f, vector2.magnitude);
  271. }
  272. vector = position + vector2.normalized * d - realClientPos;
  273. if (!this.Layer.SyncClient)
  274. {
  275. vector = realClientPos + vector;
  276. }
  277. }
  278. else if (this.Layer.EmitType == 3)
  279. {
  280. float y2;
  281. if (this.Layer.EmitUniform)
  282. {
  283. int index = node.Index;
  284. float num2 = (float)(index + 1) / (float)this.Layer.MaxENodes;
  285. y2 = 360f * num2;
  286. }
  287. else
  288. {
  289. y2 = (float)UnityEngine.Random.Range(0, 360);
  290. }
  291. float d2 = this.Layer.Radius;
  292. if (this.Layer.UseRandomCircle)
  293. {
  294. d2 = UnityEngine.Random.Range(this.Layer.CircleRadiusMin, this.Layer.CircleRadiusMax);
  295. }
  296. Quaternion rotation2 = Quaternion.Euler(0f, y2, 0f);
  297. Vector3 point2 = rotation2 * (Vector3.right * d2);
  298. Quaternion rotation3 = Quaternion.FromToRotation(Vector3.up, this.Layer.ClientTransform.rotation * this.Layer.CircleDir);
  299. vector = rotation3 * point2;
  300. if (!this.Layer.SyncClient)
  301. {
  302. vector = realClientPos + vector + this.Layer.EmitPoint;
  303. }
  304. else
  305. {
  306. vector += this.Layer.EmitPoint;
  307. }
  308. }
  309. else if (this.Layer.EmitType == 5)
  310. {
  311. if (this.Layer.EmitMesh == null)
  312. {
  313. UnityEngine.Debug.LogWarning("please set a mesh to the emitter.");
  314. return;
  315. }
  316. if (this.Layer.EmitMeshType == 0)
  317. {
  318. int vertexCount = this.Layer.EmitMesh.vertexCount;
  319. int num3;
  320. if (this.Layer.EmitUniform)
  321. {
  322. num3 = node.Index % (vertexCount - 1);
  323. }
  324. else
  325. {
  326. num3 = UnityEngine.Random.Range(0, vertexCount - 1);
  327. }
  328. vector = this.Layer.EmitMesh.vertices[num3];
  329. if (!this.Layer.SyncClient)
  330. {
  331. vector = realClientPos + vector + this.Layer.EmitPoint;
  332. }
  333. else
  334. {
  335. vector += this.Layer.EmitPoint;
  336. }
  337. }
  338. else if (this.Layer.EmitMeshType == 1)
  339. {
  340. Vector3[] vertices = this.Layer.EmitMesh.vertices;
  341. int num4 = this.Layer.EmitMesh.triangles.Length / 3;
  342. int num3;
  343. if (this.Layer.EmitUniform)
  344. {
  345. num3 = node.Index % (num4 - 1);
  346. }
  347. else
  348. {
  349. num3 = UnityEngine.Random.Range(0, num4 - 1);
  350. }
  351. int num5 = this.Layer.EmitMesh.triangles[num3 * 3];
  352. int num6 = this.Layer.EmitMesh.triangles[num3 * 3 + 1];
  353. int num7 = this.Layer.EmitMesh.triangles[num3 * 3 + 2];
  354. vector = (vertices[num5] + vertices[num6] + vertices[num7]) / 3f;
  355. if (!this.Layer.SyncClient)
  356. {
  357. vector = realClientPos + vector + this.Layer.EmitPoint;
  358. }
  359. else
  360. {
  361. vector += this.Layer.EmitPoint;
  362. }
  363. }
  364. }
  365. node.SetLocalPosition(vector);
  366. }
  367. public int GetNodes(float deltaTime)
  368. {
  369. if (this.Layer.EmitWay == EEmitWay.ByRate)
  370. {
  371. return this.EmitByRate(deltaTime);
  372. }
  373. if (this.Layer.EmitWay == EEmitWay.ByCurve)
  374. {
  375. return this.EmitByCurve(deltaTime);
  376. }
  377. return this.EmitByDistance();
  378. }
  379. public EffectLayer Layer;
  380. public float EmitterElapsedTime;
  381. protected float mElapsedTime;
  382. private bool IsFirstEmit = true;
  383. public float EmitLoop;
  384. public float EmitDist = 1f;
  385. public bool CurveEmitDone;
  386. private Vector3 LastClientPos = Vector3.zero;
  387. protected int m_emitCount;
  388. protected float m_curveCountTime;
  389. }
  390. }