Beispiel #1
0
        /// <summary>
        /// Close the camera and delete all textures
        /// Once destroyed, you need to recreate a camera to restart again
        /// </summary>
        public void Destroy()
        {
            ZEDUpdater.GetInstance().Destroy();

            cameraIsReady = false;

            dllz_close();

            DestroyAllTexture();
            instance = null;
        }
Beispiel #2
0
 /// <summary>
 /// Gets an instance of the ZEDCamera
 /// </summary>
 /// <returns>The instance</returns>
 public static ZEDCamera GetInstance()
 {
     lock (_lock)
     {
         if (instance == null)
         {
             instance = new ZEDCamera();
             CheckPlugin();
             dllz_register_callback_debuger(new DebugCallback(DebugMethod));
         }
         return instance;
     }
 }
Beispiel #3
0
    public ZEDSpatialMapping(Transform transform, sl.ZEDCamera zedCamera, ZEDManager zedManager)
    {
        zedSpatialMapping = new ZEDSpatialMappingHelper(Resources.Load("Materials/SpatialMapping/Mat_ZED_Texture") as Material, Resources.Load("Materials/SpatialMapping/Mat_ZED_Geometry_Wireframe") as Material);

        this.zedCamera    = zedCamera;
        this.zedManager   = zedManager;
        scanningInitState = sl.ERROR_CODE.FAILURE;

        holder      = new GameObject();
        holder.name = "[ZED Mesh Holder]";
        //holder.hideFlags = HideFlags.HideInInspector;
        holder.transform.position = Vector3.zero;
        holder.transform.rotation = Quaternion.identity;
        StaticBatchingUtility.Combine(holder);
    }
Beispiel #4
0
    /// <summary>
    /// Init the SVOManager
    /// </summary>
    /// <param name="zedCamera"></param>
    public void InitSVO(sl.ZEDCamera zedCamera)
    {
        if (record)
        {
            if (zedCamera.EnableRecording(videoFile, compressionMode) != sl.ERROR_CODE.SUCCESS)
            {
                record = false;
            }
        }

        if (read)
        {
            NumberFrameMax = zedCamera.GetSVONumberOfFrames();
        }
    }
 void OnApplicationQuit()
 {
     if (zedCamera != null)
     {
         if (ZedSVOManager != null)
         {
             if (ZedSVOManager.record)
             {
                 zedCamera.DisableRecording();
             }
         }
         zedCamera.Destroy();
     }
     zedCamera = null;
 }
    /// <summary>
    /// Detects the plane around screen-space coordinates specified.
    /// </summary>
    /// <returns><c>true</c>, if plane at hit was detected, <c>false</c> otherwise.</returns>
    /// <param name="screenPos">Position of the pixel in screen space (2D).</param>
    public bool DetectPlaneAtHit(ZEDManager manager, Vector2 screenPos)
    {
        if (!manager.IsZEDReady)
        {
            return(false); //Do nothing if the ZED isn't finished initializing.
        }
        sl.ZEDCamera zedcam = manager.zedCamera;
        Camera       cam    = manager.GetMainCamera();

        ZEDPlaneGameObject.PlaneData plane = new ZEDPlaneGameObject.PlaneData();
        if (zedcam.findPlaneAtHit(ref plane, screenPos) == sl.ERROR_CODE.SUCCESS) //We found a plane.
        {
            int numVertices, numTriangles = 0;
            zedcam.convertHitPlaneToMesh(planeMeshVertices, planeMeshTriangles, out numVertices, out numTriangles);
            if (numVertices > 0 && numTriangles > 0)
            {
                GameObject newhitGO = new GameObject(); //Make a new GameObject to hold the new plane.
                newhitGO.transform.SetParent(holder.transform);

                Vector3[] worldPlaneVertices  = new Vector3[numVertices];
                int[]     worldPlaneTriangles = new int[numTriangles];
                TransformCameraToLocalMesh(cam.transform, planeMeshVertices, planeMeshTriangles, worldPlaneVertices, worldPlaneTriangles, numVertices, numTriangles, plane.PlaneCenter);

                //Move the GameObject to the center of the plane. Note that the plane data's center is relative to the camera.
                newhitGO.transform.position  = cam.transform.position;                     //Add the camera's world position
                newhitGO.transform.position += cam.transform.rotation * plane.PlaneCenter; //Add the center of the plane

                ZEDPlaneGameObject hitPlane = newhitGO.AddComponent <ZEDPlaneGameObject>();

                if (overrideMaterial != null)
                {
                    hitPlane.Create(cam, plane, worldPlaneVertices, worldPlaneTriangles, planeHitCount + 1, overrideMaterial);
                }
                else
                {
                    hitPlane.Create(cam, plane, worldPlaneVertices, worldPlaneTriangles, planeHitCount + 1);
                }

                hitPlane.SetPhysics(addPhysicsOption);
                //hitPlane.SetVisible(isVisibleInSceneOption);
                hitPlaneList.Add(hitPlane);
                planeHitCount++;
                return(true);
            }
        }

        return(false);
    }
    /// <summary>
    /// Initializes all the starting values, and gets current values from the ZED.
    /// </summary>
    void FirstInit()
    {
        if (!isInit)
        {
            zedCamera = sl.ZEDCamera.GetInstance();
            EditorApplication.playmodeStateChanged += Draw;

            if (zedCamera != null && zedCamera.IsCameraReady)
            {
                isInit = true;

                if (!loaded)
                {
                    if (resetWanted)
                    {
                        ResetValues(groupAuto);
                        resetWanted = false;
                    }

                    zedCamera.RetrieveCameraSettings();
                    ZEDCameraSettingsManager.CameraSettings settings = zedCamera.GetCameraSettings();
                    groupAuto        = zedCamera.GetExposureUpdateType();
                    whiteBalanceAuto = zedCamera.GetWhiteBalanceUpdateType();

                    hue        = settings.Hue;
                    brightness = settings.Brightness;
                    contrast   = settings.Contrast;
                    saturation = settings.Saturation;

                    exposure = settings.Exposure;
                    gain     = settings.Gain;

                    zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, gain, groupAuto);
                    zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, exposure, groupAuto);
                    zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, whiteBalance, whiteBalanceAuto);
                }
                else
                {
                    LoadCameraSettings();
                }

                parameters        = zedCamera.GetCalibrationParameters(false);
                zed_serial_number = zedCamera.GetZEDSerialNumber();
                zed_fw_version    = zedCamera.GetZEDFirmwareVersion();
                zed_model         = zedCamera.GetCameraModel();
            }
        }
    }
 /// <summary>
 /// Retrieves current settings from the ZED camera.
 /// </summary>
 /// <param name="zedCamera"></param>
 public void RetrieveSettingsCamera(sl.ZEDCamera zedCamera)
 {
     if (zedCamera != null)
     {
         settings_.Brightness   = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS);
         settings_.Contrast     = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST);
         settings_.Hue          = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.HUE);
         settings_.Saturation   = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.SATURATION);
         settings_.Sharpness    = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.SHARPNESS);
         settings_.Gamma        = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.GAMMA);
         settings_.Gain         = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.GAIN);
         settings_.Exposure     = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE);
         settings_.WhiteBalance = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE);
         settings_.LEDStatus    = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS);
     }
 }
 /// <summary>
 /// Applies all settings from the container to the actual ZED camera.
 /// </summary>
 /// <param name="zedCamera">Current instance of the ZEDCamera wrapper.</param>
 public void ResetCameraSettings(sl.ZEDCamera zedCamera)
 {
     if (zedCamera != null)
     {
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS, 4);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST, 4);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.HUE, 0);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SATURATION, 3);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SHARPNESS, 3);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAMMA, 5);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, 2600);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, 0);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, 0);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS, 1);
     }
 }
