Ejemplo n.º 1
0
        public override void RenderArea(Camera camera)
        {
            var     cornerPoints = QuadMath.Calc2DQuadCornerPoints(_center, _size, _rotationDegrees);
            Vector2 topLeft      = cornerPoints[(int)QuadCorner.TopLeft];

            cornerPoints.RemoveAt((int)QuadCorner.TopLeft);

            GLRenderer.DrawTriangleFan2D(topLeft, cornerPoints, camera);
        }
Ejemplo n.º 2
0
 public override bool Raycast(Ray ray, out float t)
 {
     if (_raycastMode == Shape3DRaycastMode.Solid)
     {
         return(QuadMath.Raycast(ray, out t, _center, _size.x, _size.y, Right, Up, _epsilon));
     }
     else
     {
         return(RaycastWire(ray, out t));
     }
 }
Ejemplo n.º 3
0
 public override bool ContainsPoint(Vector2 point)
 {
     if (_ptContainMode == Shape2DPtContainMode.InsideArea)
     {
         return(QuadMath.Contains2DPoint(point, _center, _size.x, _size.y, Right, Up, _epsilon));
     }
     else
     {
         return(QuadMath.Is2DPointOnBorder(point, _center, _size.x, _size.y, Right, Up, _epsilon));
     }
 }
Ejemplo n.º 4
0
        public static bool Raycast(Ray ray, out float t, Vector3 baseCenter, float baseWidth, float baseDepth, float height, Quaternion rotation)
        {
            t   = 0.0f;
            ray = ray.InverseTransform(Matrix4x4.TRS(baseCenter, rotation, Vector3.one));

            Vector3 aabbSize = new Vector3(baseWidth, height, baseDepth);

            if (!BoxMath.Raycast(ray, Vector3.up * height * 0.5f, aabbSize, Quaternion.identity))
            {
                return(false);
            }

            List <float> tValues = new List <float>(5);

            Plane basePlane = new Plane(Vector3.up, Vector3.zero);
            float rayEnter  = 0.0f;

            if (basePlane.Raycast(ray, out rayEnter) &&
                QuadMath.Contains3DPoint(ray.GetPoint(rayEnter), false, baseCenter, baseWidth, baseDepth, Vector3.right, Vector3.forward))
            {
                tValues.Add(rayEnter);
            }

            float   halfWidth     = 0.5f * baseWidth;
            float   halfDepth     = 0.5f * baseDepth;
            Vector3 tipPosition   = Vector3.up * height;
            Vector3 p0            = tipPosition;
            Vector3 p1            = Vector3.right * halfWidth - Vector3.forward * halfDepth;
            Vector3 p2            = p1 - Vector3.right * baseWidth;
            Plane   trianglePlane = new Plane(p0, p1, p2);

            if (trianglePlane.Raycast(ray, out rayEnter) &&
                TriangleMath.Contains3DPoint(ray.GetPoint(rayEnter), false, p0, p1, p2))
            {
                tValues.Add(rayEnter);
            }

            p0            = tipPosition;
            p1            = Vector3.right * halfWidth + Vector3.forward * halfDepth;
            p2            = p1 - Vector3.forward * baseDepth;
            trianglePlane = new Plane(p0, p1, p2);
            if (trianglePlane.Raycast(ray, out rayEnter) &&
                TriangleMath.Contains3DPoint(ray.GetPoint(rayEnter), false, p0, p1, p2))
            {
                tValues.Add(rayEnter);
            }

            p0            = tipPosition;
            p1            = -Vector3.right * halfWidth + Vector3.forward * halfDepth;
            p2            = p1 + Vector3.right * baseWidth;
            trianglePlane = new Plane(p0, p1, p2);
            if (trianglePlane.Raycast(ray, out rayEnter) &&
                TriangleMath.Contains3DPoint(ray.GetPoint(rayEnter), false, p0, p1, p2))
            {
                tValues.Add(rayEnter);
            }

            p0            = tipPosition;
            p1            = -Vector3.right * halfWidth - Vector3.forward * halfDepth;
            p2            = p1 + Vector3.forward * baseDepth;
            trianglePlane = new Plane(p0, p1, p2);
            if (trianglePlane.Raycast(ray, out rayEnter) &&
                TriangleMath.Contains3DPoint(ray.GetPoint(rayEnter), false, p0, p1, p2))
            {
                tValues.Add(rayEnter);
            }

            if (tValues.Count == 0)
            {
                return(false);
            }

            tValues.Sort(delegate(float t0, float t1) { return(t0.CompareTo(t1)); });
            t = tValues[0];

            return(true);
        }
