1. 程式人生 > >Unity全景圖片加全景視訊播放(滑鼠控制視角方向自由預覽)

Unity全景圖片加全景視訊播放(滑鼠控制視角方向自由預覽)

最近也是週末做了一個全景圖片加視訊,滑鼠控制方向全景預覽和視訊預覽。

準備材料:

1.首先去歷覽器下載一個360全景圖片,網上資源很多。

2.去網上下載一個icon,ps後作為跳轉視訊的按鈕。(我就是怎麼做的,當然知識體驗效果的話,到不必要求審美)

3.除要有Unity引擎之外,我們需要一個外掛“QuickTime”用來播放視訊,當然如果你的視訊是.ogv格式的就不要外掛了。

QuickTime下載連線:連結:https://pan.baidu.com/s/1TsB41bg7wviXLxiX6F4E9Q 密碼:4grt

使用方法:直接視訊用QuickTime播放後,讓如Unity即可。

4.準備一個視訊用來播放,最好是3D高清的的要不效果會很差。

放到Sphere上的程式碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Demo : MonoBehaviour
{

    public MovieTexture mMovie;
    //電影音訊源  

    //媒體資訊定義  這裡聲音大家自己找資源,這樣效果會好點,所以也寫上去了。
    string MedName;//媒體名稱  
    string MedCount;//媒體長度  
    public Button Mybtn;
    void Start()
    {
        Mybtn.onClick.AddListener(delegate { LoadSceneFul(); });


    }
    void LoadSceneFul()
    {
        GetComponent<Renderer>().material.mainTexture = mMovie;
        mMovie.loop = true;
        //播放電影  
        mMovie.Play();
        //播放音樂  


    }

    void Update()
    {
        //在這裡更新媒體資訊  
        MedName = mMovie.name;
        MedCount = mMovie.duration.ToString();
    }
}
放到相機上的程式碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoundMain : MonoBehaviour {
    // 按下滑鼠左鍵,攝像機圍繞目標旋轉  
    //當前圍繞的目標  
    public Transform TargetTf;
    public float RotateSpeed = 10;
       //與y軸最大夾角  
    public float yMaxAngle = 160;
    // 與y軸最小夾角  
    public float yMinAngle = 120;
    void Start()
    {
        if (TargetTf == null)
            TargetTf = new GameObject("Target").transform;
        LookAtTarget();
    }
    void FixedUpdate()
    {
        float x = Input.GetAxis("Mouse X");
        float y = Input.GetAxis("Mouse Y");
        if (Input.GetMouseButton(0) && (x != 0 || y != 0))
            RotateAround(x, y);
    }
    void RotateAround(float x, float y)
    {
        this.transform.RotateAround
            (TargetTf.position, Vector3.up, x * RotateSpeed);
        LimitAxisY(ref y);
        this.transform.RotateAround
            (TargetTf.position, this.transform.right, -y);
    }
     void LimitAxisY(ref float y)
    {
        y *= RotateSpeed;
        //計算當前攝像機Z軸與世界Y軸夾角  
        float angle = Vector3.Angle(this.transform.forward, Vector3.up);
        if (angle < yMinAngle && y > 0 || angle > yMaxAngle && y < 0)
            y = 0;
    }
    private void LookAtTarget()
    {
        this.transform.LookAt(TargetTf);
    }
   
}
Shader程式碼:

Shader "Unlit/Sphere"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}

        [KeywordEnum(None, Top_Bottom, Left_Right)] Stereo ("Stereo Mode", Float) = 0
        [Toggle(STEREO_DEBUG)] _StereoDebug ("Stereo Debug Tinting", Float) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Cull Off

        Pass
        {
            CGPROGRAM
            #include "UnityCG.cginc"
            #pragma vertex vert
            #pragma fragment frag

            //#define STEREO_DEBUG 1

            #pragma multi_compile MONOSCOPIC STEREO_TOP_BOTTOM STEREO_LEFT_RIGHT
            #pragma multi_compile STEREO_DEBUG_OFF STEREO_DEBUG

            struct appdata
            {
                float4 vertex : POSITION; // vertex position
                float2 uv : TEXCOORD0; // texture coordinate
            };

            struct v2f
            {
                float4 vertex : SV_POSITION; // clip space position
                float2 uv : TEXCOORD0; // texture coordinate

#if STEREO_DEBUG
                float4 tint : COLOR;
#endif
            };

            uniform sampler2D _MainTex;
            uniform float4 _MainTex_ST;
            uniform float3 _cameraPosition;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv.xy = float2(1-o.uv.x, o.uv.y);

#if STEREO_DEBUG
                o.tint = 1;
#endif

#if STEREO_TOP_BOTTOM | STEREO_LEFT_RIGHT
                // TODO: this isn't perfect yet, there are some angles it doesn't work for...
                float3 right =  UNITY_MATRIX_V[0].xyz = mul((float3x3)UNITY_MATRIX_V,float3(1,0,0));
                float3 rightPos = (_cameraPosition + right);

                //float3 v1 =  abs(_cameraPosition - (_WorldSpaceCameraPos + right));
                //float3 v2 =  abs(_cameraPosition - (_WorldSpaceCameraPos - right));

                float3 v1 = distance(_cameraPosition, _WorldSpaceCameraPos + right).xxx;
                float3 v2 = distance(_cameraPosition, _WorldSpaceCameraPos - right).xxx;

#if STEREO_TOP_BOTTOM
                // Top-Bottom
                if (v1.x < v2.x)
                {
                    // Left eye (green)
                    o.uv.y = (o.uv.y / 2.0);
#if STEREO_DEBUG
                    o.tint = float4(0, 1, 0, 1);
#endif
                }
                else
                {
                    // Right eye (red)
                    o.uv.y = (o.uv.y / 2.0) + 0.5;
#if STEREO_DEBUG
                    o.tint = float4(1, 0, 0, 1);
#endif
                }
#elif STEREO_LEFT_RIGHT

                // Left-Right 
                if (v1.x < v2.x)
                {
                    // Left eye (green)
                    o.uv.x = (o.uv.x / 2.0);
#if STEREO_DEBUG
                    o.tint = float4(0, 1, 0, 1);
#endif
                }
                else
                {
                    // Right eye (red)
                    o.uv.x = (o.uv.x / 2.0) + 0.5;
#if STEREO_DEBUG
                    o.tint = float4(1, 0, 0, 1);
#endif
                }
#endif
#endif
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                // sample texture and return it
                fixed4 col = tex2D(_MainTex, i.uv);
#if STEREO_DEBUG
                col *= i.tint;
#endif
                return col;
            }
            ENDCG
        }
    }
}

Unity介面設定:

外部修改RoundMain的引數。

材質球新增Shader:

最後就是設定的關鍵:

 把場景的Sphere位置改為:(改動Scale,主要是為了包裹相機)

相機默認同樣的改為:(不改Scale)

執行效果圖:

可以看到滑鼠可以360度看全景。點選Button:後看是播放視訊。同樣可以用滑鼠去全景預覽視訊。

視訊全景效果:我這裡用的VR的一個,手邊也沒有高清的,就算便演示下,大家可以選高清的。