Guest User

Untitled

a guest
May 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.09 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class DefamationMangler : MonoBehaviour
  7. {
  8. [MenuItem("Tools/DefamationMangler %#d")]
  9. static void AlignMeshVertices()
  10. {
  11.  
  12. Undo.RecordObjects(Selection.transforms, "Mangle To Surface");
  13.  
  14. VerticesInit();
  15. }
  16.  
  17. [MenuItem("Tools/Reset A Plane %#r")]
  18. static void ResetMeshVertices()
  19. {
  20.  
  21. Undo.RecordObjects(Selection.transforms, "deMangle To Surface");
  22. foreach (GameObject go in Selection.gameObjects)
  23. {
  24. MeshFilter filter = go.GetComponent(typeof(MeshFilter)) as MeshFilter;
  25.  
  26. if (filter != null)
  27. {
  28. Mesh mesh = filter.sharedMesh;
  29.  
  30. go.transform.localScale = Vector3.one;
  31.  
  32. for (int m = 0; m < mesh.subMeshCount; m++)
  33. {
  34. Vector3[] verts = mesh.vertices;
  35.  
  36. for (int i = 0; i < verts.Length; i++)
  37. {
  38.  
  39. if (go.transform.rotation.eulerAngles.x == 0)
  40. {
  41. verts[i].y = 0;
  42. }
  43.  
  44. else if (go.transform.rotation.eulerAngles.x == 270)
  45. {
  46. verts[i].z =0;
  47. }
  48. else
  49. {
  50. print("UNKNOWN OBJECT ROTATION ARGH! FIX IT in the rotation!");
  51. }
  52.  
  53. }
  54.  
  55. List<Vector3> vertList = new List<Vector3>(verts);
  56.  
  57.  
  58. Debug.Log(vertList.Count);
  59.  
  60. mesh.SetVertices(vertList);
  61.  
  62. }
  63. }
  64. }
  65. }
  66.  
  67.  
  68. private static void VerticesInit()
  69. {
  70.  
  71.  
  72.  
  73. foreach (GameObject go in Selection.gameObjects)
  74. {
  75. MeshFilter filter = go.GetComponent(typeof(MeshFilter)) as MeshFilter;
  76.  
  77. if (filter != null)
  78. {
  79.  
  80. ResetMeshVertices();
  81.  
  82. // add some clearance
  83. go.transform.position += (Vector3.up * 10f);
  84.  
  85. DefamationMangle(ref filter, go);
  86.  
  87. }
  88. else
  89. {
  90. print("gameobject " + go.name + " has no mesh filter");
  91. }
  92.  
  93.  
  94.  
  95.  
  96. }
  97. }
  98.  
  99.  
  100. public static Vector3 FindClosest(float target, IList<Vector3> list)
  101. {
  102. Vector3 result = list[0];
  103. float minDelta = float.MaxValue;
  104.  
  105. foreach (Vector3 x in list)
  106. {
  107. // Can't use Math.Abs()
  108. float delta = (x.y < target) ? (target - x.y) : (x.y - target);
  109.  
  110. if (delta < minDelta)
  111. {
  112. minDelta = delta;
  113. result = x;
  114. }
  115. }
  116.  
  117. return result;
  118. }
  119.  
  120.  
  121. static void DefamationMangle(ref MeshFilter filter, GameObject go)
  122. {
  123.  
  124. print("==BEGIN==");
  125.  
  126.  
  127. if (filter != null)
  128. {
  129. Mesh mesh = filter.sharedMesh;
  130.  
  131. go.transform.localScale = Vector3.one;
  132.  
  133. for (int m = 0; m < mesh.subMeshCount; m++)
  134. {
  135.  
  136. List<Vector3> RealWorldLocations = new List<Vector3>(mesh.vertices.Length);
  137.  
  138.  
  139. Debug.Log(mesh.subMeshCount);
  140.  
  141. int[] triangles = mesh.GetTriangles(m);
  142.  
  143. Vector3[] verts;
  144. float midY;
  145. float highestY = 0f;
  146. float lowestY = 0;
  147. float range;
  148.  
  149. print("1st Range Setup");
  150. SetupTheRealWorldRanges(go, mesh, out RealWorldLocations, out midY, out highestY, out lowestY, out range);
  151.  
  152. print("MID Y:" + midY);
  153.  
  154. //HAAAACKKKK
  155.  
  156.  
  157. // Vector3 TheVertexLocation = FindClosest(midY, RealWorldLocations);
  158. Vector3 TheVertexLocation = Vector3.zero; // = FindClosest(highestY, RealWorldLocations);
  159.  
  160.  
  161. float closestResult = float.MaxValue;
  162.  
  163. foreach (var item in RealWorldLocations)
  164. {
  165.  
  166.  
  167. RaycastHit InitialTestHit;
  168. bool WeDidHit = Physics.Raycast(item + (Vector3.up * 10), -Vector3.up, out InitialTestHit);
  169.  
  170. float distance = item.y - InitialTestHit.point.y;
  171.  
  172.  
  173. // print("For location: " + item + " is " + (distance - 10f));
  174.  
  175. if (distance < closestResult)
  176. {
  177. closestResult = distance;
  178. TheVertexLocation = item;
  179. }
  180.  
  181.  
  182. }
  183.  
  184.  
  185. // print("Shortest Y is:"+ TheVertexLocation);
  186.  
  187. //Vector3 TheVertexLocation = go.transform.position;
  188.  
  189.  
  190.  
  191. // move to the approximate middle;
  192. // print(TheVertexLocation);
  193.  
  194. // OFFSET!!
  195.  
  196. // print(TheVertexLocation - go.transform.position);
  197.  
  198.  
  199. RaycastHit VertexInitialTestHit;
  200. bool DidHit = Physics.Raycast(TheVertexLocation + (Vector3.up * 10), -Vector3.up, out VertexInitialTestHit);
  201.  
  202.  
  203. print("REQUIRED OFFSET FOR BEST ORIGIN: " + ((TheVertexLocation - VertexInitialTestHit.point) - (Vector3.up * 10)));
  204. print("REQUIRED OFFSET FOR BEST ORIGIN: " + ((TheVertexLocation - VertexInitialTestHit.point) - (Vector3.up * 10)).y);
  205.  
  206. // DISABLED FOR NOW
  207. go.transform.position -= TheVertexLocation - VertexInitialTestHit.point;
  208.  
  209.  
  210. print("2st Range Setup");
  211. SetupTheRealWorldHitRanges(go, RealWorldLocations, out midY, out highestY, out lowestY, out range);
  212. print("MID Y:" + midY);
  213.  
  214. print("FINAL RANGE: " + range);
  215. print("FINAL highestY: " + highestY);
  216. print("FINAL lowestY: " + lowestY);
  217.  
  218.  
  219. verts = mesh.vertices;
  220.  
  221. for (int i = 0; i < verts.Length; i++)
  222. {
  223. RaycastHit hit;
  224.  
  225. Vector3 worldPt = go.transform.TransformPoint(mesh.vertices[i]);// currentVert]);
  226. // print("CURRENT: " + mesh.vertices[i]);// currentVert]);
  227. // currentVert--;
  228.  
  229.  
  230. if (Physics.Raycast(worldPt + (Vector3.up * 10), -Vector3.up, out hit))
  231. {
  232. Vector3 targetPosition = hit.point;
  233.  
  234. // Take the test from the highest point.
  235.  
  236.  
  237.  
  238. Vector3 ActualOffset = worldPt - targetPosition;
  239.  
  240.  
  241. // print("Actual Y: "+ActualOffset.y);
  242.  
  243.  
  244.  
  245. // create normalised version of the y - using the range.
  246. // offset to - 0.5 - 0.5 range
  247. Vector2 NormalisingHolder = new Vector2(ActualOffset.y, range).normalized;
  248.  
  249. // print(NormalisingHolder.x);
  250.  
  251.  
  252. float normalisedResult = (NormalisingHolder.x * 1) + 0.5f;
  253.  
  254.  
  255.  
  256. // Debug.Log("NORMALISED RESULT FOR VERTEX:" + normalisedResult);
  257.  
  258. // print("setting vertex index:" + i);
  259.  
  260. if (go.transform.rotation.eulerAngles.x == 0)
  261. {
  262. verts[i].y = -normalisedResult;
  263. }
  264.  
  265. else if (go.transform.rotation.eulerAngles.x == 270)
  266. {
  267. verts[i].z = -normalisedResult;
  268. }
  269. else
  270. {
  271. print("UNKNOWN OBJECT ROTATION ARGH! FIX IT in the rotation!");
  272. }
  273.  
  274.  
  275. // Debug.Log("DIFF: " + ActualOffset);
  276.  
  277. }
  278. // mesh.SetTriangles(triangles, m);
  279.  
  280.  
  281.  
  282. }
  283.  
  284. List<Vector3> vertList = new List<Vector3>(verts);
  285.  
  286.  
  287. // Debug.Log(vertList.Count);
  288.  
  289. mesh.SetVertices(vertList);
  290.  
  291. mesh.RecalculateNormals();
  292.  
  293.  
  294. mesh.RecalculateBounds();
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. go.transform.localScale = new Vector3(1, 1, range * 1.2f);
  302.  
  303. go.transform.position += (Vector3.up * (range * .95f));
  304.  
  305.  
  306. Transform[] transforms = Selection.transforms;
  307. foreach (Transform myTransform in transforms)
  308. {
  309. RaycastHit hit;
  310. if (Physics.Raycast(myTransform.position, -Vector3.up, out hit))
  311. {
  312. Vector3 targetPosition = hit.point;
  313. if (myTransform.gameObject.GetComponent<MeshFilter>() != null)
  314. {
  315.  
  316. verts = mesh.vertices;
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323. // go.transform.position = go.transform.position - (Vector3.up * (range * 0.4f));
  324.  
  325.  
  326.  
  327.  
  328. }
  329.  
  330. }
  331. }
  332. }
  333. }
  334. }
  335. private static void SetupTheRealWorldRanges(GameObject go, Mesh mesh, out List<Vector3> RealWorldLocations, out float midY, out float highestY, out float lowestY, out float range)
  336. {
  337. RealWorldLocations = new List<Vector3>(mesh.vertices.Length);
  338. Vector3[] verts = mesh.vertices;
  339.  
  340. // print("VERT COUNT: "+verts.Length);
  341.  
  342. highestY = float.MinValue;
  343. lowestY = float.MaxValue;
  344.  
  345. for (int i = 0; i < verts.Length; i++)
  346. {
  347.  
  348. Vector3 worldPt = go.transform.TransformPoint(mesh.vertices[i]);
  349.  
  350. RealWorldLocations.Add(worldPt);
  351.  
  352.  
  353. if (worldPt.y > highestY)
  354. {
  355. highestY = worldPt.y;
  356. }
  357. if (worldPt.y < lowestY)
  358. {
  359. lowestY = worldPt.y;
  360. }
  361. }
  362.  
  363. range = highestY - lowestY;
  364. midY = lowestY + (range * 0.5f);
  365. }
  366.  
  367.  
  368. private static void SetupTheRealWorldHitRanges(GameObject go, List<Vector3> RealWorldLocations, out float midY, out float highestY, out float lowestY, out float range)
  369. {
  370.  
  371. // print("LOCATIONS COUNT: "+RealWorldLocations.Count);
  372.  
  373. highestY = float.MinValue;
  374. lowestY = float.MaxValue;
  375.  
  376. for (int i = 0; i < RealWorldLocations.Count; i++)
  377. {
  378.  
  379.  
  380. RaycastHit hit;
  381. // bool DidHit = Physics.Raycast(worldPt + (Vector3.up * 10), -Vector3.up, out hit)
  382.  
  383. if (Physics.Raycast(RealWorldLocations[i] + (Vector3.up * 10), -Vector3.up, out hit))
  384. {
  385.  
  386.  
  387. Vector3 worldPt = hit.point; // go.transform.TransformPoint(RealWorldLocations[i]);
  388.  
  389. // print(worldPt);
  390.  
  391.  
  392. if (worldPt.y > highestY)
  393. {
  394. highestY = worldPt.y;
  395. }
  396. if (worldPt.y < lowestY)
  397. {
  398. lowestY = worldPt.y;
  399. }
  400. }
  401. }
  402.  
  403. range = highestY - lowestY;
  404. midY = lowestY + (range * 0.5f);
  405. }
  406.  
  407.  
  408. }
Add Comment
Please, Sign In to add comment