1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
// 给每个角色准备一些特殊的前置渲染
public class UnitPreprocessing : UnitComponent
{
[Flags]
public enum EUnitPreprocessing
{
None ,
DepthTexture, // Unit Depth Texture
WorldNormalTexture, // Unit World Normal Texture
}
public EUnitPreprocessing preprocessing;
public RenderTexture unitDepthTexture { get; private set; }
public RenderTexture unitWorldNormalTexture { get; private set; }
private CommandBuffer m_CBBeforeDepthTexture;
private Material m_MaterialDepth;
public override void Initialize()
{
base.Initialize();
}
public override void OnPostInitialize()
{
base.OnPostInitialize();
MainCamera.Instance.customRenderingPipeline.onPreCull += OnWillRenderUnit;
MainCamera.Instance.customRenderingPipeline.onPostRender += OnRenderUnit;
m_CBBeforeDepthTexture = new CommandBuffer();
PrepareRenderTextures();
PrepareMaterials();
}
void PrepareRenderTextures()
{
int unitHash = owner.GetHashCode();
int width = MainCamera.Instance.camera.pixelWidth;
int height = MainCamera.Instance.camera.pixelHeight;
unitDepthTexture = new RenderTexture(width, height, 24, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear);
unitDepthTexture.name = "UnitDepthTexture_" + owner.GetHashCode();
}
void PrepareMaterials()
{
m_MaterialDepth = new Material(Shader.Find(StaticDefine.shaders[EShader.UnitDepth].name));
}
public override void OnUpdate()
{
base.OnUpdate();
}
public override void Release()
{
MainCamera.Instance.customRenderingPipeline.onPreCull -= OnWillRenderUnit;
MainCamera.Instance.customRenderingPipeline.onPostRender -= OnRenderUnit;
base.Release();
}
private void OnWillRenderUnit()
{
RenderDepthTexture();
//RenderWorldNormal();
MainCamera.Instance.camera.AddCommandBuffer(CameraEvent.BeforeDepthTexture, m_CBBeforeDepthTexture);
}
void RenderDepthTexture()
{
var cb = m_CBBeforeDepthTexture;
cb.Clear();
cb.SetRenderTarget(unitDepthTexture);
cb.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
cb.SetGlobalVector("unity_LightShadowBias", Vector4.zero);
foreach (var r in GetRenderers())
{
BodyPartRenderer br = r as BodyPartRenderer;
if (br == null)
continue;
Renderer renderer = br.renderer as Renderer;
if (renderer == null)
continue;
cb.DrawRenderer(renderer, m_MaterialDepth);
}
}
//void RenderWorldNormal()
//{
// var cb = m_CBBeforeDepthTexture;
// cb.Clear();
// cb.GetTemporaryRT(unitWorldNormalTextureID, -1, -1, 24, FilterMode.Point, RenderTextureFormat.RG16, RenderTextureReadWrite.Linear);
// cb.SetRenderTarget(unitWorldNormalTextureID);
// cb.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
//}
IEnumerable GetRenderers()
{
IBodyRendererAgent body = owner.unitRender.body;
if (body == null || body.renderers == null || body.renderers.Length == 0)
yield break;
for (int j = 0; j < body.renderers.Length; ++j)
{
yield return body.renderers[j];
}
}
private void OnRenderUnit()
{
//m_CBBeforeDepthTexture.ReleaseTemporaryRT(unitDepthTextureID);
MainCamera.Instance.camera.RemoveCommandBuffer(CameraEvent.BeforeDepthTexture, m_CBBeforeDepthTexture);
}
}
|