Beispiel #10
0
 /// <summary>
 /// Applies all settings from the container to the actual ZED camera.
 /// </summary>
 /// <param name="zedCamera">Current instance of the ZEDCamera wrapper.</param>
 public void SetSettings(sl.ZEDCamera zedCamera)
 {
     if (zedCamera != null)
     {
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS, settings_.Brightness, false);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST, settings_.Contrast, false);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.HUE, settings_.Hue, false);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SATURATION, settings_.Saturation, false);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, settings_.Gain, false);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, settings_.Exposure, false);
         if (settings_.WhiteBalance != -1)
         {
             zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, settings_.WhiteBalance, false);
         }
     }
 }
    /// <summary>
    /// Gets the Euclidean distance from the world position of a given image pixel.
    /// </summary><remarks>
    /// Euclidean distance is distinct from forward distance/depth in that it takes into account the point's X and Y position
    /// relative to the camera. It's the actual distance between the camera and the point in world space.
    /// </remarks>
    /// <param name="pixel">Pixel coordinates in screen space. Only x,y used</param>
    /// <param name="distance">Euclidean distance to given pixel.</param>
    /// <returns></returns>
    public static bool GetEuclideanDistanceAtPixel(sl.ZEDCamera zedCam, Vector3 pixel, out float distance)
    {
        distance = 0.0f;

        if (zedCam == null)
        {
            return(false);
        }

        distance = zedCam.GetDistanceValue(pixel);
        if (distance == -1)
        {
            return(false);
        }
        return(true);
    }
Beispiel #12
0
    /// <summary>
    /// Constructor that sets up the garbage matte for the desired camera in the desired place.
    /// </summary>
    /// <param name="cam">Camera in which to apply the matte effect</param>
    /// <param name="greenScreenMaterial">Material reference, usually Mat_ZED_Greenscreen</param>
    /// <param name="target">Center location of the matte effect</param>
    /// <param name="matte">Optional reference to another garbage matte, used to copy its current edit mode. </param>
    public GarbageMatte(Camera cam, Material greenScreenMaterial, Transform target, GarbageMatte matte)
    {
        this.target       = target;
        currentPlaneIndex = 0;
        zed      = sl.ZEDCamera.GetInstance();
        this.cam = cam;
        points.Clear();

        outlineMaterial = Resources.Load("Materials/Mat_ZED_Outlined") as Material;

        go          = new List <GameObject>();
        meshFilters = new List <MeshFilter>();

        shader_greenScreen = greenScreenMaterial;
        ResetPoints(false);
        if (matte != null)
        {
            editMode = matte.editMode;
        }
        if (commandBuffer == null)
        {
            //Create a command buffer to clear the depth and stencil
            commandBuffer      = new CommandBuffer();
            commandBuffer.name = "GarbageMatte";
            commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CurrentActive, BuiltinRenderTextureType.Depth);

            //Remove the previous command buffer to set the garbage matte first
            CommandBuffer[] cmd = cam.GetCommandBuffers(CameraEvent.BeforeDepthTexture);
            cam.RemoveCommandBuffers(CameraEvent.BeforeDepthTexture);
            if (cmd.Length > 0)
            {
                cam.AddCommandBuffer(CameraEvent.BeforeDepthTexture, commandBuffer);
                for (int i = 0; i < cmd.Length; ++i)
                {
                    cam.AddCommandBuffer(CameraEvent.BeforeDepthTexture, cmd[i]);
                }
            }
        }
        if (loadAtStart && Load())
        {
            Debug.Log("Config garbage matte found, and loaded ( " + garbageMattePath + " )");
            ApplyGarbageMatte();
            editMode = false;
        }

        isInit = true;
    }
