137 lines
3.8 KiB
C#
137 lines
3.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
[CustomEditor(typeof(RoleRenderManager))]
|
|
public class RoleRenderManagerEditor : Editor
|
|
{
|
|
public override void OnInspectorGUI()
|
|
{
|
|
if(GUILayout.Button("Init"))
|
|
{
|
|
var man = (target as RoleRenderManager);
|
|
if( man.uiCam == null)
|
|
{
|
|
EditorUtility.DisplayDialog("错误", "请先配置UI Cam", "OK");
|
|
}
|
|
if (man.roleCam == null)
|
|
{
|
|
EditorUtility.DisplayDialog("错误", "请先配置Role Cam", "OK");
|
|
}
|
|
if ( man.uiCam != null && man.roleCam != null)
|
|
{
|
|
man.Init(man.uiCam, man.roleCam);
|
|
man.UpdateCMD();
|
|
}
|
|
}
|
|
base.OnInspectorGUI();
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
public class RoleRenderManager : MonoBehaviour
|
|
{
|
|
public Camera uiCam;
|
|
public Camera roleCam;
|
|
public CameraEvent camEvent = CameraEvent.BeforeForwardAlpha;
|
|
public int width = 400;
|
|
public int height = 800;
|
|
public MeshRenderer[] roleRenderer;
|
|
|
|
CommandBuffer cmd;
|
|
static readonly int[] RoleRTId = new int[] {
|
|
Shader.PropertyToID("_RoleTex0_RT"),
|
|
Shader.PropertyToID("_RoleTex1_RT"),
|
|
Shader.PropertyToID("_RoleTex2_RT"),
|
|
Shader.PropertyToID("_RoleTex3_RT"),
|
|
Shader.PropertyToID("_RoleTex4_RT"),
|
|
Shader.PropertyToID("_RoleTex5_RT"),
|
|
};
|
|
|
|
static readonly int[] RoleTexId = new int[] {
|
|
Shader.PropertyToID("_RoleTex0"),
|
|
Shader.PropertyToID("_RoleTex1"),
|
|
Shader.PropertyToID("_RoleTex2"),
|
|
Shader.PropertyToID("_RoleTex3"),
|
|
Shader.PropertyToID("_RoleTex4"),
|
|
Shader.PropertyToID("_RoleTex5"),
|
|
};
|
|
/// <summary>
|
|
/// 初始化组件
|
|
/// </summary>
|
|
/// <param name="uiCam"></param>
|
|
/// <param name="roleCam"></param>
|
|
public void Init(Camera uiCam, Camera roleCam)
|
|
{
|
|
this.roleCam = roleCam;
|
|
if( roleCam != null && roleCam.enabled ){
|
|
roleCam.enabled = false;
|
|
}
|
|
this.uiCam = uiCam;
|
|
if( uiCam != null)
|
|
{
|
|
cmd = new CommandBuffer();
|
|
uiCam.AddCommandBuffer(camEvent, cmd);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新command Buffer
|
|
/// </summary>
|
|
public void UpdateCMD()
|
|
{
|
|
cmd.Clear();
|
|
int roleCount = Mathf.Min(RoleRTId.Length, roleRenderer.Length);
|
|
RenderTextureDescriptor rtDes = new RenderTextureDescriptor(width, height, RenderTextureFormat.ARGB32, 0);
|
|
cmd.SetViewMatrix(roleCam.worldToCameraMatrix);
|
|
cmd.SetProjectionMatrix(roleCam.projectionMatrix);
|
|
for (int i = 0; i < roleCount; i++)
|
|
{
|
|
cmd.GetTemporaryRT(RoleRTId[i], rtDes);
|
|
cmd.SetRenderTarget(RoleRTId[i]);
|
|
cmd.ClearRenderTarget(false, true, Color.clear);
|
|
if (roleRenderer[i] != null)
|
|
{
|
|
cmd.DrawRenderer(roleRenderer[i], roleRenderer[i].sharedMaterial);
|
|
}
|
|
cmd.SetGlobalTexture(RoleTexId[i], RoleRTId[i]);
|
|
}
|
|
cmd.SetProjectionMatrix(uiCam.projectionMatrix);
|
|
cmd.SetViewMatrix(uiCam.worldToCameraMatrix);
|
|
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
|
|
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void OnEnable()
|
|
{
|
|
if (uiCam != null && roleCam != null && cmd != null)
|
|
{
|
|
UpdateCMD();
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (uiCam != null && cmd != null)
|
|
{
|
|
uiCam.RemoveCommandBuffer(camEvent, cmd);
|
|
cmd.Clear();
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (cmd != null)
|
|
{
|
|
cmd.Dispose();
|
|
cmd = null;
|
|
}
|
|
}
|
|
|
|
}
|