Ellipse.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Ellipse : MonoBehaviour
  5. {
  6. private ParticleSystem particleSystem0;
  7. 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(float magLen) {
  48. this.cameraX.orthographicSize
  49. = this.cameraY.orthographicSize
  50. = this.cameraZ.orthographicSize
  51. = magLen / 2 * 1.5f;
  52. }
  53. //最后绘制圆的时候,相机大小自适应
  54. public void setCameraSize(Vector3 circleSize) {
  55. Vector3 vs = new Vector3();
  56. vs.x = Mathf.Abs(circleSize.x);
  57. vs.y = Mathf.Abs(circleSize.y);
  58. vs.z = Mathf.Abs(circleSize.z);
  59. this.cameraX.orthographicSize = Mathf.Max(circleSize.y, circleSize.z) / 2 * 1.5f;
  60. this.cameraY.orthographicSize = Mathf.Max(circleSize.x, circleSize.z) / 2 * 1.5f;
  61. this.cameraZ.orthographicSize = Mathf.Max(circleSize.x, circleSize.y) / 2 * 1.5f;
  62. }
  63. public void AddAndUpdatePointArray(Vector3 addPoint) {
  64. this.arrayList.Add(addPoint);
  65. this.DrawPointCloud(this.arrayList);
  66. }
  67. public void ClearAndUpdatePointArray() {
  68. this.arrayList.Clear();
  69. this.DrawPointCloud(this.arrayList);
  70. }
  71. ParticleSystem.Particle[] allParticles;
  72. public void DrawPointCloud(List<Vector3> drawList)
  73. {
  74. var main = this.particleSystem0.main;
  75. main.startSpeed = 0.0f;
  76. main.startLifetime = 1000.0f;
  77. var pointCount = drawList.Count;
  78. allParticles = new ParticleSystem.Particle[pointCount];
  79. main.maxParticles = pointCount;
  80. this.particleSystem0.Emit(pointCount);
  81. this.particleSystem0.GetParticles(allParticles);
  82. for (int i = 0; i < pointCount; i++)
  83. {
  84. allParticles[i].position = (Vector3)drawList[i];
  85. allParticles[i].startColor = Color.yellow;
  86. allParticles[i].startSize = 0.02f;
  87. }
  88. this.particleSystem0.SetParticles(allParticles, pointCount);
  89. }
  90. }