コード例 #1
0
ファイル: GlGrid.cs プロジェクト: PawelPajor/agaq-builder
        void OnRenderObject()
        {
            float            camFarPlaneScale = 0.2f * 1000.0f / cam.farClipPlane;
            CameraViewVolume viewVolume       = cam.GetViewVolume(cam.farClipPlane * camFarPlaneScale);
            Bounds           volumeAABB       = viewVolume.AABB;

            float minX = volumeAABB.min.x;
            float minZ = volumeAABB.min.z;
            float maxX = volumeAABB.max.x;
            float maxZ = volumeAABB.max.z;

            float halfCellSizeX = 0.5f * baseSize;
            float halfCellSizeZ = 0.5f * baseSize;
            int   minCellX      = Mathf.FloorToInt((minX + halfCellSizeX) / baseSize) - 1;
            int   maxCellX      = Mathf.FloorToInt((maxX + halfCellSizeX) / baseSize) + 1;
            int   minCellZ      = Mathf.FloorToInt((minZ + halfCellSizeZ) / baseSize) - 1;
            int   maxCellZ      = Mathf.FloorToInt((maxZ + halfCellSizeZ) / baseSize) + 1;

            int minCellIndex = minCellX < minCellZ ? minCellX : minCellZ;
            int maxCellIndex = maxCellX > maxCellZ ? maxCellX : maxCellZ;

            GL.Begin(GL.LINES);
            float   startZ  = minCellIndex * baseSize;
            float   endZ    = (maxCellIndex + 1) * baseSize;
            float   startX  = minCellIndex * baseSize;
            float   endX    = (maxCellIndex + 1) * baseSize;
            Vector3 yOffset = Vector3.up * transform.position.y;

            for (int cell = minCellIndex; cell <= maxCellIndex; ++cell)
            {
                Vector3 xOffset = cell * Vector3.right * baseSize + yOffset;
                GL.Vertex(xOffset + Vector3.forward * startZ);
                GL.Vertex(xOffset + Vector3.forward * endZ);

                Vector3 zOffset = cell * Vector3.forward * baseSize + yOffset;
                GL.Vertex(zOffset + Vector3.right * startX);
                GL.Vertex(zOffset + Vector3.right * endX);
            }

            GL.End();
        }