private RegionData CheckRegionHit(PolarAngles position)
 {
     foreach (RegionData region in allRegionsData)
     {
         if ((Mathf.Abs(position.azimuth - region.getRegionCenter().x) < Mathf.Abs(region.getRegionSize().x)) &&
             (Mathf.Abs(position.zenith - region.getRegionCenter().y) < Mathf.Abs(region.getRegionSize().y)))
         {
             return(region);
         }
     }
     return(null);
 }
    private void Update()
    {
        if (!mainCam)
        {
            return;
        }

        if (!previewRegionEnabled)
        {
            highlightMaterial.SetFloatArray("_RegionsData", shaderRegionsData);    // Every frame send the active regions data to the shader, so that the highlight is visible!
            highlightMaterial.SetInt("_RegionsSize", shaderRegionsData.Length / 4);

            if (Input.GetMouseButtonDown(0))
            {
                // Check if the user clicked on a zone and update the pop-up accordingly
                rayDir = Camera.main.ScreenPointToRay(Input.mousePosition).direction;

                PolarAngles clickPosition = ToRadialCoords(rayDir);
                RegionData  hitRegion     = CheckRegionHit(clickPosition);
                if (hitRegion != null)
                {
                    if (GetRegionIndexFromShader(hitRegion) != -1)
                    {
                        Quaternion rot = Quaternion.LookRotation(rayDir, Vector3.up);
                        if (hitRegion.getRegionPopUp() == null)
                        {
                            hitRegion.setRegionPopUp(Instantiate(regionPopUpInfoCanvasPrefab, rayDir * 5.0f, rot));
                            hitRegion.getRegionPopUp().GetComponentInChildren <Text>().text = hitRegion.getRegionDescription();
                            hitRegion.getRegionPopUp().GetComponent <ZonePopUpController>().EnablePopUp();
                        }
                        else
                        {
                            if (hitRegion.getRegionPopUp().GetComponent <ZonePopUpController>().IsEnabled() == false)
                            {
                                hitRegion.getRegionPopUp().transform.position = rayDir * 5.0f;
                                hitRegion.getRegionPopUp().transform.rotation = rot;
                                hitRegion.getRegionPopUp().GetComponent <ZonePopUpController>().EnablePopUp();
                            }
                        }
                    }
                }
            }
        }
        else
        {
            highlightMaterial.SetFloatArray("_RegionsData", previewRegionData);
            highlightMaterial.SetInt("_RegionsSize", 1);
        }

        UpdateShaderRegions();
    }
    public Vector4 SetupPreviewRegion()
    {
        Vector4 defaultZoneDetails = new Vector4();

        previewRegionEnabled = true;
        rayDir = Camera.main.ScreenPointToRay(new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0.0f)).direction; // Perform a raycast in the camera orientation so that the preview zone is always centered to the user's view
        PolarAngles clickPosition = ToRadialCoords(rayDir);

        previewRegionData[0] = clickPosition.azimuth;   // Assign the coordinates of the previewRegion to be the result of the raycast, i.e.
        previewRegionData[1] = clickPosition.zenith;    // asssigning these 2 variables makes the zone be in front of the camera, so that users always see the region
        previewRegionData[2] = 0.05f;                   // Default value for the X Size
        previewRegionData[3] = 0.05f;                   // Default value for the Y Size
        defaultZoneDetails.x = clickPosition.azimuth;
        defaultZoneDetails.y = clickPosition.zenith;
        defaultZoneDetails.z = 0.05f;
        defaultZoneDetails.w = 0.05f;

        return(defaultZoneDetails);
    }