68 lines
1.7 KiB
C#
68 lines
1.7 KiB
C#
using UnityEngine;
|
|
|
|
namespace Moonlit.IceFishing
|
|
{
|
|
[RequireComponent(typeof(Camera))]
|
|
public class MaskCamera : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
public Camera _MainCamera;
|
|
|
|
[SerializeField]
|
|
private Shader _Shader;
|
|
|
|
[SerializeField]
|
|
private string _MaskLayer = "Mask";
|
|
|
|
private Camera _Camera;
|
|
|
|
private RenderTexture _MaskTexture;
|
|
|
|
private void Awake()
|
|
{
|
|
if (_MainCamera == null)
|
|
{
|
|
Debug.LogError("_MainCamera not set");
|
|
return;
|
|
}
|
|
if (_Shader == null)
|
|
{
|
|
Debug.LogError("_Shader not set");
|
|
return;
|
|
}
|
|
_MaskTexture = new RenderTexture(_MainCamera.pixelWidth, _MainCamera.pixelHeight, 0, GetSupportedFormat());
|
|
_Camera = GetComponent<Camera>();
|
|
_Camera.depth = _MainCamera.depth - 1f;
|
|
_Camera.fieldOfView = _MainCamera.fieldOfView;
|
|
_Camera.aspect = _MainCamera.aspect;
|
|
_Camera.nearClipPlane = _MainCamera.nearClipPlane;
|
|
_Camera.farClipPlane = _MainCamera.farClipPlane;
|
|
_Camera.clearFlags = CameraClearFlags.Color;
|
|
_Camera.backgroundColor = Color.white;
|
|
_Camera.cullingMask = LayerMask.GetMask(_MaskLayer);
|
|
_Camera.targetTexture = _MaskTexture;
|
|
_Camera.SetReplacementShader(_Shader, string.Empty);
|
|
Shader.SetGlobalTexture("_Mask", _MaskTexture);
|
|
}
|
|
|
|
public void ChangeCamera(Camera newCamera)
|
|
{
|
|
_Camera.depth = newCamera.depth - 1f;
|
|
_Camera.fieldOfView = newCamera.fieldOfView;
|
|
_Camera.aspect = newCamera.aspect;
|
|
_Camera.nearClipPlane = newCamera.nearClipPlane;
|
|
_Camera.farClipPlane = newCamera.farClipPlane;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_Camera.fieldOfView = _MainCamera.fieldOfView;
|
|
}
|
|
|
|
private RenderTextureFormat GetSupportedFormat()
|
|
{
|
|
return RenderTextureFormat.Default;
|
|
}
|
|
}
|
|
}
|