德州市网站建设,广州网站设计十年乐云seo,软件技术有限公司,门户网站建设工作管理办法技术背景
Unity3D可以用于创建各种类型的的应用程序#xff0c;包括虚拟现实、培训模拟器等。以下是一些可以使用Unity3D全景播放的场景#xff1a;
虚拟现实体验#xff1a;全景视频可以用来创建逼真的虚拟环境#xff0c;使用户能够感受到身临其境的感觉#xff1b;培…技术背景
Unity3D可以用于创建各种类型的的应用程序包括虚拟现实、培训模拟器等。以下是一些可以使用Unity3D全景播放的场景
虚拟现实体验全景视频可以用来创建逼真的虚拟环境使用户能够感受到身临其境的感觉培训模拟器全景视频可以用来创建真实的训练环境例如飞行模拟器、驾驶模拟器等以提供更加真实的训练体验建筑设计全景视频可以用来展示建筑设计的或室内装潢使客户能够感受到真实的的效果文旅导览全景视频可以用来展示旅游景点或城市使游客能够感受到身临其境的感觉。
在Unity3D平台上实现全景实时RTMP或RTSP流渲染可以通过以下方式
获取全景视频数据源首先需要拉取RTMP或RTSP流数据解码后把RGB或YUV数据回调到unity从而获取到全景视频流数据Unity创建个Sphere创建个材质球(Material)并把材质球挂在到Sphere实现实时渲染使用Unity3D的渲染管道您可以将纹理映射到球体或立方体的表面上并使用着色器来处理纹理的坐标以实现全景视频的实时渲染。
技术实现 本文以大牛直播SDK的RTMP推送端作为数据采集获取全景窗体数据后编码打包推送到RTMP服务或启动个轻量级RTSP服务对外提供个RTSP的拉流URL。
然后播放端拉取RTSP或RTMP的URL把YUV或RGB数据回调上来然后再在Unity窗体绘制出来。
获取数据源 public void Play(int sel){if (videoctrl[sel].is_running){Debug.Log(已经在播放..);return;}lock (videoctrl[sel].frame_lock_){videoctrl[sel].cur_video_frame_ null;}OpenPlayer(sel);if (videoctrl[sel].player_handle_ IntPtr.Zero)return;//设置播放URLNTSmartPlayerSDK.NT_SP_SetURL(videoctrl[sel].player_handle_, videoctrl[sel].videoUrl);/* 播放前参数配置可加在此处 */int play_buffer_time_ 0;NTSmartPlayerSDK.NT_SP_SetBuffer(videoctrl[sel].player_handle_, play_buffer_time_); //设置buffer timeint is_using_tcp 0; //TCP模式NTSmartPlayerSDK.NT_SP_SetRTSPTcpMode(videoctrl[sel].player_handle_, is_using_tcp);int timeout 10;NTSmartPlayerSDK.NT_SP_SetRtspTimeout(videoctrl[sel].player_handle_, timeout);int is_auto_switch_tcp_udp 1;NTSmartPlayerSDK.NT_SP_SetRtspAutoSwitchTcpUdp(videoctrl[sel].player_handle_, is_auto_switch_tcp_udp);Boolean is_mute_ false;NTSmartPlayerSDK.NT_SP_SetMute(videoctrl[sel].player_handle_, is_mute_ ? 1 : 0); //是否启动播放的时候静音int is_fast_startup 1;NTSmartPlayerSDK.NT_SP_SetFastStartup(videoctrl[sel].player_handle_, is_fast_startup); //设置快速启动模式Boolean is_low_latency_ false;NTSmartPlayerSDK.NT_SP_SetLowLatencyMode(videoctrl[sel].player_handle_, is_low_latency_ ? 1 : 0); //设置是否启用低延迟模式//设置旋转角度(设置0 90 180 270度有效其他值无效)int rotate_degrees 0;NTSmartPlayerSDK.NT_SP_SetRotation(videoctrl[sel].player_handle_, rotate_degrees);int volume 100;NTSmartPlayerSDK.NT_SP_SetAudioVolume(videoctrl[sel].player_handle_, volume); //设置播放音量, 范围是[0, 100], 0是静音100是最大音量, 默认是100// 设置上传下载报速度int is_report 0;int report_interval 2;NTSmartPlayerSDK.NT_SP_SetReportDownloadSpeed(videoctrl[sel].player_handle_, is_report, report_interval);/* -- 播放前参数配置可加在此处 -- *///video frame callback (YUV/RGB)videoctrl[sel].video_frame_call_back_ new SP_SDKVideoFrameCallBack(NT_SP_SetVideoFrameCallBack);NTSmartPlayerSDK.NT_SP_SetVideoFrameCallBack(videoctrl[sel].player_handle_, (Int32)NT.NTSmartPlayerDefine.NT_SP_E_VIDEO_FRAME_FORMAT.NT_SP_E_VIDEO_FRAME_FROMAT_I420, window_handle_, videoctrl[sel].video_frame_call_back_);UInt32 flag NTSmartPlayerSDK.NT_SP_StartPlay(videoctrl[sel].player_handle_);if (flag DANIULIVE_RETURN_OK){videoctrl[sel].is_need_get_frame_ true;Debug.Log(播放成功);}else{videoctrl[sel].is_need_get_frame_ false;Debug.LogError(播放失败);}videoctrl[sel].is_running true;}
针对数据处理 private void SDKVideoFrameCallBack(UInt32 status, IntPtr frame, int sel){//这里拿到回调frame进行相关操作NT_SP_VideoFrame video_frame (NT_SP_VideoFrame)Marshal.PtrToStructure(frame, typeof(NT_SP_VideoFrame));VideoFrame u3d_frame new VideoFrame();u3d_frame.width_ video_frame.width_;u3d_frame.height_ video_frame.height_;u3d_frame.timestamp_ (UInt64)video_frame.timestamp_;int d_y_stride video_frame.width_;int d_u_stride (video_frame.width_ 1) / 2;int d_v_stride d_u_stride;int d_y_size d_y_stride * video_frame.height_;int d_u_size d_u_stride * ((video_frame.height_ 1) / 2);int d_v_size d_u_size;int u_v_height ((u3d_frame.height_ 1) / 2);u3d_frame.y_stride_ d_y_stride;u3d_frame.u_stride_ d_u_stride;u3d_frame.v_stride_ d_v_stride;u3d_frame.y_data_ new byte[d_y_size];u3d_frame.u_data_ new byte[d_u_size];u3d_frame.v_data_ new byte[d_v_size];CopyFramePlane(u3d_frame.y_data_, d_y_stride,video_frame.plane0_, video_frame.stride0_, u3d_frame.height_);CopyFramePlane(u3d_frame.u_data_, d_u_stride,video_frame.plane1_, video_frame.stride1_, u_v_height);CopyFramePlane(u3d_frame.v_data_, d_v_stride,video_frame.plane2_, video_frame.stride2_, u_v_height);lock (videoctrl[sel].frame_lock_ ){videoctrl[sel].cur_video_frame_ u3d_frame;}}
刷新Texture
private void UpdateYUVTexture(VideoFrame video_frame, int sel)
{if (video_frame.y_data_ null || video_frame.u_data_ null || video_frame.v_data_ null){Debug.Log(video frame with null..);return;}if (videoctrl[sel].yTexture_ ! null){videoctrl[sel].yTexture_.LoadRawTextureData(video_frame.y_data_);videoctrl[sel].yTexture_.Apply();}if (videoctrl[sel].uTexture_ ! null){videoctrl[sel].uTexture_.LoadRawTextureData(video_frame.u_data_);videoctrl[sel].uTexture_.Apply();}if (videoctrl[sel].vTexture_ ! null){videoctrl[sel].vTexture_.LoadRawTextureData(video_frame.v_data_);videoctrl[sel].vTexture_.Apply();}
}
全景播放的时候如果需要移动显示区域可以用以下代码
public class MouseMove : MonoBehaviour {Vector2 p1, p2;private Vector3 PreMouseMPos;private Vector3 PreMouseLPos;public float minimumY -60F;public float maximumY 60F;float rotationY 0F;private float wheelSpeed 5.0f;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {if (Input.GetMouseButton(0)){if (PreMouseLPos.x 0){PreMouseLPos new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);}else{Vector3 CurMouseLPos new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);Vector3 offset CurMouseLPos - PreMouseLPos;Quaternion tt Quaternion.Euler(offset);float rotationX transform.localEulerAngles.y - tt.x * 20;rotationY tt.y * 20;rotationY Mathf.Clamp(rotationY, minimumY, maximumY);transform.localEulerAngles new Vector3(rotationY, rotationX, 0);PreMouseLPos CurMouseLPos;}}else{PreMouseLPos new Vector3(0.0f, 0.0f, 0.0f);}}
}总结
Unity全景播放RTMP或RTSP实时流可以广泛用于各种需要提供真实场景或沉浸式体验的场景为用户带来更加逼真的体验。与此同时Unity全景实时播放需要有非常高的延迟要求和性能要求特别是全景数据源分辨率和码率都非常高对解码效率和解码后的数据拷贝投递提了更高的要求。