teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

適正な解像度になるようにキャプチャー用スクリプトを修正

2017/09/18 14:25

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -171,4 +171,72 @@
171
171
  >
172
172
  > }
173
173
  >
174
- > ```
174
+ > ```
175
+
176
+ [さらに追記]
177
+ すみませんでした。[WebCamTexture, get correct resolution and RATIO. - Unity Answers](http://answers.unity3d.com/questions/773464/webcamtexture-correct-resolution-and-ratio.html)によると、WebCamTextureの正しい解像度は少し待たないと取得できないという罠がある様子でした。間違った解像度でfilteredTextureを作ってしまったためひどい結果になったようです。
178
+ キャプチャー用スクリプトに解像度がまともになるまでしばらく待機させるよう変更を加えてみましたが、こちらではいかがでしょうか?
179
+
180
+ ```C#
181
+ using UnityEngine;
182
+
183
+ public class WebCamController : MonoBehaviour {
184
+ public int Width = 1920;
185
+ public int Height = 1080;
186
+ public int FPS = 30;
187
+
188
+ public int mainCamNumber = 0;
189
+
190
+ public GameObject b;
191
+ public GameObject a;
192
+
193
+ public Material filterMaterial; // 加工用マテリアル
194
+
195
+ private WebCamTexture webcamTexture; // 加工前テクスチャ
196
+ private RenderTexture filteredTexture; // 加工後のテクスチャ
197
+
198
+ // Use this for initialization
199
+ void Start()
200
+ {
201
+
202
+ WebCamDevice[] devices = WebCamTexture.devices;
203
+
204
+ webcamTexture = new WebCamTexture(devices[mainCamNumber].name, Width, Height, FPS); // webcamTextureをインスタンス変数にする
205
+ webcamTexture.Play();
206
+ }
207
+
208
+ void Update()
209
+ {
210
+ // 追加...テクスチャ解像度が適正に取得できるまで待機
211
+ if (filteredTexture == null)
212
+ {
213
+ if (webcamTexture.width < 100)
214
+ {
215
+ Debug.Log("Please wait...");
216
+ return;
217
+ }
218
+ else
219
+ {
220
+ Debug.LogFormat("WebCamTexture resolution: ({0}, {1})", webcamTexture.width, webcamTexture.height);
221
+
222
+ filteredTexture = new RenderTexture(webcamTexture.width, webcamTexture.height, 0); // webcamTextureと同サイズのテクスチャを用意
223
+
224
+ a.GetComponent<Renderer>().material.mainTexture = filteredTexture; // webcamTextureの代わりにfilteredTextureをセット
225
+ b.GetComponent<Renderer>().material.mainTexture = filteredTexture; // webcamTextureの代わりにfilteredTextureをセット
226
+ }
227
+ }
228
+
229
+ if (Input.GetKey(KeyCode.Space)) // 変化を分かりやすくするため、GetKeyDownの代わりにGetKeyを使用
230
+ {
231
+ {
232
+ Debug.Log("Space");
233
+ Graphics.Blit(webcamTexture, filteredTexture, filterMaterial); // キーが押されている場合はwebcamTextureをfilterMaterialを通してfilteredTextureにコピーする
234
+ }
235
+ }
236
+ else
237
+ {
238
+ Graphics.Blit(webcamTexture, filteredTexture); // キーが押されていない場合はwebcamTextureをそのままfilteredTextureにコピーする
239
+ }
240
+ }
241
+ }
242
+ ```

1

スタンダードアセットのコードを追記

2017/09/18 14:24

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -125,4 +125,50 @@
125
125
  ![キャプチャースクリプト](227cbeadcfa400c59b5327ee782c266a.png)
126
126
 
127
127
  画像の加工に関しては、[マニュアル](https://docs.unity3d.com/jp/540/Manual/WritingImageEffects.html)で紹介されている様々なテクニックが応用可能かと思います。
128
- セピアトーンに関してですが、今回のようにグレースケール化して着色するだけでもいいかもしれませんが、[こちら](https://alastaira.wordpress.com/2013/12/02/sepia-shader/)や[こちら](http://www.techrepublic.com/blog/how-do-i/how-do-i-convert-images-to-grayscale-and-sepia-tone-using-c/)を見た感じですと、ちゃんと個々の色成分を利用した方がきれいな仕上がりになりそうです。
128
+ セピアトーンに関してですが、今回のようにグレースケール化して着色するだけでもいいかもしれませんが、[こちら](https://alastaira.wordpress.com/2013/12/02/sepia-shader/)や[こちら](http://www.techrepublic.com/blog/how-do-i/how-do-i-convert-images-to-grayscale-and-sepia-tone-using-c/)を見た感じですと、ちゃんと個々の色成分を利用した方がきれいな仕上がりになりそうです。
129
+
130
+ [追記]
131
+ [Legacy Image Effects](https://www.assetstore.unity3d.com/jp/#!/content/83913)に収録されているSepiaToneEffect.shaderの場合は、モノトーンに対してセピア調になるよう色味を加減算しているようです。
132
+
133
+ > ```
134
+ > Shader "Hidden/Sepiatone Effect" {
135
+ > Properties {
136
+ > _MainTex ("Base (RGB)", 2D) = "white" {}
137
+ > }
138
+ >
139
+ > SubShader {
140
+ > Pass {
141
+ > ZTest Always Cull Off ZWrite Off
142
+ >
143
+ > CGPROGRAM
144
+ > #pragma vertex vert_img
145
+ > #pragma fragment frag
146
+ > #include "UnityCG.cginc"
147
+ >
148
+ > uniform sampler2D _MainTex;
149
+ > half4 _MainTex_ST;
150
+ >
151
+ > fixed4 frag (v2f_img i) : SV_Target
152
+ > {
153
+ > fixed4 original = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST));
154
+ >
155
+ > // get intensity value (Y part of YIQ color space)
156
+ > fixed Y = dot (fixed3(0.299, 0.587, 0.114), original.rgb);
157
+ >
158
+ > // Convert to Sepia Tone by adding constant
159
+ > fixed4 sepiaConvert = float4 (0.191, -0.054, -0.221, 0.0);
160
+ > fixed4 output = sepiaConvert + Y;
161
+ > output.a = original.a;
162
+ >
163
+ > return output;
164
+ > }
165
+ > ENDCG
166
+ >
167
+ > }
168
+ > }
169
+ >
170
+ > Fallback off
171
+ >
172
+ > }
173
+ >
174
+ > ```