clean project
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
/************************************************************************************
|
||||
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
|
||||
|
||||
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
|
||||
https://developer.oculus.com/licenses/oculussdk/
|
||||
|
||||
Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
||||
ANY KIND, either express or implied. See the License for the specific language governing
|
||||
permissions and limitations under the License.
|
||||
************************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Oculus.Interaction.UnityCanvas
|
||||
{
|
||||
public class CanvasCylinder : CanvasRenderTextureMesh
|
||||
{
|
||||
[Serializable]
|
||||
public struct MeshGenerationSettings
|
||||
{
|
||||
[Delayed]
|
||||
public float VerticesPerDegree;
|
||||
|
||||
[Delayed]
|
||||
public int MaxHorizontalResolution;
|
||||
|
||||
[Delayed]
|
||||
public int MaxVerticalResolution;
|
||||
}
|
||||
|
||||
public const int MIN_RESOLUTION = 2;
|
||||
|
||||
[Tooltip("The radius of the cylinder that the Canvas texture is projected onto.")]
|
||||
[Delayed]
|
||||
[SerializeField]
|
||||
private float _curveRadius = 1;
|
||||
|
||||
[SerializeField]
|
||||
private MeshGenerationSettings _meshGeneration = new MeshGenerationSettings()
|
||||
{
|
||||
VerticesPerDegree = 1.4f,
|
||||
MaxHorizontalResolution = 128,
|
||||
MaxVerticalResolution = 32
|
||||
};
|
||||
|
||||
protected override OVROverlay.OverlayShape OverlayShape => OVROverlay.OverlayShape.Cylinder;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
protected virtual void OnValidate()
|
||||
{
|
||||
_curveRadius = Mathf.Max(0.01f, _curveRadius);
|
||||
_meshGeneration.MaxHorizontalResolution = Mathf.Max(MIN_RESOLUTION,
|
||||
_meshGeneration.MaxHorizontalResolution);
|
||||
_meshGeneration.MaxVerticalResolution = Mathf.Max(MIN_RESOLUTION,
|
||||
_meshGeneration.MaxVerticalResolution);
|
||||
_meshGeneration.VerticesPerDegree = Mathf.Max(0, _meshGeneration.VerticesPerDegree);
|
||||
|
||||
if (Application.isPlaying && _started)
|
||||
{
|
||||
EditorApplication.delayCall += () =>
|
||||
{
|
||||
UpdateImposter();
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
public float CurveRadius
|
||||
{
|
||||
get
|
||||
{
|
||||
return _curveRadius;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_curveRadius == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_curveRadius = value;
|
||||
|
||||
if (isActiveAndEnabled && Application.isPlaying)
|
||||
{
|
||||
UpdateImposter();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void UpdateOverlayPositionAndScale()
|
||||
{
|
||||
if (_overlay == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var resolution = _canvasRenderTexture.GetBaseResolutionToUse();
|
||||
_overlay.transform.localPosition = new Vector3(0, 0, -_curveRadius) - _runtimeOffset;
|
||||
_overlay.transform.localScale = new Vector3(_canvasRenderTexture.PixelsToUnits(resolution.x),
|
||||
_canvasRenderTexture.PixelsToUnits(resolution.y),
|
||||
_curveRadius);
|
||||
}
|
||||
|
||||
protected override Vector3 MeshInverseTransform(Vector3 localPosition)
|
||||
{
|
||||
float angle = Mathf.Atan2(localPosition.x, localPosition.z + _curveRadius);
|
||||
float x = angle * _curveRadius;
|
||||
float y = localPosition.y;
|
||||
return new Vector3(x, y);
|
||||
}
|
||||
|
||||
protected override void GenerateMesh(out List<Vector3> verts,
|
||||
out List<int> tris,
|
||||
out List<Vector2> uvs)
|
||||
{
|
||||
verts = new List<Vector3>();
|
||||
tris = new List<int>();
|
||||
uvs = new List<Vector2>();
|
||||
|
||||
var resolution = _canvasRenderTexture.GetBaseResolutionToUse();
|
||||
|
||||
float xPos = _canvasRenderTexture.PixelsToUnits(Mathf.RoundToInt(resolution.x)) * 0.5f;
|
||||
float xNeg = -xPos;
|
||||
|
||||
float yPos = _canvasRenderTexture.PixelsToUnits(Mathf.RoundToInt(resolution.y)) * 0.5f;
|
||||
float yNeg = -yPos;
|
||||
|
||||
int horizontalResolution = Mathf.Max(2,
|
||||
Mathf.RoundToInt(_meshGeneration.VerticesPerDegree * Mathf.Rad2Deg * xPos /
|
||||
_curveRadius));
|
||||
int verticalResolution =
|
||||
Mathf.Max(2, Mathf.RoundToInt(horizontalResolution * yPos / xPos));
|
||||
|
||||
horizontalResolution = Mathf.Clamp(horizontalResolution, 2,
|
||||
_meshGeneration.MaxHorizontalResolution);
|
||||
verticalResolution = Mathf.Clamp(verticalResolution, 2,
|
||||
_meshGeneration.MaxVerticalResolution);
|
||||
|
||||
Vector3 getCurvedPoint(float u, float v)
|
||||
{
|
||||
float x = Mathf.Lerp(xNeg, xPos, u);
|
||||
float y = Mathf.Lerp(yNeg, yPos, v);
|
||||
|
||||
float angle = x / _curveRadius;
|
||||
Vector3 point;
|
||||
point.x = Mathf.Sin(angle) * _curveRadius;
|
||||
point.y = y;
|
||||
point.z = Mathf.Cos(angle) * _curveRadius - _curveRadius;
|
||||
return point;
|
||||
}
|
||||
|
||||
for (int y = 0; y < verticalResolution; y++)
|
||||
{
|
||||
for (int x = 0; x < horizontalResolution; x++)
|
||||
{
|
||||
float u = x / (horizontalResolution - 1.0f);
|
||||
float v = y / (verticalResolution - 1.0f);
|
||||
|
||||
verts.Add(getCurvedPoint(u, v));
|
||||
uvs.Add(new Vector2(u, v));
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = 0; y < verticalResolution - 1; y++)
|
||||
{
|
||||
for (int x = 0; x < horizontalResolution - 1; x++)
|
||||
{
|
||||
int v00 = x + y * horizontalResolution;
|
||||
int v10 = v00 + 1;
|
||||
int v01 = v00 + horizontalResolution;
|
||||
int v11 = v00 + 1 + horizontalResolution;
|
||||
|
||||
tris.Add(v00);
|
||||
tris.Add(v11);
|
||||
tris.Add(v10);
|
||||
|
||||
tris.Add(v00);
|
||||
tris.Add(v01);
|
||||
tris.Add(v11);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region Inject
|
||||
|
||||
public void InjectAllCanvasCylinder(CanvasRenderTexture canvasRenderTexture,
|
||||
float curveRadius)
|
||||
{
|
||||
InjectAllCanvasRenderTextureMesh(canvasRenderTexture);
|
||||
InjectCurveRadius(curveRadius);
|
||||
}
|
||||
|
||||
public void InjectCurveRadius(float curveRadius)
|
||||
{
|
||||
_curveRadius = curveRadius;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 85ef10d17088b3c4cad9a5d13887b157
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,85 @@
|
||||
/************************************************************************************
|
||||
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
|
||||
|
||||
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
|
||||
https://developer.oculus.com/licenses/oculussdk/
|
||||
|
||||
Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
||||
ANY KIND, either express or implied. See the License for the specific language governing
|
||||
permissions and limitations under the License.
|
||||
************************************************************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Oculus.Interaction.UnityCanvas
|
||||
{
|
||||
public class CanvasRect : CanvasRenderTextureMesh
|
||||
{
|
||||
protected override OVROverlay.OverlayShape OverlayShape => OVROverlay.OverlayShape.Quad;
|
||||
|
||||
protected override void UpdateOverlayPositionAndScale()
|
||||
{
|
||||
if (_overlay == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var resolution = _canvasRenderTexture.GetBaseResolutionToUse();
|
||||
_overlay.transform.localPosition = -_runtimeOffset;
|
||||
_overlay.transform.localScale = new Vector3(_canvasRenderTexture.PixelsToUnits(resolution.x),
|
||||
_canvasRenderTexture.PixelsToUnits(resolution.y),
|
||||
1);
|
||||
}
|
||||
|
||||
protected override Vector3 MeshInverseTransform(Vector3 localPosition)
|
||||
{
|
||||
return localPosition;
|
||||
}
|
||||
|
||||
protected override void GenerateMesh(out List<Vector3> verts,
|
||||
out List<int> tris,
|
||||
out List<Vector2> uvs)
|
||||
{
|
||||
verts = new List<Vector3>();
|
||||
tris = new List<int>();
|
||||
uvs = new List<Vector2>();
|
||||
|
||||
var resolution = _canvasRenderTexture.GetBaseResolutionToUse();
|
||||
|
||||
float xPos = _canvasRenderTexture.PixelsToUnits(Mathf.RoundToInt(resolution.x)) * 0.5f;
|
||||
float xNeg = -xPos;
|
||||
|
||||
float yPos = _canvasRenderTexture.PixelsToUnits(Mathf.RoundToInt(resolution.y)) * 0.5f;
|
||||
float yNeg = -yPos;
|
||||
|
||||
verts.Add(new Vector3(xNeg, yNeg, 0));
|
||||
verts.Add(new Vector3(xNeg, yPos, 0));
|
||||
verts.Add(new Vector3(xPos, yPos, 0));
|
||||
verts.Add(new Vector3(xPos, yNeg, 0));
|
||||
|
||||
tris.Add(0);
|
||||
tris.Add(1);
|
||||
tris.Add(2);
|
||||
|
||||
tris.Add(0);
|
||||
tris.Add(2);
|
||||
tris.Add(3);
|
||||
|
||||
uvs.Add(new Vector2(0, 0));
|
||||
uvs.Add(new Vector2(0, 1));
|
||||
uvs.Add(new Vector2(1, 1));
|
||||
uvs.Add(new Vector2(1, 0));
|
||||
}
|
||||
|
||||
#region Inject
|
||||
|
||||
public void InjectAllCanvasRect(CanvasRenderTexture canvasRenderTexture)
|
||||
{
|
||||
InjectAllCanvasRenderTextureMesh(canvasRenderTexture);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2025423b67857c349bf8cf435762b5cd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,448 @@
|
||||
/************************************************************************************
|
||||
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
|
||||
|
||||
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
|
||||
https://developer.oculus.com/licenses/oculussdk/
|
||||
|
||||
Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
||||
ANY KIND, either express or implied. See the License for the specific language governing
|
||||
permissions and limitations under the License.
|
||||
************************************************************************************/
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Profiling;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace Oculus.Interaction.UnityCanvas
|
||||
{
|
||||
[RequireComponent(typeof(Canvas))]
|
||||
[DisallowMultipleComponent]
|
||||
public class CanvasRenderTexture : UIBehaviour
|
||||
{
|
||||
public enum DriveMode
|
||||
{
|
||||
Auto,
|
||||
Manual
|
||||
}
|
||||
|
||||
public const int DEFAULT_UI_LAYERMASK = 1 << 5; //Hardcoded as the UI layer in Unity.
|
||||
|
||||
private static readonly Vector2Int DEFAULT_TEXTURE_RES = new Vector2Int(128, 128);
|
||||
|
||||
[Tooltip("If you need extra resolution, you can use this as a whole-integer multiplier of the final resolution used to render the texture.")]
|
||||
[Range(1, 3)]
|
||||
[Delayed]
|
||||
[SerializeField]
|
||||
private int _renderScale = 1;
|
||||
|
||||
[SerializeField]
|
||||
private DriveMode _dimensionsDriveMode = DriveMode.Auto;
|
||||
|
||||
[Tooltip("The exact pixel resolution of the texture used for interface rendering. If set to auto this will take the size of the attached " +
|
||||
"RectTransform into consideration, in addition to the configured pixel-to-meter ratio.")]
|
||||
[Delayed]
|
||||
[SerializeField]
|
||||
private Vector2Int _resolution = DEFAULT_TEXTURE_RES;
|
||||
|
||||
[SerializeField]
|
||||
private int _pixelsPerUnit = 100;
|
||||
|
||||
[SerializeField]
|
||||
private RenderingMode _renderingMode = RenderingMode.AlphaCutout;
|
||||
|
||||
[Tooltip("Whether or not mip-maps should be auto-generated for the texture. Can help aliasing if the texture can be " +
|
||||
"viewed from many difference distances.")]
|
||||
[SerializeField]
|
||||
private bool _generateMipMaps = false;
|
||||
|
||||
[Tooltip(
|
||||
"Requires MSAA. Provides limited transparency useful for anti-aliasing soft edges of UI elements.")]
|
||||
[SerializeField]
|
||||
private bool _useAlphaToMask = true;
|
||||
|
||||
[Range(0, 1)]
|
||||
[SerializeField]
|
||||
private float _alphaCutoutThreshold = 0.5f;
|
||||
|
||||
[Tooltip(
|
||||
"Uses a more expensive image sampling technique for improved quality at the cost of performance.")]
|
||||
[SerializeField]
|
||||
protected bool _enableSuperSampling = true;
|
||||
|
||||
[Tooltip(
|
||||
"Attempts to anti-alias the edges of the underlay by using alpha blending. Can cause borders of " +
|
||||
"darkness around partially transparent objects.")]
|
||||
[SerializeField]
|
||||
private bool _doUnderlayAntiAliasing = false;
|
||||
|
||||
[Tooltip(
|
||||
"OVR Layers can provide a buggy or less ideal workflow while in the editor. This option allows you " +
|
||||
"emulate the layer rendering while in the editor, while still using the OVR Layer rendering in a build.")]
|
||||
[SerializeField]
|
||||
private bool _emulateWhileInEditor = true;
|
||||
|
||||
[Header("Rendering Settings")]
|
||||
[Tooltip("The layers to render when the rendering texture is created. All child renderers should be part of this mask.")]
|
||||
[SerializeField]
|
||||
private LayerMask _renderingLayers = DEFAULT_UI_LAYERMASK;
|
||||
|
||||
[SerializeField, Optional]
|
||||
private Material _defaultUIMaterial = null;
|
||||
|
||||
public RenderingMode RenderingMode => _renderingMode;
|
||||
|
||||
public LayerMask RenderingLayers => _renderingLayers;
|
||||
|
||||
public bool UseAlphaToMask => _useAlphaToMask;
|
||||
|
||||
public bool DoUnderlayAntiAliasing => _doUnderlayAntiAliasing;
|
||||
|
||||
public bool EnableSuperSampling => _enableSuperSampling;
|
||||
|
||||
public float AlphaCutoutThreshold => _alphaCutoutThreshold;
|
||||
|
||||
public Action<Texture> OnUpdateRenderTexture = delegate { };
|
||||
|
||||
public bool UseEditorEmulation()
|
||||
{
|
||||
return Application.isEditor ? _emulateWhileInEditor : false;
|
||||
}
|
||||
|
||||
public int RenderScale
|
||||
{
|
||||
get
|
||||
{
|
||||
return _renderScale;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_renderScale < 1 || _renderScale > 3)
|
||||
{
|
||||
throw new ArgumentException($"Render scale must be between 1 and 3, but was {value}");
|
||||
}
|
||||
|
||||
if (_renderScale == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_renderScale = value;
|
||||
|
||||
if (isActiveAndEnabled && Application.isPlaying)
|
||||
{
|
||||
UpdateCamera();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Material DefaultUIMaterial => _defaultUIMaterial;
|
||||
|
||||
public Camera OverlayCamera => _camera;
|
||||
|
||||
public Texture Texture => _tex;
|
||||
|
||||
private RenderTexture _tex;
|
||||
private Camera _camera;
|
||||
|
||||
protected bool _started = false;
|
||||
|
||||
public Vector2Int CalcAutoResolution()
|
||||
{
|
||||
var rectTransform = GetComponent<RectTransform>();
|
||||
if (rectTransform == null)
|
||||
{
|
||||
return DEFAULT_TEXTURE_RES;
|
||||
}
|
||||
|
||||
Vector2 size = rectTransform.sizeDelta;
|
||||
size.x *= rectTransform.lossyScale.x;
|
||||
size.y *= rectTransform.lossyScale.y;
|
||||
|
||||
int x = Mathf.RoundToInt(UnitsToPixels(size.x));
|
||||
int y = Mathf.RoundToInt(UnitsToPixels(size.y));
|
||||
return new Vector2Int(Mathf.Max(x, 1), Mathf.Max(y, 1));
|
||||
}
|
||||
|
||||
public Vector2Int GetBaseResolutionToUse()
|
||||
{
|
||||
if (_dimensionsDriveMode == DriveMode.Auto)
|
||||
{
|
||||
return CalcAutoResolution();
|
||||
}
|
||||
else
|
||||
{
|
||||
return _resolution;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2Int GetScaledResolutionToUse()
|
||||
{
|
||||
return new Vector2Int(
|
||||
Mathf.RoundToInt(GetBaseResolutionToUse().x * (float)_renderScale),
|
||||
Mathf.RoundToInt(GetBaseResolutionToUse().y * (float)_renderScale)
|
||||
);
|
||||
}
|
||||
|
||||
public float PixelsToUnits(float pixels)
|
||||
{
|
||||
return (1f / _pixelsPerUnit) * pixels;
|
||||
}
|
||||
|
||||
public float UnitsToPixels(float units)
|
||||
{
|
||||
return _pixelsPerUnit * units;
|
||||
}
|
||||
|
||||
public bool ShouldUseOVROverlay
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (_renderingMode)
|
||||
{
|
||||
case RenderingMode.OVR_Underlay:
|
||||
case RenderingMode.OVR_Overlay:
|
||||
return !UseEditorEmulation();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
protected override void OnValidate()
|
||||
{
|
||||
base.OnValidate();
|
||||
if (Application.isPlaying && _started)
|
||||
{
|
||||
EditorApplication.delayCall += () =>
|
||||
{
|
||||
UpdateCamera();
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
protected override void OnRectTransformDimensionsChange()
|
||||
{
|
||||
base.OnRectTransformDimensionsChange();
|
||||
if (_started)
|
||||
{
|
||||
UpdateCamera();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Start()
|
||||
{
|
||||
this.BeginStart(ref _started, base.Start);
|
||||
this.EndStart(ref _started);
|
||||
}
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
if (_started)
|
||||
{
|
||||
if (_defaultUIMaterial == null)
|
||||
{
|
||||
_defaultUIMaterial = new Material(Shader.Find("UI/Default (Overlay)"));
|
||||
}
|
||||
UpdateCamera();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
if (_started)
|
||||
{
|
||||
if (_camera?.gameObject != null)
|
||||
{
|
||||
Destroy(_camera.gameObject);
|
||||
}
|
||||
if (_tex != null)
|
||||
{
|
||||
DestroyImmediate(_tex);
|
||||
}
|
||||
}
|
||||
base.OnDisable();
|
||||
}
|
||||
|
||||
protected void UpdateCamera()
|
||||
{
|
||||
Profiler.BeginSample("InterfaceRenderer.UpdateCamera");
|
||||
try
|
||||
{
|
||||
if (_camera == null)
|
||||
{
|
||||
GameObject cameraObj = CreateChildObject("__Camera");
|
||||
_camera = cameraObj.AddComponent<Camera>();
|
||||
|
||||
_camera.orthographic = true;
|
||||
_camera.nearClipPlane = -0.1f;
|
||||
_camera.farClipPlane = 0.1f;
|
||||
_camera.backgroundColor = new Color(0, 0, 0, 0);
|
||||
_camera.clearFlags = CameraClearFlags.SolidColor;
|
||||
}
|
||||
|
||||
UpdateRenderTexture();
|
||||
UpdateOrthoSize();
|
||||
UpdateCameraCullingMask();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Profiler.EndSample();
|
||||
}
|
||||
}
|
||||
|
||||
protected void UpdateRenderTexture()
|
||||
{
|
||||
Profiler.BeginSample("InterfaceRenderer.UpdateRenderTexture");
|
||||
try
|
||||
{
|
||||
var resolutionToUse = GetScaledResolutionToUse();
|
||||
|
||||
//Never generate mips when using OVROverlay, they are not used
|
||||
bool desiredMipsSetting = ShouldUseOVROverlay ? false : _generateMipMaps;
|
||||
|
||||
if (_tex == null ||
|
||||
_tex.width != resolutionToUse.x ||
|
||||
_tex.height != resolutionToUse.y ||
|
||||
_tex.autoGenerateMips != desiredMipsSetting)
|
||||
{
|
||||
if (_tex != null)
|
||||
{
|
||||
_camera.targetTexture = null;
|
||||
DestroyImmediate(_tex);
|
||||
}
|
||||
|
||||
_tex = new RenderTexture(resolutionToUse.x, resolutionToUse.y, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
|
||||
_tex.filterMode = FilterMode.Bilinear;
|
||||
_tex.autoGenerateMips = desiredMipsSetting;
|
||||
_camera.targetTexture = _tex;
|
||||
|
||||
OnUpdateRenderTexture(_tex);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Profiler.EndSample();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateOrthoSize()
|
||||
{
|
||||
if (_camera != null)
|
||||
{
|
||||
_camera.orthographicSize = PixelsToUnits(GetBaseResolutionToUse().y) * 0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateCameraCullingMask()
|
||||
{
|
||||
if (_camera != null)
|
||||
{
|
||||
_camera.cullingMask = _renderingLayers.value;
|
||||
}
|
||||
}
|
||||
|
||||
protected GameObject CreateChildObject(string name)
|
||||
{
|
||||
GameObject obj = new GameObject(name);
|
||||
|
||||
obj.transform.SetParent(transform);
|
||||
obj.transform.localPosition = Vector3.zero;
|
||||
obj.transform.localRotation = Quaternion.identity;
|
||||
obj.transform.localScale = Vector3.one;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static class Properties
|
||||
{
|
||||
public static readonly string DimensionDriveMode = nameof(_dimensionsDriveMode);
|
||||
public static readonly string Resolution = nameof(_resolution);
|
||||
public static readonly string RenderScale = nameof(_renderScale);
|
||||
public static readonly string PixelsPerUnit = nameof(_pixelsPerUnit);
|
||||
public static readonly string RenderLayers = nameof(_renderingLayers);
|
||||
public static readonly string RenderingMode = nameof(_renderingMode);
|
||||
public static readonly string GenerateMips = nameof(_generateMipMaps);
|
||||
public static readonly string UseAlphaToMask = nameof(_useAlphaToMask);
|
||||
public static readonly string AlphaCutoutThreshold = nameof(_alphaCutoutThreshold);
|
||||
public static readonly string UseExpensiveSuperSample = nameof(_enableSuperSampling);
|
||||
public static readonly string DoUnderlayAA = nameof(_doUnderlayAntiAliasing);
|
||||
public static readonly string EmulateWhileInEditor = nameof(_emulateWhileInEditor);
|
||||
public static readonly string DefaultUIMaterial = nameof(_defaultUIMaterial);
|
||||
}
|
||||
|
||||
#region Inject
|
||||
|
||||
public void InjectAllCanvasRenderTexture(int pixelsPerUnit,
|
||||
int renderScale,
|
||||
LayerMask renderingLayers,
|
||||
RenderingMode renderingMode,
|
||||
bool doUnderlayAntiAliasing,
|
||||
float alphaCutoutThreshold,
|
||||
bool useAlphaToMask)
|
||||
{
|
||||
InjectPixelsPerUnit(pixelsPerUnit);
|
||||
InjectRenderScale(renderScale);
|
||||
InjectRenderingLayers(renderingLayers);
|
||||
InjectRenderingMode(renderingMode);
|
||||
InjectDoUnderlayAntiAliasing(doUnderlayAntiAliasing);
|
||||
InjectAlphaCutoutThreshold(alphaCutoutThreshold);
|
||||
InjectUseAlphaToMask(useAlphaToMask);
|
||||
}
|
||||
|
||||
public void InjectPixelsPerUnit(int pixelsPerUnit)
|
||||
{
|
||||
_pixelsPerUnit = pixelsPerUnit;
|
||||
}
|
||||
|
||||
public void InjectRenderScale(int renderScale)
|
||||
{
|
||||
_renderScale = renderScale;
|
||||
}
|
||||
|
||||
public void InjectRenderingMode(RenderingMode renderingMode)
|
||||
{
|
||||
_renderingMode = renderingMode;
|
||||
}
|
||||
|
||||
public void InjectRenderingLayers(LayerMask renderingLayers)
|
||||
{
|
||||
_renderingLayers = renderingLayers;
|
||||
}
|
||||
|
||||
public void InjectDoUnderlayAntiAliasing(bool doUnderlayAntiAliasing)
|
||||
{
|
||||
_doUnderlayAntiAliasing = doUnderlayAntiAliasing;
|
||||
}
|
||||
|
||||
public void InjectUseAlphaToMask(bool useAlphaToMask)
|
||||
{
|
||||
_useAlphaToMask = useAlphaToMask;
|
||||
}
|
||||
|
||||
public void InjectAlphaCutoutThreshold(float alphaCutoutThreshold)
|
||||
{
|
||||
_alphaCutoutThreshold = alphaCutoutThreshold;
|
||||
}
|
||||
|
||||
public void InjectOptionalDefaultUIMaterial(Material defaultUIMaterial)
|
||||
{
|
||||
_defaultUIMaterial = defaultUIMaterial;
|
||||
}
|
||||
|
||||
public void InjectOptionalGenerateMipMaps(bool generateMipMaps)
|
||||
{
|
||||
_generateMipMaps = generateMipMaps;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b7ecff74e52843a41ab3a441ac81379e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- _defaultUIMaterial: {fileID: 2100000, guid: bc9f80a2ae0e0a24d870176e48ab1b93,
|
||||
type: 2}
|
||||
- _imposterMaterial: {fileID: 2100000, guid: 5c093e4058df12042a75bcb967ca1554, type: 2}
|
||||
- _depthQuadMaterial: {fileID: 2100000, guid: c7cda63c3ebb50847950fc3a925d784f,
|
||||
type: 2}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,295 @@
|
||||
/************************************************************************************
|
||||
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
|
||||
|
||||
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
|
||||
https://developer.oculus.com/licenses/oculussdk/
|
||||
|
||||
Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
||||
ANY KIND, either express or implied. See the License for the specific language governing
|
||||
permissions and limitations under the License.
|
||||
************************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
using UnityEngine.Profiling;
|
||||
|
||||
namespace Oculus.Interaction.UnityCanvas
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
public abstract class CanvasRenderTextureMesh : MonoBehaviour
|
||||
{
|
||||
private static readonly int MainTexShaderID = Shader.PropertyToID("_MainTex");
|
||||
|
||||
[SerializeField]
|
||||
protected CanvasRenderTexture _canvasRenderTexture;
|
||||
|
||||
[SerializeField, Optional]
|
||||
private MeshCollider _meshCollider = null;
|
||||
|
||||
[Tooltip("If non-zero it will cause the position of the canvas to be offset by this amount at runtime, while " +
|
||||
"the renderer will remain where it was at edit time. This can be used to prevent the two representations from overlapping.")]
|
||||
[SerializeField]
|
||||
protected Vector3 _runtimeOffset = new Vector3(0, 0, 0);
|
||||
|
||||
protected OVROverlay _overlay;
|
||||
|
||||
private Material _material = null;
|
||||
private MeshFilter _imposterFilter;
|
||||
private MeshRenderer _imposterRenderer;
|
||||
|
||||
protected bool _started = false;
|
||||
|
||||
protected abstract Vector3 MeshInverseTransform(Vector3 localPosition);
|
||||
|
||||
protected abstract void GenerateMesh(out List<Vector3> verts, out List<int> tris, out List<Vector2> uvs);
|
||||
|
||||
protected abstract void UpdateOverlayPositionAndScale();
|
||||
|
||||
protected abstract OVROverlay.OverlayShape OverlayShape { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Transform a position in world space relative to the imposter to an associated position relative
|
||||
/// to the original canvas in world space.
|
||||
/// </summary>
|
||||
public Vector3 ImposterToCanvasTransformPoint(Vector3 worldPosition)
|
||||
{
|
||||
Vector3 localToImposter =
|
||||
_imposterFilter.transform.InverseTransformPoint(worldPosition);
|
||||
Vector3 canvasLocalPosition = MeshInverseTransform(localToImposter);
|
||||
Vector3 transformedWorldPosition = _canvasRenderTexture.transform.TransformPoint(canvasLocalPosition / _canvasRenderTexture.transform.lossyScale.x);
|
||||
return transformedWorldPosition;
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
this.BeginStart(ref _started);
|
||||
Assert.IsNotNull(_canvasRenderTexture);
|
||||
this.EndStart(ref _started);
|
||||
}
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
if (_started)
|
||||
{
|
||||
CreateMaterial();
|
||||
UpdateImposter();
|
||||
|
||||
_canvasRenderTexture.OnUpdateRenderTexture += HandleUpdateRenderTexture;
|
||||
if (_canvasRenderTexture.Texture != null)
|
||||
{
|
||||
HandleUpdateRenderTexture(_canvasRenderTexture.Texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
if (_started)
|
||||
{
|
||||
_canvasRenderTexture.OnUpdateRenderTexture -= HandleUpdateRenderTexture;
|
||||
if (_material != null)
|
||||
{
|
||||
Destroy(_material);
|
||||
_material = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void HandleUpdateRenderTexture(Texture texture)
|
||||
{
|
||||
if (_imposterRenderer != null)
|
||||
{
|
||||
_imposterRenderer.material = _material;
|
||||
|
||||
var block = new MaterialPropertyBlock();
|
||||
_imposterRenderer.GetPropertyBlock(block);
|
||||
|
||||
block.SetTexture(MainTexShaderID, _canvasRenderTexture.Texture);
|
||||
|
||||
if (_canvasRenderTexture.RenderingMode == RenderingMode.AlphaCutout &&
|
||||
!_canvasRenderTexture.UseAlphaToMask)
|
||||
{
|
||||
block.SetFloat("_Cutoff", _canvasRenderTexture.AlphaCutoutThreshold);
|
||||
}
|
||||
if (_canvasRenderTexture.RenderingMode == RenderingMode.OVR_Underlay &&
|
||||
_canvasRenderTexture.UseEditorEmulation())
|
||||
{
|
||||
block.SetFloat("_Cutoff", 0.5f);
|
||||
}
|
||||
_imposterRenderer.SetPropertyBlock(block);
|
||||
}
|
||||
|
||||
UpdateOverlay();
|
||||
UpdateImposter();
|
||||
}
|
||||
|
||||
protected void UpdateImposter()
|
||||
{
|
||||
Profiler.BeginSample("InterfaceRenderer.UpdateImposter");
|
||||
try
|
||||
{
|
||||
if (_imposterFilter == null || _imposterRenderer == null)
|
||||
{
|
||||
_imposterFilter = gameObject.AddComponent<MeshFilter>();
|
||||
_imposterRenderer = gameObject.AddComponent<MeshRenderer>();
|
||||
}
|
||||
else
|
||||
{
|
||||
_imposterRenderer.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
if (_material != null)
|
||||
{
|
||||
_imposterRenderer.material = _material;
|
||||
}
|
||||
|
||||
GenerateMesh(out List<Vector3> verts, out List<int> tris, out List<Vector2> uvs);
|
||||
|
||||
Mesh mesh = new Mesh();
|
||||
mesh.SetVertices(verts);
|
||||
mesh.SetUVs(0, uvs);
|
||||
mesh.SetTriangles(tris, 0);
|
||||
|
||||
mesh.RecalculateBounds();
|
||||
mesh.RecalculateNormals();
|
||||
|
||||
_imposterFilter.mesh = mesh;
|
||||
if (_meshCollider != null)
|
||||
{
|
||||
_meshCollider.sharedMesh = _imposterFilter.sharedMesh;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Profiler.EndSample();
|
||||
}
|
||||
}
|
||||
|
||||
protected void CreateMaterial()
|
||||
{
|
||||
Profiler.BeginSample("InterfaceRenderer.UpdateMaterial");
|
||||
try
|
||||
{
|
||||
string shaderName;
|
||||
switch (_canvasRenderTexture.RenderingMode)
|
||||
{
|
||||
case RenderingMode.AlphaBlended:
|
||||
shaderName = "Hidden/Imposter_AlphaBlended";
|
||||
break;
|
||||
case RenderingMode.AlphaCutout:
|
||||
if (_canvasRenderTexture.UseAlphaToMask)
|
||||
{
|
||||
shaderName = "Hidden/Imposter_AlphaToMask";
|
||||
}
|
||||
else
|
||||
{
|
||||
shaderName = "Hidden/Imposter_AlphaCutout";
|
||||
}
|
||||
|
||||
break;
|
||||
case RenderingMode.Opaque:
|
||||
shaderName = "Hidden/Imposter_Opaque";
|
||||
break;
|
||||
case RenderingMode.OVR_Underlay:
|
||||
if (_canvasRenderTexture.UseEditorEmulation())
|
||||
{
|
||||
shaderName = "Hidden/Imposter_AlphaCutout";
|
||||
}
|
||||
else if (_canvasRenderTexture.DoUnderlayAntiAliasing)
|
||||
{
|
||||
shaderName = "Hidden/Imposter_Underlay_AA";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
shaderName = "Hidden/Imposter_Underlay";
|
||||
}
|
||||
|
||||
break;
|
||||
case RenderingMode.OVR_Overlay:
|
||||
shaderName = "Hidden/Imposter_AlphaCutout";
|
||||
break;
|
||||
default:
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
_material = new Material(Shader.Find(shaderName));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Profiler.EndSample();
|
||||
}
|
||||
}
|
||||
|
||||
protected void UpdateOverlay()
|
||||
{
|
||||
Profiler.BeginSample("InterfaceRenderer.UpdateOverlay");
|
||||
try
|
||||
{
|
||||
if (!_canvasRenderTexture.ShouldUseOVROverlay)
|
||||
{
|
||||
_overlay?.gameObject?.SetActive(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_overlay == null)
|
||||
{
|
||||
GameObject overlayObj = CreateChildObject("__Overlay");
|
||||
_overlay = overlayObj.AddComponent<OVROverlay>();
|
||||
_overlay.isAlphaPremultiplied = !Application.isMobilePlatform;
|
||||
}
|
||||
else
|
||||
{
|
||||
_overlay.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
bool useUnderlayRendering = _canvasRenderTexture.RenderingMode == RenderingMode.OVR_Underlay;
|
||||
_overlay.textures = new Texture[1] { _canvasRenderTexture.Texture };
|
||||
_overlay.noDepthBufferTesting = useUnderlayRendering;
|
||||
_overlay.currentOverlayType = useUnderlayRendering ? OVROverlay.OverlayType.Underlay : OVROverlay.OverlayType.Overlay;
|
||||
_overlay.currentOverlayShape = OverlayShape;
|
||||
_overlay.useExpensiveSuperSample = _canvasRenderTexture.EnableSuperSampling;
|
||||
|
||||
UpdateOverlayPositionAndScale();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Profiler.EndSample();
|
||||
}
|
||||
}
|
||||
|
||||
protected GameObject CreateChildObject(string name)
|
||||
{
|
||||
GameObject obj = new GameObject(name);
|
||||
|
||||
obj.transform.SetParent(transform);
|
||||
obj.transform.localPosition = Vector3.zero;
|
||||
obj.transform.localRotation = Quaternion.identity;
|
||||
obj.transform.localScale = Vector3.one;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
#region Inject
|
||||
|
||||
public void InjectAllCanvasRenderTextureMesh(CanvasRenderTexture canvasRenderTexture)
|
||||
{
|
||||
InjectCanvasRenderTexture(canvasRenderTexture);
|
||||
}
|
||||
|
||||
public void InjectCanvasRenderTexture(CanvasRenderTexture canvasRenderTexture)
|
||||
{
|
||||
_canvasRenderTexture = canvasRenderTexture;
|
||||
}
|
||||
|
||||
public void InjectOptionalMeshCollider(MeshCollider meshCollider)
|
||||
{
|
||||
_meshCollider = meshCollider;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1e17f00312f4fea429367cb5b305689d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,31 @@
|
||||
/************************************************************************************
|
||||
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
|
||||
|
||||
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
|
||||
https://developer.oculus.com/licenses/oculussdk/
|
||||
|
||||
Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
||||
ANY KIND, either express or implied. See the License for the specific language governing
|
||||
permissions and limitations under the License.
|
||||
************************************************************************************/
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Oculus.Interaction.UnityCanvas
|
||||
{
|
||||
public enum RenderingMode
|
||||
{
|
||||
[InspectorName("Alpha-Blended")]
|
||||
AlphaBlended = 0,
|
||||
[InspectorName("Alpha-Cutout")]
|
||||
AlphaCutout,
|
||||
[InspectorName("Opaque")]
|
||||
Opaque,
|
||||
|
||||
[InspectorName("OVR/Overlay")]
|
||||
OVR_Overlay = 100,
|
||||
[InspectorName("OVR/Underlay")]
|
||||
OVR_Underlay
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e5de7ab187e8dc47a0f654ad1e1107a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,35 @@
|
||||
/************************************************************************************
|
||||
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
|
||||
|
||||
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
|
||||
https://developer.oculus.com/licenses/oculussdk/
|
||||
|
||||
Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
||||
ANY KIND, either express or implied. See the License for the specific language governing
|
||||
permissions and limitations under the License.
|
||||
************************************************************************************/
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Oculus.Interaction.UnityCanvas
|
||||
{
|
||||
/// <summary>
|
||||
/// Dropdowns menus in Unity are automatically set to sorting order 30000, which
|
||||
/// does not play nicely with world space UIs.
|
||||
/// Meant to be used in conjunction with an EventTrigger on a given Dropdown, this component
|
||||
/// can be used to set a different sorting order on this and any child canvas.
|
||||
/// </summary>
|
||||
public class UpdateCanvasSortingOrder : MonoBehaviour
|
||||
{
|
||||
public void SetCanvasSortingOrder(int sortingOrder)
|
||||
{
|
||||
Canvas[] canvases = transform.parent.gameObject.GetComponentsInChildren<Canvas>();
|
||||
if (canvases == null) return;
|
||||
foreach (Canvas canvas in canvases)
|
||||
{
|
||||
canvas.sortingOrder = sortingOrder;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f40996cdf2361b8478af26d6e2630d42
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user