Beispiel #13
0
 void OnApplicationQuit()
 {
     if (zedCamera != null)
     {
         if (ZedSVOManager != null)
         {
             // If recording module was activated, disable it to "close" the SVO file
             if (ZedSVOManager.record)
             {
                 zedCamera.DisableRecording();
             }
         }
         //Destroy the ZED Camera plugin
         zedCamera.Destroy();
     }
     zedCamera = null;
 }
    /// <summary>
    /// Gets the Euclidean distance from the given caera to a point in the world (x,y,z).
    /// </summary><remarks>
    /// Euclidean distance is distinct from forward distance/depth in that it takes into account the point's X and Y position
    /// relative to the camera. It's the actual distance between the camera and the point in world space.
    /// </remarks>
    /// <param name="position">World position to measure.</param>
    /// <param name="cam">Unity Camera used for world-camera space conversion (usually left camera)</param>
    /// <param name="distance">Euclidean distance to given position.</out>
    /// <returns></returns>
    public static bool GetEuclideanDistanceAtWorldLocation(sl.ZEDCamera zedCam, Vector3 position, Camera cam, out float distance)
    {
        distance = 0.0f;

        if (zedCam == null)
        {
            return(false);
        }

        Vector3 pixelPosition = cam.WorldToScreenPoint(position);

        distance = zedCam.GetDistanceValue(new Vector3(pixelPosition.x, pixelPosition.y, 0));
        if (distance == -1)
        {
            return(false);
        }
        return(true);
    }
    /// <summary>
    /// Gets forward distance (i.e. depth) at a given world position (x,y,z).
    /// </summary><remarks>
    /// Forward distance/depth is distinct from Euclidean distance in that it only measures
    /// distance on the Z axis; the pixel's left/right or up/down position relative to the camera
    /// makes no difference to the depth value.
    /// </remarks>
    /// <param name="position">World position to measure.</param>
    /// <param name="cam">Unity Camera used for world-camera space conversion (usually left camera)</param>
    /// <param name="depth">Forward distance/depth to given position.</out>
    /// <returns></returns>
    public static bool GetForwardDistanceAtWorldLocation(sl.ZEDCamera zedCam, Vector3 position, Camera cam, out float depth)
    {
        depth = 0.0f;

        if (zedCam == null)
        {
            return(false);
        }

        Vector3 pixelPosition = cam.WorldToScreenPoint(position);

        depth = zedCam.GetDepthValue(pixelPosition);
        if (depth == -1)
        {
            return(false);
        }
        return(true);
    }
    void Start()
    {
        if (zedManager == null)
        {
            zedManager = FindObjectOfType <ZEDManager>();
            if (ZEDManager.GetInstances().Count > 1) //We chose a ZED arbitrarily, but there are multiple cams present. Warn the user.
            {
                Debug.Log("Warning: " + gameObject.name + "'s zedManager was not specified, so the first available ZEDManager instance was " +
                          "assigned. However, there are multiple ZEDManager's in the scene. It's recommended to specify which ZEDManager you want to " +
                          "use to display a point cloud.");
            }
        }

        if (zedManager != null)
        {
            zed = zedManager.zedCamera;
        }
    }
Beispiel #17
0
    /// <summary>
    /// Raises the application quit event
    /// </summary>
    void OnApplicationQuit()
    {
        zedReady = false;
        OnCamBrightnessChange -= CameraBrightnessChangeHandler;
        Destroy();

        if (zedCamera != null)
        {
            if (zedSVOManager != null)
            {
                if (zedSVOManager.record)
                {
                    zedCamera.DisableRecording();
                }
            }
            zedCamera.Destroy();
            zedCamera = null;
        }
    }
Beispiel #18
0
    /// <summary>
    /// Gets the Euclidean distance from the world position of a given image pixel.
    /// </summary><remarks>
    /// Euclidean distance is distinct from forward distance/depth in that it takes into account the point's X and Y position
    /// relative to the camera. It's the actual distance between the camera and the point in world space.
    /// </remarks>
    /// <param name="pixel">Pixel coordinates in screen space.</param>
    /// <param name="distance">Euclidean distance to given pixel.</param>
    /// <returns></returns>
    public static bool GetEuclideanDistanceAtPixel(sl.ZEDCamera zedCam, Vector2 pixel, out float distance)
    {
        distance = 0.0f;

        if (zedCam == null)
        {
            return(false);
        }

        float d = zedCam.GetDistanceValue(new Vector3(pixel.x, pixel.y, 0));

        distance = d;

        if (d == -1)
        {
            return(false);
        }
        return(true);
    }
 /// <summary>
 /// Applies all settings from the container to the actual ZED camera.
 /// </summary>
 /// <param name="zedCamera">Current instance of the ZEDCamera wrapper.</param>
 public void SetSettings(sl.ZEDCamera zedCamera)
 {
     if (zedCamera != null)
     {
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS, settings_.Brightness);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST, settings_.Contrast);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.HUE, settings_.Hue);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SATURATION, settings_.Saturation);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SHARPNESS, settings_.Sharpness);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAMMA, settings_.Gamma);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, settings_.Gain);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, settings_.Exposure);
         zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS, settings_.LEDStatus);
         if (settings_.WhiteBalance != -1)
         {
             zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, settings_.WhiteBalance);
         }
     }
 }
