private void DrawBoundingBox(IObjectAnchorsServiceEventArgs instance) { MultiAnchorObjectPlacement placement; if (!_objectPlacements.TryGetValue(instance.InstanceId, out placement)) { var boundingBox = _objectAnchorsService.GetModelBoundingBox(instance.ModelId); Debug.Assert(boundingBox.HasValue); placement = Instantiate(MultiAnchorPlacementPrefab).GetComponent <MultiAnchorObjectPlacement>(); var bbox = placement.ModelSpaceContent.AddComponent <WireframeBoundingBox>(); bbox.UpdateBounds(boundingBox.Value.Center, Vector3.Scale(boundingBox.Value.Extents, instance.ScaleChange), boundingBox.Value.Orientation, WireframeMaterial); var mesh = new GameObject("Model Mesh"); mesh.AddComponent <MeshRenderer>().sharedMaterial = WireframeMaterial; mesh.transform.SetParent(placement.ModelSpaceContent.transform, false); MeshLoader.AddMesh(mesh, _objectAnchorsService, instance.ModelId); _objectPlacements.Add(instance.InstanceId, placement); } if (instance.SurfaceCoverage > placement.SurfaceCoverage || !instance.Location.HasValue) { placement.UpdatePlacement(instance); } }
private void DrawBoundingBox(IObjectAnchorsServiceEventArgs instance) { WireframeBoundingBox bbox; if (!_boundingBoxes.TryGetValue(instance.InstanceId, out bbox)) { var boundingBox = _objectAnchorsService.GetModelBoundingBox(instance.ModelId); Debug.Assert(boundingBox.HasValue); bbox = new GameObject("Bounding Box").AddComponent <WireframeBoundingBox>(); bbox.transform.SetParent(transform, true); bbox.gameObject.SetActive(false); bbox.UpdateBounds(boundingBox.Value.Center, Vector3.Scale(boundingBox.Value.Extents, instance.ScaleChange), boundingBox.Value.Orientation, WireframeMaterial); _boundingBoxes.Add(instance.InstanceId, bbox); } else { bbox.gameObject.SetActive(false); } var location = instance.Location; if (location.HasValue) { bbox.transform.SetPositionAndRotation(location.Value.Position, location.Value.Orientation); bbox.gameObject.SetActive(true); } }
private void _trackableObjectDataLoader_ModelsLoaded(object sender, EventArgs e) { // // Load object model from local storage // var allModels = _trackableObjectDataLoader.TrackableObjects; // // Compute a bounding box to accommodate latest object. // if (allModels.Count > 0) { LargestModelScale = new Vector3(); foreach (var model in allModels) { var boundingbox = _objectAnchorsService.GetModelBoundingBox(model.ModelId); Debug.Assert(boundingbox.HasValue); var largerDimensionOfXY = Math.Max(boundingbox.Value.Extents.x, boundingbox.Value.Extents.y); var smallerDimensionOfXY = Math.Min(boundingbox.Value.Extents.x, boundingbox.Value.Extents.y); var size = new Vector3(largerDimensionOfXY, boundingbox.Value.Extents.z, smallerDimensionOfXY); LargestModelScale = Vector3.Max(LargestModelScale, size); } LargestModelScale = new Vector3(LargestModelScale.x, LargestModelScale.z, LargestModelScale.y); PlaceSearchAreaBoundingBoxInFrontOfUser(BoundingBoxSizeFactor * LargestModelScale); } }
public async Task <bool> LoadObjectModelsAsync(string modelPath) { IObjectAnchorsService objectAnchorsService = ObjectAnchorsService.GetService(); Debug.Log($"{Application.persistentDataPath} {objectAnchorsService != null}"); string[] ouFiles = Directory.GetFiles(modelPath, "*.ou", SearchOption.AllDirectories); foreach (var file in ouFiles) { // Represent a model by TrackableObject, and load its model into OU service. var trackableObject = new TrackableObjectData(); trackableObject.ModelFilePath = file.Replace('/', '\\'); string appPath = Application.persistentDataPath.Replace('/', '\\'); if (trackableObject.ModelFilePath.Contains(appPath)) { trackableObject.ModelId = await objectAnchorsService.AddObjectModelAsync(trackableObject.ModelFilePath); } else { #if WINDOWS_UWP byte[] buffer = await ReadFileBytesAsync(trackableObject.ModelFilePath); trackableObject.ModelId = await objectAnchorsService.AddObjectModelAsync(buffer); #endif // WINDOWS_UWP } if (trackableObject.ModelId != Guid.Empty) { trackableObject.ModelMesh = new Mesh(); await trackableObject.ModelMesh.SetFromObjectModel(trackableObject.ModelId); Debug.Log($"mesh has {trackableObject.ModelMesh.triangles.Length} indices"); trackableObject.logicalBoundingBox = objectAnchorsService.GetModelBoundingBox(trackableObject.ModelId); _trackableObjects.Add(trackableObject); _modelIdToTrackableObject.Add(trackableObject.ModelId, trackableObject); Debug.Log($"Loaded Model\n{trackableObject}"); } else { Debug.LogError($"failed to load model {trackableObject.ModelFilePath}"); } } if (_trackableObjects.Count > 0) { ModelsLoaded?.Invoke(this, EventArgs.Empty); } return(_trackableObjects.Count > 0); }
public async Task <bool> LoadObjectModelsAsync(string modelPath) { IObjectAnchorsService objectAnchorsService = ObjectAnchorsService.GetService(); Debug.Log($"{Application.persistentDataPath} {objectAnchorsService != null}"); string[] ouFiles = Directory.GetFiles(modelPath, "*.ou", SearchOption.AllDirectories); foreach (var file in ouFiles) { // Represent a model by TrackableObject, and load its model into OU service. var trackableObject = new TrackableObjectData(); trackableObject.ModelFilePath = file.Replace('/', '\\'); trackableObject.ModelId = await objectAnchorsService.AddObjectModelAsync(trackableObject.ModelFilePath); if (trackableObject.ModelId != Guid.Empty) { // Query the default coverage threshold from this object model. ObjectQuery query = objectAnchorsService.CreateObjectQuery(trackableObject.ModelId); trackableObject.MinSurfaceCoverageFromObjectModel = query.MinSurfaceCoverage; trackableObject.ModelMesh = GenerateMesh(trackableObject.ModelId); trackableObject.logicalBoundingBox = objectAnchorsService.GetModelBoundingBox(trackableObject.ModelId); _trackableObjects.Add(trackableObject); _modelIdToTrackableObject.Add(trackableObject.ModelId, trackableObject); Debug.Log($"Loaded Model\n{trackableObject}"); } else { Debug.LogError($"failed to load model {trackableObject.ModelFilePath}"); } } if (_trackableObjects.Count > 0) { ModelsLoaded?.Invoke(this, EventArgs.Empty); } return(_trackableObjects.Count > 0); }