MegaShapeSXL.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. #if !UNITY_FLASH
  5. using System.Text.RegularExpressions;
  6. #endif
  7. public class MegaShapeSXL
  8. {
  9. int splineindex = 0;
  10. public void LoadXML(string sxldata, MegaShape shape, bool clear, int start)
  11. {
  12. MegaXMLReader xml = new MegaXMLReader();
  13. MegaXMLNode node = xml.read(sxldata);
  14. if ( !clear )
  15. shape.splines.Clear();
  16. shape.selcurve = start;
  17. splineindex = start;
  18. ParseXML(node, shape);
  19. }
  20. public void ParseXML(MegaXMLNode node, MegaShape shape)
  21. {
  22. foreach ( MegaXMLNode n in node.children )
  23. {
  24. switch ( n.tagName )
  25. {
  26. case "Shape": ParseShape(n, shape); break;
  27. }
  28. ParseXML(n, shape);
  29. }
  30. }
  31. MegaSpline GetSpline(MegaShape shape)
  32. {
  33. MegaSpline spline;
  34. if ( splineindex < shape.splines.Count )
  35. spline = shape.splines[splineindex];
  36. else
  37. {
  38. spline = new MegaSpline();
  39. shape.splines.Add(spline);
  40. }
  41. splineindex++;
  42. return spline;
  43. }
  44. public void ParseShape(MegaXMLNode node, MegaShape shape)
  45. {
  46. for ( int i = 0; i < node.values.Count; i++ )
  47. {
  48. MegaXMLValue val = node.values[i];
  49. //Debug.Log("Shape val " + val.name);
  50. switch ( val.name )
  51. {
  52. case "name": break;
  53. case "p": break;
  54. case "r": break;
  55. case "s": break;
  56. }
  57. }
  58. foreach ( MegaXMLNode n in node.children )
  59. {
  60. //Debug.Log("Shape tagName " + n.tagName);
  61. switch ( n.tagName )
  62. {
  63. case "Spline":
  64. ParseSpline(n, shape);
  65. break;
  66. }
  67. }
  68. }
  69. public void ParseSpline(MegaXMLNode node, MegaShape shape)
  70. {
  71. MegaSpline spline = new MegaSpline();
  72. for ( int i = 0; i < node.values.Count; i++ )
  73. {
  74. MegaXMLValue val = node.values[i];
  75. //Debug.Log("Spline val " + val.name);
  76. switch ( val.name )
  77. {
  78. case "flags": break;
  79. case "closed": spline.closed = int.Parse(val.value) > 0 ? true : false; break;
  80. }
  81. }
  82. foreach ( MegaXMLNode n in node.children )
  83. {
  84. //Debug.Log("Spline tagName " + n.tagName);
  85. switch ( n.tagName )
  86. {
  87. case "K": ParseKnot(n, shape, spline); break;
  88. }
  89. }
  90. //Debug.Log("************** Add Spline");
  91. shape.splines.Add(spline);
  92. }
  93. public void ParseKnot(MegaXMLNode node, MegaShape shape, MegaSpline spline)
  94. {
  95. Vector3 p = Vector3.zero;
  96. Vector3 invec = Vector3.zero;
  97. Vector3 outvec = Vector3.zero;
  98. for ( int i = 0; i < node.values.Count; i++ )
  99. {
  100. MegaXMLValue val = node.values[i];
  101. //Debug.Log("Knot val " + val.name);
  102. switch ( val.name )
  103. {
  104. case "p": p = ParseV3Split(val.value, 0); break;
  105. case "i": invec = ParseV3Split(val.value, 0); break;
  106. case "o": outvec = ParseV3Split(val.value, 0); break;
  107. case "l": break;
  108. }
  109. }
  110. spline.AddKnot(p, invec, outvec);
  111. }
  112. char[] commaspace = new char[] { ',', ' ' };
  113. Vector3 ParseV3Split(string str, int i)
  114. {
  115. return ParseV3(str.Split(commaspace, StringSplitOptions.RemoveEmptyEntries), i);
  116. }
  117. Vector3 ParseV3(string[] str, int i)
  118. {
  119. Vector3 p = Vector3.zero;
  120. p.x = float.Parse(str[i]);
  121. p.y = float.Parse(str[i + 1]);
  122. p.z = float.Parse(str[i + 2]);
  123. return p;
  124. }
  125. public void importData(string sxldata, MegaShape shape, float scale, bool clear, int start)
  126. {
  127. LoadXML(sxldata, shape, clear, start);
  128. for ( int i = start; i < splineindex; i++ )
  129. {
  130. float area = shape.splines[i].Area();
  131. if ( area < 0.0f )
  132. shape.splines[i].reverse = false;
  133. else
  134. shape.splines[i].reverse = true;
  135. }
  136. //shape.Centre(0.01f, new Vector3(-1.0f, 1.0f, 1.0f));
  137. //shape.Centre(scale, new Vector3(1.0f, 1.0f, 1.0f), start);
  138. shape.CoordAdjust(scale, new Vector3(1.0f, 1.0f, 1.0f), start);
  139. shape.CalcLength(); //10);
  140. }
  141. }