質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

547閲覧

WebCamTextureを使ってRGBの値をリアルタイムで変更したいです。

tig526

総合スコア7

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2017/09/16 16:16

これで指定キーを押したら指定したRGBの値に変更するというコードを書きたいのですが、よくよくは白黒やセピアのようなものにしたいとも思ってますが初歩的なところで躓いてしまい先に進めません。よろしければヒントでもいいのでいただけると幸いです。よろしくお願いします。

public int Width = 1920;
public int Height = 1080;
public int FPS = 30;

public int mainCamNumber = 0; public GameObject b; public GameObject a; // Use this for initialization void Start() { WebCamDevice[] devices = WebCamTexture.devices; WebCamTexture webcamTexture = new WebCamTexture(devices[mainCamNumber].name, Width, Height, FPS); webcamTexture.deviceName = devices[mainCamNumber].name; a.GetComponent<Renderer>().material.mainTexture = webcamTexture; b.GetComponent<Renderer>().material.mainTexture = webcamTexture; webcamTexture.Play(); } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { { Debug.Log("Space"); R(); } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

WebCamTextureと別に加工後画像用のRenderTextureを作って、Blitで加工しつつコピーするというのはどうでしょう?

キャプチャー用スクリプトを下記のようにしてみました。

C#

1using UnityEngine; 2 3public class WebCamController : MonoBehaviour { 4 public int Width = 1920; 5 public int Height = 1080; 6 public int FPS = 30; 7 8 public int mainCamNumber = 0; 9 10 public GameObject b; 11 public GameObject a; 12 13 public Material filterMaterial; // 追加...加工用マテリアル 14 15 private WebCamTexture webcamTexture; // 追加...加工前テクスチャ 16 private RenderTexture filteredTexture; // 追加...加工後のテクスチャ 17 18 // Use this for initialization 19 void Start() 20 { 21 22 WebCamDevice[] devices = WebCamTexture.devices; 23 24 webcamTexture = new WebCamTexture(devices[mainCamNumber].name, Width, Height, FPS); // 変更...webcamTextureをインスタンス変数にする 25 webcamTexture.deviceName = devices[mainCamNumber].name; 26 27 filteredTexture = new RenderTexture(webcamTexture.width, webcamTexture.height, 0); // 追加...webcamTextureと同サイズのテクスチャを用意 28 29 a.GetComponent<Renderer>().material.mainTexture = filteredTexture; // 変更...webcamTextureの代わりにfilteredTextureをセット 30 b.GetComponent<Renderer>().material.mainTexture = filteredTexture; // 変更...webcamTextureの代わりにfilteredTextureをセット 31 32 webcamTexture.Play(); 33 } 34 35 void Update() 36 { 37 if (Input.GetKey(KeyCode.Space)) // 変更...変化を分かりやすくするため、GetKeyDownの代わりにGetKeyを使用 38 { 39 { 40 Debug.Log("Space"); 41 Graphics.Blit(webcamTexture, filteredTexture, filterMaterial); // 変更...キーが押されている場合はwebcamTextureをfilterMaterialを通してfilteredTextureにコピーする 42 } 43 } 44 else 45 { 46 Graphics.Blit(webcamTexture, filteredTexture); // 追加...キーが押されていない場合はwebcamTextureをそのままfilteredTextureにコピーする 47 } 48 } 49}

さらに下記のようなシェーダーを用意して、

Shader "Custom/TintShader" { Properties { _MainTex ("Texture", 2D) = "white" {} _TintColor ("Tint Color", Color) = (1.0, 0.0, 0.0, 1.0) } SubShader { // No culling or depth Cull Off ZWrite Off ZTest Always Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" uniform fixed4 _TintColor; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } sampler2D _MainTex; fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); col.rgb = dot(col.rgb, float3(0.3, 0.59, 0.11)) * _TintColor; // (0.3, 0.59, 0.11)との積和をとってグレースケール化した上で、_TintColorと掛けて着色 return col; } ENDCG } } }

これをセットしたマテリアルを作り、

マテリアル

キャプチャー用スクリプトに参照させました。

キャプチャースクリプト

画像の加工に関しては、マニュアルで紹介されている様々なテクニックが応用可能かと思います。
セピアトーンに関してですが、今回のようにグレースケール化して着色するだけでもいいかもしれませんが、こちらこちらを見た感じですと、ちゃんと個々の色成分を利用した方がきれいな仕上がりになりそうです。

[追記]
Legacy Image Effectsに収録されているSepiaToneEffect.shaderの場合は、モノトーンに対してセピア調になるよう色味を加減算しているようです。

Shader "Hidden/Sepiatone Effect" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert_img #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex; half4 _MainTex_ST; fixed4 frag (v2f_img i) : SV_Target { fixed4 original = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST)); // get intensity value (Y part of YIQ color space) fixed Y = dot (fixed3(0.299, 0.587, 0.114), original.rgb); // Convert to Sepia Tone by adding constant fixed4 sepiaConvert = float4 (0.191, -0.054, -0.221, 0.0); fixed4 output = sepiaConvert + Y; output.a = original.a; return output; } ENDCG } } Fallback off }

[さらに追記]
すみませんでした。WebCamTexture, get correct resolution and RATIO. - Unity Answersによると、WebCamTextureの正しい解像度は少し待たないと取得できないという罠がある様子でした。間違った解像度でfilteredTextureを作ってしまったためひどい結果になったようです。
キャプチャー用スクリプトに解像度がまともになるまでしばらく待機させるよう変更を加えてみましたが、こちらではいかがでしょうか?

C#

1using UnityEngine; 2 3public class WebCamController : MonoBehaviour { 4 public int Width = 1920; 5 public int Height = 1080; 6 public int FPS = 30; 7 8 public int mainCamNumber = 0; 9 10 public GameObject b; 11 public GameObject a; 12 13 public Material filterMaterial; // 加工用マテリアル 14 15 private WebCamTexture webcamTexture; // 加工前テクスチャ 16 private RenderTexture filteredTexture; // 加工後のテクスチャ 17 18 // Use this for initialization 19 void Start() 20 { 21 22 WebCamDevice[] devices = WebCamTexture.devices; 23 24 webcamTexture = new WebCamTexture(devices[mainCamNumber].name, Width, Height, FPS); // webcamTextureをインスタンス変数にする 25 webcamTexture.Play(); 26 } 27 28 void Update() 29 { 30 // 追加...テクスチャ解像度が適正に取得できるまで待機 31 if (filteredTexture == null) 32 { 33 if (webcamTexture.width < 100) 34 { 35 Debug.Log("Please wait..."); 36 return; 37 } 38 else 39 { 40 Debug.LogFormat("WebCamTexture resolution: ({0}, {1})", webcamTexture.width, webcamTexture.height); 41 42 filteredTexture = new RenderTexture(webcamTexture.width, webcamTexture.height, 0); // webcamTextureと同サイズのテクスチャを用意 43 44 a.GetComponent<Renderer>().material.mainTexture = filteredTexture; // webcamTextureの代わりにfilteredTextureをセット 45 b.GetComponent<Renderer>().material.mainTexture = filteredTexture; // webcamTextureの代わりにfilteredTextureをセット 46 } 47 } 48 49 if (Input.GetKey(KeyCode.Space)) // 変化を分かりやすくするため、GetKeyDownの代わりにGetKeyを使用 50 { 51 { 52 Debug.Log("Space"); 53 Graphics.Blit(webcamTexture, filteredTexture, filterMaterial); // キーが押されている場合はwebcamTextureをfilterMaterialを通してfilteredTextureにコピーする 54 } 55 } 56 else 57 { 58 Graphics.Blit(webcamTexture, filteredTexture); // キーが押されていない場合はwebcamTextureをそのままfilteredTextureにコピーする 59 } 60 } 61}

投稿2017/09/16 23:37

編集2017/09/18 14:25
Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tig526

2017/09/17 10:38

リンクや画像で丁寧な説明ありがとうございます さらに技術力の向上を目指したいと思います。
tig526

2017/09/18 13:36

重ねて質問もうしわけありません。 webカメラの解像度が著しく低下するのですが 改善方法はありますか。
Bongo

2017/09/18 14:25

すみません、解像度が正しく取れていなかったようです。修正案を追記してみましたがいかがでしょう?
tig526

2017/09/18 14:58

ありがとうございます。 正常に動作しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問