Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using HoloToolkit.Unity;
  4. using UnityEngine;
  5.  
  6. public class ObjectPlacer : MonoBehaviour
  7. {
  8. public bool DrawDebugBoxes = false;
  9. public bool DrawBuildings = true;
  10. public bool DrawTrees = true;
  11.  
  12. public SpatialUnderstandingCustomMesh SpatialUnderstandingMesh;
  13.  
  14. private readonly List<BoxDrawer.Box> _lineBoxList = new List<BoxDrawer.Box>();
  15.  
  16. private readonly Queue<PlacementResult> _results = new Queue<PlacementResult>();
  17.  
  18. private bool _timeToHideMesh;
  19. private BoxDrawer _boxDrawing;
  20.  
  21. // Use this for initialization
  22. void Start()
  23. {
  24. if (DrawDebugBoxes)
  25. {
  26. _boxDrawing = new BoxDrawer(gameObject);
  27. }
  28.  
  29. }
  30.  
  31. void Update()
  32. {
  33. ProcessPlacementResults();
  34.  
  35. if (_timeToHideMesh)
  36. {
  37. SpatialUnderstandingState.Instance.HideText = true;
  38. HideGridEnableOcclulsion();
  39. _timeToHideMesh = false;
  40. }
  41.  
  42. if (DrawDebugBoxes)
  43. {
  44. _boxDrawing.UpdateBoxes(_lineBoxList);
  45. }
  46.  
  47. }
  48.  
  49. private void HideGridEnableOcclulsion()
  50. {
  51. SpatialUnderstandingMesh.DrawProcessedMesh = false;
  52. }
  53.  
  54. public void CreateScene()
  55. {
  56. // Only if we're enabled
  57. if (!SpatialUnderstanding.Instance.AllowSpatialUnderstanding)
  58. {
  59. return;
  60. }
  61.  
  62. SpatialUnderstandingDllObjectPlacement.Solver_Init();
  63.  
  64. SpatialUnderstandingState.Instance.SpaceQueryDescription = "Generating World";
  65.  
  66. List<PlacementQuery> queries = new List<PlacementQuery>();
  67.  
  68. if (DrawBuildings)
  69. {
  70. queries.AddRange(AddBuildings());
  71. }
  72.  
  73. if (DrawTrees)
  74. {
  75. queries.AddRange(AddTrees());
  76. }
  77.  
  78. GetLocationsFromSolver(queries);
  79. }
  80.  
  81. public List<PlacementQuery> AddBuildings()
  82. {
  83.  
  84. var queries = CreateLocationQueriesForSolver(ObjectCollectionManager.Instance.WideBuildingPrefabs.Count, ObjectCollectionManager.Instance.WideBuildingSize, ObjectType.WideBuilding);
  85. queries.AddRange(CreateLocationQueriesForSolver(ObjectCollectionManager.Instance.SquareBuildingPrefabs.Count, ObjectCollectionManager.Instance.SquareBuildingSize, ObjectType.SquareBuilding));
  86. queries.AddRange(CreateLocationQueriesForSolver(ObjectCollectionManager.Instance.TallBuildingPrefabs.Count, ObjectCollectionManager.Instance.TallBuildingSize, ObjectType.TallBuilding));
  87. return queries;
  88. }
  89.  
  90. public List<PlacementQuery> AddTrees()
  91. {
  92. var queries = CreateLocationQueriesForSolver(ObjectCollectionManager.Instance.TreePrefabs.Count, ObjectCollectionManager.Instance.TreeSize, ObjectType.Tree);
  93.  
  94. return queries;
  95. }
  96.  
  97. private int _placedSquareBuilding;
  98. private int _placedTallBuilding;
  99. private int _placedWideBuilding;
  100. private int _placedTree;
  101.  
  102. private void ProcessPlacementResults()
  103. {
  104. if (_results.Count > 0)
  105. {
  106. var toPlace = _results.Dequeue();
  107. // Output
  108. if (DrawDebugBoxes)
  109. {
  110. DrawBox(toPlace, Color.red);
  111. }
  112.  
  113. var rotation = Quaternion.LookRotation(toPlace.Normal, Vector3.up);
  114.  
  115. switch (toPlace.ObjType)
  116. {
  117. case ObjectType.SquareBuilding:
  118. ObjectCollectionManager.Instance.CreateSquareBuilding(_placedSquareBuilding++, toPlace.Position, rotation);
  119. break;
  120. case ObjectType.TallBuilding:
  121. ObjectCollectionManager.Instance.CreateTallBuilding(_placedTallBuilding++, toPlace.Position, rotation);
  122. break;
  123. case ObjectType.WideBuilding:
  124. ObjectCollectionManager.Instance.CreateWideBuilding(_placedWideBuilding++, toPlace.Position, rotation);
  125. break;
  126. case ObjectType.Tree:
  127. ObjectCollectionManager.Instance.CreateTree(_placedTree++, toPlace.Position, rotation);
  128. break;
  129. }
  130. }
  131. }
  132.  
  133. private void DrawBox(PlacementResult boxLocation, Color color)
  134. {
  135. if (boxLocation != null)
  136. {
  137. _lineBoxList.Add(
  138. new BoxDrawer.Box(
  139. boxLocation.Position,
  140. Quaternion.LookRotation(boxLocation.Normal, Vector3.up),
  141. color,
  142. boxLocation.Dimensions * 0.5f)
  143. );
  144. }
  145. }
  146.  
  147. private void GetLocationsFromSolver(List<PlacementQuery> placementQueries)
  148. {
  149. #if UNITY_WSA && !UNITY_EDITOR
  150. System.Threading.Tasks.Task.Run(() =>
  151. {
  152. // Go through the queries in the list
  153. for (int i = 0; i < placementQueries.Count; ++i)
  154. {
  155. var result = PlaceObject(placementQueries[i].ObjType.ToString() + i,
  156. placementQueries[i].PlacementDefinition,
  157. placementQueries[i].Dimensions,
  158. placementQueries[i].ObjType,
  159. placementQueries[i].PlacementRules,
  160. placementQueries[i].PlacementConstraints);
  161. if (result != null)
  162. {
  163. _results.Enqueue(result);
  164. }
  165. }
  166.  
  167. _timeToHideMesh = true;
  168. });
  169. #else
  170. _timeToHideMesh = true;
  171. #endif
  172. }
  173.  
  174. private PlacementResult PlaceObject(string placementName,
  175. SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition,
  176. Vector3 boxFullDims,
  177. ObjectType objType,
  178. List<SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule> placementRules = null,
  179. List<SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = null)
  180. {
  181.  
  182. // New query
  183. if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
  184. placementName,
  185. SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementDefinition),
  186. (placementRules != null) ? placementRules.Count : 0,
  187. ((placementRules != null) && (placementRules.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()) : IntPtr.Zero,
  188. (placementConstraints != null) ? placementConstraints.Count : 0,
  189. ((placementConstraints != null) && (placementConstraints.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()) : IntPtr.Zero,
  190. SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()) > 0)
  191. {
  192. SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();
  193.  
  194. return new PlacementResult(placementResult.Clone() as SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult, boxFullDims, objType);
  195. }
  196.  
  197. return null;
  198. }
  199.  
  200. private List<PlacementQuery> CreateLocationQueriesForSolver(int desiredLocationCount, Vector3 boxFullDims, ObjectType objType)
  201. {
  202. List<PlacementQuery> placementQueries = new List<PlacementQuery>();
  203.  
  204. var halfBoxDims = boxFullDims * .5f;
  205.  
  206. var disctanceFromOtherObjects = halfBoxDims.x > halfBoxDims.z ? halfBoxDims.x * 3f : halfBoxDims.z * 3f;
  207.  
  208. for (int i = 0; i < desiredLocationCount; ++i)
  209. {
  210. var placementRules = new List<SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>
  211. {
  212. SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromOtherObjects(disctanceFromOtherObjects)
  213. };
  214.  
  215. var placementConstraints = new List<SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint>();
  216.  
  217. SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnFloor(halfBoxDims);
  218.  
  219. placementQueries.Add(
  220. new PlacementQuery(placementDefinition,
  221. boxFullDims,
  222. objType,
  223. placementRules,
  224. placementConstraints
  225. ));
  226. }
  227.  
  228. return placementQueries;
  229. }
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement