Example #1
0
        public void Render_SystemCall(Camera renderCamera)
        {
            if (!Settings.IsVisible)
            {
                return;
            }
            if (IsRenderIgnoreCamera(renderCamera))
            {
                return;
            }

            AABB camViewVolumeAABB = renderCamera.CalculateVolumeAABB();

            Vector3 gridPlanePos   = WorldPlane.ProjectPoint(camViewVolumeAABB.Center);
            Vector3 gridPlaneScale = camViewVolumeAABB.Size * 2.0f;

            gridPlaneScale.y = 1.0f;
            Matrix4x4 transformMatrix = Matrix4x4.TRS(gridPlanePos, Rotation, gridPlaneScale);

            // Continue based on whether cell fading is used
            if (LookAndFeel.UseCellFading)
            {
                // We will need to render the grid twice to simulate the grid effect which exists in Unity.
                // When zooming out, it appears that smaller cells fade away while newer and bigger ones
                // appear instead. We can do this by rendering the grid twice with different cell sizes and
                // alpha values. The first step is to calculate the camera zoom (abs y position) and then
                // count the number of digits in this value.
                float cameraZoom = CalculateCellFadeZoom(renderCamera);
                int   numDigits  = MathEx.GetNumDigits((int)cameraZoom);

                // The number of digits inside the camera zoom can be used to calculate two power of 10 values.
                // These values will be used to calculate 2 cell sizes: the small size which is fading out and
                // the bigger size which is fading in.
                // Example (assume XZ cell size of 1 and camera Y pos of 10.5)
                //      Zoom = 10 => NumDigits = 2;
                //      Power0 = 1; Power1 = 2;
                //      SmallCellSize = baseSize * 10 ^ Power0 = 10;
                //      BigCellSize = baseSize * 10 ^ Power1 = 100;
                // So the cell size increases in powers of 10.
                int csPower0 = numDigits - 1;
                int csPower1 = csPower0 + 1;

                // Calculate the cell size scale value based on the calculated powers of 10
                float csScale0 = Mathf.Pow(10.0f, csPower0);
                float csScale1 = Mathf.Pow(10.0f, csPower1);

                // Calculate the alpha scale values. The first alpha scale value is used to fade out the small cells.
                // The second alpha is used to fade in the bigger ones.
                Color color  = LookAndFeel.LineColor;
                float alpha0 = (csScale1 - cameraZoom) / (csScale1 - csScale0);
                float alpha1 = 1.0f - alpha0;

                // Set material properties
                Material material = MaterialPool.Get.XZGrid_Plane;
                material.SetFloat("_CamFarPlaneDist", renderCamera.farClipPlane);
                material.SetVector("_CamWorldPos", renderCamera.transform.position);
                material.SetVector("_GridOrigin", Vector3.zero);
                material.SetVector("_GridRight", Right);
                material.SetVector("_GridLook", Look);
                material.SetMatrix("_TransformMatrix", transformMatrix);

                // Render in 2 passes to achieve the cell fade effect
                color.a = LookAndFeel.LineColor.a * alpha0;
                if (color.a != 0.0f)
                {
                    // Set material properties for this pass
                    material.SetFloat("_CellSizeX", Settings.CellSizeX * csScale0);
                    material.SetFloat("_CellSizeZ", Settings.CellSizeZ * csScale0);
                    material.SetColor("_LineColor", color);
                    material.SetPass(0);

                    // Render
                    Graphics.DrawMeshNow(MeshPool.Get.UnitQuadXZ, transformMatrix);
                }
                color.a = LookAndFeel.LineColor.a * alpha1;
                if (color.a != 0.0f)
                {
                    // Set material properties for this pass
                    material.SetFloat("_CellSizeX", Settings.CellSizeX * csScale1);
                    material.SetFloat("_CellSizeZ", Settings.CellSizeZ * csScale1);
                    material.SetColor("_LineColor", color);
                    material.SetPass(0);

                    // Render
                    Graphics.DrawMeshNow(MeshPool.Get.UnitQuadXZ, transformMatrix);
                }
            }
            else
            {
                // Just render the grid once with the chosen cell size
                Material material = MaterialPool.Get.XZGrid_Plane;
                material.SetFloat("_CamFarPlaneDist", renderCamera.farClipPlane);
                material.SetVector("_CamWorldPos", renderCamera.transform.position);
                material.SetMatrix("_TransformMatrix", transformMatrix);
                material.SetFloat("_CellSizeX", Settings.CellSizeX);
                material.SetFloat("_CellSizeZ", Settings.CellSizeZ);
                material.SetColor("_LineColor", LookAndFeel.LineColor);
                material.SetVector("_GridOrigin", Vector3.zero);
                material.SetVector("_GridRight", Right);
                material.SetVector("_GridLook", Look);
                material.SetPass(0);
                Graphics.DrawMeshNow(MeshPool.Get.UnitQuadXZ, transformMatrix);
            }
        }