Exemplo n.º 1
0
        public void Calculate(Camera camera, CameraViewVolume cameraViewVolume)
        {
            Transform cameraTransform = camera.transform;

            _center = cameraTransform.position + cameraTransform.forward * cameraViewVolume.FarClipPlaneDistance * 0.5f;
            _radius = (cameraViewVolume.TopLeftPointOnFarPlane - _center).magnitude * 1.01f;
        }
        public void Calculate(Camera camera, CameraViewVolume cameraViewVolume)
        {
            Transform cameraTransform = camera.transform;
            _orientedBox = new OrientedBox();

            _orientedBox.Rotation = cameraTransform.rotation;
            _orientedBox.Center = cameraTransform.position + cameraTransform.forward * cameraViewVolume.FarClipPlaneDistance * 0.5f;
            _orientedBox.ModelSpaceSize = new Vector3((cameraViewVolume.TopLeftPointOnFarPlane - cameraViewVolume.TopRightPointOnFarPlane).magnitude,
                                                      (cameraViewVolume.TopLeftPointOnFarPlane - cameraViewVolume.BottomLeftPointOnFarPlane).magnitude,
                                                      cameraViewVolume.FarClipPlaneDistance - cameraViewVolume.NearClipPlaneDistance);
        }
Exemplo n.º 3
0
        public void RenderGizmos(XZGrid grid, CameraViewVolume cameraViewVolume)
        {
            if (!grid.RenderSettings.IsVisible)
            {
                return;
            }

            // Note: Can not figure out how to render a finite grid inside a shader yet... :D
            if (grid.DimensionSettings.DimensionType == XZGridDimensionType.Finite)
            {
                RenderGizmos_Obsolete(grid, cameraViewVolume);
                return;
            }

            Plane   gridPlane       = grid.Plane;
            Vector3 gridPlaneCenter = gridPlane.ProjectPoint(SceneViewCamera.Camera.transform.position);

            Box            camVolumeAABB          = cameraViewVolume.WorldSpaceAABB;
            List <Vector3> projectedVolumeAABBPts = gridPlane.ProjectAllPoints(camVolumeAABB.GetCornerPoints());
            List <Vector3> modelSpacePrjPts       = Vector3Extensions.GetTransformedPoints(projectedVolumeAABBPts, grid.TransformMatrix.ToMatrix4x4x.inverse);
            Box            modelSpacePtsBox       = Box.FromPoints(modelSpacePrjPts);
            Vector3        gridPlaneSize          = modelSpacePtsBox.Size;

            Matrix4x4 planeTransformMatrix = Matrix4x4.TRS(gridPlaneCenter, grid.Rotation, gridPlaneSize);
            Material  xzGridMaterial       = MaterialPool.Get().XZGridMaterial;

            xzGridMaterial.SetFloat("_CellSizeX", grid.CellSizeSettings.CellSizeX);
            xzGridMaterial.SetFloat("_CellSizeZ", grid.CellSizeSettings.CellSizeZ);
            xzGridMaterial.SetVector("_CellOffset", grid.GetOriginPosition());
            xzGridMaterial.SetColor("_LineColor", grid.RenderSettings.CellLineColor);
            xzGridMaterial.SetColor("_PlaneColor", grid.RenderSettings.PlaneColor);
            xzGridMaterial.SetFloat("_CamFarPlaneDist", SceneViewCamera.Camera.farClipPlane);
            xzGridMaterial.SetVector("_CamWorldPos", SceneViewCamera.Camera.transform.position);
            xzGridMaterial.SetMatrix("_InvRotMatrix", Matrix4x4.TRS(Vector3.zero, grid.Rotation, Vector3.one).inverse);
            xzGridMaterial.SetMatrix("_PlaneTransformMtx", planeTransformMatrix);

            int numPasses = xzGridMaterial.passCount;

            for (int passIndex = 0; passIndex < numPasses; ++passIndex)
            {
                xzGridMaterial.SetPass(passIndex);
                Graphics.DrawMeshNow(GizmosEx.XZRectangleMesh, planeTransformMatrix);
            }

            GizmosMatrix.Push(grid.TransformMatrix.ToMatrix4x4x);
            grid.RenderableCoordinateSystem.RenderGizmos();
            GizmosMatrix.Pop();
        }
Exemplo n.º 4
0
        private void RenderGizmos_Obsolete(XZGrid grid, CameraViewVolume cameraViewVolume)
        {
            if (!grid.RenderSettings.IsVisible)
            {
                return;
            }

            var visibleCellRangeCalculator          = new XZVisibleGridCellRangeCalculator();
            XZVisibleGridCellRange visibleCellRange = visibleCellRangeCalculator.Calculate(grid, cameraViewVolume);

            GizmosMatrix.Push(grid.TransformMatrix.ToMatrix4x4x);
            RenderGridPlane_Obsolete(grid, visibleCellRange);
            RenderGridCellLines_Obsolete(grid, visibleCellRange);
            grid.RenderableCoordinateSystem.RenderGizmos();
            GizmosMatrix.Pop();
        }
        /// <summary>
        /// Calculates and returns the edge rays which connect the points of the specified camera
        /// view volume.
        /// </summary>
        public static Ray3D[] CalculateWorldSpaceVolumeEdgeRays(CameraViewVolume cameraViewVolume)
        {
            var worldSpaceVolumeEdgeRays = new Ray3D[12];

            // Calculate the rays that connect the points on the near plane
            worldSpaceVolumeEdgeRays[0] = new Ray3D(cameraViewVolume.TopLeftPointOnNearPlane, cameraViewVolume.TopRightPointOnNearPlane - cameraViewVolume.TopLeftPointOnNearPlane);
            worldSpaceVolumeEdgeRays[1] = new Ray3D(cameraViewVolume.TopRightPointOnNearPlane, cameraViewVolume.BottomRightPointOnNearPlane - cameraViewVolume.TopRightPointOnNearPlane);
            worldSpaceVolumeEdgeRays[2] = new Ray3D(cameraViewVolume.BottomRightPointOnNearPlane, cameraViewVolume.BottomLeftPointOnNearPlane - cameraViewVolume.BottomRightPointOnNearPlane);
            worldSpaceVolumeEdgeRays[3] = new Ray3D(cameraViewVolume.BottomLeftPointOnNearPlane, cameraViewVolume.TopLeftPointOnNearPlane - cameraViewVolume.BottomLeftPointOnNearPlane);

            // Calculate the rays that connect the points on the far plane
            worldSpaceVolumeEdgeRays[4] = new Ray3D(cameraViewVolume.TopLeftPointOnFarPlane, cameraViewVolume.TopRightPointOnFarPlane - cameraViewVolume.TopLeftPointOnFarPlane);
            worldSpaceVolumeEdgeRays[5] = new Ray3D(cameraViewVolume.TopRightPointOnFarPlane, cameraViewVolume.BottomRightPointOnFarPlane - cameraViewVolume.TopRightPointOnFarPlane);
            worldSpaceVolumeEdgeRays[6] = new Ray3D(cameraViewVolume.BottomRightPointOnFarPlane, cameraViewVolume.BottomLeftPointOnFarPlane - cameraViewVolume.BottomRightPointOnFarPlane);
            worldSpaceVolumeEdgeRays[7] = new Ray3D(cameraViewVolume.BottomLeftPointOnFarPlane, cameraViewVolume.TopLeftPointOnFarPlane - cameraViewVolume.BottomLeftPointOnFarPlane);

            // Calculate the rays that connect the points between the near and far planes
            worldSpaceVolumeEdgeRays[8]  = new Ray3D(cameraViewVolume.TopLeftPointOnNearPlane, cameraViewVolume.TopLeftPointOnFarPlane - cameraViewVolume.TopLeftPointOnNearPlane);
            worldSpaceVolumeEdgeRays[9]  = new Ray3D(cameraViewVolume.TopRightPointOnNearPlane, cameraViewVolume.TopRightPointOnFarPlane - cameraViewVolume.TopRightPointOnNearPlane);
            worldSpaceVolumeEdgeRays[10] = new Ray3D(cameraViewVolume.BottomRightPointOnNearPlane, cameraViewVolume.BottomRightPointOnFarPlane - cameraViewVolume.BottomRightPointOnNearPlane);
            worldSpaceVolumeEdgeRays[11] = new Ray3D(cameraViewVolume.BottomLeftPointOnNearPlane, cameraViewVolume.BottomLeftPointOnFarPlane - cameraViewVolume.BottomLeftPointOnNearPlane);

            return(worldSpaceVolumeEdgeRays);
        }
Exemplo n.º 6
0
        public XZVisibleGridCellRange Calculate(XZGrid grid, CameraViewVolume cameraViewVolume)
        {
            List <Vector3> gridPlaneIntersectionPoints = GetGridPlaneIntersectionPointsWithVolumeRays(grid, cameraViewVolume);

            return(CalculateVisibleCellRangeFromGridPlaneIntersectionPoints(grid, gridPlaneIntersectionPoints));
        }
Exemplo n.º 7
0
        private List <Vector3> GetGridPlaneIntersectionPointsWithVolumeRays(XZGrid grid, CameraViewVolume cameraViewVolume)
        {
            Plane gridPlane = grid.Plane;

            Ray3D[] volumeRays = cameraViewVolume.WorldSpaceVolumeEdgeRays;

            float t;
            var   intersectionPoints = new List <Vector3>();

            foreach (Ray3D ray in volumeRays)
            {
                if (ray.IntersectsPlane(gridPlane, out t))
                {
                    intersectionPoints.Add(ray.Origin + ray.Direction * t);
                }
            }

            return(intersectionPoints);
        }