Ejemplo n.º 5
0
        public static bool Raycast(Ray ray, out float t, Vector3 boxCenter, Vector3 boxSize, Quaternion boxRotation, BoxEpsilon epsilon = new BoxEpsilon())
        {
            t        = 0.0f;
            boxSize += epsilon.SizeEps;

            const float minBoxSize         = 1e-6f;
            int         invalidSizeCounter = 0;

            if (boxSize.x < minBoxSize)
            {
                ++invalidSizeCounter;
            }
            if (boxSize.y < minBoxSize)
            {
                ++invalidSizeCounter;
            }
            if (boxSize.z < minBoxSize)
            {
                ++invalidSizeCounter;
            }
            if (invalidSizeCounter > 1)
            {
                return(false);
            }

            if (invalidSizeCounter == 1)
            {
                if (boxSize.x < minBoxSize)
                {
                    Vector3 quadRight = boxRotation * Vector3.forward;
                    Vector3 quadUp    = boxRotation * Vector3.up;
                    return(QuadMath.Raycast(ray, out t, boxCenter, boxSize.z, boxSize.y, quadRight, quadUp));
                }
                else
                if (boxSize.y < minBoxSize)
                {
                    Vector3 quadRight = boxRotation * Vector3.right;
                    Vector3 quadUp    = boxRotation * Vector3.forward;
                    return(QuadMath.Raycast(ray, out t, boxCenter, boxSize.x, boxSize.z, quadRight, quadUp));
                }
                else
                {
                    Vector3 quadRight = boxRotation * Vector3.right;
                    Vector3 quadUp    = boxRotation * Vector3.up;
                    return(QuadMath.Raycast(ray, out t, boxCenter, boxSize.x, boxSize.y, quadRight, quadUp));
                }
            }

            Matrix4x4 boxMatrix = Matrix4x4.TRS(boxCenter, boxRotation, boxSize);
            Ray       modelRay  = ray.InverseTransform(boxMatrix);

            if (modelRay.direction.sqrMagnitude == 0.0f)
            {
                return(false);
            }

            Bounds unitCube = new Bounds(Vector3.zero, Vector3.one);

            if (unitCube.IntersectRay(modelRay, out t))
            {
                Vector3 intersectPt = boxMatrix.MultiplyPoint(modelRay.GetPoint(t));
                t = (intersectPt - ray.origin).magnitude;
                return(true);
            }

            return(false);

            /*t = 0.0f;
             * boxSize += epsilon.SizeEps;
             *
             * Matrix4x4 boxMatrix = Matrix4x4.TRS(boxCenter, boxRotation, boxSize);
             * Ray modelRay = ray.InverseTransform(boxMatrix);
             * if (modelRay.direction.sqrMagnitude == 0.0f) return false;
             *
             * Vector3 rayOrigin = modelRay.origin;
             * Vector3 rayDirection = modelRay.direction;
             * Vector3 rayDirectionRec = new Vector3(1.0f / rayDirection.x, 1.0f / rayDirection.y, 1.0f / rayDirection.z);
             *
             * float halfBoxWidth = 0.5f;
             * float halfBoxHeight = 0.5f;
             * float halfBoxDepth = 0.5f;
             *
             * Vector3 boxExtentsMin = new Vector3(-halfBoxWidth, -halfBoxHeight, -halfBoxDepth);
             * Vector3 boxExtentsMax = new Vector3(halfBoxWidth, halfBoxHeight, halfBoxDepth);
             *
             * float highestMinimumT = float.MinValue;
             * float lowestMaximumT = float.MaxValue;
             *
             * for (int slabIndex = 0; slabIndex < 3; ++slabIndex)
             * {
             *  if (Mathf.Abs(rayDirection[slabIndex]) > 1e-4f)
             *  {
             *      float minimumT = (boxExtentsMin[slabIndex] - rayOrigin[slabIndex]) * rayDirectionRec[slabIndex];
             *      float maximumT = (boxExtentsMax[slabIndex] - rayOrigin[slabIndex]) * rayDirectionRec[slabIndex];
             *
             *      if (minimumT > maximumT)
             *      {
             *          float temp = minimumT;
             *          minimumT = maximumT;
             *          maximumT = temp;
             *      }
             *
             *      if (minimumT > highestMinimumT) highestMinimumT = minimumT;
             *      if (maximumT < lowestMaximumT) lowestMaximumT = maximumT;
             *
             *      if (highestMinimumT > lowestMaximumT) return false;
             *      if (lowestMaximumT < 0.0f) return false;
             *  }
             *  else
             *  {
             *      if (rayOrigin[slabIndex] < boxExtentsMin[slabIndex] ||
             *          rayOrigin[slabIndex] > boxExtentsMax[slabIndex]) return false;
             *  }
             * }
             *
             * t = highestMinimumT;
             * if (t < 0.0f) t = lowestMaximumT;
             *
             * return true;*/
        }
Ejemplo n.º 6
0
 public List <Vector3> GetCorners()
 {
     return(QuadMath.Calc3DQuadCornerPoints(_center, _size, _rotation));
 }
Ejemplo n.º 7
0
 public bool ContainsPoint(Vector3 point, bool checkOnPlane)
 {
     return(QuadMath.Contains3DPoint(point, checkOnPlane, _center, _size.x, _size.y, Right, Up, _epsilon));
 }
Ejemplo n.º 8
0
 public override bool RaycastWire(Ray ray, out float t)
 {
     return(QuadMath.RaycastWire(ray, out t, _center, _size.x, _size.y, Right, Up, _epsilon));
 }
Ejemplo n.º 9
0
 public Vector3 GetCornerPosition(QuadCorner quadCorner)
 {
     return(QuadMath.Calc3DQuadCorner(_center, _size, _rotation, quadCorner));
 }
Ejemplo n.º 10
0
        public override Rect GetEncapsulatingRect()
        {
            var cornerPoints = QuadMath.Calc2DQuadCornerPoints(_center, _size, _rotationDegrees);

            return(RectEx.FromPoints(cornerPoints));
        }
Ejemplo n.º 11
0
        public override void RenderBorder(Camera camera)
        {
            var cornerPoints = QuadMath.Calc2DQuadCornerPoints(_center, _size, _rotationDegrees);

            GLRenderer.DrawLineLoop2D(cornerPoints, camera);
        }
Ejemplo n.º 12
0
 public override Rect GetEncapsulatingRect()
 {
     System.Collections.Generic.List <Vector2> cornerPoints = QuadMath.Calc2DQuadCornerPoints(_center, _size, _rotationDegrees);
     return(RectEx.FromPoints(cornerPoints));
 }
Ejemplo n.º 13
0
 public override void RenderBorder(Camera camera)
 {
     System.Collections.Generic.List <Vector2> cornerPoints = QuadMath.Calc2DQuadCornerPoints(_center, _size, _rotationDegrees);
     GLRenderer.DrawLineLoop2D(cornerPoints, camera);
 }