Esempio n. 1
0
        public override void GetClipPlanes(ref ClipPlanes clipPlanes, Transform clipPlaneTransform)
        {
#if UNITY_EDITOR
            if (m_transform == null)
            {
                m_transform = transform;
            }
#endif
            var x = m_transform.right;
            var y = m_transform.up;
            var z = m_transform.forward;
            var o = m_transform.position;
            if (m_isOrthographic)
            {
                clipPlanes.SetClipPlaneNum(3, 2, true);
                var xSize = m_orthographicSize * m_aspectRatio;
                var ySize = m_orthographicSize;
                clipPlanes.clipPlanes[0]           = new Plane(x, o);
                clipPlanes.clipPlanes[0].distance += xSize;
                clipPlanes.maxDistance[0]          = 2 * xSize;
                clipPlanes.clipPlanes[1]           = new Plane(y, o);
                clipPlanes.clipPlanes[1].distance += ySize;
                clipPlanes.maxDistance[1]          = 2 * ySize;
                clipPlanes.clipPlanes[2]           = new Plane(z, o);
                clipPlanes.clipPlanes[2].distance -= m_nearClipPlane;
                clipPlanes.maxDistance[2]          = m_farClipPlane - m_nearClipPlane;
            }
            else
            {
                clipPlanes.SetClipPlaneNum(5, 4, false);
                var ySize = Mathf.Tan(0.5f * Mathf.Deg2Rad * m_fieldOfView);
                var xSize = ySize * m_aspectRatio;
                var x0    = (x + xSize * z).normalized;
                var x1    = (-x + xSize * z).normalized;
                var y0    = (y + ySize * z).normalized;
                var y1    = (-y + ySize * z).normalized;
                clipPlanes.clipPlanes[0]           = new Plane(x0, o);
                clipPlanes.clipPlanes[1]           = new Plane(y0, o);
                clipPlanes.clipPlanes[2]           = new Plane(x1, o);
                clipPlanes.clipPlanes[3]           = new Plane(y1, o);
                clipPlanes.clipPlanes[4]           = new Plane(-z, o);
                clipPlanes.clipPlanes[4].distance += m_farClipPlane;
                clipPlanes.maxDistance[0]          = clipPlanes.maxDistance[2] = 2.0f * xSize * m_farClipPlane;
                clipPlanes.maxDistance[1]          = clipPlanes.maxDistance[3] = 2.0f * ySize * m_farClipPlane;
                clipPlanes.maxDistance[4]          = m_farClipPlane - m_nearClipPlane;
            }
            if (clipPlaneTransform != null)
            {
                var     m = clipPlaneTransform.localToWorldMatrix.transpose;
                Vector3 t = m.GetRow(3);
                for (var i = 0; i < clipPlanes.clipPlaneCount; ++i)
                {
                    var d = Vector3.Dot(clipPlanes.clipPlanes[i].normal, t);
                    clipPlanes.clipPlanes[i].distance += d;
                    clipPlanes.clipPlanes[i].normal    = m.MultiplyVector(clipPlanes.clipPlanes[i].normal);
                }
            }
        }
