Unity APP 实例
Xvisio SDK 文档主页

Viewer

1. Viewer 基础场景

场景:Viewer.unity
在此场景中我们将左右鱼眼图像、rgb摄像头图像以及Tof图像显示在三维空间中。
使用手势点击场景中左侧的按钮可以打开场景中对应内容的图像数据。
image


2. Rgb图像显示

if( API.xslam_ready()&&XvXR.Engine.XvisioDeviceManager.Manager.isRgbOn()){
    		int width = API.xslam_get_rgb_width();
    		int height = API.xslam_get_rgb_height();
    		if( width > 0 && height > 0 ){
				if( lastWidth != width || lastHeight != height ){
                    try{
                        double r = 1.0;
                        int w = (int)(width * r);
                        int h = (int)(height * r);
                        Debug.Log("Create RGB texture " + w + "x" + h);
                        TextureFormat format = TextureFormat.RGBA32;
                        tex = new Texture2D(w, h, format, false);
                        tex.Apply();
                        try {
                            pixelHandle.Free();
                        } catch {}
                        pixel32 = tex.GetPixels32();
                        pixelHandle = GCHandle.Alloc(pixel32, GCHandleType.Pinned);
                        pixelPtr = pixelHandle.AddrOfPinnedObject();
                        GetComponent<Renderer>().material.mainTexture = tex;
                    }catch (Exception e)
                    {
                        Debug.LogException(e, this);
                        return;
                    }
                    lastWidth = width;
                    lastHeight = height;
				}

                try
                {
                    if( API.xslam_get_rgb_image_RGBA(pixelPtr,tex.width, tex.height, ref rgbTimestamp) ){
                        Debug.Log("vr_log:pixelPtr RGBRecord Update xslam_get_rgb_image_RGBA pixelPtr: " + pixelPtr);
                        Debug.Log("vr_log:tex.width RGBRecord Update xslam_get_rgb_image_RGBA width: " + tex.width);
                        Debug.Log("vr_log:tex.height RGBRecord Update xslam_get_rgb_image_RGBA height: " + tex.height);
                        Debug.Log("vr_log:timestamp RGBRecord Update xslam_get_rgb_image_RGBA rgbTimestamp: " + rgbTimestamp);
                        
                        tex.SetPixels32(pixel32);
                        tex.Apply();
                    }else{
                        Debug.Log("Invalid texture");
                    }
                }catch (Exception e)
                {
                    Debug.LogException(e, this);
                    return;
                }
            }
	    }

3. Tof图像显示

if( API.xslam_ready()&&XvXR.Engine.XvisioDeviceManager.Manager.isTofOn()){
    		int width = API.xslam_get_tof_width();
    		int height = API.xslam_get_tof_height();
    		
    		if( width > 0 && height > 0){
			
				if( !tex ){
					Debug.Log("Create TOF texture " + width + "x" + height);
					TextureFormat format = TextureFormat.RGBA32;
		    		tex = new Texture2D(width, height, format, false);
		    		
		    		
			        pixel32 = tex.GetPixels32();
        			pixelHandle = GCHandle.Alloc(pixel32, GCHandleType.Pinned);
        			pixelPtr = pixelHandle.AddrOfPinnedObject();
        			
        			GetComponent<Renderer>().material.mainTexture = tex;
				}
				
               
                if( API.xslam_get_tof_image(pixelPtr, tex.width, tex.height) ){
                    //Update the Texture2D with array updated in C++
                    tex.SetPixels32(pixel32);
                    tex.Apply();
                }else{
                    Debug.Log("Invalid TOF texture");
                }	
			}
	    }

4. Stereo图像显示

if( API.xslam_ready()&&XvXR.Engine.XvisioDeviceManager.Manager.isStereoOn()){
    	
    		int width = API.xslam_get_stereo_width();
    		int height = API.xslam_get_stereo_height();
			int size = width * height;
    		
    		if( width > 0 && height > 0 && size > 0){				
			
				if( !tex ){
					Debug.Log("Create STEREO texture " + width + "x" + height);
					TextureFormat format = TextureFormat.RGBA32;
		    		tex = new Texture2D(width, height, format, false);
		    		
		    		
			        pixel32 = tex.GetPixels32();
        			pixelHandle = GCHandle.Alloc(pixel32, GCHandleType.Pinned);
        			pixelPtr = pixelHandle.AddrOfPinnedObject();
        			
        			GetComponent<Renderer>().material.mainTexture = tex;
				}
			
                if(isLeft){
                    double ts = 0;
                    if (API.xslam_get_left_image(pixelPtr, tex.width, tex.height, ref ts) ){
                        //Update the Texture2D with array updated in C++
                        tex.SetPixels32(pixel32);
                        tex.Apply();
                    }else{
                        Debug.Log("Invalid Stereo texture");
                    }
                }else{
                    double ts = 0;
                    if (API.xslam_get_right_image(pixelPtr, tex.width, tex.height, ref ts) ){
                        //Update the Texture2D with array updated in C++
                        tex.SetPixels32(pixel32);
                        tex.Apply();
                    }else{
                        Debug.Log("Invalid Stereo texture");
                    }
                }
			}		    
	    }

Unity APP 实例
Xvisio SDK 文档主页

Viewer