Beispiel #20
0
    /// <summary>
    /// Gets forward distance (i.e. depth) value at a given image pixel.
    /// </summary><remarks>
    /// Forward distance/depth is distinct from Euclidean distance in that it only measures
    /// distance on the Z axis; the pixel's left/right or up/down position relative to the camera
    /// makes no difference to the depth value.
    /// </remarks>
    /// <param name="pixel">Pixel coordinates in screen space.</param>
    /// <param name="depth">Forward distance/depth to given pixel.</param>
    /// <returns></returns>
    public static bool GetForwardDistanceAtPixel(sl.ZEDCamera zedCam, Vector2 pixel, out float depth)
    {
        depth = 0.0f;

        if (zedCam == null)
        {
            return(false);
        }

        float d = zedCam.GetDepthValue(new Vector3(pixel.x, pixel.y, 0));

        depth = d;

        if (d == -1)
        {
            return(false);
        }
        return(true);
    }
Beispiel #21
0
    void FirstInit()
    {
        if (!isInit)
        {
            zedCamera = sl.ZEDCamera.GetInstance();
            EditorApplication.playmodeStateChanged += Draw;
            if (zedCamera != null && zedCamera.CameraIsReady)
            {
                isInit = true;

                if (!loaded)
                {
                    if (resetWanted)
                    {
                        ResetValues(groupAuto);
                        resetWanted = false;
                    }
                    zedCamera.RetrieveCameraSettings();
                    ZEDCameraSettingsManager.CameraSettings settings = zedCamera.GetCameraSettings();

                    hue        = settings.Hue;
                    brightness = settings.Brightness;
                    contrast   = settings.Contrast;
                    saturation = settings.Saturation;
                    if (groupAuto)
                    {
                        exposure = settings.Exposure;
                        gain     = settings.Gain;

                        zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, gain, true);
                        zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, exposure, true);
                        zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, -1, false);
                    }
                }
                else
                {
                    LoadCameraSettings();
                    //loaded = false;
                }
                parameters = zedCamera.GetCameraInformation();
            }
        }
    }
Beispiel #22
0
    /// <summary>
    /// Checks if a real-world location is visible from the camera (true) or masked by a virtual object (with a collider).
    /// </summary>
    /// <warning>The virtual object must have a collider for this to work as it uses a collision test.</warning>
    /// <param name="position">Position to check in world space. Must be in camera's view to check against the real world.</param>
    /// <param name="cam">Unity Camera used for world-camera space conversion (usually left camera)</param>
    /// <returns>True if visible, false if obscurred.</returns>
    public static bool IsLocationVisible(sl.ZEDCamera zedCam, Vector3 position, Camera cam)
    {
        if (zedCam == null)
        {
            return(false);
        }

        RaycastHit hit;
        float      d;

        GetForwardDistanceAtWorldLocation(zedCam, position, cam, out d);
        if (Physics.Raycast(cam.transform.position, position - cam.transform.position, out hit))
        {
            if (hit.distance < d)
            {
                return(false);
            }
        }
        return(true);
    }
    /// <summary>
    /// Changes the value of record if recording fails, and gets the length of a read SVO file.
    /// </summary>
    /// <param name="zedCamera">Reference to the Scene's ZEDCamera instance.</param>
    public void InitSVO(sl.ZEDCamera zedCamera)
    {
        if (record)
        {
            sl.ERROR_CODE svoerror = zedCamera.EnableRecording(videoFile, compressionMode);
            if (svoerror != sl.ERROR_CODE.SUCCESS)
            {
                record = false;
            }
            else if (svoerror == sl.ERROR_CODE.SVO_RECORDING_ERROR)
            {
                Debug.LogError("SVO recording failed. Check that there is enough space on the drive and that the "
                               + "path provided is valid.");
            }
        }

        if (read)
        {
            NumberFrameMax = zedCamera.GetSVONumberOfFrames();
        }
    }
Beispiel #24
0
    /// <summary>
    /// Gets the world position of the given image pixel.
    /// </summary>
    /// <param name="pixel">Pixel coordinates in screen space.</param>
    /// <param name="cam">Unity Camera used for world-camera space conversion (usually left camera)</param>
    /// <param name="worldPos">Filled with the world position of the specified pixel.</param>
    /// <returns>True if it found a value, false otherwise (such as if it's outside the camera's view frustum)</returns>
    public static bool GetWorldPositionAtPixel(sl.ZEDCamera zedCam, Vector2 pixel, Camera cam, out Vector3 worldPos)
    {
        worldPos = Vector3.zero;

        if (zedCam == null)
        {
            return(false);
        }

        float d;

        worldPos = Vector3.zero;
        if (!GetForwardDistanceAtPixel(zedCam, pixel, out d))
        {
            return(false);
        }

        //Extract world position using screen-to-world transform.
        worldPos = cam.ScreenToWorldPoint(new Vector3(pixel.x, pixel.y, d));
        return(true);
    }
Beispiel #25
0
    void Start()
    {
        //Set textures to the shader
        //matRGB = canvas.GetComponent<Renderer>().material;
        sl.ZEDCamera zedCamera = sl.ZEDCamera.GetInstance();
        if (videoType == sl.VIEW.LEFT_GREY || videoType == sl.VIEW.RIGHT_GREY || videoType == sl.VIEW.LEFT_UNRECTIFIED_GREY || videoType == sl.VIEW.RIGHT_UNRECTIFIED_GREY)
        {
            matRGB.SetInt("_isGrey", 1);
        }
        else
        {
            matRGB.SetInt("_isGrey", 0);
        }

        //Create two textures and fill them with the ZED computed images

        // TextureFormat.RGBA32
        camZedLeft = zedCamera.CreateTextureImageType(videoType);

        // TextureFormat.RGBAFloat,
        depthXYZZed = zedCamera.CreateTextureMeasureType(sl.MEASURE.XYZ);
        matRGB.SetTexture("_CameraTex", camZedLeft);
        matRGB.SetTexture("_DepthXYZTex", depthXYZZed);

        //matEncoded.SetTexture("_CameraTex", camZedLeft);
        matEncoded.SetTexture("_DepthXYZTex", depthXYZZed);


        if (zedCamera.CameraIsReady)
        {
            mainCamera.fieldOfView      = zedCamera.GetFOV() * Mathf.Rad2Deg;
            mainCamera.projectionMatrix = zedCamera.Projection;

            scale(canvas.gameObject, GetFOVFromProjectionMatrix(mainCamera.projectionMatrix));
        }
        else
        {
            scale(canvas.gameObject, mainCamera.fieldOfView);
        }
    }
Beispiel #26
0
    /// <summary>
    /// Checks if the real world at an image pixel is visible from the camera (true) or masked by a virtual object (with a collider).
    /// </summary>
    /// <warning>The virtual object must have a collider for this to work as it uses a collision test.</warning>
    /// <param name="pixel">Screen space coordinates of the real-world pixel.</param>
    /// <param name="cam">Unity Camera used for world-camera space conversion (usually left camera)</param>
    /// <returns>True if visible, false if obscurred.</returns>
    public static bool IsPixelVisible(sl.ZEDCamera zedCam, Vector2 pixel, Camera cam)
    {
        if (zedCam == null)
        {
            return(false);
        }

        RaycastHit hit;
        float      d;

        GetForwardDistanceAtPixel(zedCam, pixel, out d);
        Vector3 position = cam.ScreenToWorldPoint(new Vector3(pixel.x, pixel.y, d));

        if (Physics.Raycast(cam.transform.position, position - cam.transform.position, out hit))
        {
            if (hit.distance < d)
            {
                return(false);
            }
        }
        return(true);
    }