Esempio n. 2
0
        public override void GetClipPlanes(ref ClipPlanes clipPlanes,
                                           Transform clipPlaneTransform,
                                           ITransformPredictor predictor)
        {
            Vector3 z;
            var     moveBounds = predictor.PredictNextFramePositionChanges();

            if (m_light.type == LightType.Directional)
            {
                z = lightTransform.forward;
            }
            else
            {
                z =
                    (m_target.TransformPoint(m_targetBounds.center + moveBounds.center) - lightTransform.position)
                    .normalized;
            }
            var right       = m_target.right;
            var up          = m_target.up;
            var forward     = m_target.forward;
            var angleBounds = predictor.PredictNextFrameEulerAngleChanges();

            if (angleBounds.center != Vector3.zero)
            {
                var rot = m_target.rotation * Quaternion.Euler(angleBounds.center) * Quaternion.Inverse(m_target.rotation);
                right   = rot * right;
                up      = rot * up;
                forward = rot * forward;
            }
            var xExtent = m_targetBounds.extents.x + moveBounds.extents.x;
            var yExtent = m_targetBounds.extents.y + moveBounds.extents.y;
            var zExtent = m_targetBounds.extents.z + moveBounds.extents.z;

            if (angleBounds.extents.x != 0.0f)
            {
                var cos    = Mathf.Cos(Mathf.Deg2Rad * Mathf.Min(90, angleBounds.extents.x));
                var rcpLen = 1.0f / Mathf.Sqrt(yExtent * yExtent + zExtent * zExtent);
                var cos_y  = Mathf.Max(cos, yExtent * rcpLen);
                var cos_z  = Mathf.Max(cos, zExtent * rcpLen);
                var ySin   = Mathf.Sqrt(1.0f - cos_z * cos_z) * yExtent;
                var zSin   = Mathf.Sqrt(1.0f - cos_y * cos_y) * zExtent;
                yExtent = cos_y * yExtent + zSin;
                zExtent = cos_z * zExtent + ySin;
            }
            if (angleBounds.extents.y != 0.0f)
            {
                var cos    = Mathf.Cos(Mathf.Deg2Rad * Mathf.Min(90, angleBounds.extents.y));
                var rcpLen = 1.0f / Mathf.Sqrt(zExtent * zExtent + xExtent * xExtent);
                var cos_z  = Mathf.Max(cos, zExtent * rcpLen);
                var cos_x  = Mathf.Max(cos, xExtent * rcpLen);
                var zSin   = Mathf.Sqrt(1.0f - cos_x * cos_x) * zExtent;
                var xSin   = Mathf.Sqrt(1.0f - cos_z * cos_z) * xExtent;
                zExtent = cos_z * zExtent + xSin;
                xExtent = cos_x * xExtent + zSin;
            }
            if (angleBounds.extents.z != 0.0f)
            {
                var cos    = Mathf.Cos(Mathf.Deg2Rad * Mathf.Min(90, angleBounds.extents.z));
                var rcpLen = 1.0f / Mathf.Sqrt(xExtent * xExtent + yExtent * yExtent);
                var cos_x  = Mathf.Max(cos, xExtent * rcpLen);
                var cos_y  = Mathf.Max(cos, yExtent * rcpLen);
                var xSin   = Mathf.Sqrt(1.0f - cos_y * cos_y) * xExtent;
                var ySin   = Mathf.Sqrt(1.0f - cos_x * cos_x) * yExtent;
                xExtent = cos_x * xExtent + ySin;
                yExtent = cos_y * yExtent + xSin;
            }
            var     xDepth = Mathf.Abs(Vector3.Dot(right, z));
            var     xSize  = xExtent * Mathf.Sqrt(Mathf.Max(0.0f, 1.0f - xDepth * xDepth));
            var     yDepth = Mathf.Abs(Vector3.Dot(up, z));
            var     ySize  = yExtent * Mathf.Sqrt(Mathf.Max(0.0f, 1.0f - yDepth * yDepth));
            var     zDepth = Mathf.Abs(Vector3.Dot(forward, z));
            var     zSize  = zExtent * Mathf.Sqrt(Mathf.Max(0.0f, 1.0f - zDepth * zDepth));
            Vector3 x;

            if (ySize <= xSize && zSize <= xSize)
            {
                x = right;
            }
            else if (xSize <= ySize && zSize <= ySize)
            {
                x = up;
            }
            else
            {
                x = forward;
            }
            var y = Vector3.Cross(z, x).normalized;

            x     = Vector3.Cross(y, z);
            xSize = xExtent * Mathf.Abs(Vector3.Dot(right, x)) + yExtent * Mathf.Abs(Vector3.Dot(up, x)) +
                    zExtent * Mathf.Abs(Vector3.Dot(forward, x));
            ySize = xExtent * Mathf.Abs(Vector3.Dot(right, y)) + yExtent * Mathf.Abs(Vector3.Dot(up, y)) +
                    zExtent * Mathf.Abs(Vector3.Dot(forward, y));
            if (m_light.type == LightType.Directional)
            {
                var o = m_target.TransformPoint(m_targetBounds.center + moveBounds.center);
                clipPlanes.SetClipPlaneNum(3, 2, true);
                clipPlanes.clipPlanes[0]           = new Plane(x, o);
                clipPlanes.clipPlanes[0].distance += xSize;
                clipPlanes.maxDistance[0]          = 2 * xSize;
                clipPlanes.clipPlanes[1]           = new Plane(y, o);
                clipPlanes.clipPlanes[1].distance += ySize;
                clipPlanes.maxDistance[1]          = 2 * ySize;
                clipPlanes.clipPlanes[2]           = new Plane(z, o);
                clipPlanes.clipPlanes[2].distance -= m_nearClipPlane;
                clipPlanes.maxDistance[2]          = m_farClipPlane - m_nearClipPlane;
            }
            else
            {
                var zPos = Vector3.Dot(z, (m_target.position - lightTransform.position));
                var o    = lightTransform.position;
                clipPlanes.SetClipPlaneNum(5, 4, false);
                var depth = zPos - xDepth - yDepth - zDepth;
                if (depth < m_nearClipPlane)
                {
                    depth = m_nearClipPlane;
                }
                depth  = 1.0f / depth;
                xSize *= depth;
                ySize *= depth;
                var x0 = (x + xSize * z).normalized;
                var x1 = (-x + xSize * z).normalized;
                var y0 = (y + ySize * z).normalized;
                var y1 = (-y + ySize * z).normalized;
                clipPlanes.clipPlanes[0]           = new Plane(x0, o);
                clipPlanes.clipPlanes[1]           = new Plane(y0, o);
                clipPlanes.clipPlanes[2]           = new Plane(x1, o);
                clipPlanes.clipPlanes[3]           = new Plane(y1, o);
                clipPlanes.clipPlanes[4]           = new Plane(-z, o);
                clipPlanes.clipPlanes[4].distance += m_farClipPlane + zPos;
                clipPlanes.maxDistance[0]          = clipPlanes.maxDistance[2] = 2.0f * xSize * m_farClipPlane;
                clipPlanes.maxDistance[1]          = clipPlanes.maxDistance[3] = 2.0f * ySize * m_farClipPlane;
                clipPlanes.maxDistance[4]          = m_farClipPlane - m_nearClipPlane;
            }
            if (clipPlaneTransform != null)
            {
                var     m = clipPlaneTransform.localToWorldMatrix.transpose;
                Vector3 t = m.GetRow(3);
                for (var i = 0; i < clipPlanes.clipPlaneCount; ++i)
                {
                    var d = Vector3.Dot(clipPlanes.clipPlanes[i].normal, t);
                    clipPlanes.clipPlanes[i].distance += d;
                    clipPlanes.clipPlanes[i].normal    = m.MultiplyVector(clipPlanes.clipPlanes[i].normal);
                }
            }
        }
Esempio n. 3
0
        public override void GetClipPlanes(ref ClipPlanes clipPlanes, Transform clipPlaneTransform)
        {
            var     z       = direction;
            var     right   = m_target.right;
            var     up      = m_target.up;
            var     forward = m_target.forward;
            var     xDepth  = Mathf.Abs(Vector3.Dot(right, z));
            var     xSize   = m_targetBounds.extents.x * Mathf.Sqrt(Mathf.Max(0.0f, 1.0f - xDepth * xDepth));
            var     yDepth  = Mathf.Abs(Vector3.Dot(up, z));
            var     ySize   = m_targetBounds.extents.y * Mathf.Sqrt(Mathf.Max(0.0f, 1.0f - yDepth * yDepth));
            var     zDepth  = Mathf.Abs(Vector3.Dot(forward, z));
            var     zSize   = m_targetBounds.extents.z * Mathf.Sqrt(Mathf.Max(0.0f, 1.0f - zDepth * zDepth));
            Vector3 x;

            if (ySize <= xSize && zSize <= xSize)
            {
                x = m_target.right;
            }
            else if (xSize <= ySize && zSize <= ySize)
            {
                x = m_target.up;
            }
            else
            {
                x = m_target.forward;
            }
            var y = Vector3.Cross(z, x).normalized;

            x     = Vector3.Cross(y, z);
            xSize = m_targetBounds.extents.x * Mathf.Abs(Vector3.Dot(right, x))
                    + m_targetBounds.extents.y * Mathf.Abs(Vector3.Dot(up, x))
                    + m_targetBounds.extents.z * Mathf.Abs(Vector3.Dot(forward, x));
            ySize = m_targetBounds.extents.x * Mathf.Abs(Vector3.Dot(right, y))
                    + m_targetBounds.extents.y * Mathf.Abs(Vector3.Dot(up, y))
                    + m_targetBounds.extents.z * Mathf.Abs(Vector3.Dot(forward, y));
            if (m_light.type == LightType.Directional)
            {
                var o = position;
                clipPlanes.SetClipPlaneNum(3, 2, true);
                clipPlanes.clipPlanes[0]           = new Plane(x, o);
                clipPlanes.clipPlanes[0].distance += xSize;
                clipPlanes.maxDistance[0]          = 2 * xSize;
                clipPlanes.clipPlanes[1]           = new Plane(y, o);
                clipPlanes.clipPlanes[1].distance += ySize;
                clipPlanes.maxDistance[1]          = 2 * ySize;
                clipPlanes.clipPlanes[2]           = new Plane(z, o);
                clipPlanes.clipPlanes[2].distance -= m_nearClipPlane;
                clipPlanes.maxDistance[2]          = m_farClipPlane - m_nearClipPlane;
            }
            else
            {
                var zPos = Vector3.Dot(z, (m_target.position - lightTransform.position));
                var o    = lightTransform.position;
                clipPlanes.SetClipPlaneNum(5, 4, false);
                var depth = zPos - xDepth - yDepth - zDepth;
                if (depth < m_nearClipPlane)
                {
                    depth = m_nearClipPlane;
                }
                depth  = 1.0f / depth;
                xSize *= depth;
                ySize *= depth;
                var x0 = (x + xSize * z).normalized;
                var x1 = (-x + xSize * z).normalized;
                var y0 = (y + ySize * z).normalized;
                var y1 = (-y + ySize * z).normalized;
                clipPlanes.clipPlanes[0]           = new Plane(x0, o);
                clipPlanes.clipPlanes[1]           = new Plane(y0, o);
                clipPlanes.clipPlanes[2]           = new Plane(x1, o);
                clipPlanes.clipPlanes[3]           = new Plane(y1, o);
                clipPlanes.clipPlanes[4]           = new Plane(-z, o);
                clipPlanes.clipPlanes[4].distance += m_farClipPlane + zPos;
                clipPlanes.maxDistance[0]          = clipPlanes.maxDistance[2] = 2.0f * xSize * m_farClipPlane;
                clipPlanes.maxDistance[1]          = clipPlanes.maxDistance[3] = 2.0f * ySize * m_farClipPlane;
                clipPlanes.maxDistance[4]          = m_farClipPlane - m_nearClipPlane;
            }
            if (clipPlaneTransform != null)
            {
                var     m = clipPlaneTransform.localToWorldMatrix.transpose;
                Vector3 t = m.GetRow(3);
                for (var i = 0; i < clipPlanes.clipPlaneCount; ++i)
                {
                    var d = Vector3.Dot(clipPlanes.clipPlanes[i].normal, t);
                    clipPlanes.clipPlanes[i].distance += d;
                    clipPlanes.clipPlanes[i].normal    = m.MultiplyVector(clipPlanes.clipPlanes[i].normal);
                }
            }
        }
