-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGBDSampler.cs
More file actions
195 lines (159 loc) · 6.27 KB
/
RGBDSampler.cs
File metadata and controls
195 lines (159 loc) · 6.27 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering;
using static UnityEditor.PlayerSettings;
using static UnityEngine.GraphicsBuffer;
[CustomEditor(typeof(RGBDSampler))]
public class RGBDCameraCaptureEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
RGBDSampler captureScript = (RGBDSampler)target;
//if (GUILayout.Button("Init"))
//{
// captureScript.InitCameraAndTexture();
//}
//if (GUILayout.Button("Test"))
//{
// captureScript.Test();
//}
if (GUILayout.Button("Capture Current View"))
{
string path = string.Format("{0}/input/", "SampleOutput");
Directory.CreateDirectory(path);
path = string.Format("{0}/depth/", "SampleOutput");
Directory.CreateDirectory(path);
captureScript.CaptureRGBDasFile(1);
}
if (GUILayout.Button("Manual Sample Scene"))
{
captureScript.ManualSampleScene();
}
}
}
[ExecuteAlways]
public class RGBDSampler : MonoBehaviour
{
private Camera sampleCamera;
private RenderTexture rgbTexture, depthTexture;
private bool depthOnly = false;
[Header("Sample File Settings")]
public string savePath = "SampleOutput"; //this should be the path under your project's folder
public Vector2Int resolution = new(1296, 864);
[Header("Camera Sample Params")]
public int sampleCount = 100;
public GameObject sampleCenter;
public float sampleDistance; //max sample distance from center
public enum RPPL {BiRP, URP, HDRP};
[Header("Rendering Pipeline Settings")]
public RPPL rppl = RPPL.BiRP;
public Material BiRPDepthMaterial;
public GameObject HDRPDepthVolume;
private int SampleCnt = 0;
// This function only works in Built-in Render Pipeline
private void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (depthOnly)
{
Graphics.Blit(src, dest, BiRPDepthMaterial);
}
else
{
Graphics.Blit(src, dest);
}
}
public void InitCameraAndTexture()
{
if (!GetComponent<Camera>())
this.gameObject.AddComponent<Camera>();
sampleCamera = GetComponent<Camera>();
//sample camera must be physical camera to get correct params for training
sampleCamera.usePhysicalProperties = true;
sampleCamera.sensorSize = new Vector2(24f, 18f);
sampleCamera.focalLength = 20f;
sampleCamera.aperture = 22f;
sampleCamera.focusDistance = 10f;
sampleCamera.depthTextureMode = DepthTextureMode.Depth;
rgbTexture = new RenderTexture(resolution.x, resolution.y, 24);
depthTexture = new RenderTexture(resolution.x, resolution.y, 24);
}
private void OnEnable()
{
InitCameraAndTexture();
}
public void CaptureRGBDasFile(int numid)
{
//Create 24 bit Texture(8 for each RGB)
rgbTexture = new RenderTexture(resolution.x, resolution.y, 24);
depthTexture = new RenderTexture(resolution.x, resolution.y, 24);
//Sample RGB
if (rppl == RPPL.BiRP) depthOnly = false;
else if (rppl == RPPL.HDRP) HDRPDepthVolume.SetActive(false);
else
{
Debug.LogError("This script doesn't support for URP yet!");
return;
}
sampleCamera.targetTexture = rgbTexture;
sampleCamera.Render();
RenderTexture.active = rgbTexture;
Texture2D rgbTex = new Texture2D(resolution.x, resolution.y, TextureFormat.RGB24, false);
rgbTex.ReadPixels(new Rect(0, 0, resolution.x, resolution.y), 0, 0);
rgbTex.Apply();
byte[] rgbBytes = rgbTex.EncodeToPNG();
string filename = string.Format("{0}/input/{1}.png", savePath, numid);
File.WriteAllBytes(filename, rgbBytes);
sampleCamera.targetTexture = null;
RenderTexture.active = null;
//Sample Depth
if (rppl == RPPL.BiRP) depthOnly = true;
else if (rppl == RPPL.HDRP) HDRPDepthVolume.SetActive(true);
sampleCamera.targetTexture = depthTexture;
sampleCamera.Render();
RenderTexture.active = depthTexture;
Texture2D depthImage = new Texture2D(resolution.x, resolution.y, TextureFormat.R8, false);
depthImage.ReadPixels(new Rect(0, 0, resolution.x, resolution.y), 0, 0);
depthImage.Apply();
byte[] bytes = depthImage.EncodeToPNG();
string depthFilename = string.Format("{0}/depth/{1}.png", savePath, numid);
File.WriteAllBytes(depthFilename, bytes);
sampleCamera.targetTexture = null;
RenderTexture.active = null;
//Clear states
if (rppl == RPPL.BiRP) depthOnly = false;
else if (rppl == RPPL.HDRP) HDRPDepthVolume.SetActive(false);
}
public void ManualSampleScene()
{
InitCameraAndTexture();
string path = string.Format("{0}/input/", savePath);
Directory.CreateDirectory(path);
path = string.Format("{0}/depth/", savePath);
Directory.CreateDirectory(path);
SampleCnt = 0;
for (int total = 0; total < sampleCount; total++)
{
float degree = UnityEngine.Random.Range(0, 360);
float pitch = UnityEngine.Random.Range(-90, 90);
float dist = UnityEngine.Random.Range(0.01f, sampleDistance);
SampleAt(sampleCenter.transform.position, pitch, degree, dist);
}
}
private void SampleAt(Vector3 center, float pitch, float degree, float dist)
{
Vector3 dir = new Vector3(0, 0, dist);
Quaternion rotation = Quaternion.Euler(pitch, degree, 0);
transform.position = center + rotation * dir;
transform.LookAt(center);
CaptureRGBDasFile(SampleCnt++);
}
public void Test()
{
Vector3 dir = new Vector3(0, 0, sampleDistance);
HDRPDepthVolume.GetComponent<CustomPassVolume>().customPasses[0].targetColorBuffer = CustomPass.TargetBuffer.Custom;
}
}