技术背景

随着技术的不断进步和应用的不断深化,Unity3D VR应用的前景非常广阔,它广泛应用于教育、医疗、军事、工业设计、虚拟数字人等多个领域。

教育领域:Unity3D VR技术可以用来创建虚拟现实教室,让学生能够身临其境地体验课程内容,提高学习效果和兴趣;医疗领域:Unity3D VR技术可以用来创建虚拟手术室,让医生能够在真实手术之前进行模拟操作,提高手术技能和安全性;军事领域:Unity3D VR技术可以用来创建虚拟战场环境,进行军事训练和战术演练,提高士兵的战斗能力和应变能力;工业设计领域:Unity3D VR技术可以用来创建虚拟现实工作环境,让设计师能够在真实产品推出之前进行虚拟测试和修改,提高产品设计和制造的效率和质量。虚拟数字人是指使用虚拟现实技术创建的数字人物,具有人的外貌、动作、语言和思维等特征。VR虚拟数字人可以用来进行虚拟互动、虚拟演讲、虚拟展览、虚拟客服等多种应用场景。例如,在虚拟展览中,VR虚拟数字人可以作为虚拟讲解员,为参观者介绍展品,提供全方位的互动体验。在虚拟客服中,VR虚拟数字人可以作为企业形象代表,与消费者进行互动交流,提高客户满意度和品牌形象。

技术实现

从技术的角度,分析如何在unity环境下,采集到camera数据,然后编码打包推RTMP或启动轻量级RTSP服务。我们老早实现了Unity环境下的RTMP低延迟推送,原生环境下,比如windows下,可轻松实现50帧+的编码和RTMP推送(需要播放端也有高帧率播放的能力)。

数据源是高帧率的基础,比如,我们在跟外部公司合作的时候,比如无人机在一些工业场景下的智能躲避等,帧率要求非常高,这时候,如果单独还好,多路的话,ReadPixel()读取数据耗时还是非常大的。读取到的数据,特别是高分辨率高帧率的,编码一般建议硬编码,帧率的控制,需要有个好的算法机制,确保比如我可以采集到60帧,但是我实际值需要编码45帧,如何drop数据,达到流畅无卡顿感。

此外,除了视频数据外,音频可以采集麦克风、Unity内部音频、麦克风+unity内部音频混音或Unity下2路内部音频混音。Unity内部audio数据采集,可以使用AudioClip,编码格式建议AAC。

以Windows平台为例,Frame的构建,可以参考一下设计:

/*

* 构建FrameTexture

* Author: daniusdk.com

*/

public class FrameTexture

{

public FrameTexture(Texture2D texture, IntPtr video_buffer, int video_buffer_size,

int video_width, int video_height, int is_vertical_flip, int is_horizontal_flip, int scale_width, int scale_height, bool is_alpha)

{

texture_ = texture;

video_buffer_ = video_buffer;

video_buffer_size_ = video_buffer_size;

video_width_ = video_width;

video_height_ = video_height;

is_vertical_flip_ = is_vertical_flip;

is_horizontal_flip_ = is_horizontal_flip;

scale_width_ = scale_width;

scale_height_ = scale_height;

is_alpha_ = is_alpha;

}

public Texture2D texture_;

public IntPtr video_buffer_;

public int video_buffer_size_;

public int video_width_;

public int video_height_;

public int is_vertical_flip_;

public int is_horizontal_flip_;

public int scale_width_;

public int scale_height_;

public bool is_alpha_;

}

PostImageWorker类,实现数据投递到原始模块:

private class PostImageWorker

{

public PostImageWorker(TexturesPool pool, nt_publisher_wrapper handle)

{

pool_ = pool;

handle_ = handle;

}

public void run()

{

if (null == pool_ || null == handle_)

return;

while (!is_exit_)

{

event_.WaitOne(100);

if (is_exit_)

break;

while (sendImage()) ;

}

Debug.Log("PostImageWorker.run out...");

FrameTexture frame;

while (frames_.TryDequeue(out frame))

{

if (frame != null && frame.texture_)

{

pool_.add(frame.texture_);

frame.texture_ = null;

}

}

frame = null;

}

private bool sendImage()

{

FrameTexture frame;

if (frames_.TryDequeue(out frame))

{

if (frame != null && frame.texture_ != null)

{

if (frame.video_buffer_ != IntPtr.Zero)

{

handle_.OnPostRGBXData(0, frame.video_buffer_, video_buffer_size_, frame.video_width_ * 4, frame.video_width_, -frame.video_height_, frame.is_alpha_);

}

pool_.add(frame.texture_);

frame.texture_ = null;

}

frame = null;

return true;

}

frame = null;

return false;

}

Windows平台,构建个承载的图层:

NT_PB_ExternalVideoFrameLayerConfig external_layer_c1 = new NT_PB_ExternalVideoFrameLayerConfig();

external_layer_c1.base_.type_ = (Int32)NTSmartPublisherDefine.NT_PB_E_LAYER_TYPE.NT_PB_E_LAYER_TYPE_EXTERNAL_VIDEO_FRAME;

external_layer_c1.base_.index_ = 0;

external_layer_c1.base_.enable_ = 1;

external_layer_c1.base_.region_.x_ = 0;

external_layer_c1.base_.region_.y_ = 0;

external_layer_c1.base_.region_.width_ = video_width_;

external_layer_c1.base_.region_.height_ = video_height_;

external_layer_c1.base_.offset_ = Marshal.OffsetOf(external_layer_c1.GetType(), "base_").ToInt32();

external_layer_c1.base_.cb_size_ = (uint)Marshal.SizeOf(external_layer_c1);

IntPtr external_layer_conf = Marshal.AllocHGlobal(Marshal.SizeOf(external_layer_c1));

Marshal.StructureToPtr(external_layer_c1, external_layer_conf, true);

UInt32 external_r = NTSmartPublisherSDK.NT_PB_AddLayerConfig(publisher_handle_, 0,

external_layer_conf, (int)NTSmartPublisherDefine.NT_PB_E_LAYER_TYPE.NT_PB_E_LAYER_TYPE_EXTERNAL_VIDEO_FRAME,

0, IntPtr.Zero);

Marshal.FreeHGlobal(external_layer_conf);

然后通过NT_PB_PostLayerImage()给图层投递数据即可:

/*

* 给index层投递Image数据,目前主要是用来把rgb和yuv视频数据传给相关层

* reserve: 保留字段,请传0

* index: 层索引

* image: 图像

* flag: 请传0

* pReserve: 保留字段,请传0

*

* 成功返回 NT_ERC_OK

*/

[DllImport("SmartPublisherSDK", EntryPoint = "NT_PB_PostLayerImage", CallingConvention = CallingConvention.StdCall)]

public static extern UInt32 NT_PB_PostLayerImage(IntPtr handle, Int32 reserve,

Int32 index, IntPtr image,

UInt32 flag, IntPtr pReserve);

如果需要预览推送的数据:

//预览数据回调

public void SDKVideoPreviewImageCallBack(IntPtr handle, IntPtr user_data, IntPtr image)

{

NT_PB_Image pb_image = (NT_PB_Image)Marshal.PtrToStructure(image, typeof(NT_PB_Image));

NT_VideoFrame pVideoFrame = new NT_VideoFrame();

pVideoFrame.width_ = pb_image.width_;

pVideoFrame.height_ = pb_image.height_;

pVideoFrame.stride_ = pb_image.stride_[0];

Int32 argb_size = pb_image.stride_[0] * pb_image.height_;

pVideoFrame.plane_data_ = new byte[argb_size];

if (argb_size > 0)

{

Marshal.Copy(pb_image.plane_[0],pVideoFrame.plane_data_,0, argb_size);

}

{

cur_image_ = pVideoFrame;

}

}

总结

Unity下的“多端同屏”云渲染以及相关可视化平台解决方案,成为助力了工业领域数字化转型。除上述场景外,还需要考虑多实例多camera模式,实现高效率低延迟和低资源占有的互动体验。

 

文章链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: