Skip to content

Instantly share code, notes, and snippets.

@yowasou
Created March 14, 2015 07:22
Show Gist options
  • Save yowasou/0882e2e330cad2da209e to your computer and use it in GitHub Desktop.
Save yowasou/0882e2e330cad2da209e to your computer and use it in GitHub Desktop.
RayBlackOut.cs Oclusカメラ用のコード。当たり判定があるものに近づきすぎると徐々にブラックアウト&FOV減少して、めり込みされる状態の描画を防ぐ
using UnityEngine;
using System.Collections;
public class RayBlackOut : MonoBehaviour {
/// <summary>暗転用黒テクスチャ</summary>
private Texture2D blackTexture;
/// <summary>フェード中の透明度</summary>
private float fadeAlpha = 0;
/// <summary>フェード中かどうか</summary>
private bool isFading = false;
float blackDist = 0.15f;
float blackStartDist = 0.25f;
[SerializeField]
GameObject OVRCamera = null;
OVRCameraController ovrcc = null;
float fovORG = 0f;
public void Start()
{
DontDestroyOnLoad (this.gameObject);
//ここで黒テクスチャ作る
this.blackTexture = new Texture2D (32, 32, TextureFormat.RGB24, false);
this.blackTexture.ReadPixels (new Rect (0, 0, 32, 32), 0, 0, false);
this.blackTexture.SetPixel (0, 0, Color.white);
this.blackTexture.Apply ();
ovrcc = OVRCamera.GetComponent<OVRCameraController>();
fovORG = OVRDevice.VerticalFOV();
}
public void OnGUI ()
{
if (!isFading)
return;
//透明度を更新して黒テクスチャを描画
GUI.color = new Color (0, 0, 0, this.fadeAlpha);
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), this.blackTexture);
}
void Update()
{
RaycastHit hit;
//カメラからみたマウス位置のレイ
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
fadeAlpha = 1f;
isFading = true;
//レイを投射してオブジェクトを検出
if (Physics.Raycast(ray, out hit))
{
if (hit.distance < blackDist)
{
isFading = true;
fadeAlpha = 1f;
ovrcc.VerticalFOV = 60f;
}
else if (hit.distance < blackStartDist)
{
isFading = true;
fadeAlpha = (blackStartDist - hit.distance) * 10;
ovrcc.VerticalFOV = fovORG - (blackStartDist - hit.distance) * 300f;
}
else if (isFading)
{
ovrcc.VerticalFOV = fovORG;
isFading = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment