IntelRealSense / librealsense

Intel® RealSense™ SDK
https://www.intelrealsense.com/
Apache License 2.0
7.53k stars 4.81k forks source link

I can't connect to RealSense 515L from Unity. #6949

Closed vertex798 closed 4 years ago

vertex798 commented 4 years ago

Required Info
Camera Model { 515 }
Firmware Version (Open RealSense Viewer --> Click info)
Operating System & Version Win 10
Kernel Version (Linux Only) (e.g. 4.14.13)
Platform PC
SDK Version 2.0
Language unity }
Segment others

Issue Description

<Describe your issue / question / feature request / etc..>

I can't connect to RealSense 515L from Unity. In the current RealSense View, it has been confirmed that the sensor is operating normally. image

However, after importing the Unity package file from the Unity project (Unity 2020.1.0f1) and executing the StartHere scene file, the following message appears.

image

When importing the Unity package file for the first time, the following error message occurred, I solved it by commenting out the part. image

The full text of the commented code is:

`using System.Collections; using System.Collections.Generic; using Intel.RealSense; using UnityEngine; using UnityEngine.Events; using UnityEngine.Rendering; using UnityEngine.XR;

public class RsARBackgroundRenderer : MonoBehaviour { public RsFrameProvider Source; public Material material; private Camera cam; //private ARBackgroundRenderer bg; private Intrinsics intrinsics; private RenderTexture rt; Vector2Int screenSize;

IEnumerator Start()
{
    yield return new WaitUntil(() => Source && Source.Streaming);

    using (var profile = Source.ActiveProfile.GetStream<VideoStreamProfile>(Stream.Color))
    {
        intrinsics = profile.GetIntrinsics();
    }

    cam = GetComponent<Camera>();

    //bg = new ARBackgroundRenderer()
    //{
    //    backgroundMaterial = material,
    //    mode = ARRenderMode.MaterialAsBackground,
    //    backgroundTexture = material.mainTexture
    //};

    cam.depthTextureMode |= DepthTextureMode.Depth;

    // Uses the same material as above to update camera's depth texture
    // Unity will use it when calculating shadows
    var updateCamDepthTexture = new CommandBuffer() { name = "UpdateDepthTexture" };
    updateCamDepthTexture.Blit(BuiltinRenderTextureType.None, BuiltinRenderTextureType.CurrentActive, material);
    updateCamDepthTexture.SetGlobalTexture("_ShadowMapTexture", Texture2D.whiteTexture);
    cam.AddCommandBuffer(CameraEvent.AfterDepthTexture, updateCamDepthTexture);

    // assume single directional light
    var light = FindObjectOfType<Light>();

    // Copy resulting screenspace shadow map, ARBackgroundRenderer's material will multiply it over color image
    var copyScreenSpaceShadow = new CommandBuffer { name = "CopyScreenSpaceShadow" };
    int shadowCopyId = Shader.PropertyToID("_ShadowMapTexture");
    copyScreenSpaceShadow.GetTemporaryRT(shadowCopyId, -1, -1, 0);
    copyScreenSpaceShadow.CopyTexture(BuiltinRenderTextureType.CurrentActive, shadowCopyId);
    copyScreenSpaceShadow.SetGlobalTexture(shadowCopyId, shadowCopyId);
    light.AddCommandBuffer(LightEvent.AfterScreenspaceMask, copyScreenSpaceShadow);
}

//void OnEnable()
//{
//    if (bg != null)
//        bg.mode = ARRenderMode.MaterialAsBackground;
//}

//void OnDisable()
//{
//    if (bg != null)
//        bg.mode = ARRenderMode.StandardBackground;
//}

void Update()
{
    if (cam == null)
        return;

    var s = new Vector2Int(Screen.width, Screen.height);
    if (screenSize != s)
    {
        screenSize = s;

        var projectionMatrix = new Matrix4x4
        {
            m00 = intrinsics.fx,
            m11 = -intrinsics.fy,
            m03 = intrinsics.ppx / intrinsics.width,
            m13 = intrinsics.ppy / intrinsics.height,
            m22 = (cam.nearClipPlane + cam.farClipPlane) * 0.5f,
            m23 = cam.nearClipPlane * cam.farClipPlane,
        };
        float r = (float)intrinsics.width / Screen.width;
        projectionMatrix = Matrix4x4.Ortho(0, Screen.width * r, Screen.height * r, 0, cam.nearClipPlane, cam.farClipPlane) * projectionMatrix;
        projectionMatrix.m32 = -1;

        cam.projectionMatrix = projectionMatrix;
    }

    // if (Input.GetMouseButtonDown(0))
    // Debug.Log(GetImagePoint(Input.mousePosition));
}

public Vector2Int GetImagePoint(Vector2 screenPos)
{
    var vp = (Vector2)Camera.main.ScreenToViewportPoint(screenPos);
    vp.y = 1f - vp.y;

    float sr = (float)Screen.width / Screen.height;
    float tr = (float)intrinsics.height / intrinsics.width;
    float sh = sr * tr;
    vp -= 0.5f * Vector2.one;
    vp.y /= sh;
    vp += 0.5f * Vector2.one;

    return new Vector2Int((int)(vp.x * intrinsics.width), (int)(vp.y * intrinsics.height));
}

} `

ogoshen commented 4 years ago

Looks like you're on a new version of Unity (2020)

Seems like the ARBackgroundRenderer has moved to a package: https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@3.1/api/UnityEngine.XR.ARFoundation.ARCameraBackground.html The two aren't compatible though, the older is still available in Unity 2019, if that's an option for you..

vertex798 commented 4 years ago

@ogoshen thank you i used using UnityEngine.XR.ARFoundation

RealSenseSupport commented 4 years ago

Were you able to get things working?

*If we don’t hear from you in 7 days, this issue will be closed.