t1 / TFDContents / Assets / KinectDemos / FittingRoomDemo / Scripts / UserBodyBlender.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (5.83 KB)
1 |
using UnityEngine; |
---|---|
2 |
using System.Collections; |
3 |
|
4 |
|
5 |
public class UserBodyBlender : MonoBehaviour |
6 |
{ |
7 |
[Tooltip("Allowed depth distance between the user and the clothing model, in meters.")] |
8 |
[Range(-0.5f, 0.5f)] |
9 |
public float depthThreshold = 0.1f; |
10 |
|
11 |
[Tooltip("Camera used to render the scene background. The background camera gets disabled, when this component is enabled.")] |
12 |
public Camera backroundCamera; |
13 |
|
14 |
private Material userBlendMat; |
15 |
private KinectManager kinectManager; |
16 |
private long lastDepthFrameTime; |
17 |
|
18 |
private Vector2[] color2DepthCoords; |
19 |
private ComputeBuffer color2DepthBuffer; |
20 |
|
21 |
private float[] depthImageBufferData; |
22 |
private ComputeBuffer depthImageBuffer; |
23 |
|
24 |
private Rect shaderUvRect = new Rect(0, 0, 1, 1); |
25 |
private bool shaderRectInited = false; |
26 |
|
27 |
//private float depthFactor = 1f; |
28 |
|
29 |
private RenderTexture copyToTex; |
30 |
|
31 |
|
32 |
// sets texture to copy to |
33 |
/// <summary> |
34 |
/// Sets the texture to copy the camera image to. |
35 |
/// </summary> |
36 |
/// <param name="tex">The target texture</param> |
37 |
public void SetCopyToTexture(RenderTexture tex) |
38 |
{ |
39 |
copyToTex = tex; |
40 |
} |
41 |
|
42 |
|
43 |
void OnEnable() |
44 |
{ |
45 |
// set camera to clear the background |
46 |
Camera thisCamera = gameObject.GetComponent<Camera>(); |
47 |
if(thisCamera) |
48 |
{ |
49 |
thisCamera.clearFlags = CameraClearFlags.SolidColor; |
50 |
} |
51 |
|
52 |
// disable the background camera |
53 |
if (backroundCamera) |
54 |
{ |
55 |
backroundCamera.gameObject.SetActive(false); |
56 |
} |
57 |
} |
58 |
|
59 |
|
60 |
void OnDisable() |
61 |
{ |
62 |
// set camera to clear the depth buffser only |
63 |
Camera thisCamera = gameObject.GetComponent<Camera>(); |
64 |
if(thisCamera) |
65 |
{ |
66 |
thisCamera.clearFlags = CameraClearFlags.Depth; |
67 |
} |
68 |
|
69 |
// enable the background camera |
70 |
if (backroundCamera) |
71 |
{ |
72 |
backroundCamera.gameObject.SetActive(true); |
73 |
} |
74 |
} |
75 |
|
76 |
|
77 |
void Start () |
78 |
{ |
79 |
kinectManager = KinectManager.Instance; |
80 |
|
81 |
if(kinectManager && kinectManager.IsInitialized() && |
82 |
kinectManager.GetSensorData().sensorIntPlatform == KinectInterop.DepthSensorPlatform.KinectSDKv2) |
83 |
{ |
84 |
Shader userBlendShader = Shader.Find("Custom/UserBlendShader"); |
85 |
KinectInterop.SensorData sensorData = kinectManager.GetSensorData(); |
86 |
|
87 |
if(userBlendShader != null && sensorData != null) |
88 |
{ |
89 |
userBlendMat = new Material(userBlendShader); |
90 |
|
91 |
userBlendMat.SetFloat("_ColorResX", (float)sensorData.colorImageWidth); |
92 |
userBlendMat.SetFloat("_ColorResY", (float)sensorData.colorImageHeight); |
93 |
userBlendMat.SetFloat("_DepthResX", (float)sensorData.depthImageWidth); |
94 |
userBlendMat.SetFloat("_DepthResY", (float)sensorData.depthImageHeight); |
95 |
|
96 |
//depthFactor = 1f + Mathf.Sin (Mathf.Abs (kinectManager.sensorAngle) * Mathf.Deg2Rad); |
97 |
//userBlendMat.SetFloat("_DepthFactor", depthFactor); |
98 |
|
99 |
color2DepthCoords = new Vector2[sensorData.colorImageWidth * sensorData.colorImageHeight]; |
100 |
|
101 |
color2DepthBuffer = new ComputeBuffer(sensorData.colorImageWidth * sensorData.colorImageHeight, sizeof(float) * 2); |
102 |
userBlendMat.SetBuffer("_DepthCoords", color2DepthBuffer); |
103 |
|
104 |
depthImageBufferData = new float[sensorData.depthImage.Length]; |
105 |
|
106 |
depthImageBuffer = new ComputeBuffer(sensorData.depthImage.Length, sizeof(float)); |
107 |
userBlendMat.SetBuffer("_DepthBuffer", depthImageBuffer); |
108 |
|
109 |
//userBlendMat.SetTexture("_ColorTex", sensorData.colorImageTexture); |
110 |
} |
111 |
} |
112 |
else |
113 |
{ |
114 |
// disable the component |
115 |
gameObject.GetComponent<UserBodyBlender>().enabled = false; |
116 |
} |
117 |
} |
118 |
|
119 |
void OnDestroy() |
120 |
{ |
121 |
if(color2DepthBuffer != null) |
122 |
{ |
123 |
color2DepthBuffer.Release(); |
124 |
color2DepthBuffer = null; |
125 |
} |
126 |
|
127 |
if(depthImageBuffer != null) |
128 |
{ |
129 |
depthImageBuffer.Release(); |
130 |
depthImageBuffer = null; |
131 |
} |
132 |
|
133 |
color2DepthCoords = null; |
134 |
depthImageBufferData = null; |
135 |
} |
136 |
|
137 |
void Update() |
138 |
{ |
139 |
if (!shaderRectInited) |
140 |
{ |
141 |
PortraitBackground portraitBack = PortraitBackground.Instance; |
142 |
if(portraitBack && portraitBack.IsInitialized()) |
143 |
{ |
144 |
shaderUvRect = portraitBack.GetShaderUvRect(); |
145 |
} |
146 |
|
147 |
if (userBlendMat != null) |
148 |
{ |
149 |
userBlendMat.SetFloat("_ColorOfsX", shaderUvRect.x); |
150 |
userBlendMat.SetFloat("_ColorMulX", shaderUvRect.width); |
151 |
userBlendMat.SetFloat("_ColorOfsY", shaderUvRect.y); |
152 |
userBlendMat.SetFloat("_ColorMulY", shaderUvRect.height); |
153 |
} |
154 |
|
155 |
shaderRectInited = true; |
156 |
} |
157 |
|
158 |
if(kinectManager && kinectManager.IsInitialized()) |
159 |
{ |
160 |
KinectInterop.SensorData sensorData = kinectManager.GetSensorData(); |
161 |
|
162 |
if(sensorData != null && sensorData.depthImage != null && sensorData.colorImageTexture && |
163 |
userBlendMat != null && lastDepthFrameTime != sensorData.lastDepthFrameTime) |
164 |
{ |
165 |
lastDepthFrameTime = sensorData.lastDepthFrameTime; |
166 |
//userBlendMat.SetTexture("_ColorTex", sensorData.colorImageTexture); |
167 |
|
168 |
//if (kinectManager.autoHeightAngle == KinectManager.AutoHeightAngle.AutoUpdate || kinectManager.autoHeightAngle == KinectManager.AutoHeightAngle.AutoUpdateAndShowInfo) |
169 |
//{ |
170 |
// depthFactor = 1f + Mathf.Sin (Mathf.Abs (kinectManager.sensorAngle) * Mathf.Deg2Rad); |
171 |
// userBlendMat.SetFloat("_DepthFactor", depthFactor); |
172 |
//} |
173 |
|
174 |
if(KinectInterop.MapColorFrameToDepthCoords(sensorData, ref color2DepthCoords)) |
175 |
{ |
176 |
color2DepthBuffer.SetData(color2DepthCoords); |
177 |
} |
178 |
|
179 |
// buffer for depths |
180 |
for (int i = 0; i < sensorData.depthImage.Length; i++) |
181 |
{ |
182 |
ushort depth = sensorData.depthImage[i]; |
183 |
depthImageBufferData[i] = (float)depth; |
184 |
} |
185 |
|
186 |
depthImageBuffer.SetData(depthImageBufferData); |
187 |
|
188 |
// color camera texture |
189 |
userBlendMat.SetTexture("_ColorTex", sensorData.colorImageTexture); |
190 |
} |
191 |
} |
192 |
} |
193 |
|
194 |
void OnRenderImage (RenderTexture source, RenderTexture destination) |
195 |
{ |
196 |
if(userBlendMat != null) |
197 |
{ |
198 |
userBlendMat.SetFloat("_Threshold", depthThreshold); |
199 |
Graphics.Blit(source, destination, userBlendMat); |
200 |
|
201 |
if (copyToTex != null) |
202 |
{ |
203 |
Graphics.Blit(destination, copyToTex); |
204 |
} |
205 |
} |
206 |
} |
207 |
} |