《图说VR入门》——360全景视频
本文章由cartzhang编写,转载请注明出处。 全部权利保留。  
文章链接:http://blog.csdn.net/cartzhang/article/details/53674647 
作者:cartzhang
《图说VR入门》——360全景视频
本章用使用较早的Unity OC 插件来实现一个360全景视频。且通过使用不同的路径配置,能够任意切换视频内容。这样省去了多次打包的过程。简单易用。
 
当然,你能够花费40刀来购买一个。 
https://www.assetstore.unity3d.com/cn/#!/content/35102 
这个还是听贵的。
 
还是制作一个免费的吧!!
!
 
打包后的可下载地址,没有password: 
OC 版本号 0.8.0 云盘地址: 
http://pan.baidu.com/s/1nv4QLuD 
OC版本号0.4.4云盘地址: 
http://pan.baidu.com/s/1jI8uDRC
一、所需资源
 
简单制作一个360的全景视频,须要资源有: 
AVPro Windows Media2.9.0 unity插件 
下载地址: 
http://pan.baidu.com/s/1bz7pc6 
 
须要安装OC的0.4.4runtime: 
http://pan.baidu.com/s/1jIJPXCa
 
图1 
 
Untiy所使用版本号:
 
图2
二、制作一个360的VR视频
1.导入插件
 
导入AVPro Windows Media2.9.0插件
 
图6 
 
找到他给出的预制体例如以下图:
 
图7 
 
然后选择视频文件:
 
图8
2.解码器安装
 
若这时候执行,就会报错,例如以下 
依据报错: 
For MP4 files you need an MP4 splitter such as Haali Media Splitter or GDCL.
 
图9 
 
若感兴趣想深入了解的,能够看看例如以下地址:
http://haali.su/mkv/ 
 
若来这里直接下载所须要的解码器: 
下载地址: 
 
然后安装:
 
图10
 
图11 
 
然后再次载入Load測试: 
若看到例如以下就说明解码器成功安装
 
图12
3.设置视频载入配置和执行设置
 
首先,须要把原来的主相机屏蔽,然后加入OC的相机预制体
 
图13 
 
然后,加入写好的配置文件代码:
 
图15 
 
最后要注意。不要勾选Virtual Reality supported选项。由于我们使用的是低版本号的OCruntime插件。
 
图14 
 
当然,我也打包測试了OC 0.8.0的Runtime,可能相对与0.4.4版本号,OC造成的崩溃可能性会小点。这个没有具体測试,仅仅是相信Unity嵌入和OC的版本号升级优化能力。 
打包后的文件也会在后面给出下载链接地址。
三、使用配置文件来打包后随时改动视频内容
 
直接改动AVProWindowsMediaMovie.cs中的LoadMovie函数。
        bool allowNativeFormat = (_colourFormat != ColourFormat.RGBA32);
        //string filePath = GetFilePath();
        string filePath = ConfigFile.Cconfig.VediofullPathName;
        if (_moviePlayer.StartVideo(filePath, allowNativeFormat, _colourFormat == ColourFormat.YCbCr_HD, _allowAudio, _useAudioDelay, _useAudioMixer, _useDisplaySync, _ignoreFlips, _textureFilterMode, _textureWrapMode))
        {
            if (_allowAudio)
            {
                _moviePlayer.Volume = _volume;
                _moviePlayer.AudioBalance = _audioBalance;
            }
            _moviePlayer.Loop = _loop;
            if (autoPlay)
            {
                _moviePlayer.Play();
            }
        }
        else事实上所要改动的仅仅是视频的载入路径。仅此而已。 
仅仅需更改:
//string filePath = GetFilePath();
        string filePath = ConfigFile.Cconfig.VediofullPathName;这样就能够了。 
 
配置文件的脚本:
using UnityEngine;
using System;
using System.IO;
using System.Xml.Serialization;
public class ConfigFile : MonoBehaviour {
    public static ConfigFile Instance { private set; get; }
    public static ClientConfigReference Cconfig;
    void Awake()
    {
        try
        {
            ReadConfigFile(Application.streamingAssetsPath + "/Configfile.xml");
        }
        catch (Exception ex)
        {
            Debug.LogError("Read XML Failed !  " + ex.Message);
        }
    }
    void ReadConfigFile(string filePath)
    {
        try
        {
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            XmlSerializer xs = new XmlSerializer(typeof(ClientConfigReference));
            Cconfig = xs.Deserialize(fs) as ClientConfigReference;
            fs.Dispose();
            Debug.Log("Configfile :" + filePath);
        }
        catch (Exception ex)
        {
            Debug.LogError("Read ConfigFile Failed !  " + ex.Message);
        }
    }
}
// configure class
public class ClientConfigReference
{
    public string VediofullPathName;
}
 
这个仅仅须要挂载在场景中就可以。不须要不论什么操作。
 
 
图15
四、球内使用的shader
 
360SphereVideo中sphere中使用的材质的Shader.
Shader "Unlit/InsideSphere"
{
    Properties
    {
        // we have removed support for texture tiling/offset,
        // so make them not be displayed in material inspector
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Cull Off
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            struct appdata
            {
                float4 vertex : POSITION; // vertex position
                float2 uv : TEXCOORD0; // texture coordinate
            };
            struct v2f
            {
                float2 uv : TEXCOORD0; // texture coordinate
                float4 vertex : SV_POSITION; // clip space position
            };
            uniform float4 _MainTex_ST;
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);                
                o.uv.xy = TRANSFORM_TEX(float2(1-v.uv.x, v.uv.y), _MainTex);
                return o;
            }
            sampler2D _MainTex;
            fixed4 frag (v2f i) : SV_Target
            {
                // sample texture and return it
                fixed4 col = tex2D(_MainTex, i.uv);
                return col;
            }
            ENDCG
        }
    }
} 
有兴趣的自行研究下。
五、打包后执行測试:
1. 先使用的星空:
资源能够在文章后面地址下载: 
看看漂亮星空: 
 
图16
 
图17
 
图18
 
图19 
 
能够看到。这是单眼效果,由于使用大鹏头盔,使用复制屏的效果,分辨率不正确造成的。可是在直接执行过中。不影响使用和体验。
2. 来看美女了
通过切换配置文件里
 <VediofullPathName>H:\Unity\UnitySay\360Video\360Demo\360VedioTest\Assets\StreamingAssets\猫耳娘SEASON 2.mp4</VediofullPathName>来切换视频内容了。 
 
图20
 
图21
 
图22
 
图23 
 
由于纹理清晰度不是非常高,这个须要提高的。 
还有就是由于使用旧版的OC,所以可能会崩溃。崩溃日志例如以下: 
检重要的贴出来
========== OUTPUTING STACK TRACE ==================
  ERROR: SymGetSymFromAddr64, GetLastError: '试图訪问无效的地址。
' (Address: 6CF02036)
6CF02036 (OculusPlugin) 
6CF03012 (OculusPlugin) OVR_SetViewport
6CF030D3 (OculusPlugin) UnitySetGraphicsDevice
01040016 (001) RectT<int>::Contains
01040058 (001) RectT<int>::Contains
0104054B (001) RectT<int>::Contains
100399D8 (mono) mono_lookup_pinvoke_call
100484DE (mono) mono_marshal_string_to_utf16
100CF9DD (mono) mono_inst_name
100EF02C (mono) mono_set_defaults
100EFE79 (mono) mono_set_defaults
100F03D4 (mono) mono_set_defaults
100F059D (mono) mono_set_defaults
1005D872 (mono) mono_runtime_invoke
00FC47FE (001) scripting_gchandle_get_target
01077709 (001) ScriptingArguments::AddString
0107777E (001) ScriptingArguments::AddString
010777DC (001) ScriptingArguments::AddString
00FB3462 (001) ReportScriptingObjectsTransfer::TransferTypeless
00FB3669 (001) ReportScriptingObjectsTransfer::TransferTypeless
00FB5988 (001) GetMonoBehaviourInConstructor
00FB3B7C (001) ReportScriptingObjectsTransfer::TransferTypeless
0109768B (001) RegisterAllowNameConversionInDerivedTypes
0109785D (001) RegisterAllowNameConversionInDerivedTypes
01050908 (001) CallbackArray3<std::basic_string<char,std::char_traits<char>,stl_allocator<char,59,16> > const &,AwakeFromLoadQueue &,enum LoadSceneOperation::LoadingMode>::Invoke
01050B0F (001) CallbackArray3<std::basic_string<char,std::char_traits<char>,stl_allocator<char,59,16> > const &,AwakeFromLoadQueue &,enum LoadSceneOperation::LoadingMode>::Invoke
....
========== END OF STACKTRACE ===========
就这样。
 
 
所以,还是刚才说的。你能够使用更高版本号的OC的0.8.0版本号的打包版本号。
六、资源下载地址:
 
猫耳娘视频地址云盘下载:  https://pan.baidu.com/s/1qYtVhfe 
 
星空视频云盘下载:https://pan.baidu.com/s/1c2rBNo8 
 
很多其它可地址:http://vr.diyiapp.com/vrsp/ 
 
project下载github地址: 
https://github.com/cartzhang/ImgSayVRabc/tree/master/360Vedio/360Demo/360VedioTest 
 
project下载后,须要把视频文件放在StreamingAssets下。然后使用配置文件Configfile.xml就能够使用了。
 
 
projectrelease地址:打包后,直接在StreamingAssets下加入新的视频文件,然后使用配置文件Configfile.xml就能够使用。就是这么简单,快捷。
 
解码器下载地址: 
http://pan.baidu.com/s/1o80fjXS 
http://pan.baidu.com/s/1mhVbk5U 
 
OC 0.4.4插件下载地址: 
http://pan.baidu.com/s/1gfA2TwR 
 
AVPro Windows Media2.9.0 下载地址: 
http://pan.baidu.com/s/1gfPvXyr 
 
360视频游戏打包完整地址: 
OC版本号0.4.4云盘地址: 
http://pan.baidu.com/s/1jI8uDRC 
 
OC 版本号 0.8.0 云盘地址: 
http://pan.baidu.com/s/1nv4QLuD
七、參考
[1]. http://www.panduoduo.net/s/name/%E5%85%A8%E6%99%AF%E8%A7%86%E9%A2%91
[2]. http://bernieroehl.com/360stereoinunity/
[3]. http://download.videolan.org/pub/videolan/x264/binaries/win64/
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号