104 lines
3.1 KiB
C#
104 lines
3.1 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace Thousandto.Launcher.ExternalLibs
|
|||
|
{
|
|||
|
public class ReflectEffectScript : MonoBehaviour
|
|||
|
{
|
|||
|
|
|||
|
//目标材质索引
|
|||
|
[SerializeField]
|
|||
|
private int _EyeCorneaMatIndex = 0;
|
|||
|
[SerializeField]
|
|||
|
private int _ViewCullMask = -1;
|
|||
|
|
|||
|
//摄像机
|
|||
|
private Camera _viewCamera;
|
|||
|
//材质
|
|||
|
private Material _m;
|
|||
|
//RenderTexture
|
|||
|
private RenderTexture _rt;
|
|||
|
|
|||
|
private void OnEnable()
|
|||
|
{
|
|||
|
if (_rt == null) _rt = RenderTexture.GetTemporary(1136, 640, 24, RenderTextureFormat.ARGB32);
|
|||
|
_rt.antiAliasing = 1;
|
|||
|
_rt.name = "RenderTexture8";
|
|||
|
if (_m == null)
|
|||
|
{
|
|||
|
var r = GetComponent<Renderer>();
|
|||
|
if (r.sharedMaterials != null && r.sharedMaterials.Length > _EyeCorneaMatIndex && _EyeCorneaMatIndex > 0)
|
|||
|
{
|
|||
|
_m = r.sharedMaterials[_EyeCorneaMatIndex];
|
|||
|
}
|
|||
|
}
|
|||
|
if (_viewCamera == null)
|
|||
|
{
|
|||
|
var trans = transform.Find("[ViewCamera]");
|
|||
|
GameObject go;
|
|||
|
if (trans == null)
|
|||
|
{
|
|||
|
go = new GameObject("[ViewCamera]");
|
|||
|
trans = go.transform;
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
go = trans.gameObject;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
trans.parent = transform;
|
|||
|
trans.localPosition = Vector3.zero;
|
|||
|
trans.localRotation = Quaternion.identity;
|
|||
|
|
|||
|
//获取Camera组件
|
|||
|
_viewCamera = go.GetComponent<Camera>();
|
|||
|
if (_viewCamera == null)
|
|||
|
{
|
|||
|
_viewCamera = go.AddComponent<Camera>();
|
|||
|
}
|
|||
|
_viewCamera.enabled = false;
|
|||
|
_viewCamera.clearFlags = CameraClearFlags.SolidColor;
|
|||
|
_viewCamera.backgroundColor = Color.black;
|
|||
|
_viewCamera.targetTexture = _rt;
|
|||
|
_viewCamera.transform.right = transform.right;
|
|||
|
if (_ViewCullMask >= 0) _viewCamera.cullingMask = _ViewCullMask;
|
|||
|
|
|||
|
if (_m != null) _m.mainTexture = _rt;
|
|||
|
_viewCamera.Render();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void OnDestroy()
|
|||
|
{
|
|||
|
if (_rt != null)
|
|||
|
{
|
|||
|
RenderTexture.ReleaseTemporary(_rt);
|
|||
|
_rt = null;
|
|||
|
if (_m != null) _m.mainTexture = null;
|
|||
|
}
|
|||
|
|
|||
|
if (_viewCamera != null)
|
|||
|
{
|
|||
|
GameObject.DestroyImmediate(_viewCamera.gameObject);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
// Update is called once per frame
|
|||
|
void Update()
|
|||
|
{
|
|||
|
if (_viewCamera != null)
|
|||
|
{
|
|||
|
_viewCamera.transform.right = transform.right;
|
|||
|
_viewCamera.Render();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void OnDrawGizmos()
|
|||
|
{
|
|||
|
Gizmos.DrawRay(transform.position, transform.right.normalized);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|