Ellipse.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Ellipse : MonoBehaviour
  5. {
  6. private ParticleSystem particleSystem0;
  7. [System.NonSerialized] public List<Vector3> arrayList = new List<Vector3>();
  8. public Transform ellipseTran;
  9. public Transform cameraXTran;
  10. public Transform cameraYTran;
  11. public Transform cameraZTran;
  12. private Camera cameraX;
  13. private Camera cameraY;
  14. private Camera cameraZ;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. cameraX = cameraXTran.GetComponent<Camera>();
  19. cameraY = cameraYTran.GetComponent<Camera>();
  20. cameraZ = cameraZTran.GetComponent<Camera>();
  21. particleSystem0 = GetComponentInChildren<ParticleSystem>();
  22. /*ArrayList arrayList = new ArrayList();
  23. for (int i = 0; i < 100000; i++) {
  24. int xSymbol = Random.value > 0.5 ? -1 : 1;
  25. int ySymbol = Random.value > 0.5 ? -1 : 1;
  26. int zSymbol = Random.value > 0.5 ? -1 : 1;
  27. var vec = new Vector3(Random.value* xSymbol, Random.value* ySymbol, Random.value* zSymbol);
  28. arrayList.Add(vec);
  29. }
  30. this.DrawPointCloud(arrayList);*/
  31. }
  32. //绘制椭圆的大小
  33. public void setEllipseLocalScaleAndCenter(Vector3 radius,Vector3 center) {
  34. this.ellipseTran.localScale = radius;
  35. this.ellipseTran.localPosition = center;
  36. }
  37. public void setCameraPos(Vector3 center)
  38. {
  39. this.cameraXTran.localPosition = new Vector3(center.x - 10, center.y, center.z);
  40. this.cameraYTran.localPosition = new Vector3(center.x, center.y + 10, center.z);
  41. this.cameraZTran.localPosition = new Vector3(center.x, center.y, center.z - 10);
  42. // this.cameraXTran.localPosition = new Vector3(this.cameraXTran.localPosition.x, center.y, center.z);
  43. // this.cameraYTran.localPosition = new Vector3(center.x, this.cameraYTran.localPosition.y, center.z);
  44. // this.cameraZTran.localPosition = new Vector3(center.x, center.y, this.cameraZTran.localPosition.z);
  45. }
  46. //最后绘制圆的时候,相机大小自适应
  47. public void setCameraSize(Vector3 circleSize) {
  48. Vector3 vs = new Vector3();
  49. vs.x = Mathf.Abs(circleSize.x);
  50. vs.y = Mathf.Abs(circleSize.y);
  51. vs.z = Mathf.Abs(circleSize.z);
  52. this.cameraX.orthographicSize = Mathf.Max(circleSize.y, circleSize.z) / 2 * 1.5f;
  53. this.cameraY.orthographicSize = Mathf.Max(circleSize.x, circleSize.z) / 2 * 1.5f;
  54. this.cameraZ.orthographicSize = Mathf.Max(circleSize.x, circleSize.y) / 2 * 1.5f;
  55. }
  56. public void AddAndUpdatePointArray(Vector3 addPoint) {
  57. this.arrayList.Add(addPoint);
  58. this.DrawPointCloud(this.arrayList);
  59. }
  60. public void ClearAndUpdatePointArray() {
  61. this.arrayList.Clear();
  62. this.DrawPointCloud(this.arrayList);
  63. }
  64. ParticleSystem.Particle[] allParticles;
  65. public void DrawPointCloud(List<Vector3> drawList)
  66. {
  67. var main = this.particleSystem0.main;
  68. main.startSpeed = 0.0f;
  69. main.startLifetime = 1000.0f;
  70. var pointCount = drawList.Count;
  71. allParticles = new ParticleSystem.Particle[pointCount];
  72. main.maxParticles = pointCount;
  73. this.particleSystem0.Emit(pointCount);
  74. this.particleSystem0.GetParticles(allParticles);
  75. for (int i = 0; i < pointCount; i++)
  76. {
  77. allParticles[i].position = (Vector3)drawList[i];
  78. allParticles[i].startColor = Color.yellow;
  79. allParticles[i].startSize = 0.02f;
  80. }
  81. this.particleSystem0.SetParticles(allParticles, pointCount);
  82. }
  83. }