Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using HoloToolkit.Unity;
  3. using UnityEngine;
  4.  
  5. public class ObjectCollectionManager : Singleton<ObjectCollectionManager>
  6. {
  7.  
  8. [Tooltip("A collection of square building prefabs to generate in the world.")]
  9. public List<GameObject> SquareBuildingPrefabs;
  10.  
  11. [Tooltip("The desired size of square buildings in the world.")]
  12. public Vector3 SquareBuildingSize = new Vector3(.5f, .5f, .5f);
  13.  
  14. [Tooltip("A collection of Wide building prefabs to generate in the world.")]
  15. public List<GameObject> WideBuildingPrefabs;
  16.  
  17. [Tooltip("The desired size of wide buildings in the world.")]
  18. public Vector3 WideBuildingSize = new Vector3(1.0f, .5f, .5f);
  19.  
  20. [Tooltip("A collection of tall building prefabs to generate in the world.")]
  21. public List<GameObject> TallBuildingPrefabs;
  22.  
  23. [Tooltip("The desired size of tall buildings in the world.")]
  24. public Vector3 TallBuildingSize = new Vector3(.25f, .05f, .25f);
  25.  
  26. [Tooltip("A collection of tree prefabs to generate in the world.")]
  27. public List<GameObject> TreePrefabs;
  28.  
  29. [Tooltip("The desired size of trees in the world.")]
  30. public Vector3 TreeSize = new Vector3(.25f, .5f, .25f);
  31.  
  32. [Tooltip("Will be calculated at runtime if is not preset.")]
  33. public float ScaleFactor;
  34.  
  35. public List<GameObject> ActiveHolograms = new List<GameObject>();
  36.  
  37. public void CreateSquareBuilding(int number, Vector3 positionCenter, Quaternion rotation)
  38. {
  39. CreateBuilding(SquareBuildingPrefabs[number], positionCenter, rotation, SquareBuildingSize);
  40. }
  41.  
  42. public void CreateTallBuilding(int number, Vector3 positionCenter, Quaternion rotation)
  43. {
  44. CreateBuilding(TallBuildingPrefabs[number], positionCenter, rotation, TallBuildingSize);
  45. }
  46.  
  47. public void CreateWideBuilding(int number, Vector3 positionCenter, Quaternion rotation)
  48. {
  49. CreateBuilding(WideBuildingPrefabs[number], positionCenter, rotation, WideBuildingSize);
  50. }
  51.  
  52. private void CreateBuilding(GameObject buildingToCreate, Vector3 positionCenter, Quaternion rotation, Vector3 desiredSize)
  53. {
  54. // Stay center in the square but move down to the ground
  55. var position = positionCenter - new Vector3(0, desiredSize.y * .5f, 0);
  56.  
  57. GameObject newObject = Instantiate(buildingToCreate, position, rotation);
  58.  
  59. if (newObject != null)
  60. {
  61. // Set the parent of the new object the GameObject it was placed on
  62. newObject.transform.parent = gameObject.transform;
  63.  
  64. newObject.transform.localScale = RescaleToSameScaleFactor(buildingToCreate);
  65. newObject.AddComponent<MeshCollider>();
  66. ActiveHolograms.Add(newObject);
  67. }
  68. }
  69.  
  70. public void CreateTree(int number, Vector3 positionCenter, Quaternion rotation)
  71. {
  72. // Stay center in the square but move down to the ground
  73. var position = positionCenter - new Vector3(0, TreeSize.y * .5f, 0);
  74.  
  75. GameObject newObject = Instantiate(TreePrefabs[number], position, rotation);
  76.  
  77. if (newObject != null)
  78. {
  79. // Set the parent of the new object the GameObject it was placed on
  80. newObject.transform.parent = gameObject.transform;
  81.  
  82. newObject.transform.localScale = RescaleToSameScaleFactor(TreePrefabs[number]);
  83. newObject.AddComponent<MeshCollider>();
  84. ActiveHolograms.Add(newObject);
  85. }
  86. }
  87.  
  88.  
  89. private Vector3 RescaleToSameScaleFactor(GameObject objectToScale)
  90. {
  91. // ReSharper disable once CompareOfFloatsByEqualityOperator
  92. if (ScaleFactor == 0f)
  93. {
  94. CalculateScaleFactor();
  95. }
  96.  
  97. return objectToScale.transform.localScale * ScaleFactor;
  98. }
  99.  
  100. private Vector3 StretchToFit(GameObject obj, Vector3 desiredSize)
  101. {
  102. var curBounds = GetBoundsForAllChildren(obj).size;
  103.  
  104. return new Vector3(desiredSize.x / curBounds.x / 2, desiredSize.y, desiredSize.z / curBounds.z / 2);
  105. }
  106.  
  107. private void CalculateScaleFactor()
  108. {
  109. float maxScale = float.MaxValue;
  110.  
  111. var ratio = CalcScaleFactorHelper(WideBuildingPrefabs, WideBuildingSize);
  112. if (ratio < maxScale)
  113. {
  114. maxScale = ratio;
  115. }
  116.  
  117. ScaleFactor = maxScale;
  118. }
  119.  
  120. private float CalcScaleFactorHelper(List<GameObject> objects, Vector3 desiredSize)
  121. {
  122. float maxScale = float.MaxValue;
  123.  
  124. foreach (var obj in objects)
  125. {
  126. var curBounds = GetBoundsForAllChildren(obj).size;
  127. var difference = curBounds - desiredSize;
  128.  
  129. float ratio;
  130.  
  131. if (difference.x > difference.y && difference.x > difference.z)
  132. {
  133. ratio = desiredSize.x / curBounds.x;
  134. }
  135. else if (difference.y > difference.x && difference.y > difference.z)
  136. {
  137. ratio = desiredSize.y / curBounds.y;
  138. }
  139. else
  140. {
  141. ratio = desiredSize.z / curBounds.z;
  142. }
  143.  
  144. if (ratio < maxScale)
  145. {
  146. maxScale = ratio;
  147. }
  148. }
  149.  
  150. return maxScale;
  151. }
  152.  
  153. private Bounds GetBoundsForAllChildren(GameObject findMyBounds)
  154. {
  155. Bounds result = new Bounds(Vector3.zero, Vector3.zero);
  156.  
  157. foreach (var curRenderer in findMyBounds.GetComponentsInChildren<Renderer>())
  158. {
  159. if (result.extents == Vector3.zero)
  160. {
  161. result = curRenderer.bounds;
  162. }
  163. else
  164. {
  165. result.Encapsulate(curRenderer.bounds);
  166. }
  167. }
  168.  
  169. return result;
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement