LineSegment.cs 6.8 KB

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