MegaShapeSXL.cs 3.5 KB

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