Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 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. ActiveHolograms.Add(newObject);
  66. }
  67. }
  68.  
  69. public void CreateTree(int number, Vector3 positionCenter, Quaternion rotation)
  70. {
  71. // Stay center in the square but move down to the ground
  72. var position = positionCenter - new Vector3(0, TreeSize.y * .5f, 0);
  73.  
  74. GameObject newObject = Instantiate(TreePrefabs[number], position, rotation);
  75.  
  76. if (newObject != null)
  77. {
  78. // Set the parent of the new object the GameObject it was placed on
  79. newObject.transform.parent = gameObject.transform;
  80.  
  81. newObject.transform.localScale = RescaleToSameScaleFactor(TreePrefabs[number]);
  82. ActiveHolograms.Add(newObject);
  83. }
  84. }
  85.  
  86.  
  87. private Vector3 RescaleToSameScaleFactor(GameObject objectToScale)
  88. {
  89. // ReSharper disable once CompareOfFloatsByEqualityOperator
  90. if (ScaleFactor == 0f)
  91. {
  92. CalculateScaleFactor();
  93. }
  94.  
  95. return objectToScale.transform.localScale * ScaleFactor;
  96. }
  97.  
  98. private Vector3 StretchToFit(GameObject obj, Vector3 desiredSize)
  99. {
  100. var curBounds = GetBoundsForAllChildren(obj).size;
  101.  
  102. return new Vector3(desiredSize.x / curBounds.x / 2, desiredSize.y, desiredSize.z / curBounds.z / 2);
  103. }
  104.  
  105. private void CalculateScaleFactor()
  106. {
  107. float maxScale = float.MaxValue;
  108.  
  109. var ratio = CalcScaleFactorHelper(WideBuildingPrefabs, WideBuildingSize);
  110. if (ratio < maxScale)
  111. {
  112. maxScale = ratio;
  113. }
  114.  
  115. ScaleFactor = maxScale;
  116. }
  117.  
  118. private float CalcScaleFactorHelper(List<GameObject> objects, Vector3 desiredSize)
  119. {
  120. float maxScale = float.MaxValue;
  121.  
  122. foreach (var obj in objects)
  123. {
  124. var curBounds = GetBoundsForAllChildren(obj).size;
  125. var difference = curBounds - desiredSize;
  126.  
  127. float ratio;
  128.  
  129. if (difference.x > difference.y && difference.x > difference.z)
  130. {
  131. ratio = desiredSize.x / curBounds.x;
  132. }
  133. else if (difference.y > difference.x && difference.y > difference.z)
  134. {
  135. ratio = desiredSize.y / curBounds.y;
  136. }
  137. else
  138. {
  139. ratio = desiredSize.z / curBounds.z;
  140. }
  141.  
  142. if (ratio < maxScale)
  143. {
  144. maxScale = ratio;
  145. }
  146. }
  147.  
  148. return maxScale;
  149. }
  150.  
  151. private Bounds GetBoundsForAllChildren(GameObject findMyBounds)
  152. {
  153. Bounds result = new Bounds(Vector3.zero, Vector3.zero);
  154.  
  155. foreach (var curRenderer in findMyBounds.GetComponentsInChildren<Renderer>())
  156. {
  157. if (result.extents == Vector3.zero)
  158. {
  159. result = curRenderer.bounds;
  160. }
  161. else
  162. {
  163. result.Encapsulate(curRenderer.bounds);
  164. }
  165. }
  166.  
  167. return result;
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement