03-29-2015 03:06 AM
03-29-2015 12:14 PM
03-30-2015 01:52 AM
03-31-2015 01:55 AM
04-02-2015 04:26 PM
using UnityEngine;
using System.Collections;
public class FpsCounter : VRGUI
{
// Attach this to a GUIText to make a frames/second indicator.
//
// It calculates frames/second over each updateInterval,
// so the display does not keep changing wildly.
//
// It is also fairly accurate at very low FPS counts (<10).
// We do this not by simply counting frames per interval, but
// by accumulating FPS for each frame. This way we end up with
// correct overall FPS even if the interval renders something like
// 5.5 frames.
public float updateInterval = 0.5F;
private float accum = 0; // FPS accumulated over the interval
private int frames = 0; // Frames drawn over the interval
private float timeleft; // Left time for current interval
private string fpsStr;
public override void OnVRGUI()
{
GUILayout.BeginArea(new Rect(Screen.width / 2.25f, Screen.height / 2.25f, Screen.width, Screen.height));
GUILayout.Label(fpsStr);
GUILayout.EndArea();
}
void Start()
{
timeleft = updateInterval;
}
void Update()
{
timeleft -= Time.deltaTime;
accum += Time.timeScale / Time.deltaTime;
++frames;
// Interval ended - update GUI text and start new interval
if (timeleft <= 0.0)
{
// display two fractional digits (f2 format)
float fps = accum / frames;
string format = System.String.Format("FPS: {0:F2}", fps);
fpsStr = format;
// DebugConsole.Log(format,level);
timeleft = updateInterval;
accum = 0.0F;
frames = 0;
}
}
}
using UnityEngine;
using System.Collections;
/// <summary>
/// Source code by boone188:
/// https://forums.oculus.com/viewtopic.php?f=37&t=4944
/// </summary>
public abstract class VRGUI : MonoBehaviour
{
public Vector3 guiPosition = new Vector3(0f, 0f, 1f);
public float guiSize = 1f;
public bool useCurvedSurface = true;
public bool acceptMouse = true;
public bool acceptKeyboard = true;
public int cursorSize = 32;
public Texture customCursor = null;
private GameObject guiRenderPlane = null;
private RenderTexture guiRenderTexture = null;
private Vector2 cursorPosition = Vector2.zero;
private Texture cursor = null;
private bool isInitialized = false;
private void Initialize ()
{
// create the render plane
if (useCurvedSurface)
{
guiRenderPlane = Instantiate(Resources.Load("VRGUICurvedSurface")) as GameObject;
}
else
{
guiRenderPlane = Instantiate(Resources.Load("VRGUIFlatSurface")) as GameObject;
}
// position the render plane
guiRenderPlane.transform.parent = this.transform;
guiRenderPlane.transform.localPosition = guiPosition;
guiRenderPlane.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
guiRenderPlane.transform.localScale = new Vector3(guiSize, guiSize, guiSize);
// create the render texture
guiRenderTexture = new RenderTexture(Screen.width, Screen.height, 24);
// assign the render texture to the render plane
guiRenderPlane.GetComponent<Renderer>().material.mainTexture = guiRenderTexture;
if (acceptMouse)
{
// create the cursor
if (customCursor != null)
{
cursor = customCursor;
}
else
{
cursor = Resources.Load("SimpleCursor") as Texture;
}
}
isInitialized = true;
}
protected void OnEnable()
{
if (guiRenderPlane != null)
{
guiRenderPlane.SetActive(true);
}
}
protected void OnDisable()
{
if (guiRenderPlane != null)
{
guiRenderPlane.SetActive(false);
}
}
protected void OnGUI()
{
if (!isInitialized)
{
Initialize();
}
// handle mouse events
if (Event.current.isMouse)
{
// return if not accepting mouse events
if (!acceptMouse)
{
return;
}
}
if (acceptMouse)
{
// save the mouse position
cursorPosition = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
}
// handle key events
if (Event.current.isKey)
{
// return if not accepting key events
if (!acceptKeyboard)
{
return;
}
}
// save current render texture
RenderTexture tempRenderTexture = RenderTexture.active;
// set the render texture to render the GUI onto
if (Event.current.type == EventType.Repaint)
{
RenderTexture.active = guiRenderTexture;
GL.Clear (false, true, new Color (0.0f, 0.0f, 0.0f, 0.0f));
}
// draw the VRGUI
OnVRGUI();
if (Event.current.type == EventType.Repaint)
{
if (acceptMouse)
{
// draw the cursor
GUI.DrawTexture(new Rect(cursorPosition.x, cursorPosition.y, cursorSize, cursorSize),
cursor, ScaleMode.StretchToFill);
}
// restore the previous render texture
RenderTexture.active = tempRenderTexture;
}
}
public abstract void OnVRGUI();
}
04-02-2015 05:20 PM
04-03-2015 10:03 AM
04-03-2015 03:48 PM
04-07-2015 01:10 PM
04-07-2015 01:31 PM