public static bool Raycast(Ray ray, out float t, Vector3 circleCenter, float circleRadius, Vector3 circleNormal, CircleEpsilon epsilon = new CircleEpsilon())
        {
            t             = 0.0f;
            circleRadius += epsilon.RadiusEps;
            Plane circlePlane = new Plane(circleNormal, circleCenter);

            float rayEnter;

            if (circlePlane.Raycast(ray, out rayEnter) && (ray.GetPoint(rayEnter) - circleCenter).magnitude <= circleRadius)
            {
                t = rayEnter;
                return(true);
            }

            if (epsilon.ExtrudeEps != 0.0f)
            {
                float dot = Vector3Ex.AbsDot(ray.direction, circleNormal);
                if (dot < ExtrudeEpsThreshold.Get)
                {
                    Vector3 cylinderAxisPt0 = circleCenter - circleNormal * epsilon.ExtrudeEps;
                    Vector3 cylinderAxisPt1 = circleCenter + circleNormal * epsilon.ExtrudeEps;
                    return(CylinderMath.Raycast(ray, out t, cylinderAxisPt0, cylinderAxisPt1, circleRadius));
                }
            }

            return(false);
        }
        public static bool RaycastShArc(Ray ray, out float t, Vector3 arcOrigin, Vector3 arcStartPoint, Vector3 arcPlaneNormal, float degreesFromStart, ArcEpsilon epsilon = new ArcEpsilon())
        {
            t = 0.0f;

            degreesFromStart = ConvertToSh3DArcAngle(arcOrigin, arcStartPoint, arcPlaneNormal, degreesFromStart);
            Plane arcPlane = new Plane(arcPlaneNormal, arcOrigin);

            float rayEnter;

            if (arcPlane.Raycast(ray, out rayEnter) &&
                ShArcContains3DPoint(ray.GetPoint(rayEnter), false, arcOrigin,
                                     arcStartPoint, arcPlaneNormal, degreesFromStart, epsilon))
            {
                t = rayEnter;
                return(true);
            }

            if (epsilon.ExtrudeEps != 0.0f)
            {
                float dot = Vector3Ex.AbsDot(ray.direction, arcPlaneNormal);
                if (dot < ExtrudeEpsThreshold.Get)
                {
                    OBB arcOBB = CalcSh3DArcOBB(arcOrigin, arcStartPoint, arcPlaneNormal, degreesFromStart, epsilon);
                    return(BoxMath.Raycast(ray, arcOBB.Center, arcOBB.Size, arcOBB.Rotation));
                }
            }

            return(false);
        }
        public static bool RaycastWire(Ray ray, out float t, Vector3 circleCenter, float circleRadius, Vector3 circleNormal, CircleEpsilon epsilon = new CircleEpsilon())
        {
            t = 0.0f;
            Plane circlePlane = new Plane(circleNormal, circleCenter);

            float rayEnter;

            if (circlePlane.Raycast(ray, out rayEnter))
            {
                Vector3 intersectPt    = ray.GetPoint(rayEnter);
                float   distFromOrigin = (circleCenter - intersectPt).magnitude;

                if (distFromOrigin >= circleRadius - epsilon.WireEps &&
                    distFromOrigin <= circleRadius + epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }
            }

            if (epsilon.ExtrudeEps != 0.0f)
            {
                float dot = Vector3Ex.AbsDot(ray.direction, circleNormal);
                if (dot < ExtrudeEpsThreshold.Get)
                {
                    Vector3 cylinderAxisPt0 = circleCenter - circleNormal * epsilon.ExtrudeEps;
                    Vector3 cylinderAxisPt1 = circleCenter + circleNormal * epsilon.ExtrudeEps;
                    return(CylinderMath.Raycast(ray, out t, cylinderAxisPt0, cylinderAxisPt1, circleRadius + epsilon.WireEps));
                }
            }

            return(false);
        }
        public override void Render(Camera camera)
        {
            var sgLookAndFeel = _sceneGizmo.LookAndFeel;
            RTSceneGizmoCamera sceneGizmoCamera = _sceneGizmo.SceneGizmoCamera;

            _cap.Render(camera);

            if (_axisDesc.IsPositive)
            {
                GizmoLabelMaterial labelMaterial = GizmoLabelMaterial.Get;
                labelMaterial.SetZWriteEnabled(false);
                labelMaterial.SetZTestLessEqual();
                labelMaterial.SetColor(ColorEx.KeepAllButAlpha(sgLookAndFeel.AxesLabelTint, _color.Value.a));
                labelMaterial.SetTexture(_labelTexture);
                labelMaterial.SetPass(0);

                Vector3 gizmoAxis  = _sceneGizmo.Gizmo.Transform.GetAxis3D(_axisDesc);
                Vector3 labelScale = Vector3Ex.FromValue(sgLookAndFeel.GetAxesLabelWorldSize(sceneGizmoCamera.Camera, _cap.Position));
                Vector3 labelPos   = _cap.Position + gizmoAxis * (labelScale.x * 0.5f);

                Vector2 labelScreenPos   = sceneGizmoCamera.Camera.WorldToScreenPoint(labelPos);
                Vector2 midAxisScreenPos = sceneGizmoCamera.Camera.WorldToScreenPoint(_sceneGizmo.SceneGizmoCamera.LookAtPoint);
                Vector2 labelScreenDir   = (labelScreenPos - midAxisScreenPos).normalized;

                float absDotCamLook = Mathf.Abs(Vector3Ex.AbsDot(sceneGizmoCamera.Look, gizmoAxis));
                labelScreenPos = labelScreenPos + Vector2.Scale(labelScreenDir, Vector2Ex.FromValue(SceneGizmoLookAndFeel.AxisLabelScreenSize)) * absDotCamLook;
                labelPos       = sceneGizmoCamera.Camera.ScreenToWorldPoint(new Vector3(labelScreenPos.x, labelScreenPos.y, (labelPos - sceneGizmoCamera.WorldPosition).magnitude));

                Quaternion labelRotation     = Quaternion.LookRotation(sceneGizmoCamera.Look, sceneGizmoCamera.Up);
                Matrix4x4  labelTransformMtx = Matrix4x4.TRS(labelPos, labelRotation, labelScale);

                Graphics.DrawMeshNow(MeshPool.Get.UnitQuadXY, labelTransformMtx);
            }
        }
Beispiel #5
0
        public static bool Raycast(Ray ray, out float t, Vector3 quadCenter, float quadWidth, float quadHeight, Vector3 quadRight, Vector3 quadUp, QuadEpsilon epsilon = new QuadEpsilon())
        {
            t = 0.0f;
            Vector3 quadNormal = Vector3.Normalize(Vector3.Cross(quadRight, quadUp));
            Plane   quadPlane  = new Plane(quadNormal, quadCenter);

            float rayEnter;

            if (quadPlane.Raycast(ray, out rayEnter) &&
                Contains3DPoint(ray.GetPoint(rayEnter), false, quadCenter, quadWidth, quadHeight, quadRight, quadUp, epsilon))
            {
                t = rayEnter;
                return(true);
            }

            if (epsilon.ExtrudeEps != 0.0f)
            {
                float dot = Vector3Ex.AbsDot(ray.direction, quadPlane.normal);
                if (dot < ExtrudeEpsThreshold.Get)
                {
                    OBB quadOBB = Calc3DQuadOBB(quadCenter, new Vector2(quadWidth, quadHeight), Quaternion.LookRotation(quadNormal, quadUp), epsilon);
                    return(BoxMath.Raycast(ray, quadOBB.Center, quadOBB.Size, quadOBB.Rotation));
                }
            }

            return(false);
        }
        public static bool Raycast(Ray ray, out float t, Vector3 p0, Vector3 p1, Vector3 p2, TriangleEpsilon epsilon = new TriangleEpsilon())
        {
            t = 0.0f;

            float rayEnter;
            Plane trianglePlane = new Plane(p0, p1, p2);

            if (trianglePlane.Raycast(ray, out rayEnter) &&
                Contains3DPoint(ray.GetPoint(rayEnter), false, p0, p1, p2, epsilon))
            {
                t = rayEnter;
                return(true);
            }

            if (epsilon.ExtrudeEps != 0.0f)
            {
                float dot = Vector3Ex.AbsDot(ray.direction, trianglePlane.normal);
                if (dot < ExtrudeEpsThreshold.Get)
                {
                    OBB obb = Calc3DTriangleOBB(p0, p1, p2, trianglePlane.normal, epsilon);
                    return(BoxMath.Raycast(ray, obb.Center, obb.Size, obb.Rotation));
                }
            }

            return(false);
        }
Beispiel #7
0
        public override float GetRealSizeAlongDirection(Vector3 direction, float zoomFactor)
        {
            var     slider         = _data.Slider;
            float   cylinderLength = slider.GetRealLength(zoomFactor);
            float   cylinderRadius = slider.GetRealCylinderRadius(zoomFactor);
            Vector3 size           = _data.Cylinder.Rotation * new Vector3(cylinderRadius * 2.0f, cylinderLength, cylinderRadius * 2.0f);

            return(Vector3Ex.AbsDot(direction, size));
        }
Beispiel #8
0
        public static bool RaycastWire(Ray ray, out float t, Vector3 quadCenter, float quadWidth, float quadHeight, Vector3 quadRight, Vector3 quadUp, QuadEpsilon epsilon = new QuadEpsilon())
        {
            t = 0.0f;
            Vector3    quadNormal   = Vector3.Normalize(Vector3.Cross(quadRight, quadUp));
            Plane      quadPlane    = new Plane(quadNormal, quadCenter);
            Vector2    quadSize     = new Vector2(quadWidth, quadHeight);
            Quaternion quadRotation = Quaternion.LookRotation(quadNormal, quadUp);

            float rayEnter;

            if (quadPlane.Raycast(ray, out rayEnter))
            {
                Vector3 intersectPt  = ray.GetPoint(rayEnter);
                var     cornerPoints = Calc3DQuadCornerPoints(quadCenter, quadSize, quadRotation);

                float distFromSegment = intersectPt.GetDistanceToSegment(cornerPoints[(int)QuadCorner.TopLeft], cornerPoints[(int)QuadCorner.TopRight]);
                if (distFromSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }

                distFromSegment = intersectPt.GetDistanceToSegment(cornerPoints[(int)QuadCorner.TopRight], cornerPoints[(int)QuadCorner.BottomRight]);
                if (distFromSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }

                distFromSegment = intersectPt.GetDistanceToSegment(cornerPoints[(int)QuadCorner.BottomRight], cornerPoints[(int)QuadCorner.BottomLeft]);
                if (distFromSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }

                distFromSegment = intersectPt.GetDistanceToSegment(cornerPoints[(int)QuadCorner.BottomLeft], cornerPoints[(int)QuadCorner.TopLeft]);
                if (distFromSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }
            }

            if (epsilon.ExtrudeEps != 0.0f)
            {
                float dot = Vector3Ex.AbsDot(ray.direction, quadPlane.normal);
                if (dot < ExtrudeEpsThreshold.Get)
                {
                    OBB quadOBB = Calc3DQuadOBB(quadCenter, quadSize, Quaternion.LookRotation(quadNormal, quadUp), epsilon);
                    return(BoxMath.Raycast(ray, quadOBB.Center, quadOBB.Size, quadOBB.Rotation));
                }
            }

            return(false);
        }
        public override float GetRealSizeAlongDirection(Vector3 direction, float zoomFactor)
        {
            direction.Normalize();

            var     slider    = _data.Slider;
            float   boxWidth  = slider.GetRealLength(zoomFactor);
            float   boxHeight = slider.GetRealBoxHeight(zoomFactor);
            float   boxDepth  = slider.GetRealBoxDepth(zoomFactor);
            Vector3 size      = _data.Box.Rotation * new Vector3(boxWidth, boxHeight, boxDepth);

            return(Vector3Ex.AbsDot(direction, size));
        }
        private void UpdateColor()
        {
            var   sgLookAndFeel    = _sceneGizmo.LookAndFeel;
            Color lookAndFeelColor = sgLookAndFeel.GetAxisCapColor(_axisDesc.Index, _axisDesc.Sign);

            if (_cap.IsHovered)
            {
                lookAndFeelColor = sgLookAndFeel.HoveredColor;
            }
            ColorTransition.State ctState = _colorTransition.TransitionState;

            Vector3 axis      = _sceneGizmo.Gizmo.Transform.GetAxis3D(_axisDesc);
            float   alignment = Vector3Ex.AbsDot(axis, _sceneGizmo.SceneGizmoCamera.Look);

            if (alignment > SceneGizmoLookAndFeel.AxisCamAlignFadeOutThreshold)
            {
                if (ctState != ColorTransition.State.CompleteFadeOut &&
                    ctState != ColorTransition.State.FadingOut)
                {
                    _colorTransition.DurationInSeconds = SceneGizmoLookAndFeel.AxisCamAlignFadeOutDuration;
                    _colorTransition.FadeOutColor      = ColorEx.KeepAllButAlpha(lookAndFeelColor, SceneGizmoLookAndFeel.AxisCamAlignFadeOutAlpha);
                    _colorTransition.BeginFadeOut(true);
                }
            }
            else
            {
                if (ctState != ColorTransition.State.FadingIn &&
                    ctState != ColorTransition.State.CompleteFadeIn &&
                    ctState != ColorTransition.State.Ready)
                {
                    _colorTransition.DurationInSeconds = SceneGizmoLookAndFeel.AxisCamAlignFadeOutDuration;
                    _colorTransition.FadeInColor       = lookAndFeelColor;
                    _colorTransition.BeginFadeIn(true);
                }
                else
                {
                    _color.Value = lookAndFeelColor;
                }
            }

            _colorTransition.Update(Time.deltaTime);
            _cap.OverrideColor.IsActive = true;
            _cap.OverrideColor.Color    = _color.Value;
        }
        public static bool RaycastWire(Ray ray, out float t, Vector3 p0, Vector3 p1, Vector3 p2, TriangleEpsilon epsilon = new TriangleEpsilon())
        {
            t = 0.0f;

            float rayEnter;
            Plane trianglePlane = new Plane(p0, p1, p2);

            if (trianglePlane.Raycast(ray, out rayEnter))
            {
                Vector3 intersectPt   = ray.GetPoint(rayEnter);
                float   distToSegment = intersectPt.GetDistanceToSegment(p0, p1);
                if (distToSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }

                distToSegment = intersectPt.GetDistanceToSegment(p1, p2);
                if (distToSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }

                distToSegment = intersectPt.GetDistanceToSegment(p2, p0);
                if (distToSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }
            }

            if (epsilon.ExtrudeEps != 0.0f)
            {
                float dot = Vector3Ex.AbsDot(ray.direction, trianglePlane.normal);
                if (dot < ExtrudeEpsThreshold.Get)
                {
                    OBB obb = Calc3DTriangleOBB(p0, p1, p2, trianglePlane.normal, epsilon);
                    return(BoxMath.Raycast(ray, obb.Center, obb.Size, obb.Rotation));
                }
            }

            return(false);
        }
        public static OBB CalcSh3DArcOBB(Vector3 arcOrigin, Vector3 arcStartPoint, Vector3 arcPlaneNormal, float degreesFromStart, ArcEpsilon epsilon = new ArcEpsilon())
        {
            degreesFromStart = ConvertToSh3DArcAngle(arcOrigin, arcStartPoint, arcPlaneNormal, degreesFromStart);

            Vector3 toStartPt     = (arcStartPoint - arcOrigin);
            Vector3 toMidBorderPt = Quaternion.AngleAxis(degreesFromStart * 0.5f, arcPlaneNormal) * toStartPt;
            Vector3 midBorderPt   = arcOrigin + toMidBorderPt;

            Quaternion obbRotation = Quaternion.LookRotation(toMidBorderPt.normalized, arcPlaneNormal);
            Vector3    obbCenter   = arcOrigin + toMidBorderPt * 0.5f;
            OBB        obb         = new OBB(obbCenter, obbRotation);

            Vector3 toEndPt    = Quaternion.AngleAxis(degreesFromStart, arcPlaneNormal) * toStartPt;
            float   sizeEpsAdd = 2.0f * epsilon.AreaEps;
            float   obbWidth   = Vector3Ex.AbsDot(toEndPt, obb.Right) + Vector3Ex.AbsDot(toStartPt, obb.Right) + sizeEpsAdd;
            float   obbDepth   = midBorderPt.magnitude + sizeEpsAdd;
            float   obbHeight  = epsilon.ExtrudeEps * 2.0f;

            obb.Size = new Vector3(obbWidth, obbHeight, obbDepth);

            return(obb);
        }
Beispiel #13
0
        public override float GetRealSizeAlongDirection(Vector3 direction, float zoomFactor)
        {
            var slider = _data.Slider;

            return(Vector3Ex.AbsDot(direction, slider.Direction * slider.GetRealLength(zoomFactor)));
        }
Beispiel #14
0
 public float GetSizeAlongDirection(Vector3 direction)
 {
     return(Vector3Ex.AbsDot(direction, _rotation * _size));
 }