一.全景图片
1.创建一个sphere,position为(0,0,0),Scale为(10,10,10)根据情况调整sphere的大小
2.将摄像机作为子物体放到sphere中,position(0,0,0)
3.创建材质球,把下面的360image.Shader脚本放到材质球上,并将全景图拖上去
4.将材质球拖到sphere上
【360image】
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unlit/360Images"
{
Properties{
_MainTex("Base (RGB)", 2D) = "white" {}
}
SubShader{
Tags { "RenderType" = "Opaque" }
Cull front // ADDED BY BERNIE, TO FLIP THE SURFACES
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
// ADDED BY BERNIE:
v.texcoord.x = 1 - v.texcoord.x;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord);
return col;
}
ENDCG
}
}
}
【陀螺仪脚本】
// ***********************************************************
// Written by Heyworks Unity Studio http://unity.heyworks.com/
// ***********************************************************
using UnityEngine;
/// <summary>
/// Gyroscope controller that works with any device orientation.
/// </summary>
public class GyroController : MonoBehaviour
{
#region [Private fields]
private bool gyroEnabled = true;
private const float lowPassFilterFactor = 0.2f;
private readonly Quaternion baseIdentity = Quaternion.Euler(90, 0, 0);
private readonly Quaternion landscapeRight = Quaternion.Euler(0, 0, 90);
private readonly Quaternion landscapeLeft = Quaternion.Euler(0, 0, -90);
private readonly Quaternion upsideDown = Quaternion.Euler(0, 0, 180);
private Quaternion cameraBase = Quaternion.identity;
private Quaternion calibration = Quaternion.identity;
private Quaternion baseOrientation = Quaternion.Euler(90, 0, 0);
private Quaternion baseOrientationRotationFix = Quaternion.identity;
private Quaternion referanceRotation = Quaternion.identity;
private bool debug = true;
#endregion
#region [Unity events]
protected void Start()
{
AttachGyro();
}
protected void Update()
{
if (!gyroEnabled)
return;
transform.rotation = Quaternion.Slerp(transform.rotation,
cameraBase * (ConvertRotation(referanceRotation * Input.gyro.attitude) * GetRotFix()), lowPassFilterFactor);
}
protected void OnGUI()
{
if (!debug)
return;
GUILayout.Label("Orientation: " + Screen.orientation);
GUILayout.Label("Calibration: " + calibration);
GUILayout.Label("Camera base: " + cameraBase);
GUILayout.Label("input.gyro.attitude: " + Input.gyro.attitude);
GUILayout.Label("transform.rotation: " + transform.rotation);
if (GUILayout.Button("On/off gyro: " + Input.gyro.enabled, GUILayout.Height(100)))
{
Input.gyro.enabled = !Input.gyro.enabled;
}
if (GUILayout.Button("On/off gyro controller: " + gyroEnabled, GUILayout.Height(100)))
{
if (gyroEnabled)
{
DetachGyro();
}
else
{
AttachGyro();
}
}
if (GUILayout.Button("Update gyro calibration (Horizontal only)", GUILayout.Height(80)))
{
UpdateCalibration(true);
}
if (GUILayout.Button("Update camera base rotation (Horizontal only)", GUILayout.Height(80)))
{
UpdateCameraBaseRotation(true);
}
if (GUILayout.Button("Reset base orientation", GUILayout.Height(80)))
{
ResetBaseOrientation();
}
if (GUILayout.Button("Reset camera rotation", GUILayout.Height(80)))
{
transform.rotation = Quaternion.identity;
}
}
#endregion
#region [Public methods]
/// <summary>
/// Attaches gyro controller to the transform.
/// </summary>
private void AttachGyro()
{
gyroEnabled = true;
ResetBaseOrientation();
UpdateCalibration(true);
UpdateCameraBaseRotation(true);
RecalculateReferenceRotation();
}
/// <summary>
/// Detaches gyro controller from the transform
/// </summary>
private void DetachGyro()
{
gyroEnabled = false;
}
#endregion
#region [Private methods]
/// <summary>
/// Update the gyro calibration.
/// </summary>
private void UpdateCalibration(bool onlyHorizontal)
{
if (onlyHorizontal)
{
var fw = (Input.gyro.attitude) * (-Vector3.forward);
fw.z = 0;
if (fw == Vector3.zero)
{
calibration = Quaternion.identity;
}
else
{
calibration = (Quaternion.FromToRotation(baseOrientationRotationFix * Vector3.up, fw));
}
}
else
{
calibration = Input.gyro.attitude;
}
}
/// <summary>
/// Update the camera base rotation.
/// </summary>
/// <param name='onlyHorizontal'>
/// Only y rotation.
/// </param>
private void UpdateCameraBaseRotation(bool onlyHorizontal)
{
if (onlyHorizontal)
{
var fw = transform.forward;
fw.y = 0;
if (fw == Vector3.zero)
{
cameraBase = Quaternion.identity;
}
else
{
cameraBase = Quaternion.FromToRotation(Vector3.forward, fw);
}
}
else
{
cameraBase = transform.rotation;
}
}
/// <summary>
/// Converts the rotation from right handed to left handed.
/// </summary>
/// <returns>
/// The result rotation.
/// </returns>
/// <param name='q'>
/// The rotation to convert.
/// </param>
private static Quaternion ConvertRotation(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
/// <summary>
/// Gets the rot fix for different orientations.
/// </summary>
/// <returns>
/// The rot fix.
/// </returns>
private Quaternion GetRotFix()
{
#if UNITY_3_5
if (Screen.orientation == ScreenOrientation.Portrait)
return Quaternion.identity;
if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.Landscape)
return landscapeLeft;
if (Screen.orientation == ScreenOrientation.LandscapeRight)
return landscapeRight;
if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
return upsideDown;
return Quaternion.identity;
#else
return Quaternion.identity;
#endif
}
/// <summary>
/// Recalculates reference system.
/// </summary>
private void ResetBaseOrientation()
{
baseOrientationRotationFix = GetRotFix();
baseOrientation = baseOrientationRotationFix * baseIdentity;
}
/// <summary>
/// Recalculates reference rotation.
/// </summary>
private void RecalculateReferenceRotation()
{
referanceRotation = Quaternion.Inverse(baseOrientation) * Quaternion.Inverse(calibration);
}
#endregion
}
二、全景视频
【组件控制】
1.创建一个sphere,position为(0,0,0),Scale为(10,10,10)根据情况调整sphere的大小
2.将摄像机放到sphere中,并添加上VideoPlayer组件和AudioSouse组件
3.创建Material材质球,shader设置为Sprite/Default,并将其拖拽到sphere
4.把全景视频片段拖拽到摄像机的VideoPlayer组件上,并且把RenderMode设置为MaterialOverride,并把Hierarchy面板的sphere拖拽到Renderer里
5.将VideoPlayer组件的AudioOutMode设置为AudioSource,并将摄像机Camera拖拽进去(通过摄像机来监听声音)
【脚本控制】
gameObject.AddComponent<AudioSource>();
gameObject.AddComponent<VideoPlayer>().clip = Resources.Load("Resources/StarSky") as VideoClip;
URL导入外部资源
public class AddScriptTest : MonoBehaviour
{
//public string urlName;
//public VideoClip videoSky;
void Start()
{
UrlTest(@"E:\IE Download\StarSky.mp4");
}
void Update()
{
}
void UrlTest(string urlName)
{
gameObject.AddComponent<AudioSource>();
VideoPlayer videoPlayer = gameObject.AddComponent<VideoPlayer>();
if (videoPlayer == null)
{
return;
}
else
{
videoPlayer.source = VideoSource.Url;
//游戏开始前不播放 等找到 路径之后才进行播放
videoPlayer.playOnAwake = false;
videoPlayer.url = urlName;
videoPlayer.Play();
}
}
}
三.VideoPlyer
1.RenderMode
(1)Camera Far Plane 视频渲染永远在最后
(2)Camera Near Plane 视频渲染永远在最前
(3)Material Override 常用于 VR全景