6 namespace droid.Runtime.Utilities.GameObjects.NeodroidCamera.Experimental {
10 [RequireComponent(typeof(Camera))]
15 [SerializeField] Shader copy_shader = null;
16 [SerializeField] Material _copy_material = null;
17 [SerializeField] Material _off_screen_mat = null;
18 [SerializeField] Camera _camera = null;
19 [SerializeField] Boolean _debugging =
true;
20 [SerializeField] GUISkin gui_style = null;
22 CommandBuffer[] _copy_cbs = null;
23 CommandBuffer _copy_fb_cb = null;
24 CommandBuffer _copy_gb_cb = null;
25 CommandBuffer _clear_gb_cb = null;
26 CommandBuffer _copy_velocity_cb = null;
27 RenderTexture[] _fb_rts = null;
28 RenderTexture[] _gb_rts = null;
29 Mesh _quad_mesh = null;
31 RenderTargetIdentifier[] _m_rt_fb_ids = null;
32 RenderTargetIdentifier[] _m_rt_gb_ids = null;
33 int _tmp_texture_id = Shader.PropertyToID(
"_TmpFrameBuffer");
34 static readonly
int _clear_color = Shader.PropertyToID(
"_ClearColor");
36 static readonly Tuple<Int32, Int32> _texture_wh =
new Tuple<int, int>(256, 256);
38 const int _preview_size = 100;
39 const int _preview_margin = 20;
46 public Boolean Debugging {
get {
return this._debugging; }
set { this._debugging = value; } }
49 var w = this._camera.pixelWidth;
50 var h = this._camera.pixelHeight;
51 var aspect = (float)h / w;
52 w = _texture_wh.Item1;
53 h = (int)(w * aspect);
54 return new Tuple<int, int>(w, h);
58 if (_texture_wh.Item1 == Screen.width && _texture_wh.Item2 == Screen.height) {
62 var xw = _texture_wh.Item1;
63 var yh = _texture_wh.Item2;
65 var x = Screen.width / 2 - xw / 2;
66 var y = Screen.height / 2 - yh / 2;
68 this._camera.pixelRect =
new Rect(x, y, xw, yh);
72 if (!this.gui_style) {
73 this.gui_style = Resources.FindObjectsOfTypeAll<GUISkin>().First(a => a.name ==
"BoundingBox");
76 if (!this._copy_material) {
77 this._copy_material =
new Material(this.copy_shader);
80 if (!this._quad_mesh) {
81 this._quad_mesh = CreateFullscreenQuad();
85 this._camera = this.GetComponent<Camera>();
90 if (this._fb_rts == null || this._fb_rts.Length != 2) {
91 this._fb_rts =
new RenderTexture[2];
92 for (var i = 0; i < this._fb_rts.Length; ++i) {
94 new RenderTexture(_texture_wh.Item1, _texture_wh.Item2, 0, RenderTextureFormat.ARGBHalf) {
101 this._fb_rts[i].Create();
105 this._m_rt_gb_ids =
new RenderTargetIdentifier[] {this._fb_rts[0], this._fb_rts[1]};
107 if (this._gb_rts == null || this._gb_rts.Length != 8) {
127 this._gb_rts =
new RenderTexture[8];
128 for (var i = 0; i < this._gb_rts.Length; ++i) {
130 new RenderTexture(_texture_wh.Item1, _texture_wh.Item2, 0, RenderTextureFormat.ARGBHalf) {
137 this._gb_rts[i].Create();
141 this._m_rt_fb_ids =
new RenderTargetIdentifier[] {
161 new Vector3(1.0f, 1.0f, 0.0f),
162 new Vector3(-1.0f, 1.0f, 0.0f),
163 new Vector3(-1.0f, -1.0f, 0.0f),
164 new Vector3(1.0f, -1.0f, 0.0f)
166 triangles =
new[] {0, 1, 2, 2, 3, 0}
168 r.UploadMeshData(
true);
175 if (this.copy_shader == null) {
176 Debug.LogError(
"Copy shader is missing!");
180 if (this._off_screen_mat != null) {
181 if (this._camera.targetTexture != null) {
182 this._off_screen_mat.EnableKeyword(
"OFFSCREEN");
184 this._off_screen_mat.DisableKeyword(
"OFFSCREEN");
188 this._copy_fb_cb =
new CommandBuffer {name =
"Copy FrameBuffer"};
189 this._copy_fb_cb.GetTemporaryRT(this._tmp_texture_id, -1, -1, 0, FilterMode.Point);
190 this._copy_fb_cb.Blit(BuiltinRenderTextureType.CurrentActive,
this._tmp_texture_id);
191 this._copy_fb_cb.SetRenderTarget(this._m_rt_gb_ids, this._fb_rts[0]);
192 this._copy_fb_cb.DrawMesh(this._quad_mesh, Matrix4x4.identity,
this._copy_material, 0, 0);
193 this._copy_fb_cb.ReleaseTemporaryRT(this._tmp_texture_id);
194 this._camera.AddCommandBuffer(CameraEvent.AfterEverything,
this._copy_fb_cb);
196 this._clear_gb_cb =
new CommandBuffer {
197 name =
"Cleanup GBuffer" 199 if (this._camera.allowHDR) {
200 this._clear_gb_cb.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
202 this._clear_gb_cb.SetRenderTarget(BuiltinRenderTextureType.GBuffer3);
205 this._clear_gb_cb.DrawMesh(this._quad_mesh, Matrix4x4.identity,
this._copy_material, 0, 3);
206 this._copy_material.SetColor(_clear_color, this._camera.backgroundColor);
208 this._copy_gb_cb =
new CommandBuffer {name =
"Copy GBuffer"};
209 this._copy_gb_cb.SetRenderTarget(this._m_rt_fb_ids, this._gb_rts[0]);
210 this._copy_gb_cb.DrawMesh(this._quad_mesh, Matrix4x4.identity,
this._copy_material, 0, 2);
211 this._camera.AddCommandBuffer(CameraEvent.BeforeGBuffer,
this._clear_gb_cb);
212 this._camera.AddCommandBuffer(CameraEvent.BeforeLighting,
this._copy_gb_cb);
214 this._copy_velocity_cb =
new CommandBuffer {name =
"Copy Velocity"};
215 this._copy_velocity_cb.SetRenderTarget(this._gb_rts[7]);
216 this._copy_velocity_cb.DrawMesh(this._quad_mesh, Matrix4x4.identity,
this._copy_material, 0, 4);
217 this._camera.AddCommandBuffer(CameraEvent.BeforeImageEffectsOpaque,
this._copy_velocity_cb);
218 this._camera.depthTextureMode = DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
220 this._copy_cbs =
new[] {this._copy_fb_cb, this._clear_gb_cb, this._copy_gb_cb, this._copy_velocity_cb};
224 if (this._debugging) {
227 if (this._gb_rts != null) {
228 foreach (var pass
in this._gb_rts) {
229 var xi = (_preview_size + _preview_margin) * index++;
230 var x = xi % (Screen.width - _preview_size);
231 var y = (_preview_size + _preview_margin) * (xi / (Screen.width - _preview_size));
232 var r =
new Rect(_preview_margin + x, _preview_margin + y, _preview_size, _preview_size);
235 GUI.DrawTexture(r, pass, ScaleMode.ScaleToFit);
236 GUI.TextField(r, pass.name,
this.gui_style.box);
240 if (this._fb_rts != null) {
241 foreach (var pass
in this._fb_rts) {
242 var xi = (_preview_size + _preview_margin) * index++;
243 var x = xi % (Screen.width - _preview_size);
244 var y = (_preview_size + _preview_margin) * (xi / (Screen.width - _preview_size));
245 var r =
new Rect(_preview_margin + x, _preview_margin + y, _preview_size, _preview_size);
248 GUI.DrawTexture(r, pass, ScaleMode.ScaleToFit);
249 GUI.TextField(r, pass.name,
this.gui_style.box);
260 this._camera.RemoveAllCommandBuffers();
262 if (this._copy_fb_cb != null) {
263 this._camera.RemoveCommandBuffer(CameraEvent.AfterEverything,
this._copy_fb_cb);
264 this._copy_fb_cb.Release();
265 this._copy_fb_cb = null;
268 if (this._clear_gb_cb != null) {
269 this._camera.RemoveCommandBuffer(CameraEvent.BeforeGBuffer,
this._clear_gb_cb);
270 this._clear_gb_cb.Release();
271 this._clear_gb_cb = null;
274 if (this._copy_gb_cb != null) {
275 this._camera.RemoveCommandBuffer(CameraEvent.BeforeLighting,
this._copy_gb_cb);
276 this._copy_gb_cb.Release();
277 this._copy_gb_cb = null;
280 if (this._copy_velocity_cb != null) {
281 this._camera.RemoveCommandBuffer(CameraEvent.BeforeImageEffectsOpaque,
this._copy_velocity_cb);
282 this._copy_velocity_cb.Release();
283 this._copy_velocity_cb = null;
286 if (this._fb_rts != null) {
287 foreach (var rt
in this._fb_rts) {
294 if (this._gb_rts != null) {
295 foreach (var rt
in this._gb_rts) {
Tuple< int, int > GetCaptureResolutionFromCamera()
static Mesh CreateFullscreenQuad()