/// <summary> /// Starts spatial mapping, which will scan the room and visualize this until the target time is reached, or it is manually stopped. /// </summary> public static void RunSpatialMapping(bool overrideExisting = false, float timeToScan = 60f) { WorldErrors.Print("WTF"); if (wrapper == null) { wrapper = new GameObject("SpatialWrapper").AddComponent <SpatialWrapper>(); } if (!running) { if (!SpatialInfoReady || overrideExisting) { targetTime = timeToScan; wrapper.StartCoroutine(DoMapping()); WorldErrors.Print("DO MAPPING"); } else { WorldErrors.Print("Eh: " + SpatialInfoReady + "," + overrideExisting); } } else { WorldErrors.Print("Already running"); } }
void StartDrag() { if (movable) { //remove anchor WorldErrors.Print("Remove Anchor"); WorldAnchorManager.Instance.RemoveAnchor(gameObject); } }
void StopDrag() { //re-add anchor if (movable) { WorldErrors.Print("Attach Anchor"); WorldAnchorManager.Instance.AttachAnchor(gameObject, anchorName); StartCoroutine(AnchorTest()); } }
IEnumerator AnchorTest() { yield return(new WaitForSeconds(5f)); UnityEngine.XR.WSA.WorldAnchor wa = store.Load(anchorName, gameObject); if (wa == null) { WorldErrors.Print("Anchor null"); } else { WorldErrors.Print("Anchor exists"); } }
void Awake() { if (movable) { HandDraggable dragComp = gameObject.AddComponent <HandDraggable>(); dragComp.StartedDragging += StartDrag; dragComp.StoppedDragging += StopDrag; dragComp.RotationMode = rotationMode; dragComp.IsDraggingEnabled = true; HoloToolkit.Unity.InputModule.Cursor c = FindObjectOfType <HoloToolkit.Unity.InputModule.Cursor>(); dragComp.hackedCursorReference = c.transform; WorldErrors.Print("Created Hand-draggable"); } }
// Use this for initialization IEnumerator Start() { //mCol = GetComponent<Collider>(); if (deferredParent == null) { deferredParent = transform; } while (!WorldAnchorManager.IsInitialized) { yield return(null); } WorldErrors.Print("World Anchor Manager Initialized"); if (store == null) //store should be loaded once { WorldErrors.Print("Getting Store"); if (!GettingStore) { GettingStore = true; UnityEngine.XR.WSA.Persistence.WorldAnchorStore.GetAsync(GotStore); } while (store == null) { yield return(null); } WorldErrors.Print("Got Store"); } UnityEngine.XR.WSA.WorldAnchor wa = store.Load(anchorName, gameObject); if (wa == null) //no anchor found { WorldErrors.Print("No Anchor, creating one"); NoAnchor(); WorldAnchorManager.Instance.AttachAnchor(deferredParent.gameObject, anchorName); //StartCoroutine(AnchorTest()); } else { WorldErrors.Print("Loaded Anchor"); LoadedAnchor(); } yield return(null); }
// Use this for initialization IEnumerator Start() { //give it a frame to settle yield return(null); WorldErrors.Print("Running spacial mapper"); SpatialWrapper.RunSpatialMapping(false, 30f); while (!SpatialWrapper.SpatialInfoReady) { yield return(null); } WorldErrors.Print("Loading scene"); yield return(new WaitForSeconds(1f)); SceneManager.LoadScene(sceneName); yield return(null); }
IEnumerator DoPlacement(int tries) { if (tries > 1) { yield break; } if (spatialMapIfNotAnchored) { SpatialWrapper.RunSpatialMapping(false); while (!SpatialWrapper.SpatialInfoReady) { yield return(null); } Vector3?customHalfDims = null; if (useCustomShape) { customHalfDims = customShape * .5f; } if (!PlacementWrapper.PlaceObject(deferredParent.gameObject, placementType, customHalfDims)) { WorldErrors.Print("Could not place object"); //retry in a little while yield return(new WaitForSeconds(10f)); DoPlacement(tries++); } else { WorldErrors.Print("Placed Object: " + anchorName); foreach (GameObject g in activateWhenPlaced) { g.SetActive(true); } } } yield return(null); }
private async void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { // lock multi event socket.MessageReceived -= Socket_MessageReceived; //Debug.Log("OSCSERVER UWP Socket_MessageReceived"); //Read the message that was received from the UDP echo client. //Stream streamIn = args.GetDataStream().AsStreamForRead(); DataReader reader = args.GetDataReader(); //StreamReader reader = new StreamReader(streamIn); try { uint stringLength = reader.UnconsumedBufferLength; byte[] bytes = new byte[stringLength]; reader.ReadBytes(bytes); //string message = await reader.ReadToEndAsync() // .ConfigureAwait(continueOnCapturedContext: false); //byte[] bytes = System.Text.Encoding.UTF8.GetBytes(message); OSCPacket packet = OSCPacket.Unpack(bytes); _lastReceivedPacket = packet; PacketReceivedEvent(this, _lastReceivedPacket); } catch (System.Exception e) { WorldErrors.Print(e.Message); } finally { //streamIn.Dispose(); reader.Dispose(); // unlock multi event socket.MessageReceived += Socket_MessageReceived; } }
static IEnumerator DoMapping() { if (SpatialInfoReady) { WorldErrors.Print("Spatial Info Already There"); yield break; } //turn off rendering if we're not scanning, store old mode UnityEngine.XR.WSA.SpatialMappingRenderer smRend = FindObjectOfType <UnityEngine.XR.WSA.SpatialMappingRenderer>(); UnityEngine.XR.WSA.SpatialMappingRenderer.RenderState oldState = UnityEngine.XR.WSA.SpatialMappingRenderer.RenderState.Occlusion; if (smRend != null) { oldState = smRend.renderState; smRend.renderState = UnityEngine.XR.WSA.SpatialMappingRenderer.RenderState.None; } running = true; //is there an instance present? SpatialUnderstanding puInst = SpatialUnderstanding.Instance; if (puInst == null || !puInst.AllowSpatialUnderstanding) { Debug.LogError("No Spatial Understanding Instance, or not supported in build."); WorldErrors.Print("No Instance"); yield break; } //can it scan? while (!SpatialUnderstanding.IsInitialized) { WorldErrors.Print("Not initialized"); yield return(null); } puInst.UnderstandingCustomMesh.DrawProcessedMesh = true; bool requestedFinish = false; WorldErrors.Print("" + puInst.AutoBeginScanning + ", " + puInst.ScanState); if (!puInst.AutoBeginScanning && (puInst.ScanState == SpatialUnderstanding.ScanStates.ReadyToScan || puInst.ScanState == SpatialUnderstanding.ScanStates.None || puInst.ScanState == SpatialUnderstanding.ScanStates.Done)) { WorldErrors.Print("Request Begin Scan"); puInst.RequestBeginScanning(); } //yield until scan automatically finishes, or it is stopped, or time runs out while (puInst.ScanState != SpatialUnderstanding.ScanStates.Done) { timer += Time.deltaTime; if (!requestedFinish && timer > targetTime) { puInst.RequestFinishScan(); requestedFinish = true; puInst.UnderstandingCustomMesh.DrawProcessedMesh = false; } yield return(null); } //signal we're ready to share data SpatialInfoReady = true; if (ready != null) { ready(); } //reset rendering again if we have a mapping renderer if (smRend != null) { smRend.renderState = oldState; } running = false; }
/// <summary> /// Attempts to place an object given the parameters. /// Returns false if object could not be placed, or spatial mapping could not be initialized. /// </summary> /// <param name="target"></param> /// <param name="position"></param> /// <param name="customHalfDims"></param> /// <param name="minDistFromOthers"></param> /// <param name="minDistFromPlayer"></param> /// <param name="maxDistFromPlayer"></param> /// <returns></returns> public static bool PlaceObject(GameObject target, WrappedPlacement position, Vector3?customHalfDims = null, float minDistFromOthers = 3f, float minDistFromPlayer = .25f, float maxDistFromPlayer = 4.0f) { if (!Init()) { return(false); } Vector3 halfDims = Vector3.one * .5f; Renderer r = target.GetComponentInChildren <Renderer>(); if (r != null) { halfDims = (r.bounds.size * .5f); WorldErrors.Print("halfDims: " + (r.bounds.size * .5f).ToString()); } if (customHalfDims != null) { halfDims = customHalfDims.Value; } List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule> placementRules = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>() { SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromOtherObjects(halfDims.magnitude * minDistFromOthers) }; List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint>() { SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint.Create_NearPoint(Camera.main.transform.position, minDistFromPlayer, maxDistFromPlayer) }; SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition def = new SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition(); switch (position) { case WrappedPlacement.Place_InMidAir: def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_InMidAir(halfDims); break; case WrappedPlacement.Place_OnCeiling: def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnCeiling(halfDims); break; case WrappedPlacement.Place_OnEdge: { Vector3 halfDimsBot = halfDims; halfDimsBot.y *= .5f; def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnEdge(halfDims, halfDimsBot); } break; case WrappedPlacement.Place_OnFloor: def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnFloor(halfDims); break; case WrappedPlacement.Place_OnFloorAndCeiling: { Vector3 halfDimsBot = halfDims; halfDimsBot.y *= .5f; def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnFloorAndCeiling(halfDims, halfDimsBot); } break; case WrappedPlacement.Place_OnShape: Debug.LogWarning("Not supported"); def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnShape(halfDims, "DefaultShape", 0); break; case WrappedPlacement.Place_OnWall: def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnWall(halfDims, halfDims.y, 2.4f); break; case WrappedPlacement.Place_RandomInAir: def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_RandomInAir(halfDims); break; case WrappedPlacement.Place_UnderPlatformEdge: def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_UnderPlatformEdge(halfDims); break; } //SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult result = new SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult(); if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(target.name, SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(def), placementRules.Count, SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()), //rules placementConstraints.Count, SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()), //constraints SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr() ) > 0) { target.transform.position = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult().Position; return(true); } return(false); }
/// <summary> /// Update the position of the object being dragged. /// </summary> private void UpdateDragging() { Vector3 newHandPosition; Transform cameraTransform = CameraCache.Main.transform; currentInputSource.TryGetGripPosition(currentInputSourceId, out newHandPosition); WorldErrors.Print(newHandPosition.ToString()); if (hackedCursorReference != null) { Renderer r = GetComponentInChildren <Renderer>(); float rendAdd = 0f; if (r != null) { rendAdd = Mathf.Max(r.bounds.size.x, r.bounds.size.y, r.bounds.size.z); } Vector3 dir = (hackedCursorReference.position - cameraTransform.position).normalized; draggingPosition = cameraTransform.position + dir * (.75f + rendAdd); draggingRotation = HostTransform.rotation; // Apply Final Position HostTransform.position = Vector3.Lerp(HostTransform.position, draggingPosition + cameraTransform.TransformDirection(objRefGrabPoint), PositionLerpSpeed); // Apply Final Rotation HostTransform.rotation = Quaternion.Lerp(HostTransform.rotation, draggingRotation, RotationLerpSpeed); return; } Vector3 pivotPosition = GetHandPivotPosition(cameraTransform); Vector3 newHandDirection = Vector3.Normalize(newHandPosition - pivotPosition); newHandDirection = cameraTransform.InverseTransformDirection(newHandDirection); // in camera space Vector3 targetDirection = Vector3.Normalize(gazeAngularOffset * newHandDirection); targetDirection = cameraTransform.TransformDirection(targetDirection); // back to world space float currentHandDistance = Vector3.Magnitude(newHandPosition - pivotPosition); float distanceRatio = currentHandDistance / handRefDistance; float distanceOffset = distanceRatio > 0 ? (distanceRatio - 1f) * DistanceScale : 0; float targetDistance = objRefDistance + distanceOffset; draggingPosition = pivotPosition + (targetDirection * targetDistance); if (RotationMode == RotationModeEnum.OrientTowardUser || RotationMode == RotationModeEnum.OrientTowardUserAndKeepUpright) { draggingRotation = Quaternion.LookRotation(HostTransform.position - pivotPosition); } else if (RotationMode == RotationModeEnum.LockObjectRotation) { draggingRotation = HostTransform.rotation; } else // RotationModeEnum.Default { Vector3 objForward = cameraTransform.TransformDirection(objRefForward); // in world space Vector3 objUp = cameraTransform.TransformDirection(objRefUp); // in world space draggingRotation = Quaternion.LookRotation(objForward, objUp); } // Apply Final Position HostTransform.position = Vector3.Lerp(HostTransform.position, draggingPosition + cameraTransform.TransformDirection(objRefGrabPoint), PositionLerpSpeed); // Apply Final Rotation HostTransform.rotation = Quaternion.Lerp(HostTransform.rotation, draggingRotation, RotationLerpSpeed); if (RotationMode == RotationModeEnum.OrientTowardUserAndKeepUpright) { Quaternion upRotation = Quaternion.FromToRotation(HostTransform.up, Vector3.up); HostTransform.rotation = upRotation * HostTransform.rotation; } }
public void ButtonPressed() { WorldErrors.Print("Button Pressed"); done = true; }