Esempio n. 4
0
 public abstract void GetClipPlanes(ref ClipPlanes clipPlanes,
                                    Transform clipPlaneTransform,
                                    ITransformPredictor predictor);
Esempio n. 5
0
 public abstract void GetClipPlanes(ref ClipPlanes clipPlanes, Transform clipPlaneTransform);
Esempio n. 6
0
        public void GetClipPlanes(ref ClipPlanes clipPlanes, Transform clipPlaneTransform, ITransformPredictor predictor)
        {
            var x           = m_projectorTransform.right;
            var y           = m_projectorTransform.up;
            var z           = m_projectorTransform.forward;
            var angleBounds = predictor.PredictNextFrameEulerAngleChanges();

            if (angleBounds.center != Vector3.zero)
            {
                var rot = m_projectorTransform.rotation * Quaternion.Euler(angleBounds.center) *
                          Quaternion.Inverse(m_projectorTransform.rotation);
                x = rot * x;
                y = rot * y;
                z = rot * z;
            }
            var o          = m_projectorTransform.position;
            var moveBounds = predictor.PredictNextFramePositionChanges();

            o += m_projectorTransform.TransformDirection(moveBounds.center);
            if (m_projector.orthographic)
            {
                clipPlanes.SetClipPlaneNum(3, 2, true);
                var farExtentX = Mathf.Tan(Mathf.Deg2Rad * Mathf.Min(80, angleBounds.extents.y)) * m_projector.farClipPlane;
                var farExtentY = Mathf.Tan(Mathf.Deg2Rad * Mathf.Min(80, angleBounds.extents.x)) * m_projector.farClipPlane;
                var xSize      = m_projector.orthographicSize * m_projector.aspectRatio + farExtentX + moveBounds.extents.x;
                var ySize      = m_projector.orthographicSize + farExtentY + moveBounds.extents.y;
                var cosZ       = Mathf.Cos(Mathf.Deg2Rad * Mathf.Min(90, angleBounds.extents.z));
                var rcpLen     = 1.0f / Mathf.Sqrt(xSize * xSize + ySize * ySize);
                var cosZ_x     = Mathf.Max(cosZ, xSize * rcpLen);
                var cosZ_y     = Mathf.Max(cosZ, ySize * rcpLen);
                var xSin       = Mathf.Sqrt(1.0f - cosZ_y * cosZ_y) * xSize;
                var ySin       = Mathf.Sqrt(1.0f - cosZ_x * cosZ_x) * ySize;
                xSize = cosZ_x * xSize + ySin;
                ySize = cosZ_y * ySize + xSin;
                clipPlanes.clipPlanes[0]           = new Plane(x, o);
                clipPlanes.clipPlanes[0].distance += xSize;
                clipPlanes.maxDistance[0]          = 2 * xSize;
                clipPlanes.clipPlanes[1]           = new Plane(y, o);
                clipPlanes.clipPlanes[1].distance += ySize;
                clipPlanes.maxDistance[1]          = 2 * ySize;
                clipPlanes.clipPlanes[2]           = new Plane(z, o);
                clipPlanes.clipPlanes[2].distance -= m_projector.nearClipPlane - moveBounds.extents.z;
                clipPlanes.maxDistance[2]          = m_projector.farClipPlane - m_projector.nearClipPlane +
                                                     2.0f * moveBounds.extents.z;
            }
            else
            {
                clipPlanes.SetClipPlaneNum(5, 4, false);
                var ySize =
                    Mathf.Tan(Mathf.Deg2Rad * Mathf.Min(0.5f * m_projector.fieldOfView + angleBounds.extents.x, 80)) +
                    moveBounds.extents.y;
                var xSize   = Mathf.Tan(0.5f * Mathf.Deg2Rad * m_projector.fieldOfView) * m_projector.aspectRatio;
                var extentX = Mathf.Tan(Mathf.Deg2Rad * Mathf.Min(80, angleBounds.extents.y));
                xSize = (xSize + extentX) / Mathf.Max(0.1f, 1.0f - xSize * extentX) + moveBounds.extents.x;
                var cosZ   = Mathf.Cos(Mathf.Deg2Rad * Mathf.Min(90, angleBounds.extents.z));
                var rcpLen = 1.0f / Mathf.Sqrt(xSize * xSize + ySize * ySize);
                var cosZ_x = Mathf.Max(cosZ, xSize * rcpLen);
                var cosZ_y = Mathf.Max(cosZ, ySize * rcpLen);
                var xSin   = Mathf.Sqrt(1.0f - cosZ_y * cosZ_y) * xSize;
                var ySin   = Mathf.Sqrt(1.0f - cosZ_x * cosZ_x) * ySize;
                xSize = cosZ_x * xSize + ySin;
                ySize = cosZ_y * ySize + xSin;
                o     = o - moveBounds.extents.z * z;
                var far = m_projector.farClipPlane + 2.0f * moveBounds.extents.z;
                var x0  = (x + xSize * z).normalized;
                var x1  = (-x + xSize * z).normalized;
                var y0  = (y + ySize * z).normalized;
                var y1  = (-y + ySize * z).normalized;
                clipPlanes.clipPlanes[0]           = new Plane(x0, o);
                clipPlanes.clipPlanes[1]           = new Plane(y0, o);
                clipPlanes.clipPlanes[2]           = new Plane(x1, o);
                clipPlanes.clipPlanes[3]           = new Plane(y1, o);
                clipPlanes.clipPlanes[4]           = new Plane(-z, o);
                clipPlanes.clipPlanes[4].distance += far;
                clipPlanes.maxDistance[0]          = clipPlanes.maxDistance[2] = 2.0f * xSize * far;
                clipPlanes.maxDistance[1]          = clipPlanes.maxDistance[3] = 2.0f * ySize * far;
                clipPlanes.maxDistance[4]          = far - m_projector.nearClipPlane;
            }
            if (clipPlaneTransform != null)
            {
                var     m = clipPlaneTransform.localToWorldMatrix.transpose;
                Vector3 t = m.GetRow(3);
                for (var i = 0; i < clipPlanes.clipPlaneCount; ++i)
                {
                    var d = Vector3.Dot(clipPlanes.clipPlanes[i].normal, t);
                    clipPlanes.clipPlanes[i].distance += d;
                    clipPlanes.clipPlanes[i].normal    = m.MultiplyVector(clipPlanes.clipPlanes[i].normal);
                }
            }
        }