-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCommandBuffy.cs
More file actions
152 lines (115 loc) · 3.87 KB
/
CommandBuffy.cs
File metadata and controls
152 lines (115 loc) · 3.87 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Events;
[System.Serializable]
public class UnityEvent_OnCaptureTextureCreated : UnityEvent <Texture> {}
public class CommandBuffy : MonoBehaviour {
public RenderTexture Target;
[ShowIfAttribute("TargetIsNull")]
public RenderTextureFormat TargetFormat = RenderTextureFormat.ARGBFloat;
[Range(1,4096)]
[ShowIfAttribute("TargetIsNull")]
public int TargetWidth = 2048;
[Range(1,4096)]
[ShowIfAttribute("TargetIsNull")]
public int TargetHeight = 2048;
[Header("Called if we create a target so you can assign to a material etc, or just called in start")]
public UnityEvent_OnCaptureTextureCreated OnCaptureTextureCreated;
[Header("When to capture from the screen")]
public CameraEvent CaptureAfterEvent = CameraEvent.AfterSkybox;
[ShowIfAttribute("CaptureEventRequiresDepthMode")]
public bool SetCameraDepthMode = false;
public BuiltinRenderTextureType CaptureSource = BuiltinRenderTextureType.CurrentActive;
public bool AssignToAllCameras = true;
[ShowIfAttribute("IsAssignToSpecificCameras")]
public List<Camera> AssignToCameraList;
Dictionary<Camera,CommandBuffer> CameraCommands;
bool CaptureEventRequiresDepthMode()
{
switch ( CaptureAfterEvent )
{
case CameraEvent.AfterDepthNormalsTexture:
case CameraEvent.AfterDepthTexture:
case CameraEvent.BeforeDepthNormalsTexture:
case CameraEvent.BeforeDepthTexture:
return true;
default:
return false;
}
}
bool TargetIsNull()
{
return Target == null;
}
bool IsAssignToSpecificCameras()
{
return !AssignToAllCameras;
}
void Start ()
{
if (Target == null) {
Target = new RenderTexture (TargetWidth, TargetHeight, 0, TargetFormat);
}
OnCaptureTextureCreated.Invoke (Target);
}
void OnEnable()
{
var CameraList = AssignToAllCameras ? Camera.allCameras : AssignToCameraList.ToArray ();
if (CameraList != null) {
foreach (var Cam in CameraList) {
CreateCommand (Cam);
}
}
}
void OnDisable()
{
if (CameraCommands != null) {
foreach (var Cam in CameraCommands.Keys) {
ReleaseCommand (Cam);
}
}
}
void CreateCommand(Camera Cam)
{
// if command already exists, replace it
ReleaseCommand(Cam);
// make new command
var Command = new CommandBuffer();
Command.name = this.name;
var DepthNormalsEvent = (CaptureAfterEvent == CameraEvent.AfterDepthNormalsTexture) || (CaptureAfterEvent == CameraEvent.BeforeDepthNormalsTexture);
var DepthEvent = (CaptureAfterEvent == CameraEvent.AfterDepthTexture) || (CaptureAfterEvent == CameraEvent.BeforeDepthTexture);
if (DepthEvent && Cam.depthTextureMode == DepthTextureMode.None) {
if (SetCameraDepthMode)
Cam.depthTextureMode = DepthTextureMode.Depth;
else
Debug.LogWarning ("Trying to capture " + CaptureSource + " but camera depth mode is " + Cam.depthTextureMode);
}
if (DepthNormalsEvent && Cam.depthTextureMode != DepthTextureMode.DepthNormals) {
if (SetCameraDepthMode)
Cam.depthTextureMode = DepthTextureMode.DepthNormals;
else
Debug.LogWarning ("Trying to capture " + CaptureSource + " but camera depth mode is " + Cam.depthTextureMode);
}
Debug.Log ("Camera " + Cam.name + " depth mode is " + Cam.depthTextureMode);
// get target id
var TargetId = new RenderTargetIdentifier(Target);
var SourceId = new RenderTargetIdentifier (CaptureSource);
Command.Blit (SourceId, TargetId);
Cam.AddCommandBuffer (CaptureAfterEvent,Command);
if ( CameraCommands == null )
CameraCommands = new Dictionary<Camera,CommandBuffer> ();
CameraCommands.Add (Cam, Command);
}
void ReleaseCommand(Camera Cam)
{
if (CameraCommands == null)
return;
if (!CameraCommands.ContainsKey (Cam))
return;
Cam.RemoveCommandBuffer (CaptureAfterEvent,CameraCommands [Cam]);
CameraCommands [Cam].Release ();
CameraCommands.Remove (Cam);
}
}