回答編集履歴

2

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

2017/09/18 14:25

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -345,3 +345,139 @@
345
345
  >
346
346
 
347
347
  > ```
348
+
349
+
350
+
351
+ [さらに追記]
352
+
353
+ すみませんでした。[WebCamTexture, get correct resolution and RATIO. - Unity Answers](http://answers.unity3d.com/questions/773464/webcamtexture-correct-resolution-and-ratio.html)によると、WebCamTextureの正しい解像度は少し待たないと取得できないという罠がある様子でした。間違った解像度でfilteredTextureを作ってしまったためひどい結果になったようです。
354
+
355
+ キャプチャー用スクリプトに解像度がまともになるまでしばらく待機させるよう変更を加えてみましたが、こちらではいかがでしょうか?
356
+
357
+
358
+
359
+ ```C#
360
+
361
+ using UnityEngine;
362
+
363
+
364
+
365
+ public class WebCamController : MonoBehaviour {
366
+
367
+ public int Width = 1920;
368
+
369
+ public int Height = 1080;
370
+
371
+ public int FPS = 30;
372
+
373
+
374
+
375
+ public int mainCamNumber = 0;
376
+
377
+
378
+
379
+ public GameObject b;
380
+
381
+ public GameObject a;
382
+
383
+
384
+
385
+ public Material filterMaterial; // 加工用マテリアル
386
+
387
+
388
+
389
+ private WebCamTexture webcamTexture; // 加工前テクスチャ
390
+
391
+ private RenderTexture filteredTexture; // 加工後のテクスチャ
392
+
393
+
394
+
395
+ // Use this for initialization
396
+
397
+ void Start()
398
+
399
+ {
400
+
401
+
402
+
403
+ WebCamDevice[] devices = WebCamTexture.devices;
404
+
405
+
406
+
407
+ webcamTexture = new WebCamTexture(devices[mainCamNumber].name, Width, Height, FPS); // webcamTextureをインスタンス変数にする
408
+
409
+ webcamTexture.Play();
410
+
411
+ }
412
+
413
+
414
+
415
+ void Update()
416
+
417
+ {
418
+
419
+ // 追加...テクスチャ解像度が適正に取得できるまで待機
420
+
421
+ if (filteredTexture == null)
422
+
423
+ {
424
+
425
+ if (webcamTexture.width < 100)
426
+
427
+ {
428
+
429
+ Debug.Log("Please wait...");
430
+
431
+ return;
432
+
433
+ }
434
+
435
+ else
436
+
437
+ {
438
+
439
+ Debug.LogFormat("WebCamTexture resolution: ({0}, {1})", webcamTexture.width, webcamTexture.height);
440
+
441
+
442
+
443
+ filteredTexture = new RenderTexture(webcamTexture.width, webcamTexture.height, 0); // webcamTextureと同サイズのテクスチャを用意
444
+
445
+
446
+
447
+ a.GetComponent<Renderer>().material.mainTexture = filteredTexture; // webcamTextureの代わりにfilteredTextureをセット
448
+
449
+ b.GetComponent<Renderer>().material.mainTexture = filteredTexture; // webcamTextureの代わりにfilteredTextureをセット
450
+
451
+ }
452
+
453
+ }
454
+
455
+
456
+
457
+ if (Input.GetKey(KeyCode.Space)) // 変化を分かりやすくするため、GetKeyDownの代わりにGetKeyを使用
458
+
459
+ {
460
+
461
+ {
462
+
463
+ Debug.Log("Space");
464
+
465
+ Graphics.Blit(webcamTexture, filteredTexture, filterMaterial); // キーが押されている場合はwebcamTextureをfilterMaterialを通してfilteredTextureにコピーする
466
+
467
+ }
468
+
469
+ }
470
+
471
+ else
472
+
473
+ {
474
+
475
+ Graphics.Blit(webcamTexture, filteredTexture); // キーが押されていない場合はwebcamTextureをそのままfilteredTextureにコピーする
476
+
477
+ }
478
+
479
+ }
480
+
481
+ }
482
+
483
+ ```

1

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

2017/09/18 14:24

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -253,3 +253,95 @@
253
253
  画像の加工に関しては、[マニュアル](https://docs.unity3d.com/jp/540/Manual/WritingImageEffects.html)で紹介されている様々なテクニックが応用可能かと思います。
254
254
 
255
255
  セピアトーンに関してですが、今回のようにグレースケール化して着色するだけでもいいかもしれませんが、[こちら](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/)を見た感じですと、ちゃんと個々の色成分を利用した方がきれいな仕上がりになりそうです。
256
+
257
+
258
+
259
+ [追記]
260
+
261
+ [Legacy Image Effects](https://www.assetstore.unity3d.com/jp/#!/content/83913)に収録されているSepiaToneEffect.shaderの場合は、モノトーンに対してセピア調になるよう色味を加減算しているようです。
262
+
263
+
264
+
265
+ > ```
266
+
267
+ > Shader "Hidden/Sepiatone Effect" {
268
+
269
+ > Properties {
270
+
271
+ > _MainTex ("Base (RGB)", 2D) = "white" {}
272
+
273
+ > }
274
+
275
+ >
276
+
277
+ > SubShader {
278
+
279
+ > Pass {
280
+
281
+ > ZTest Always Cull Off ZWrite Off
282
+
283
+ >
284
+
285
+ > CGPROGRAM
286
+
287
+ > #pragma vertex vert_img
288
+
289
+ > #pragma fragment frag
290
+
291
+ > #include "UnityCG.cginc"
292
+
293
+ >
294
+
295
+ > uniform sampler2D _MainTex;
296
+
297
+ > half4 _MainTex_ST;
298
+
299
+ >
300
+
301
+ > fixed4 frag (v2f_img i) : SV_Target
302
+
303
+ > {
304
+
305
+ > fixed4 original = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST));
306
+
307
+ >
308
+
309
+ > // get intensity value (Y part of YIQ color space)
310
+
311
+ > fixed Y = dot (fixed3(0.299, 0.587, 0.114), original.rgb);
312
+
313
+ >
314
+
315
+ > // Convert to Sepia Tone by adding constant
316
+
317
+ > fixed4 sepiaConvert = float4 (0.191, -0.054, -0.221, 0.0);
318
+
319
+ > fixed4 output = sepiaConvert + Y;
320
+
321
+ > output.a = original.a;
322
+
323
+ >
324
+
325
+ > return output;
326
+
327
+ > }
328
+
329
+ > ENDCG
330
+
331
+ >
332
+
333
+ > }
334
+
335
+ > }
336
+
337
+ >
338
+
339
+ > Fallback off
340
+
341
+ >
342
+
343
+ > }
344
+
345
+ >
346
+
347
+ > ```