protected virtual RaycastResult CalculateRayIntersect() { RaycastResult result = new RaycastResult(); result.Hit = GazeManager.Instance.Hit; result.Position = GazeManager.Instance.Position; result.Normal = GazeManager.Instance.Normal; return result; }
public void TestMethod1() { var frac = 1; var dist = 2; var node = new Node(""); var point = new Vector3(1,2,3); var normal = new Vector3(1,2,3); var hit = new RaycastResult(frac, dist, node, point, normal); Assert.AreEqual (true, hit.Hit); Assert.AreEqual(frac, hit.Fraction); Assert.AreEqual (dist, hit.Distance); Assert.AreEqual (node, hit.Node); Assert.AreEqual (point, hit.Point); Assert.AreEqual (normal, hit.Normal); var nohit = new RaycastResult (); Assert.AreEqual (false, nohit.Hit); }
/// @endcond private void CastRay() { if (pointer == null || pointer.PointerTransform == null) { return; } Vector2 currentPose = NormalizedCartesianToSpherical(pointer.PointerTransform.forward); if (pointerData == null) { pointerData = new PointerEventData(eventSystem); lastPose = currentPose; } // Store the previous raycast result. RaycastResult previousRaycastResult = pointerData.pointerCurrentRaycast; // The initial cast must use the enter radius. if (pointer != null) { pointer.ShouldUseExitRadiusForRaycast = false; } // Cast a ray into the scene pointerData.Reset(); // Set the position to the center of the camera. // This is only necessary if using the built-in Unity raycasters. RaycastResult raycastResult; pointerData.position = GetViewportCenter(); bool isPointerActiveAndAvailable = IsPointerActiveAndAvailable(); if (isPointerActiveAndAvailable) { eventSystem.RaycastAll(pointerData, m_RaycastResultCache); raycastResult = FindFirstRaycast(m_RaycastResultCache); } else { raycastResult = new RaycastResult(); raycastResult.Clear(); } // If we were already pointing at an object we must check that object against the exit radius // to make sure we are no longer pointing at it to prevent flicker. if (previousRaycastResult.gameObject != null && raycastResult.gameObject != previousRaycastResult.gameObject && isPointerActiveAndAvailable) { if (pointer != null) { pointer.ShouldUseExitRadiusForRaycast = true; } m_RaycastResultCache.Clear(); eventSystem.RaycastAll(pointerData, m_RaycastResultCache); RaycastResult firstResult = FindFirstRaycast(m_RaycastResultCache); if (firstResult.gameObject == previousRaycastResult.gameObject) { raycastResult = firstResult; } } if (raycastResult.gameObject != null && raycastResult.worldPosition == Vector3.zero) { raycastResult.worldPosition = GetIntersectionPosition(pointerData.enterEventCamera, raycastResult); } pointerData.pointerCurrentRaycast = raycastResult; // Find the real screen position associated with the raycast // Based on the results of the hit and the state of the pointerData. if (raycastResult.gameObject != null) { pointerData.position = raycastResult.screenPosition; } else { Transform pointerTransform = pointer.PointerTransform; float maxPointerDistance = pointer.MaxPointerDistance; Vector3 pointerPos = pointerTransform.position + (pointerTransform.forward * maxPointerDistance); if (pointerData.pressEventCamera != null) { pointerData.position = pointerData.pressEventCamera.WorldToScreenPoint(pointerPos); } else if (Camera.main != null) { pointerData.position = Camera.main.WorldToScreenPoint(pointerPos); } } m_RaycastResultCache.Clear(); pointerData.delta = currentPose - lastPose; lastPose = currentPose; // Check to make sure the Raycaster being used is a GvrRaycaster. if (raycastResult.module != null && !(raycastResult.module is GvrPointerGraphicRaycaster) && !(raycastResult.module is GvrPointerPhysicsRaycaster)) { //Debug.LogWarning("Using Raycaster (Raycaster: " + raycastResult.module.GetType() + // ", Object: " + raycastResult.module.name + "). It is recommended to use " + // "GvrPointerPhysicsRaycaster or GvrPointerGrahpicRaycaster with GvrPointerInputModule."); } }
private void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList, Ray ray, bool checkForBlocking) { //This function is closely based on //void GraphicRaycaster.Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList) if (canvas == null) { return; } float hitDistance = float.MaxValue; if (checkForBlocking && blockingObjects != BlockingObjects.None) { float dist = eventCamera.farClipPlane; if (blockingObjects == BlockingObjects.ThreeD || blockingObjects == BlockingObjects.All) { var hits = Physics.RaycastAll(ray, dist, m_BlockingMask); if (hits.Length > 0 && hits[0].distance < hitDistance) { hitDistance = hits[0].distance; } } if (blockingObjects == BlockingObjects.TwoD || blockingObjects == BlockingObjects.All) { var hits = Physics2D.GetRayIntersectionAll(ray, dist, m_BlockingMask); if (hits.Length > 0 && hits[0].fraction * dist < hitDistance) { hitDistance = hits[0].fraction * dist; } } } m_RaycastResults.Clear(); GraphicRaycast(canvas, ray, m_RaycastResults); for (var index = 0; index < m_RaycastResults.Count; index++) { var go = m_RaycastResults[index].graphic.gameObject; bool appendGraphic = true; if (ignoreReversedGraphics) { // If we have a camera compare the direction against the cameras forward. var cameraFoward = ray.direction; var dir = go.transform.rotation * Vector3.forward; appendGraphic = Vector3.Dot(cameraFoward, dir) > 0; } // Ignore points behind us (can happen with a canvas pointer) if (eventCamera.transform.InverseTransformPoint(m_RaycastResults[index].worldPos).z <= 0) { appendGraphic = false; } if (appendGraphic) { float distance = Vector3.Distance(ray.origin, m_RaycastResults[index].worldPos); if (distance >= hitDistance) { continue; } var castResult = new RaycastResult { gameObject = go, module = this, distance = distance, index = resultAppendList.Count, depth = m_RaycastResults[index].graphic.depth, worldPosition = m_RaycastResults[index].worldPos }; resultAppendList.Add(castResult); } } }
public override void OnPointerHover(GameObject targetGameObject, RaycastResult ray, bool isTargetInteractive) { MiraReticle.Instance.reticleHover(ray, isTargetInteractive); }
/// <summary> /// Perform a Unity UI Raycast, compare with the latest 3D raycast, and overwrite the hit object info if the UI gets focus /// </summary> private void RaycastUnityUI() { if (UnityUIPointerEvent == null) { UnityUIPointerEvent = new PointerEventData(EventSystem.current); } Camera mainCamera = CameraCache.Main; // 2D cursor position Vector2 cursorScreenPos = mainCamera.WorldToScreenPoint(HitPosition); UnityUIPointerEvent.delta = cursorScreenPos - UnityUIPointerEvent.position; UnityUIPointerEvent.position = cursorScreenPos; // Graphics raycast raycastResultList.Clear(); EventSystem.current.RaycastAll(UnityUIPointerEvent, raycastResultList); RaycastResult uiRaycastResult = FindClosestRaycastHitInLayerMasks(raycastResultList, RaycastLayerMasks); UnityUIPointerEvent.pointerCurrentRaycast = uiRaycastResult; // If we have a raycast result, check if we need to overwrite the 3D raycast info if (uiRaycastResult.gameObject != null) { bool superseded3DObject = false; if (IsGazingAtObject) { // Check layer prioritization if (RaycastLayerMasks.Length > 1) { // Get the index in the prioritized layer masks int uiLayerIndex = FindLayerListIndex(uiRaycastResult.gameObject.layer, RaycastLayerMasks); int threeDLayerIndex = FindLayerListIndex(hitInfo.collider.gameObject.layer, RaycastLayerMasks); if (threeDLayerIndex > uiLayerIndex) { superseded3DObject = true; } else if (threeDLayerIndex == uiLayerIndex) { if (hitInfo.distance > uiRaycastResult.distance) { superseded3DObject = true; } } } else { if (hitInfo.distance > uiRaycastResult.distance) { superseded3DObject = true; } } } // Check if we need to overwrite the 3D raycast info if (!IsGazingAtObject || superseded3DObject) { IsGazingAtObject = true; Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(uiRaycastResult.screenPosition.x, uiRaycastResult.screenPosition.y, uiRaycastResult.distance)); hitInfo = new RaycastHit { distance = uiRaycastResult.distance, normal = -uiRaycastResult.gameObject.transform.forward, point = worldPos }; HitObject = uiRaycastResult.gameObject; HitPosition = HitInfo.point; lastHitDistance = HitInfo.distance; } } }
private static bool CheckDistance(RaycastResult rr, Pair<bool, float> hit) { // if it was a hit check if its the closest if (hit.first) { if ((rr.Distance < 0.0f) || (hit.second < rr.Distance)) { // this is the closest so far, save it off rr.Distance = hit.second; return true; } } return false; }
public RaycastResult Set(RaycastResult argOther) { Lambda = argOther.Lambda; Normal.Set(argOther.Normal); return this; }
public override void OnPointerHover(RaycastResult rayastResult, bool isInteractive) { GazePointer.OnPointerHover(rayastResult, isInteractive); }
public bool Equals(RaycastResult other) { return string.Equals(_objectName, other._objectName) && _distance.Equals(other._distance); }
public RaycastResult set(RaycastResult argOther) { lambda = argOther.lambda; normal.set(argOther.normal); return this; }
private static void _processUserInput(Vector3 mp, RaycastResult raycast, bool validPos) { var handled = false; switch (Mode) { case AddonMode.Link: { if (Input.GetKeyDown(KeyCode.Mouse0)) { if (validPos && raycast.HittedPart.Modules.Contains(Config.Instance.ModuleName)) { var moduleActiveStrut = raycast.HittedPart.Modules[Config.Instance.ModuleName] as ModuleActiveStrut; if (moduleActiveStrut != null) { moduleActiveStrut.SetAsTarget(); handled = true; } } } else if (Input.GetKeyDown(KeyCode.X)) { CurrentTargeter.AbortLink(); handled = true; } } break; case AddonMode.FreeAttach: { if (Input.GetKeyDown(KeyCode.Mouse0)) { if (validPos) { CurrentTargeter.PlaceFreeAttach(raycast.HittedPart, raycast.Hit.point); handled = true; } } else if (Input.GetKeyDown(KeyCode.X)) { Mode = AddonMode.None; handled = true; } } break; } if (HighLogic.LoadedSceneIsEditor && handled) { Input.ResetInputAxes(); InputLockManager.RemoveControlLock(Config.Instance.EditorInputLockId); } }
private static bool _determineColor(Vector3 mp, RaycastResult raycast) { var validPosition = IsValidPosition(raycast); var mr = _connector.GetComponent<MeshRenderer>(); mr.material.color = Util.Util.MakeColorTransparent(validPosition ? (Mode == AddonMode.Link && !raycast.HittedPart.Modules.Contains(Config.Instance.ModuleName)) || (Mode == AddonMode.FreeAttach && (raycast.HittedPart.Modules.Contains(Config.Instance.ModuleName) || !raycast.HittedPart.Modules.Contains(Config.Instance.ModuleActiveStrutFreeAttachTarget))) ? Color.yellow : Color.green : Color.red); if (Mode == AddonMode.FreeAttach) { validPosition = validPosition && !raycast.HittedPart.Modules.Contains(Config.Instance.ModuleName) && raycast.HittedPart.Modules.Contains(Config.Instance.ModuleActiveStrutFreeAttachTarget); } return validPosition; }
private static bool IsValidPosition(RaycastResult raycast) { var valid = raycast.HitResult && raycast.HittedPart != null && raycast.DistanceFromOrigin <= Config.Instance.MaxDistance && raycast.RayAngle <= Config.Instance.MaxAngle; switch (Mode) { case AddonMode.Link: { if (raycast.HittedPart != null && raycast.HittedPart.Modules.Contains(Config.Instance.ModuleName)) { var moduleActiveStrut = raycast.HittedPart.Modules[Config.Instance.ModuleName] as ModuleActiveStrut; if (moduleActiveStrut != null) { valid = valid && raycast.HittedPart != null && raycast.HittedPart.Modules.Contains(Config.Instance.ModuleName) && moduleActiveStrut.IsConnectionFree; } } } break; } return valid; }
public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList) { if (this.canvas == null) { return; } if (this.eventCamera == null) { return; } if (!this.IsPointerAvailable()) { return; } if (this.canvas.renderMode != RenderMode.WorldSpace) { Debug.LogError("PointerGraphicRaycaster requires that the canvase renderMode is set to WorldSpace."); return; } if (this.cachedPointerEventCamera == null && LaserPointer.Instance != null) { this.cachedPointerEventCamera = LaserPointer.Instance.gameObject.GetComponent <Camera>(); } Ray ray = GetRay(); float hitDistance = float.MaxValue; if (this.BlockingObjectType != BlockingObjects.None) { float dist = this.eventCamera.farClipPlane - this.eventCamera.nearClipPlane; if (this.BlockingObjectType == BlockingObjects.ThreeD || this.BlockingObjectType == BlockingObjects.All) { RaycastHit hit; if (Physics.Raycast(ray, out hit, dist, this.BlockingMask)) { hitDistance = hit.distance; } } if (this.BlockingObjectType == BlockingObjects.TwoD || this.BlockingObjectType == BlockingObjects.All) { RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, dist, this.BlockingMask); if (hit.collider != null) { hitDistance = hit.fraction * dist; } } } this.raycastResults.Clear(); Ray finalRay; if (this.cachedPointerEventCamera != null) { Raycast(this.canvas, ray, this.cachedPointerEventCamera, this.MaxPointerDistance, this.raycastResults, out finalRay); // this.eventCamera } else { Raycast(this.canvas, ray, this.eventCamera, this.MaxPointerDistance, this.raycastResults, out finalRay); } for (int index = 0; index < this.raycastResults.Count; index++) { GameObject go = this.raycastResults[index].gameObject; bool appendGraphic = true; if (this.IgnoreReversedGraphics) { // If we have a camera compare the direction against the cameras forward. Vector3 cameraFoward = Vector3.zero; if (this.cachedPointerEventCamera != null) { cameraFoward = this.cachedPointerEventCamera.transform.rotation * Vector3.forward; } else { cameraFoward = this.eventCamera.transform.rotation * Vector3.forward; } Vector3 dir = go.transform.rotation * Vector3.forward; appendGraphic = Vector3.Dot(cameraFoward, dir) > 0; } if (appendGraphic) { float distance = 0; Transform trans = go.transform; Vector3 transForward = trans.forward; distance = Vector3.Dot(transForward, trans.position - finalRay.origin) / Vector3.Dot(transForward, finalRay.direction); if (distance < 0) { continue; } if (distance >= hitDistance) { continue; } RaycastResult castResult = new RaycastResult { gameObject = go, module = this, distance = distance, worldPosition = finalRay.origin + (finalRay.direction * distance), screenPosition = this.eventCamera.WorldToScreenPoint(finalRay.origin + (finalRay.direction * distance)), index = resultAppendList.Count, depth = this.raycastResults[index].depth, sortingLayer = this.canvas.sortingLayerID, sortingOrder = this.canvas.sortingOrder }; resultAppendList.Add(castResult); } } }
// invoke by Pointer3DInputModule public virtual void Raycast() { sortedRaycastResults.Clear(); breakPoints.Clear(); var zScale = transform.lossyScale.z; var amountDistance = (FarDistance - NearDistance) * zScale; var origin = transform.TransformPoint(0f, 0f, NearDistance); breakPoints.Add(origin); bool hasNext = true; Vector3 direction; float distance; Ray ray; RaycastResult firstHit = default(RaycastResult); var generator = CurrentSegmentGenerator(); if (ReferenceEquals(generator, null)) { // process default raycast direction = transform.forward; distance = amountDistance; ray = new Ray(origin, direction); // move event camera in place eventCamera.farClipPlane = eventCamera.nearClipPlane + distance; eventCamera.transform.position = ray.origin - (ray.direction * eventCamera.nearClipPlane); eventCamera.transform.rotation = Quaternion.LookRotation(ray.direction, transform.up); ForeachRaycastMethods(ray, distance, sortedRaycastResults); firstHit = FirstRaycastResult(); breakPoints.Add(firstHit.isValid ? firstHit.worldPosition : ray.GetPoint(distance)); #if UNITY_EDITOR if (showDebugRay) { Debug.DrawLine(breakPoints[0], breakPoints[1], firstHit.isValid ? Color.green : Color.red); } #endif } else { generator.ResetSegments(); do { hasNext = generator.NextSegment(out direction, out distance); if (distance < MIN_SEGMENT_DISTANCE) { Debug.LogWarning("RaySegment.distance cannot smaller than " + MIN_SEGMENT_DISTANCE + "! distance=" + distance.ToString("0.000")); break; } distance *= zScale; if (distance < amountDistance) { amountDistance -= distance; } else { distance = amountDistance; amountDistance = 0f; } ray = new Ray(origin, direction); // move event camera in place eventCamera.farClipPlane = eventCamera.nearClipPlane + distance; eventCamera.transform.position = ray.origin - (ray.direction * eventCamera.nearClipPlane); eventCamera.transform.rotation = Quaternion.LookRotation(ray.direction, transform.up); ForeachRaycastMethods(ray, distance, sortedRaycastResults); firstHit = FirstRaycastResult(); // end loop if raycast hit if (firstHit.isValid) { breakPoints.Add(firstHit.worldPosition); #if UNITY_EDITOR if (showDebugRay) { Debug.DrawLine(breakPoints[breakPoints.Count - 2], breakPoints[breakPoints.Count - 1], Color.green); } #endif break; } // otherwise, shift to next iteration origin = ray.GetPoint(distance); breakPoints.Add(origin); #if UNITY_EDITOR if (showDebugRay) { Debug.DrawLine(breakPoints[breakPoints.Count - 2], breakPoints[breakPoints.Count - 1], Color.red); } #endif }while (hasNext && amountDistance > 0f); } }
void OnTick(object sender, EventArgs e) { try { if (Activated) { Game.Player.Character.Health = 100; if (IsSprinting) { if (!Game.Player.Character.IsInAir) { DoSuperSprint(); } else { DoGliding(); } // Rendering Camera is fake news //Game.Player.Character.Rotation = new Vector3(Game.Player.Character.Rotation.X, Game.Player.Character.Rotation.Y, World.RenderingCamera.Rotation.Z); SuperSprintTimer++; } else { SuperSprintTimer = 0; } //if (Game.Player.IsAiming) { Game.TimeScale = 0.25f; } else { Game.TimeScale = 1f; } if (Game.Player.Character.Position.Z < 2.75f) { Game.Player.Character.Velocity = new Vector3(Game.Player.Character.Velocity.X, Game.Player.Character.Velocity.Y, -(Game.Player.Character.Position.Z - 2.75f)); if (Game.Player.Character.IsSwimming || Game.Player.Character.IsInParachuteFreeFall) { Game.Player.Character.Task.ClearAllImmediately(); } } // No fall dmg anim if (Game.Player.Character.IsInParachuteFreeFall && Game.Player.Character.Velocity.Z < 0 && Game.Player.Character.HeightAboveGround < 2 || Game.Player.Character.IsFalling && Game.Player.Character.Velocity.Z < 0 && Game.Player.Character.HeightAboveGround < 2 || Game.Player.Character.IsGettingUp) { ResetPlayerAnim(); } // Superjump if (!Game.IsKeyPressed(Keys.Space) && SpaceWasDownLastFrame && !Game.Player.Character.IsInAir) { float Percentage = -(1 / ((SuperSprintTimer + SuperSprintInvertedAcc) / SuperSprintInvertedAcc)) + 1; Game.Player.Character.Weapons.Give(GTA.Native.WeaponHash.Parachute, 1, false, true); Game.Player.Character.Task.Skydive(); Game.Player.Character.ApplyForce(new Vector3(0, 0, SuperJumpChargeTimer * Percentage + 5) + Game.Player.Character.ForwardVector * (SuperJumpChargeTimer * Percentage / 2.5f)); SuperJumpTimer = 0; SuperJumpChargeTimer = 25; } if (SuperJumpChargeTimer == 26) { ResetPlayerAnim(); } if (Game.IsKeyPressed(Keys.Space) /* && Game.Player.Character.Velocity.Z <= 15*/ && !Game.Player.Character.IsInAir) { SuperJumpChargeTimer += 1 + SuperJumpChargeTimer / 50; } if (SuperJumpChargeTimer > 450) { SuperJumpChargeTimer = 450; } if (SuperJumpChargeTimer > 25) { UI.ShowSubtitle("Super-Jump: " + SuperJumpChargeTimer.ToString()); } else { string output = ""; try { output += "CanRagdoll: " + Game.Player.Character.CanRagdoll.ToString(); if (TelekinesisEntity == null) { output += " | TelekinesisEntity: null"; } else { output += " | TelekinesisEntity: " + TelekinesisEntity.Model.ToString(); } output += " | PlayerIsSetToGround: " + (!Game.Player.Character.IsInAir && SuperJumpTimer > 100 && Game.Player.Character.Position.Z > 3 || Game.Player.Character.IsFalling).ToString(); output += " | IsFalling: " + Game.Player.Character.IsFalling.ToString(); output += " | HeightAboveGround: " + Math.Round(Game.Player.Character.HeightAboveGround, 1).ToString(); output += " | TelekinesisModelSize: " + Math.Round(TelekinesisModelSize, 1).ToString(); output += " | (FakeNews)RenderingCameraPos: " + World.RenderingCamera.Position.ToString(); } catch (Exception ex) { output += ex.Message + ex.InnerException + ex.StackTrace; } UI.ShowSubtitle(output); } // Telekinesis if (Game.IsKeyPressed(Keys.Q) && !WasQPressedLastFrame) { ResetPlayerAnim(); if (TelekinesisEntity == null) { RaycastResult RayResult = World.GetCrosshairCoordinates(); if (RayResult.DitHitEntity && RayResult.HitEntity != Game.Player.Character) { TelekinesisEntity = RayResult.HitEntity; TelekinesisEntity.HasGravity = true; if (TelekinesisEntity.GetType() == typeof(Ped)) { ((Ped)TelekinesisEntity).Task.Jump(); } Vector3 Dimensions = TelekinesisEntity.Model.GetDimensions(); TelekinesisModelSize = (float)Math.Sqrt(Dimensions.X * Dimensions.X + Dimensions.Y * Dimensions.Y); } } else { TelekinesisEntity.ApplyForce((TelekinesisEntity.Position - Game.Player.Character.Position) * 20 + Game.Player.Character.Velocity); TelekinesisEntity = null; } } if (TelekinesisEntity != null) { RaycastResult RayResult = World.GetCrosshairCoordinates(); if (RayResult.DitHitAnything && !RayResult.DitHitEntity || RayResult.DitHitEntity && RayResult.HitEntity != Game.Player.Character) { Vector3 Hit = RayResult.HitCoords; Vector3 ToHit = Hit - Game.Player.Character.Position; ToHit.Normalize(); TelekinesisEntity.Position = Game.Player.Character.Position + ToHit * (TelekinesisModelSize + 0.5f); } else { TelekinesisEntity.Position = Game.Player.Character.Position + Game.Player.Character.ForwardVector * (TelekinesisModelSize + 0.5f); } } // Setting to Ground if (!Game.Player.Character.IsInAir && SuperJumpTimer > 100 && Game.Player.Character.Position.Z > 3 || Game.Player.Character.IsFalling) { Game.Player.Character.Velocity += new Vector3(0, 0, (-Game.Player.Character.HeightAboveGround + 1) * 2); } // Safety Reset if (Game.IsKeyPressed(Keys.D0)) { Game.Player.Character.Position = new Vector3(0, 0, 100); Game.Player.Character.Velocity = Vector3.Zero; TelekinesisEntity = null; } // Last Instructions if (Game.IsKeyPressed(Keys.Space)) { SpaceWasDownLastFrame = true; } else { SpaceWasDownLastFrame = false; } if (Game.IsKeyPressed(Keys.Q)) { WasQPressedLastFrame = true; } else { WasQPressedLastFrame = false; } PerformanceHeavyOperationCooldown--; SuperJumpTimer++; Timer++; } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message + "\n\n\n" + ex.InnerException + "\n\n\n" + ex.StackTrace); } }
private void PerformRaycast(TrackedDeviceEventData eventData, List <RaycastResult> resultAppendList) { if (canvas == null) { return; } if (eventCamera == null) { return; } var ray = eventData.ray; var hitDistance = eventData.maxDistance; if (m_CheckFor3DOcclusion) { var hits = Physics.RaycastAll(ray, hitDistance, m_BlockingMask); if (hits.Length > 0 && hits[0].distance < hitDistance) { hitDistance = hits[0].distance; } } if (m_CheckFor2DOcclusion) { var raycastDistance = hitDistance; var hits = Physics2D.GetRayIntersectionAll(ray, raycastDistance, m_BlockingMask); if (hits.Length > 0 && hits[0].fraction * raycastDistance < hitDistance) { hitDistance = hits[0].fraction * raycastDistance; } } m_RaycastResultsCache.Clear(); SortedRaycastGraphics(canvas, ray, m_RaycastResultsCache); //Now that we have a list of sorted hits, process any extra settings and filters. for (var i = 0; i < m_RaycastResultsCache.Count; i++) { var validHit = true; var hitData = m_RaycastResultsCache[i]; var go = hitData.graphic.gameObject; if (m_IgnoreReversedGraphics) { var forward = ray.direction; var goDirection = go.transform.rotation * Vector3.forward; validHit = Vector3.Dot(forward, goDirection) > 0; } validHit &= hitData.distance < hitDistance; if (validHit) { Transform trans = go.transform; Vector3 transForward = trans.forward; var castResult = new RaycastResult { gameObject = go, module = this, index = resultAppendList.Count, distance = hitData.distance, depth = hitData.graphic.depth, sortingLayer = canvas.sortingLayerID, sortingOrder = canvas.sortingOrder, worldPosition = hitData.worldHitPosition, worldNormal = -transForward }; resultAppendList.Add(castResult); } } }
/// <summary> /// /// </summary> public override void Process() { if (bl_GamePadPointer.Instance == null) { return; } GetPointerData(0, out pointer, true); if (Cursor.lockState == CursorLockMode.None || Cursor.lockState == CursorLockMode.Confined) { cursorLocked = false; } if (!cursorLocked && Cursor.lockState == CursorLockMode.Locked) { cursorLocked = true; if (m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay) { screenPos = bl_GamePadPointer.Instance.Position; } else { screenPos = m_Canvas.worldCamera.WorldToScreenPoint(bl_GamePadPointer.Instance.WorldPosition); } auxVec2.x = screenPos.x; auxVec2.y = screenPos.y; } if (cursorLocked && pointer.pointerDrag) { if (m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay) { draggedObjectPos = pointer.pointerDrag.GetComponent <RectTransform>().anchoredPosition; } else { draggedObjectPos = m_Canvas.worldCamera.WorldToScreenPoint(pointer.pointerDrag.transform.position); } dragLockedPointerPos = new Vector2(auxVec2.x - draggedObjectPos.x, auxVec2.y - draggedObjectPos.y); pointer.position = auxVec2 + dragLockedPointerPos; } else { if (m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay) { screenPos = bl_GamePadPointer.Instance.Position; } else { screenPos = m_Canvas.worldCamera.WorldToScreenPoint(bl_GamePadPointer.Instance.WorldPosition); } auxVec2.x = screenPos.x; auxVec2.y = screenPos.y; pointer.position = auxVec2; } eventSystem.RaycastAll(pointer, this.m_RaycastResultCache); raycastResult = FindFirstRaycast(this.m_RaycastResultCache); pointer.pointerCurrentRaycast = raycastResult; this.ProcessDrag(pointer); this.ProcessMove(pointer); if (raycastResult.distance > maxInteractDistance) { //Debug.Log("Distance too great"); // return; } if (Input.GetKeyDown(submitButton)) { pointer.pressPosition = auxVec2; pointer.clickTime = Time.unscaledTime; pointer.pointerPressRaycast = raycastResult; pointer.eligibleForClick = true; float timeBetweenClicks = pointer.clickTime - lastClickTime; if (timeBetweenClicks > multipleClicksTimeout) { pointer.clickCount = 0; } pointer.clickCount++; lastClickTime = Time.unscaledTime; if (this.m_RaycastResultCache.Count > 0) { pointer.selectedObject = raycastResult.gameObject; pointer.pointerPress = raycastResult.gameObject; pointer.rawPointerPress = raycastResult.gameObject; pointer.pointerDrag = ExecuteEvents.ExecuteHierarchy(raycastResult.gameObject, pointer, ExecuteEvents.pointerDownHandler); dragLockedPointerPos = pointer.position; } else { pointer.selectedObject = null; pointer.pointerPress = null; pointer.rawPointerPress = null; } } else if (Input.GetKeyUp(submitButton)) { pointer.pointerPress = ExecuteEvents.ExecuteHierarchy(raycastResult.gameObject, pointer, ExecuteEvents.submitHandler); pointer.pointerPress = null; pointer.rawPointerPress = null; pointer.pointerDrag = null; pointer.dragging = false; pointer.eligibleForClick = false; } }
private void UpdatePointing(PointerData pointer) { pointer.PointingSource.UpdatePointer(); Ray pointingRay = pointer.PointingSource.Ray; float extent = (pointer.PointingSource.ExtentOverride ?? pointingExtent); IList <LayerMask> prioritizedLayerMasks = (pointer.PointingSource.PrioritizedLayerMasksOverride ?? pointingPrioritizedLayerMasks); LayerMask combinedLayerMasks = GetCombinedLayerMask(prioritizedLayerMasks); RaycastHit? physicsHit; RaycastResult?uiHit; if (prioritizedLayerMasks.Count > 1) { RaycastHit[] hits = Physics.RaycastAll(pointingRay, extent, combinedLayerMasks); physicsHit = TryGetPreferredHit(hits, prioritizedLayerMasks); } else { RaycastHit hit; physicsHit = Physics.Raycast(pointingRay, out hit, extent, combinedLayerMasks) ? (RaycastHit?)hit : null; } if ((pointableCanvases.Count == 0) || (EventSystem.current == null)) { uiHit = null; } else { if (uiRaycastCamera == null) { uiRaycastCamera = new GameObject("UI Raycast Camera").AddComponent <Camera>(); uiRaycastCamera.transform.parent = transform; // Make sure the raycast starts as close the the camera as possible. Ideally, this would // be 0, but as of Unity 5.5, setting it to 0 causes the raycast to complain that the // screen point isn't in the camera's view frustum. uiRaycastCamera.nearClipPlane = 0.01f; // Make sure the camera's pixel rect is large enough for raycasting to work as desired. As // of Unity 5.5, setting it much smaller than this makes the raycast miss things. uiRaycastCamera.pixelRect = new Rect(0, 0, 100, 100); // Don't waste performance rendering anything. uiRaycastCamera.enabled = false; foreach (Canvas canvas in pointableCanvases) { canvas.worldCamera = uiRaycastCamera; } if (uiRaycastPointerData == null) { uiRaycastPointerData = new PointerEventData(EventSystem.current); } Debug.Assert(uiRaycastResults == null); uiRaycastResults = new List <RaycastResult>(); } Debug.Assert(uiRaycastCamera != null); Debug.Assert(uiRaycastPointerData != null); Debug.Assert(uiRaycastResults != null); foreach (Canvas canvas in pointableCanvases) { Debug.Assert(canvas.worldCamera == uiRaycastCamera); } uiRaycastCamera.transform.position = pointingRay.origin; uiRaycastCamera.transform.forward = pointingRay.direction; Clear(uiRaycastPointerData); uiRaycastPointerData.position = new Vector2((uiRaycastCamera.pixelWidth / 2), (uiRaycastCamera.pixelHeight / 2)); uiRaycastResults.Clear(); EventSystem.current.RaycastAll(uiRaycastPointerData, uiRaycastResults); uiHit = TryGetPreferredHit(uiRaycastResults, prioritizedLayerMasks); if (uiHit != null) { RaycastResult patchedUiHit = uiHit.Value; float totalDistance = (patchedUiHit.distance + uiRaycastCamera.nearClipPlane); patchedUiHit.distance = totalDistance; Debug.Assert((patchedUiHit.worldPosition == Vector3.zero), "As of Unity 5.5, UI Raycasts always" + " return worldPosition (0,0,0), so we'll fill it in here with the correct value. If this" + " assertion fires, see what data is available, and consider using it instead of our fill in." ); patchedUiHit.worldPosition = (pointingRay.origin + (totalDistance * pointingRay.direction)); Debug.Assert((patchedUiHit.worldNormal == Vector3.zero), "As of Unity 5.5, UI Raycasts always" + " return worldNormal (0,0,0), so we'll fill it in here with something incorrect, but" + " reasonable. If this assertion fires, see what data is available, and consider using it" + " instead of our fill in." ); patchedUiHit.worldNormal = (-pointingRay.direction); uiHit = patchedUiHit; } } if ((physicsHit != null) && (uiHit != null)) { for (int iMask = 0; iMask < prioritizedLayerMasks.Count; iMask++) { LayerMask mask = prioritizedLayerMasks[iMask]; bool physicsIsInMask = physicsHit.Value.transform.gameObject.IsInLayerMask(mask); bool uiIsInMask = uiHit.Value.gameObject.IsInLayerMask(mask); if (physicsIsInMask && uiIsInMask) { // In the case of tie in priority and distance, we give preference to the UI, // assuming that if people stick UI on top of 3D objects, they probably want // the UI to take the pointer. if (uiHit.Value.distance <= physicsHit.Value.distance) { pointer.UpdateHit(uiHit.Value); break; } else { pointer.UpdateHit(physicsHit.Value); break; } } else if (physicsIsInMask) { pointer.UpdateHit(physicsHit.Value); break; } else if (uiIsInMask) { pointer.UpdateHit(uiHit.Value); break; } else { // Nothing... keep searching for a mask that contains at least one of the hits. } } Debug.Assert(pointer.End.Object != null); } else if (physicsHit != null) { pointer.UpdateHit(physicsHit.Value); } else if (uiHit != null) { pointer.UpdateHit(uiHit.Value); } else { pointer.UpdateHit(extent); } }
private void RaycastResultsHandler(RaycastResult result) { // we just receive ray cast information from physics. Currently we just use // the distance measurement for each impact point reported. However, our simulation // engine also provides you the material properties so you can decide here to simulate // scattering, reflections, noise etc. // Raul - Note the closest intersection. double minDistance = SONAR_RANGE; // Raul - The P3DX Frontal Sonar Ring have 8 SONAR transducers: // Raul - From left to right: S7, S6, S5, S4, S3, S2, S1, and S0. // Raul - Each one has an aperture of 15 degress, and their orientations are: // Raul - -90, -50, -30, -10, +10, +30, +50, +90 degrees respectively. // Raul - Get all distance measurements double[] distances = new double[SonarArrayLength]; for (int i=0; i < SonarArrayLength; i++) { distances[i] = SONAR_RANGE; } // Raul - This code obviously needs to be rewritten: foreach (RaycastImpactPoint pt in result.ImpactPoints) { // LogInfo("Point:" + pt.ReadingIndex + " v:" + pt.Position.W); // Rays corresponding to transducer S7 if (pt.ReadingIndex >= 0 && pt.ReadingIndex <= 15) { if (distances[7] > pt.Position.W * 1000f) { // Get closest intersection in S7 distances[7] = (float)pt.Position.W * 1000f; } } // Rays corresponding to transducer S6 if (pt.ReadingIndex >= 40 && pt.ReadingIndex <= 55) { // the distance to the impact has been pre-calculted from the origin // and it's in the fourth element of the vector if (distances[6] > pt.Position.W * 1000f) { // Get closest intersection in S6 distances[6] = (float)pt.Position.W * 1000f; } } // Rays corresponding to transducer S5 if (pt.ReadingIndex >= 60 && pt.ReadingIndex <= 75) { // the distance to the impact has been pre-calculted from the origin // and it's in the fourth element of the vector if (distances[5] > pt.Position.W * 1000f) { // Get closest intersection in S5 distances[5] = (float)pt.Position.W * 1000f; } } // Rays corresponding to transducer S4 if (pt.ReadingIndex >= 80 && pt.ReadingIndex <= 95) { // the distance to the impact has been pre-calculted from the origin // and it's in the fourth element of the vector if (distances[4] > pt.Position.W * 1000f) { // Get closest intersection in S4 distances[4] = (float)pt.Position.W * 1000f; } } // Rays corresponding to transducer S3 if (pt.ReadingIndex >= 100 && pt.ReadingIndex <= 115) { // the distance to the impact has been pre-calculted from the origin // and it's in the fourth element of the vector if (distances[3] > pt.Position.W * 1000f) { // Get closest intersection in S3 distances[3] = (float)pt.Position.W * 1000f; } } // Rays corresponding to transducer S2 if (pt.ReadingIndex >= 120 && pt.ReadingIndex <= 135) { // the distance to the impact has been pre-calculted from the origin // and it's in the fourth element of the vector if (distances[2] > pt.Position.W * 1000f) { // Get closest intersection in S2 distances[2] = (float)pt.Position.W * 1000f; } } // Rays corresponding to transducer S1 if (pt.ReadingIndex >= 140 && pt.ReadingIndex <= 155) { // the distance to the impact has been pre-calculted from the origin // and it's in the fourth element of the vector if (distances[1] > pt.Position.W * 1000f) { // Get closest intersection in S1 distances[1] = (float)pt.Position.W * 1000f; } } // Rays corresponding to transducer S0 if (pt.ReadingIndex >= 180 && pt.ReadingIndex <= 195) { // the distance to the impact has been pre-calculted from the origin // and it's in the fourth element of the vector if (distances[0] > pt.Position.W * 1000f) { // Get closest intersection in S0 distances[0] = (float)pt.Position.W * 1000f; } } } // Raul - Get minimum distance for (int i = 0; i < SonarArrayLength; i++ ) { if (minDistance > distances[i]) { minDistance = distances[i]; } } // Raul - Build Sonar State pxsonar.SonarState latestResults = new pxsonar.SonarState(); // Distance between former reading and current reading is calculated double distance = 0.0; for (int i = 0; i < SonarArrayLength; i++ ) { _state.DistanceMeasurements[i] = distances[i]; // Calculate distance for each SONAR device distance += Math.Abs(_state.DistanceMeasurements[i] - formerDistanceMeasurements[i]); // Store former reading for subsequent notifications formerDistanceMeasurements[i] = _state.DistanceMeasurements[i]; } // ONLY NOTIFY Significative chanes in sonar readings if (distance > SonarChangeThreshold) { // I am using SonarState.DistanceMeasurement to store the delta // between last and current readings. _state.DistanceMeasurement = distance; _state.MaxDistance = minDistance; // Select the closest intersection. _state.AngularRange = (int)Math.Abs(_entity.RaycastProperties.EndAngle - _entity.RaycastProperties.StartAngle); _state.AngularResolution = _entity.RaycastProperties.AngleIncrement; _state.TimeStamp = DateTime.Now; // send replace message to self pxsonar.Replace replace = new pxsonar.Replace(); // for perf reasons dont set response port, we are just talking to ourself anyway replace.ResponsePort = null; replace.Body = _state; _mainPort.Post(replace); } }
private RaycastResult?TryGetPreferredHit(IList <RaycastResult> semiOrderedHits, IList <LayerMask> prioritizedLayerMasks) { Debug.Assert(semiOrderedHits != null); Debug.Assert(prioritizedLayerMasks != null); RaycastResult?preferred = null; int preferredLayerMaskIndex = int.MaxValue; for (int iHit = 0; iHit < semiOrderedHits.Count; iHit++) { RaycastResult hit = semiOrderedHits[iHit]; int hitLayerMaskIndex = GetLayerMaskIndex(hit.gameObject, prioritizedLayerMasks); // First, prefer by order in priority layer masks list: if ((preferred == null) || (hitLayerMaskIndex < preferredLayerMaskIndex)) { preferred = hit; preferredLayerMaskIndex = hitLayerMaskIndex; continue; } else if (hitLayerMaskIndex > preferredLayerMaskIndex) { continue; } Debug.Assert(hitLayerMaskIndex == preferredLayerMaskIndex); // Then by biggest sorting layer: if (hit.sortingLayer > preferred.Value.sortingLayer) { preferred = hit; preferredLayerMaskIndex = hitLayerMaskIndex; continue; } else if (hit.sortingLayer < preferred.Value.sortingLayer) { continue; } Debug.Assert(hit.sortingLayer == preferred.Value.sortingLayer); // Then by biggest order in layer: if (hit.sortingOrder > preferred.Value.sortingOrder) { preferred = hit; preferredLayerMaskIndex = hitLayerMaskIndex; continue; } else if (hit.sortingOrder < preferred.Value.sortingOrder) { continue; } Debug.Assert(hit.sortingOrder == preferred.Value.sortingOrder); // Then by smallest distance: if (hit.distance < preferred.Value.distance) { preferred = hit; preferredLayerMaskIndex = hitLayerMaskIndex; continue; } else if (hit.distance > preferred.Value.distance) { continue; } Debug.Assert(hit.distance == preferred.Value.distance); // Then by order in hits list, which seems to break the tie correctly for UI layered flat on // the same canvas. By virtue of letting the loop continue here without updating preferred, // we break the tie with the order in hits list. } return(preferred); }
private void MyRayCast(int index, int device_index, Ray ray) { if (index < 0 || device_index < 0) { if (_Ray[index].line != null) { _Ray[index].line.gameObject.SetActive(false); } return; } int mask = _nCollideLayerMask != 0 ? _nCollideLayerMask : -1; RaycastHit[] hits = Physics.RaycastAll(ray, 1000, mask); if (hits.Length <= 0) { if (_Ray[index].spark != null) { _Ray[index].spark.gameObject.SetActive(false); } return; } Vector2 screenpos = new Vector2(-1, -1); GraphicRaycaster grarcater = null; Canvas ca = null; for (int i = 0; i < hits.Length; ++i) { ca = hits[i].collider.transform.GetComponentInChildren <Canvas>(); if (ca != null) { ca.worldCamera = _OVSDKCamera; grarcater = ca.GetComponent <GraphicRaycaster>(); if (grarcater == null) { ca.gameObject.AddComponent <GraphicRaycaster>(); } Vector3 ve = ca.transform.worldToLocalMatrix.MultiplyPoint3x4(hits[i].point); screenpos = Camera.main.WorldToScreenPoint(ca.transform.localToWorldMatrix.MultiplyPoint3x4(ve)); _Ray[index].line.gameObject.SetActive(true); if (index == RAY_MOUSE && _Ray[index].line_renderer != null) { _Ray[index].line_renderer.gameObject.SetActive(false); } if (_Ray[index].spark != null) { _Ray[index].spark.gameObject.SetActive(true); _Ray[index].spark.gameObject.transform.position = hits[i].point; } break; } } if (grarcater == null || ca == null) { return; } _PointerEventData.delta = screenpos - _PointerEventData.position; _PointerEventData.position = screenpos; _PointerEventData.scrollDelta = SteamVR_Controller.Input(device_index).GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad); List <RaycastResult> resultAppendList = new List <RaycastResult>(); grarcater.Raycast(_PointerEventData, resultAppendList); RaycastResult CurrentRaycas = this.FindFristRayCast(resultAppendList); _PointerEventData.pointerCurrentRaycast = CurrentRaycas; }
public override void OnPointerEnter(RaycastResult raycastResultResult, bool isInteractive) { base.OnPointerEnter(raycastResultResult, isInteractive); SetPointerTarget(raycastResultResult.worldPosition, isInteractive); }
/*--------------------------------------------------------------------------------------------*/ public override Vector3 GetNearestWorldPosition(Ray pFromWorldRay, out RaycastResult pRaycast) { return(GetComponent <HoverShape>().GetNearestWorldPosition(pFromWorldRay, out pRaycast)); }
public static void RunUtilityTool(UnturnedPlayer Player, RaycastResult Raycast) { if (Raycast.ParentHasComponent <InteractableCharge>()) { Raycast.TryGetEntity <InteractableCharge>().detonate(Player.CSteamID); } if (Raycast.ParentHasComponent <InteractableFire>()) { var f = Raycast.TryGetEntity <InteractableFire>(); BarricadeManager.ServerSetFireLit(f, !f.isLit); } if (Raycast.ParentHasComponent <InteractableGenerator>()) { var f = Raycast.TryGetEntity <InteractableGenerator>(); BarricadeManager.ServerSetGeneratorPowered(f, !f.isPowered); } if (Raycast.ParentHasComponent <InteractableOven>()) { var f = Raycast.TryGetEntity <InteractableOven>(); BarricadeManager.ServerSetOvenLit(f, !f.isLit); } if (Raycast.ParentHasComponent <InteractableOxygenator>()) { var f = Raycast.TryGetEntity <InteractableOxygenator>(); BarricadeManager.ServerSetOxygenatorPowered(f, !f.isPowered); } if (Raycast.ParentHasComponent <InteractableSafezone>()) { var f = Raycast.TryGetEntity <InteractableSafezone>(); BarricadeManager.ServerSetSafezonePowered(f, !f.isPowered); } if (Raycast.ParentHasComponent <InteractableSpot>()) { var f = Raycast.TryGetEntity <InteractableSpot>(); BarricadeManager.ServerSetSpotPowered(f, !f.isPowered); } if (Raycast.ParentHasComponent <InteractableVehicle>()) { var f = Raycast.TryGetEntity <InteractableVehicle>(); VehicleManager.ServerForcePassengerIntoVehicle(Player.Player, f); } if (Raycast.ParentHasComponent <InteractableBed>()) { var f = Raycast.TryGetEntity <InteractableBed>(); if (f.owner.m_SteamID != 0) { UnturnedChat.Say(Player, "PointTool_Utility_Bed_ShowOwner".Translate($"{AdminToolsPlugin.Instance.GetPlayerName(f.owner.m_SteamID, "Unknown Player")} ({f.owner})")); } else { UnturnedChat.Say(Player, "PointTool_Utility_Bed_NotClaimed".Translate());; } } if (Raycast.ParentHasComponent <InteractableDoor>()) { var f = Raycast.TryGetEntity <InteractableDoor>(); SendOpenDoor(Raycast.BarricadePlant, Raycast.BarricadeX, Raycast.BarricadeY, Raycast.BarricadeIndex, f, Raycast.BarricadeRegion); } if (Raycast.ParentHasComponent <InteractableStorage>()) { InteractableStorage Storage = Raycast.TryGetEntity <InteractableStorage>(); Player.Player.inventory.updateItems(7, Storage.items); Player.Player.inventory.sendStorage(); } }
/// <summary>Perform raycast on the scene.</summary> /// <param name="pointerRay">The ray to use for the operation.</param> /// <param name="radius">The radius of the ray to use when testing for hits.</param> /// <param name="eventData">The event data triggered by any resultant Raycast hits.</param> /// <param name="resultAppendList">The results are appended to this list.</param> /// <returns>Returns `true` if the Raycast has at least one hit, `false` otherwise.</returns> protected override bool PerformRaycast(GvrBasePointer.PointerRay pointerRay, float radius, PointerEventData eventData, List <RaycastResult> resultAppendList) { if (targetCanvas == null) { targetCanvas = GetComponent <Canvas>(); if (targetCanvas == null) { return(false); } } if (eventCamera == null) { return(false); } if (targetCanvas.renderMode != RenderMode.WorldSpace) { Debug.LogError("GvrPointerGraphicRaycaster requires that the canvas renderMode is set " + "to WorldSpace."); return(false); } float hitDistance = float.MaxValue; if (blockingObjects != BlockingObjects.None) { float dist = pointerRay.distance; if (blockingObjects == BlockingObjects.ThreeD || blockingObjects == BlockingObjects.All) { RaycastHit hit; if (Physics.Raycast(pointerRay.ray, out hit, dist, blockingMask)) { hitDistance = hit.distance; } } if (blockingObjects == BlockingObjects.TwoD || blockingObjects == BlockingObjects.All) { RaycastHit2D hit = Physics2D.Raycast(pointerRay.ray.origin, pointerRay.ray.direction, dist, blockingMask); if (hit.collider != null) { hitDistance = hit.fraction * dist; } } } raycastResults.Clear(); Ray finalRay; Raycast(targetCanvas, pointerRay.ray, eventCamera, pointerRay.distance, raycastResults, out finalRay); bool foundHit = false; for (int index = 0; index < raycastResults.Count; index++) { GameObject go = raycastResults[index].gameObject; bool appendGraphic = true; if (ignoreReversedGraphics) { // If we have a camera compare the direction against the cameras forward. Vector3 cameraFoward = eventCamera.transform.rotation * Vector3.forward; Vector3 dir = go.transform.rotation * Vector3.forward; appendGraphic = Vector3.Dot(cameraFoward, dir) > 0; } if (appendGraphic) { float resultDistance = 0; Transform trans = go.transform; Vector3 transForward = trans.forward; // http://geomalgorithms.com/a06-_intersect-2.html float transDot = Vector3.Dot(transForward, trans.position - pointerRay.ray.origin); float rayDot = Vector3.Dot(transForward, pointerRay.ray.direction); resultDistance = transDot / rayDot; Vector3 hitPosition = pointerRay.ray.origin + (pointerRay.ray.direction * resultDistance); // Check to see if the go is behind the camera. if (resultDistance < 0 || resultDistance >= hitDistance || resultDistance > pointerRay.distance) { continue; } resultDistance = resultDistance + pointerRay.distanceFromStart; Transform pointerTransform = GvrPointerInputModule.Pointer.PointerTransform; float delta = (hitPosition - pointerTransform.position).magnitude; if (delta < pointerRay.distanceFromStart) { continue; } RaycastResult castResult = new RaycastResult { gameObject = go, module = this, distance = resultDistance, worldPosition = hitPosition, screenPosition = eventCamera.WorldToScreenPoint(hitPosition), index = resultAppendList.Count, depth = raycastResults[index].depth, sortingLayer = targetCanvas.sortingLayerID, sortingOrder = targetCanvas.sortingOrder }; resultAppendList.Add(castResult); foundHit = true; } } return(foundHit); }
public static void RunJumpTool(UnturnedPlayer Player, RaycastResult Raycast, UnturnedPlayerEvents.PlayerGesture gesture) { if (gesture == UnturnedPlayerEvents.PlayerGesture.PunchRight) { int RunMode = 0; float Pitch = Player.Player.look.pitch; if (Player.Stance == EPlayerStance.STAND) { if (Pitch <= 5) { RunMode = 1; } else if (Pitch >= 175) { RunMode = 2; } else { RunMode = 0; } } else if (Player.Stance == EPlayerStance.CROUCH) { if (Pitch <= 22) { RunMode = 1; } else if (Pitch >= 155) { RunMode = 2; } else { RunMode = 0; } } if (RunMode == 0) { Player.Player.teleportToLocationUnsafe(Raycast.Raycast.point, Player.Rotation); } else if (RunMode == 1) { if (Raycast.Raycast.distance < 300) { Vector3 Target = new Vector3(Raycast.Raycast.point.x, Raycast.Raycast.point.y + (float)1.75, Raycast.Raycast.point.z); Player.Player.teleportToLocationUnsafe(Target, Player.Rotation); } else { Player.Player.teleportToLocationUnsafe(Raycast.Raycast.point, Player.Rotation); } } else if (RunMode == 2) { if (Raycast.Raycast.distance > 300) { Player.Player.teleportToLocationUnsafe(Raycast.Raycast.point, Player.Rotation); } else { Vector3 ViewDir = Vector3.down; RaycastResult FloorCast = RaycastUtility.Raycast(Player.Position, Vector3.down, RayMasks.GROUND, 1500); float DistanceToGround = 9999; if (FloorCast.RaycastHit) { DistanceToGround = FloorCast.Raycast.distance - (float)0.5; } RaycastResult DownCast = RaycastUtility.RaycastAll(new Vector3(Raycast.Raycast.point.x, Raycast.Raycast.point.y - 3, Raycast.Raycast.point.z), ViewDir, 300); bool Cont = !DownCast.RaycastHit; int Displacement = 3; while (Cont) { DownCast = RaycastUtility.RaycastAll(new Vector3(Raycast.Raycast.point.x, Raycast.Raycast.point.y - Displacement, Raycast.Raycast.point.z), ViewDir, 300); Displacement += 3; if (Displacement > 15 || DownCast.RaycastHit) { Cont = false; } } if (DownCast.RaycastHit && DownCast.Raycast.distance != 0 && DownCast.Raycast.distance < DistanceToGround) { Player.Player.teleportToLocationUnsafe(new Vector3(DownCast.Raycast.point.x, DownCast.Raycast.point.y + 0.2f, DownCast.Raycast.point.z), Player.Rotation); } else { UnturnedChat.Say(Player, "PointTool_Jump_NoPlatformBelow".Translate()); } } } } else if (gesture == UnturnedPlayerEvents.PlayerGesture.PunchLeft) { Vector3 TargetPos = Raycast.Raycast.point; float Height = 15; RaycastResult[] Placings = { GetDropPlacement(new Vector3(TargetPos.x, TargetPos.y + Height, TargetPos.z)), GetDropPlacement(new Vector3(TargetPos.x + 1, TargetPos.y + Height, TargetPos.z)), GetDropPlacement(new Vector3(TargetPos.x - 1, TargetPos.y + Height, TargetPos.z)), GetDropPlacement(new Vector3(TargetPos.x, TargetPos.y + Height, TargetPos.z + 1)), GetDropPlacement(new Vector3(TargetPos.x, TargetPos.y + Height, TargetPos.z - 1)) }; Vector3[] Placing = Placings.Where(X => X.RaycastHit && X.Raycast.distance != 0).OrderByDescending(V => V.Raycast.point.y).CastEnumeration(E => E.Raycast.point).ToArray(); if (Placing.Count() != 0) { Player.Player.teleportToLocationUnsafe(new Vector3(Placing[0].x, Placing[0].y + 0.5f, Placing[0].z), Player.Rotation); } else { Player.Player.teleportToLocationUnsafe(new Vector3(TargetPos.x, TargetPos.y + 0.5f, TargetPos.z), Player.Rotation); } } else if (gesture == UnturnedPlayerEvents.PlayerGesture.Point) { Vector3 TargetPos = Raycast.Raycast.point; Vector3 CurrentPos = Player.Position; Vector3 ResultPos = Vector3.MoveTowards(TargetPos, CurrentPos, 1); Player.Player.teleportToLocationUnsafe(new Vector3(ResultPos.x, ResultPos.y + 0.5f, ResultPos.z), Player.Rotation); } }
private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMasks) { GetPointerEventData(); Debug.Assert(pointer.End.Point != Vector3.zero); // 2D pointer position UnityUIPointerEvent.position = CameraCache.Main.WorldToScreenPoint(pointer.End.Point); // Graphics raycast RaycastResult uiRaycastResult = EventSystem.current.Raycast(UnityUIPointerEvent, prioritizedLayerMasks); UnityUIPointerEvent.pointerCurrentRaycast = uiRaycastResult; // If we have a raycast result, check if we need to overwrite the physics raycast info if (uiRaycastResult.gameObject != null) { bool overridePhysicsRaycast = false; if (pointer.End.Object != null) { // Check layer prioritization if (prioritizedLayerMasks.Length > 1) { // Get the index in the prioritized layer masks int uiLayerIndex = uiRaycastResult.gameObject.layer.FindLayerListIndex(prioritizedLayerMasks); int threeDLayerIndex = pointer.LastRaycastHit.collider.gameObject.layer.FindLayerListIndex(prioritizedLayerMasks); if (threeDLayerIndex > uiLayerIndex) { overridePhysicsRaycast = true; } else if (threeDLayerIndex == uiLayerIndex) { if (pointer.LastRaycastHit.distance > uiRaycastResult.distance) { overridePhysicsRaycast = true; } } } else { if (pointer.LastRaycastHit.distance > uiRaycastResult.distance) { overridePhysicsRaycast = true; } } } // Check if we need to overwrite the physics raycast info if (pointer.End.Object == null || overridePhysicsRaycast) { Vector3 worldPos = CameraCache.Main.ScreenToWorldPoint(new Vector3(uiRaycastResult.screenPosition.x, uiRaycastResult.screenPosition.y, uiRaycastResult.distance)); var hitInfo = new RaycastHit { distance = uiRaycastResult.distance, normal = -uiRaycastResult.gameObject.transform.forward, point = worldPos }; pointer.UpdateHit(uiRaycastResult, hitInfo); } } }
public override void OnPointerEnter(RaycastResult raycastResultResult, bool isInteractive) { SetPointerTarget(raycastResultResult.worldPosition, isInteractive); gazedAt = raycastResultResult.gameObject; gazeStartTime = Time.time; }
private void PopulateGameObjectsForTouch(int pointerId, float x, float y) { // Find a game object for a touch id if (EventSystem.current == null) { return; } List <GameObject> list; if (gameObjectsForTouch.TryGetValue(pointerId, out list)) { list.Clear(); } else { list = new List <GameObject>(); gameObjectsForTouch[pointerId] = list; } captureRaycastResults.Clear(); PointerEventData p = new PointerEventData(EventSystem.current); p.Reset(); p.position = new Vector2(x, y); p.clickCount = 1; EventSystem.current.RaycastAll(p, captureRaycastResults); // HACK: Unity doesn't get collider2d on UI element, get those now int hackCount = Physics2D.OverlapPointNonAlloc(p.position, hackResults); for (int i = 0; i < hackCount; i++) { RaycastResult result = new RaycastResult { gameObject = hackResults[i].gameObject }; if (captureRaycastResults.FindIndex((cmp) => { return(cmp.gameObject == result.gameObject); }) < 0) { captureRaycastResults.Add(result); } } System.Array.Clear(hackResults, 0, hackCount); if (captureRaycastResults.Count == 0) { captureRaycastResults.Add(new RaycastResult()); } // determine what game object, if any should capture the gesture foreach (RaycastResult r in captureRaycastResults) { switch (ShouldCaptureGesture(r.gameObject)) { case CaptureResult.ForcePassThrough: list.Clear(); return; case CaptureResult.ForceDenyPassThrough: // unless a platform specific view matches, deny the gesture list.Add(r.gameObject); return; case CaptureResult.Default: list.Add(r.gameObject); break; default: break; } } }
public RaycastResult Raycast(Vector2 rayStart, Vector2 rayEnd) { float closestFraction = float.PositiveInfinity; RaycastResult closestHit = new RaycastResult(); PhysicsWorld.RayCast((Fixture fixture, Vector2 position, Vector2 normal, float fraction) => { Entity hitEntity = null; if (EntityIdByPhysicsBody.ContainsKey(fixture.Body)) { hitEntity = Game.Match.World.EntityById(EntityIdByPhysicsBody[fixture.Body]); } if (fraction < closestFraction) { closestFraction = fraction; closestHit = new RaycastResult { HasHit = true, Position = position, Normal = normal, Entity = hitEntity }; } return fraction; }, rayStart, rayEnd); return closestHit; }
public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList) { if (canvas == null) { return; } var canvasGraphics = GraphicRegistry.GetGraphicsForCanvas(canvas); if (canvasGraphics == null || canvasGraphics.Count == 0) { return; } //是否支持 MultiDisplay int displayIndex; var currentEventCamera = eventCamera; // Propery can call Camera.main, so cache the reference if (canvas.renderMode == RenderMode.ScreenSpaceOverlay || currentEventCamera == null) { displayIndex = canvas.targetDisplay; } else { displayIndex = currentEventCamera.targetDisplay; } var eventPosition = Display.RelativeMouseAt(eventData.position); if (eventPosition != Vector3.zero) { // We support multiple display and display identification based on event position. int eventDisplayIndex = (int)eventPosition.z; // Discard events that are not part of this display so the user does not interact with multiple displays at once. if (eventDisplayIndex != displayIndex) { return; } } else { // The multiple display system is not supported on all platforms, when it is not supported the returned position // will be all zeros so when the returned index is 0 we will default to the event data to be safe. eventPosition = eventData.position; // We dont really know in which display the event occured. We will process the event assuming it occured in our display. } // Convert to view space Vector2 pos; if (currentEventCamera == null) { // Multiple display support only when not the main display. For display 0 the reported // resolution is always the desktops resolution since its part of the display API, // so we use the standard none multiple display method. (case 741751) float w = Screen.width; float h = Screen.height; if (displayIndex > 0 && displayIndex < Display.displays.Length) { w = Display.displays[displayIndex].systemWidth; h = Display.displays[displayIndex].systemHeight; } pos = new Vector2(eventPosition.x / w, eventPosition.y / h); } else { pos = currentEventCamera.ScreenToViewportPoint(eventPosition); } // If it's outside the camera's viewport, do nothing if (pos.x < 0f || pos.x > 1f || pos.y < 0f || pos.y > 1f) { return; } float hitDistance = float.MaxValue; Ray ray = new Ray(); if (currentEventCamera != null) { ray = currentEventCamera.ScreenPointToRay(eventPosition); } //通过两个属性来阻断射线检测部分; if (canvas.renderMode != RenderMode.ScreenSpaceOverlay && blockingObjects != BlockingObjects.None) { float distanceToClipPlane = 100.0f; if (currentEventCamera != null) { float projectionDirection = ray.direction.z; distanceToClipPlane = Mathf.Approximately(0.0f, projectionDirection) ? Mathf.Infinity : Mathf.Abs((currentEventCamera.farClipPlane - currentEventCamera.nearClipPlane) / projectionDirection); } if (blockingObjects == BlockingObjects.ThreeD || blockingObjects == BlockingObjects.All) { if (ReflectionMethodsCache.Singleton.raycast3D != null) { var hits = ReflectionMethodsCache.Singleton.raycast3DAll(ray, distanceToClipPlane, (int)m_BlockingMask); if (hits.Length > 0) { hitDistance = hits[0].distance; } } } if (blockingObjects == BlockingObjects.TwoD || blockingObjects == BlockingObjects.All) { if (ReflectionMethodsCache.Singleton.raycast2D != null) { var hits = ReflectionMethodsCache.Singleton.getRayIntersectionAll(ray, distanceToClipPlane, (int)m_BlockingMask); if (hits.Length > 0) { hitDistance = hits[0].distance; } } } } m_RaycastResults.Clear(); Raycast(canvas, currentEventCamera, eventPosition, canvasGraphics, m_RaycastResults); int totalCount = m_RaycastResults.Count; for (var index = 0; index < totalCount; index++) { var go = m_RaycastResults[index].gameObject; bool appendGraphic = true; //将背向 Camera 的对象过滤掉 if (ignoreReversedGraphics) { //判断当前 Graphic 方向与正方向 Vector3.forward 是否相交 if (currentEventCamera == null) { // If we dont have a camera we know that we should always be facing forward var dir = go.transform.rotation * Vector3.forward; appendGraphic = Vector3.Dot(Vector3.forward, dir) > 0; } else { // If we have a camera compare the direction against the cameras forward. var cameraFoward = currentEventCamera.transform.rotation * Vector3.forward; var dir = go.transform.rotation * Vector3.forward; appendGraphic = Vector3.Dot(cameraFoward, dir) > 0; } } //检测 Distance if (appendGraphic) { float distance = 0; if (currentEventCamera == null || canvas.renderMode == RenderMode.ScreenSpaceOverlay) { distance = 0; } else { Transform trans = go.transform; Vector3 transForward = trans.forward; // http://geomalgorithms.com/a06-_intersect-2.html distance = (Vector3.Dot(transForward, trans.position - ray.origin) / Vector3.Dot(transForward, ray.direction)); // Check to see if the go is behind the camera. if (distance < 0) { continue; } } if (distance >= hitDistance) { continue; } var castResult = new RaycastResult { gameObject = go, module = this, distance = distance, screenPosition = eventPosition, index = resultAppendList.Count, depth = m_RaycastResults[index].depth, sortingLayer = canvas.sortingLayerID, sortingOrder = canvas.sortingOrder }; resultAppendList.Add(castResult); } } }
void Start() { raycaster = null; raycastResult = new RaycastResult(); teleporter = GameObject.FindObjectOfType <Teleporter>(); }
/// Called every frame the user is still pointing at a valid GameObject. This /// can be a 3D or UI element. /// /// The targetObject is the object the user is pointing at. /// The intersectionPosition is where the ray intersected with the targetObject. /// The intersectionRay is the ray that was cast to determine the intersection. public override void OnPointerHover(RaycastResult rayastResult, Ray ray, bool isInteractive) { SetPointerTarget(rayastResult.worldPosition, isInteractive); }
/// @endcond private void CastRay() { Vector2 currentPose = NormalizedCartesianToSpherical(pointer.GetPointerTransform().forward); if (pointerData == null) { pointerData = new PointerEventData(eventSystem); lastPose = currentPose; } // Store the previous raycast result. RaycastResult previousRaycastResult = pointerData.pointerCurrentRaycast; // The initial cast must use the enter radius. if (pointer != null) { pointer.ShouldUseExitRadiusForRaycast = false; } // Cast a ray into the scene pointerData.Reset(); // Set the position to the center of the camera. // This is only necessary if using the built-in Unity raycasters. pointerData.position = GetViewportCenter(); eventSystem.RaycastAll(pointerData, m_RaycastResultCache); RaycastResult raycastResult = FindFirstRaycast(m_RaycastResultCache); // If we were already pointing at an object we must check that object against the exit radius // to make sure we are no longer pointing at it to prevent flicker. if (previousRaycastResult.gameObject != null && raycastResult.gameObject != previousRaycastResult.gameObject) { if (pointer != null) { pointer.ShouldUseExitRadiusForRaycast = true; } m_RaycastResultCache.Clear(); eventSystem.RaycastAll(pointerData, m_RaycastResultCache); RaycastResult firstResult = FindFirstRaycast(m_RaycastResultCache); if (firstResult.gameObject == previousRaycastResult.gameObject) { raycastResult = firstResult; } } if (raycastResult.gameObject != null && raycastResult.worldPosition == Vector3.zero) { raycastResult.worldPosition = GetIntersectionPosition(pointerData.enterEventCamera, raycastResult); } pointerData.pointerCurrentRaycast = raycastResult; // Find the real screen position associated with the raycast // Based on the results of the hit and the state of the pointerData. if (raycastResult.gameObject != null) { pointerData.position = raycastResult.screenPosition; } else { Transform pointerTransform = pointer.GetPointerTransform(); float maxPointerDistance = pointer.GetMaxPointerDistance(); Vector3 pointerPos = pointerTransform.position + (pointerTransform.forward * maxPointerDistance); if (pointerData.pressEventCamera != null) { pointerData.position = pointerData.pressEventCamera.WorldToScreenPoint(pointerPos); } else if (Camera.main != null) { pointerData.position = Camera.main.WorldToScreenPoint(pointerPos); } } m_RaycastResultCache.Clear(); pointerData.delta = currentPose - lastPose; lastPose = currentPose; }
/// Called every frame the user is still pointing at a valid GameObject. This /// can be a 3D or UI element. /// /// **raycastResult** is the hit detection result for the object being pointed at. /// **isInteractive** is true if the object being pointed at is interactive. public abstract void OnPointerHover(RaycastResult raycastResultResult, bool isInteractive);
protected virtual void Hover(VRTK_UIPointer pointer, List <RaycastResult> results) { if (pointer.pointerEventData.pointerEnter != null) { CheckPointerHoverClick(pointer, results); if (!ValidElement(pointer.pointerEventData.pointerEnter)) { pointer.pointerEventData.pointerEnter = null; return; } if (NoValidCollision(pointer, results)) { ExecuteEvents.ExecuteHierarchy(pointer.pointerEventData.pointerEnter, pointer.pointerEventData, ExecuteEvents.pointerExitHandler); pointer.pointerEventData.hovered.Remove(pointer.pointerEventData.pointerEnter); pointer.pointerEventData.pointerEnter = null; } } else { for (int i = 0; i < results.Count; i++) { RaycastResult result = results[i]; if (!ValidElement(result.gameObject)) { continue; } GameObject target = ExecuteEvents.ExecuteHierarchy(result.gameObject, pointer.pointerEventData, ExecuteEvents.pointerEnterHandler); target = (target == null ? result.gameObject : target); if (target != null) { Selectable selectable = target.GetComponent <Selectable>(); if (selectable != null) { Navigation noNavigation = new Navigation(); noNavigation.mode = Navigation.Mode.None; selectable.navigation = noNavigation; } if (pointer.hoveringElement != null && pointer.hoveringElement != target) { pointer.OnUIPointerElementExit(pointer.SetUIPointerEvent(result, null, pointer.hoveringElement)); } pointer.OnUIPointerElementEnter(pointer.SetUIPointerEvent(result, target, pointer.hoveringElement)); pointer.hoveringElement = target; pointer.pointerEventData.pointerCurrentRaycast = result; pointer.pointerEventData.pointerEnter = target; pointer.pointerEventData.hovered.Add(pointer.pointerEventData.pointerEnter); break; } if (result.gameObject != pointer.hoveringElement) { pointer.OnUIPointerElementEnter(pointer.SetUIPointerEvent(result, result.gameObject, pointer.hoveringElement)); } pointer.hoveringElement = result.gameObject; } if (pointer.hoveringElement && results.Count == 0) { pointer.OnUIPointerElementExit(pointer.SetUIPointerEvent(new RaycastResult(), null, pointer.hoveringElement)); pointer.hoveringElement = null; } } }
/// <summary> /// Get screen position of worldPosition contained in this RaycastResult /// </summary> /// <param name="worldPosition"></param> /// <returns></returns> public Vector2 GetScreenPosition(RaycastResult raycastResult) { // In future versions of Uinty RaycastResult will contain screenPosition so this will not be necessary return(eventCamera.WorldToScreenPoint(raycastResult.worldPosition)); }
public void Execute(Entity e) { AIEntity entity = (AIEntity)e; // check point 1 { // check runaway condition (life) if (entity.Ai.IsRunawayCondition()) { entity.Event = eEntityState.Runaway; return; } // check target in sight ( attack range) if (entity.target && entity.target.isAlive) { RaycastResult result = Facade_AI.IsRaycastHit(entity.property, entity.target, entity.property.tb.attackRange); if (result == RaycastResult.FoundTarget) { entity.Ai.agent.isStopped = true; entity.Event = eEntityState.Attack; return; } // blocked sight if (result == RaycastResult.BlockedTarget) { Vector3 distance = entity.property.DistanceFrom(entity.target); if (distance.magnitude < entity.property.tb.chaseRange) { entity.Ai.agent.destination = entity.target.transform.position; entity.Ai.agent.isStopped = false; return; } } } else { ObjectProperty target; if (Facade_AI.DetectTarget(entity.property, entity.property.tb.attackRange, out target)) { // replace target and mode change to attack entity.Ai.agent.isStopped = true; entity.target = target; entity.Event = eEntityState.Attack; return; } } } //check point 2 { // follow target if it is in chaseRange if (entity.target && entity.target.isAlive) { Vector3 distance = entity.property.DistanceFrom(entity.target); if (distance.magnitude < entity.property.tb.chaseRange) { entity.Ai.agent.destination = entity.target.transform.position; return; } } } // check reach them and something if (!entity.Ai.IsMoving) { entity.Event = eEntityState.Wander; } }
public RaycastResult RaycastUntil(List<RaycastCollision> passThroughCollisions, IntegerVector origin, Vector2 direction, int passThroughMask, int haltMask, float range = 100000.0f) { passThroughMask &= ~haltMask; Vector2 d = direction * range; Vector2 chunkD = range <= this.RaycastChunkSize ? d : direction * this.RaycastChunkSize; IntegerVector halfwayPoint = new IntegerVector(chunkD / 2.0f) + origin; IntegerVector rangeVector = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.55f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.55f)); List<IntegerCollider> possibleCollisions = this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), passThroughMask | haltMask); Vector2 positionModifier = Vector2.zero; IntegerVector position = origin; float incX = d.x; float incY = d.y; if (Mathf.Abs(incX) > RAYCAST_MAX_POSITION_INCREMENT || Mathf.Abs(incY) > RAYCAST_MAX_POSITION_INCREMENT) { Vector2 dNormalized = d.normalized * RAYCAST_MAX_POSITION_INCREMENT; incX = dNormalized.x; incY = dNormalized.y; } Vector2 projected = Vector2.zero; Vector2 soFar = Vector2.zero; float dMagnitude = d.magnitude; RaycastResult result = new RaycastResult(); List<IntegerCollider> collided = new List<IntegerCollider>(); bool endReached = false; int chunksSoFar = 1; while (true) { if (soFar.magnitude >= this.RaycastChunkSize * chunksSoFar) { // Recalculate chunk halfwayPoint = new IntegerVector(chunkD / 2.0f) + position; rangeVector = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.55f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.55f)); possibleCollisions = this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), passThroughMask | haltMask); foreach (IntegerCollider collider in collided) possibleCollisions.Remove(collider); ++chunksSoFar; } projected.x += incX; projected.y += incY; if (projected.magnitude > dMagnitude) { incX = d.x - soFar.x; incY = d.y - soFar.y; endReached = true; } positionModifier.x += incX; int move = (int)positionModifier.x; positionModifier.x -= move; int unitDir = Math.Sign(move); while (move != 0) { IntegerVector checkPos = new IntegerVector(position.X + unitDir, position.Y); GameObject collision = this.CollidePointFirst(checkPos, possibleCollisions); if (collision) { IntegerCollider collider = collision.GetComponent<IntegerCollider>(); possibleCollisions.Remove(collider); RaycastCollision hit = new RaycastCollision(); hit.CollidedObject = collision; hit.CollisionPoint = position; hit.CollidedX = true; if (((1 << collision.layer) & passThroughMask) != 0) { passThroughCollisions.Add(hit); collided.Add(collider); } else { result.Collisions = new RaycastCollision[1]; result.Collisions[0] = hit; } } position = checkPos; if (result.Collided) break; move -= unitDir; } if (result.Collided) break; positionModifier.y += incY; move = (int)positionModifier.y; positionModifier.y -= move; unitDir = Math.Sign(move); while (move != 0) { IntegerVector checkPos = new IntegerVector(position.X, position.Y + unitDir); GameObject collision = this.CollidePointFirst(checkPos, possibleCollisions); if (collision) { IntegerCollider collider = collision.GetComponent<IntegerCollider>(); possibleCollisions.Remove(collider); RaycastCollision hit = new RaycastCollision(); hit.CollidedObject = collision; hit.CollisionPoint = position; hit.CollidedY = true; if (((1 << collision.layer) & passThroughMask) != 0) { passThroughCollisions.Add(hit); collided.Add(collider); } else { result.Collisions = new RaycastCollision[1]; result.Collisions[0] = hit; } } position = checkPos; if (result.Collided) break; move -= unitDir; } if (result.Collided || endReached) break; soFar.x = projected.x; soFar.y = projected.y; } result.FarthestPointReached = position; return result; }
public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList) { if (canvas == null) { return; } // Convert to view space Vector2 pos; if (eventCamera == null) { // Removed multiple display support until it supports none native resolutions(case 741751) //int displayIndex = canvas.targetDisplay; float w = Screen.width; float h = Screen.height; //if (Screen.fullScreen && displayIndex < Display.displays.Length) //{ // w = Display.displays[displayIndex].systemWidth; // h = Display.displays[displayIndex].systemHeight; //} pos = new Vector2(eventData.position.x / w, eventData.position.y / h); } else { pos = eventCamera.ScreenToViewportPoint(eventData.position); } // If it's outside the camera's viewport, do nothing if (pos.x < 0f || pos.x > 1f || pos.y < 0f || pos.y > 1f) { return; } float hitDistance = float.MaxValue; Ray ray = new Ray(); if (eventCamera != null) { ray = eventCamera.ScreenPointToRay(eventData.position); } if (canvas.renderMode != RenderMode.ScreenSpaceOverlay && blockingObjects != BlockingObjects.None) { float dist = 100.0f; if (eventCamera != null) { dist = eventCamera.farClipPlane - eventCamera.nearClipPlane; } if (blockingObjects == BlockingObjects.ThreeD || blockingObjects == BlockingObjects.All) { RaycastHit hit; if (Physics.Raycast(ray, out hit, dist, m_BlockingMask)) { hitDistance = hit.distance; } } if (blockingObjects == BlockingObjects.TwoD || blockingObjects == BlockingObjects.All) { RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, dist, m_BlockingMask); if (hit.collider != null) { hitDistance = hit.fraction * dist; } } } m_RaycastResults.Clear(); Raycast(canvas, eventCamera, eventData.position, m_RaycastResults); for (var index = 0; index < m_RaycastResults.Count; index++) { var go = m_RaycastResults[index].gameObject; bool appendGraphic = true; if (ignoreReversedGraphics) { if (eventCamera == null) { // If we dont have a camera we know that we should always be facing forward var dir = go.transform.rotation * Vector3.forward; appendGraphic = Vector3.Dot(Vector3.forward, dir) > 0; } else { // If we have a camera compare the direction against the cameras forward. var cameraFoward = eventCamera.transform.rotation * Vector3.forward; var dir = go.transform.rotation * Vector3.forward; appendGraphic = Vector3.Dot(cameraFoward, dir) > 0; } } if (appendGraphic) { float distance = 0; if (eventCamera == null || canvas.renderMode == RenderMode.ScreenSpaceOverlay) { distance = 0; } else { Transform trans = go.transform; Vector3 transForward = trans.forward; // http://geomalgorithms.com/a06-_intersect-2.html distance = (Vector3.Dot(transForward, trans.position - ray.origin) / Vector3.Dot(transForward, ray.direction)); // Check to see if the go is behind the camera. if (distance < 0) { continue; } } if (distance >= hitDistance) { continue; } var castResult = new RaycastResult { gameObject = go, module = this, distance = distance, screenPosition = eventData.position, index = resultAppendList.Count, depth = m_RaycastResults[index].depth, sortingLayer = canvas.sortingLayerID, sortingOrder = canvas.sortingOrder }; resultAppendList.Add(castResult); } } }
public virtual RaycastResult set_Renamed(RaycastResult argOther) { lambda = argOther.lambda; normal.set_Renamed(argOther.normal); return this; }
public RaycastResult Raycast(Ray ray, uint queryMask) { RaycastResult rr = new RaycastResult(); RaySceneQuery raySceneQuery = this.sceneMgr.CreateRayQuery(new Ray()); raySceneQuery.SetSortByDistance(true); // check we are initialised if (raySceneQuery != null) { // create a query object raySceneQuery.Ray = ray; raySceneQuery.SetSortByDistance(true); raySceneQuery.QueryMask = queryMask; using (RaySceneQueryResult queryResult = raySceneQuery.Execute()) { // execute the query, returns a vector of hits if (queryResult.Count <= 0) { // raycast did not hit an objects bounding box this.sceneMgr.DestroyQuery(raySceneQuery); raySceneQuery.Dispose(); return null; } // at this point we have raycast to a series of different objects bounding boxes. // we need to test these different objects to see which is the first polygon hit. // there are some minor optimizations (distance based) that mean we wont have to // check all of the objects most of the time, but the worst case scenario is that // we need to test every triangle of every object. // Ogre::Real closest_distance = -1.0f; rr.Distance = -1.0f; Vector3 closestResult = Vector3.ZERO; for (int qridx = 0; qridx < queryResult.Count; qridx++) { // stop checking if we have found a raycast hit that is closer // than all remaining entities if (rr.Distance >= 0.0f && rr.Distance < queryResult[qridx].distance) { break; } // only check this result if its a hit against an entity if (queryResult[qridx].movable != null && queryResult[qridx].movable.MovableType == "Entity" && !queryResult[qridx].movable.ParentSceneNode.Name.Contains("tile") ) { // get the entity to check Entity entity = (Entity)queryResult[qridx].movable; // mesh data to retrieve Vector3[] vertices; int[] indices; RenderOperation.OperationTypes opType; // get the mesh information using (MeshPtr mesh = entity.GetMesh()) { opType = mesh.GetSubMesh(0).operationType; Debug.Assert(CheckSubMeshOpType(mesh, opType)); GetMeshInformation( mesh, out vertices, out indices, entity.ParentNode._getDerivedPosition(), entity.ParentNode._getDerivedOrientation(), entity.ParentNode._getDerivedScale()); } int vertexCount = vertices.Length; int indexCount = indices.Length; // test for hitting individual triangles on the mesh bool newClosestFound = false; Pair<bool, float> hit; switch (opType) { case RenderOperation.OperationTypes.OT_TRIANGLE_LIST: for (int i = 0; i < indexCount; i += 3) { hit = Mogre.Math.Intersects(ray, vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]], true, false); if (CheckDistance(rr, hit)) { newClosestFound = true; } } break; case RenderOperation.OperationTypes.OT_TRIANGLE_STRIP: for (int i = 0; i < indexCount - 2; i++) { hit = Mogre.Math.Intersects(ray, vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]], true, true); if (CheckDistance(rr, hit)) { newClosestFound = true; } } break; case RenderOperation.OperationTypes.OT_TRIANGLE_FAN: for (int i = 0; i < indexCount - 2; i++) { hit = Mogre.Math.Intersects(ray, vertices[indices[0]], vertices[indices[i + 1]], vertices[indices[i + 2]], true, true); if (CheckDistance(rr, hit)) { newClosestFound = true; } } break; default: throw new Exception("invalid operation type"); } // if we found a new closest raycast for this object, update the // closest_result before moving on to the next object. if (newClosestFound) { rr.Target = entity; closestResult = ray.GetPoint(rr.Distance); } } } this.sceneMgr.DestroyQuery(raySceneQuery); raySceneQuery.Dispose(); // return the result if (rr.Distance >= 0.0f) { // raycast success rr.Position = closestResult; return rr; } else { return null; } } } else { return null; } }