LineSegment.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using Newtonsoft.Json;
  2. using PublicTools.Unity;
  3. using ShadowStudio.Model;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using XRTool.Util;
  9. namespace ShadowStudio.Tool
  10. {
  11. public struct LineData
  12. {
  13. public string lineName;
  14. public int index;
  15. public Vector3 position;
  16. public List<Vector3> positions;
  17. public XColor color;
  18. public float radius;
  19. }
  20. [RequireComponent(typeof(LineRenderer))]
  21. public class LineSegment : MonoBehaviour
  22. {
  23. private LineRenderer line;
  24. private LineData lineData = new LineData();
  25. private bool isSendWeb = false;
  26. private LineContainer container;
  27. private static MaterialPropertyBlock block;
  28. private bool isSingleLine = true;
  29. public LineRenderer Line
  30. {
  31. get
  32. {
  33. if (!line)
  34. {
  35. line = GetComponent<LineRenderer>();
  36. }
  37. return line;
  38. }
  39. }
  40. public LineContainer Container { get => container; set => container = value; }
  41. public bool IsSingleLine { get => isSingleLine; set => isSingleLine = value; }
  42. /// <summary>
  43. /// 设置材质
  44. /// </summary>
  45. /// <param name="mate"></param>
  46. public void SetLine(Material mate)
  47. {
  48. if (Line && mate)
  49. {
  50. UnityUtil.CopyMate(Line.sharedMaterial, mate);
  51. }
  52. }
  53. public void SetLine(LineData lineData)
  54. {
  55. if (line.positionCount < 1)
  56. {
  57. SetLine(lineData.color.Trans(), lineData.radius);
  58. Vector3[] pos = lineData.positions.ToArray();
  59. line.positionCount = pos.Length;
  60. line.SetPositions(pos);
  61. }
  62. else
  63. {
  64. if (line.positionCount == lineData.positions.Count - 1)
  65. {
  66. Line.positionCount = lineData.positions.Count;
  67. Line.SetPosition(Line.positionCount - 1, lineData.positions[lineData.positions.Count - 1]);
  68. }
  69. else
  70. {
  71. Vector3[] pos = lineData.positions.ToArray();
  72. line.positionCount = pos.Length;
  73. line.SetPositions(pos);
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// 设置线的相关属性
  79. /// </summary>
  80. /// <param name="lineColor"></param>
  81. /// <param name="radius"></param>
  82. public void SetLine(Color lineColor, float radius, string mainColorName = "_Color")
  83. {
  84. if (Line)
  85. {
  86. if (block == null)
  87. {
  88. block = new MaterialPropertyBlock();
  89. }
  90. UnityUtil.ChangeMateColor(block, Line, lineColor, mainColorName);
  91. Line.widthMultiplier = radius;
  92. lineData.color = new XColor(lineColor);
  93. lineData.radius = radius;
  94. lineData.lineName = name;
  95. }
  96. }
  97. /// <summary>
  98. /// 添加新的点
  99. /// 每添加一个点即开始同步
  100. /// </summary>
  101. /// <param name="newPos"></param>
  102. public void AddPosition(Vector3 newPos)
  103. {
  104. if (Line)
  105. {
  106. Line.positionCount = Line.positionCount + 1;
  107. newPos = transform.InverseTransformPoint(newPos);
  108. newPos.x = (float)Math.Round(newPos.x, 3);
  109. newPos.y = (float)Math.Round(newPos.y, 3);
  110. newPos.z = (float)Math.Round(newPos.z, 3);
  111. Line.SetPosition(Line.positionCount - 1, newPos);
  112. if (Line.positionCount > 1)
  113. {
  114. SendData(newPos);
  115. }
  116. }
  117. }
  118. public void SendData(Vector3 newPos)
  119. {
  120. lineData.index = Line.positionCount;
  121. lineData.position = newPos;
  122. if (lineData.positions == null)
  123. {
  124. lineData.positions = new List<Vector3>();
  125. }
  126. lineData.positions.Add(newPos);
  127. if (!IsSingleLine)
  128. {
  129. return;
  130. }
  131. ///没有发送,则发送创建线的事件
  132. ///如果已发送,需要判断容器是否已创建,如果没有创建,则不同步消息
  133. ///如果已创建,则开始同步消息
  134. if (!isSendWeb)
  135. {
  136. isSendWeb = true;
  137. ArtInfo info = ArtInfoMgr.Instance.GetArtInfo(DrawPener.artLineId);
  138. if (info != null)
  139. {
  140. if (WSHandler.Room != null)
  141. {
  142. //Debug.Log(lineData.color);
  143. //Debug.Log(lineData.index);
  144. //Debug.Log(lineData.lineName);
  145. //Debug.Log(lineData.position);
  146. //Debug.Log(lineData.positions);
  147. //Debug.Log(lineData.radius);
  148. WSHandler.Room.CreateGood(info.AID,info.ArtId, info.ArtName, UnityUtil.TransferToString(transform, 2), JsonConvert.SerializeObject(lineData));
  149. }
  150. }
  151. else
  152. {
  153. UnityLog.Instance.Log(DrawPener.artLineId + " is null");
  154. }
  155. }
  156. else if (Container)
  157. {
  158. SendData();
  159. }
  160. }
  161. public void SendData()
  162. {
  163. if (Container)
  164. {
  165. Container.SetExtendData(JsonConvert.SerializeObject(lineData));
  166. }
  167. }
  168. /// <summary>
  169. /// 此线段已完成绘制,最后同步一次,不再同步
  170. /// </summary>
  171. public void StopSyn()
  172. {
  173. if (IsSingleLine && Container)
  174. {
  175. Container.SetExtendData(JsonConvert.SerializeObject(lineData), false);
  176. }
  177. }
  178. /// <summary>
  179. /// 删除此线段,同步删除
  180. /// </summary>
  181. public void DelLine()
  182. {
  183. if (IsSingleLine && Container)
  184. {
  185. Container.DelLine();
  186. }
  187. Container = null;
  188. }
  189. public void SetPosition(Vector3 newPos)
  190. {
  191. newPos = transform.InverseTransformPoint(newPos);
  192. newPos.x = (float)Math.Round(newPos.x, 3);
  193. newPos.y = (float)Math.Round(newPos.y, 3);
  194. newPos.z = (float)Math.Round(newPos.z, 3);
  195. Line.SetPosition(Line.positionCount - 1, newPos);
  196. SendData(newPos);
  197. }
  198. }
  199. }