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

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

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

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

Q&A

解決済

1回答

6154閲覧

Texture2DとrawImageの関係について

Student_

総合スコア11

C#

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

0グッド

0クリップ

投稿2018/11/21 11:57

前提・実現したいこと

現在,WebCameraから取得したImageを,Texture2D型に変換し,保持しています.
そのTexture2D型の画像をGameObjectのPlaneに
meshRenderer.material.SetTexture("_MainTex", texture2D);
したものと,
Canvasの中のrawImageに
rawImage.texture = texture2D;
したものの2つの方法で描画しました.
すると,同じTexture2D型の画像であるにも関わらず,色調が違うものが出力されてしまいます.

具体的に言うと,Planeに貼り付けた方は正常で,rawImageの方が黄色がかったような感じです.

同じ色で出力させたいのですが,解決策を知っている人はいらっしゃるでしょうか.

よろしくお願いします.

発生している問題・エラーメッセージ

エラーはありません.

該当のソースコード

C#

1meshRenderer.material.SetTexture("_MainTex", texture2D); 2(GameObjectのPlaneにアタッチされたスクリプト) 34rawImage.texture = texture2D;

補足情報(FW/ツールのバージョンなど)

Unity 2018.1.0f

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

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

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

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

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

Bongo

2018/11/21 22:06

RawImageのMaterialやColorは変更していないのですよね?でしたら「Texture2D型に変換し」の部分のコードもご提示いただけますでしょうか。Planeでうまくいっているのでしたら、そこに問題はないかもしれませんが念のため...
Student_

2018/11/22 07:33

回答ありがとうございます.MaterialやColorは初期のままです.Texture2Dへ変換している部分のソースコードを貼り付けます.改行の仕方が分からないので読みにくくなってしまいすみません... if (colors != null) { webcamTexture.GetPixels32 (colors); int width = webcamTexture.width; int height = webcamTexture.height; Color32 rc = new Color32(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color32 c = colors [x + y * width]; byte gray = (byte)(0.1f * c.r + 0.7f * c.g + 0.2f * c.b); rc.r = rc.g = rc.b = gray; colors [x + y * width] = rc; } } texture.SetPixels32 (colors); texture.Apply (); }
guest

回答1

0

ベストアンサー

すみません、まだ原因特定ができていない段階ですが、追記欄だとコードが見づらくなってしまうため回答欄を使わせていただきました。ご容赦ください。

「MaterialやColorは初期のまま」とのことですが、Planeの方もRawImageの方もデフォルトのまま...Planeのマテリアルは「Default-Material」、RawImageのマテリアルは「None (Material)」でColorは不透明な白ということでしょうか?

下記のようなコード(【Unity】Webカメラの画像を加工して表示する - おもちゃラボWebCamControllerをベースにしました)のUpdate内に、ご提示いただいたTexture2D変換部分のコードを使用してみましたが、そのままの状態ではPlaneには映像が映りましたが、RawImageは完全透明になってしまいました。
rcの初期化部分をnew Color32()からnew Color32(0, 0, 0, byte.MaxValue)に変えて完全不透明にすることで正しくグレースケール化された映像が映ったのですが、ご質問者さんの場合はnew Color32()でも表示される、ただし映像は黄色っぽくなってしまう...という状況でしょうか。

C#

1using System.Collections; 2using UnityEngine; 3using UnityEngine.UI; 4 5namespace WebCamToTex2D 6{ 7 public class WebCamController : MonoBehaviour 8 { 9 public MeshRenderer DestinationMeshRenderer; 10 public RawImage DestinationRawImage; 11 private Color32[] colors; 12 private Texture2D texture; 13 private WebCamTexture webcamTexture; 14 15 private IEnumerator Start() 16 { 17 var devices = WebCamTexture.devices; 18 if ((this.DestinationMeshRenderer == null) || (this.DestinationRawImage == null) || (devices == null) || 19 (devices.Length <= 0)) 20 { 21 this.enabled = false; 22 yield break; 23 } 24 25 this.webcamTexture = new WebCamTexture(devices[0].name, 160, 120, 30); 26 this.webcamTexture.Play(); 27 while (true) 28 { 29 if ((this.webcamTexture.width > 16) && (this.webcamTexture.height > 16)) 30 { 31 var w = this.webcamTexture.width; 32 var h = this.webcamTexture.height; 33 Debug.LogFormat("WebCamTexture size:({0}, {1})", w, h); 34 this.colors = new Color32[w * h]; 35 this.texture = new Texture2D(w, h, TextureFormat.RGBA32, false); 36 this.texture.name = "GrayTexture"; 37 this.DestinationMeshRenderer.material.mainTexture = this.texture; 38 this.DestinationRawImage.texture = this.texture; 39 break; 40 } 41 42 yield return null; 43 } 44 } 45 46 private void Update() 47 { 48 if (this.colors != null) 49 { 50 this.webcamTexture.GetPixels32(this.colors); 51 var width = this.webcamTexture.width; 52 var height = this.webcamTexture.height; 53 var rc = new Color32(0, 0, 0, byte.MaxValue); 54 for (var x = 0; x < width; x++) 55 { 56 for (var y = 0; y < height; y++) 57 { 58 var c = this.colors[x + (y * width)]; 59 var gray = (byte)((0.1f * c.r) + (0.7f * c.g) + (0.2f * c.b)); 60 rc.r = rc.g = rc.b = gray; 61 this.colors[x + (y * width)] = rc; 62 } 63 } 64 65 this.texture.SetPixels32(this.colors); 66 this.texture.Apply(); 67 } 68 } 69 } 70}

投稿2018/11/22 23:36

Bongo

総合スコア10807

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問