Beispiel #27
0
    void ZEDReady()
    {
        //Add the fader
        gameObject.AddComponent <LoadingFade>();
        zedCamera = sl.ZEDCamera.GetInstance();
        SetTextures(zedCamera, viewMode);
        canvas.SetActive(true);
        canvas.transform.SetParent(mainCamera.transform);
        ConfigureLightAndShadow(mainCamera.actualRenderingPath);

        //Move the plane with the optical centers
        float   plane_distance = 0.15f;
        Vector4 opticalCenters = zedCamera.ComputeOpticalCenterOffsets(plane_distance);

        if (side == 0)
        {
            canvas.transform.localPosition = new Vector3(opticalCenters.x, -1.0f * opticalCenters.y, plane_distance);
        }
        else if (side == 1)
        {
            canvas.transform.localPosition = new Vector3(opticalCenters.z, -1.0f * opticalCenters.w, plane_distance);
        }


        //Set the camera parameters and scale the screen
        if (zedCamera.IsCameraReady)
        {
            mainCamera.fieldOfView      = zedCamera.VerticalFieldOfView * Mathf.Rad2Deg;
            mainCamera.projectionMatrix = zedCamera.Projection;
            mainCamera.nearClipPlane    = 0.1f;
            mainCamera.farClipPlane     = 500.0f;
            scale(canvas.gameObject, GetFOVYFromProjectionMatrix(mainCamera.projectionMatrix));
        }
        else
        {
            scale(canvas.gameObject, mainCamera.fieldOfView);
        }
    }
Beispiel #28
0
    /// <summary>
    /// Sets references not set in ZEDManager.CreateZEDRigDisplayer(), sets materials,
    /// adjusts final plane scale, loads the ZED calibration offset and other misc. values.
    /// </summary>
    void Start()
    {
        hasVRDevice = UnityEngine.XR.XRDevice.isPresent;
        manager     = transform.parent.GetComponent <CustomZedManager>();
        zedCamera   = sl.ZEDCamera.GetInstance();
        // leftScreen = ZEDEyeLeft.GetComponent<ZEDRenderingPlane>();
        // rightScreen = ZEDEyeRight.GetComponent<ZEDRenderingPlane>();
        // finalLeftEye = finalCameraLeft.GetComponent<Camera>();
        // finalRightEye = finalCameraRight.GetComponent<Camera>();

        // rightMaterial = quadRight.GetComponent<Renderer>().material;
        // leftMaterial = quadLeft.GetComponent<Renderer>().material;
        // finalLeftEye.SetReplacementShader(leftMaterial.shader, "");
        // finalRightEye.SetReplacementShader(rightMaterial.shader, "");

        // float plane_dist = (float)sl.Constant.PLANE_DISTANCE;
        // scale(quadLeft.gameObject,  new Vector2(1.78f*plane_dist, 1.0f*plane_dist));
        // scale(quadRight.gameObject,  new Vector2(1.78f*plane_dist, 1.0f*plane_dist));
        zedReady            = false;
        Camera.onPreRender += PreRender;

        LoadHmdToZEDCalibration();
    }
Beispiel #29
0
    /// <summary>
    /// Performs a "raycast" by checking for collisions/hit in a series of points on a ray.
    /// Calls HitTestAtPoint at each point on the ray, spaced apart by distbetweendots.
    /// </summary>
    /// <param name="camera">Unity Camera used for world-camera space conversion (usually left camera)</param>
    /// <param name="startpos">Starting position of the ray</param>
    /// <param name="rot">Direction of the ray.</param>
    /// <param name="maxdistance">Maximum distance of the ray</param>
    /// <param name="distbetweendots">Distance between sample dots. 1cm (0.01f) is recommended for most casses, but
    /// increase to improve performance at the cost of accuracy.</param>
    /// <param name="collisionpoint">Fills the point where the collision occurred, if any.</param>
    /// <param name="countinvalidascollision">Whether a collision that can't be tested (such as when it's off-screen)
    /// is counted as hitting something.</param>
    /// <param name="realworldthickness">Sets the assumed thickness of the real world. Points further away than the world by
    /// more than this amount won't return true, considered "behind" the real world instead of inside it.</param>
    /// <returns></returns>
    public static bool HitTestOnRay(sl.ZEDCamera zedCam, Camera camera, Vector3 startpos, Quaternion rot, float maxdistance, float distbetweendots, out Vector3 collisionpoint,
                                    bool countinvalidascollision = false, float realworldthickness = Mathf.Infinity)
    {
        collisionpoint = Vector3.zero;

        if (zedCam == null)
        {
            return(false);
        }

        //Check for occlusion in a series of dots, spaced apart evenly.
        Vector3 lastvalidpoint = startpos;

        for (float i = 0; i < maxdistance; i += distbetweendots)
        {
            Vector3 pointtocheck = rot * new Vector3(0f, 0f, i);
            pointtocheck += startpos;

            bool hit = HitTestAtPoint(zedCam, camera, pointtocheck, countinvalidascollision, realworldthickness);

            if (hit)
            {
                //Return the last valid place before the collision.
                collisionpoint = lastvalidpoint;
                return(true);
            }
            else
            {
                lastvalidpoint = pointtocheck;
            }
        }

        //There was no collision at any of the points checked.
        collisionpoint = lastvalidpoint;
        return(false);
    }
Beispiel #30
0
    /***********************************************************************************************
    ********************             HIT TEST  FUNCTIONS             ******************************
    ***********************************************************************************************/

    /// <summary>
    /// Static functions for checking collisions or 'hits' with the real world. This does not require
    /// scanning/spatial mapping or plane detection as it used the live depth map.
    /// Each is based on the premise that if a point is behind the real world, it has intersected with it (except when
    /// using realworldthickness). This is especially when checked each frame on a moving object, like a projectile.
    /// In each function, "countinvalidascollision" specifies if off-screen pixels or missing depth values should count as collisions.
    /// "realworldthickness" specifies how far back a point needs to be behind the real world before it's not considered a collision.
    /// </summary>



    /// <summary>
    /// Checks an individual point in world space to see if it's occluded by the real world.
    /// </summary>
    /// <param name="camera">Unity Camera used for world-camera space conversion (usually left camera).</param>
    /// <param name="point">3D point in the world that belongs to a virtual object.</param>
    /// <param name="countinvalidascollision">Whether a collision that can't be tested (such as when it's off-screen)
    /// is counted as hitting something.</param>
    /// <param name="realworldthickness">Sets the assumed thickness of the real world. Points further away than the world by
    /// more than this amount won't return true, considered "behind" the real world instead of inside it.</param>
    /// <returns>True if the test represents a valid hit test.</returns>
    public static bool HitTestAtPoint(sl.ZEDCamera zedCam, Camera camera, Vector3 point, bool countinvalidascollision = false, float realworldthickness = Mathf.Infinity)
    {
        if (zedCam == null)
        {
            return(false);
        }

        //Transform the point into screen space.
        Vector3 screenpoint = camera.WorldToScreenPoint(point);

        //Make sure it's within our view frustrum (excluding clipping planes).
        if (!CheckScreenView(point, camera))
        {
            return(countinvalidascollision);
        }

        //Compare distance in virtual camera to corresponding point in distance map.
        float realdistance;

        GetEuclideanDistanceAtPixel(zedCam, new Vector2(screenpoint.x, screenpoint.y), out realdistance);

        //If we pass bad parameters, or we don't have an accurate reading on the depth, we can't test.
        if (realdistance <= 0f)
        {
            return(countinvalidascollision);            //We can't read the depth from that pixel.
        }

        if (realdistance <= Vector3.Distance(point, camera.transform.position) && Vector3.Distance(point, camera.transform.position) - realdistance <= realworldthickness)
        {
            return(true);            //The real pixel is closer or at the same depth as the virtual point. That's a collision (unless closer by more than realworldthickness).
        }
        else
        {
            return(false);         //The real pixel is behind the virtual point.
        }
    }