行いたいこと
FieldのTEXTUREである,「Field_texture」に白い四角を描画したい.
###エラー部分
以下のコードにある
field_maintex = field.GetComponent<Renderer>().material.mainTexture;
の部分において,
ArgumentNullException: Value cannot be null.Parameter name: texture2D == null
のエラーが出る.
###行った解決策
field.GetComponent<Renderer>().materialをすると,Field_textureを取得できたが,MaterialなのでTexture2Dに変更できなかった.
ソースコードの説明
- フィールドのテクスチャを取得 ←この部分において,NULLがでる.
- Texture型のテクスチャをTexture2D型にキャストしてMat型に変更
- キーボード「T」を押すと,テクスチャの真ん中に200*200の四角形を描く
- Mat型をtexture2D型に変更
- フィールドのテクスチャに四角を描いたテクスチャを適用
###ソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using OpenCVForUnity.CoreModule; 5using OpenCVForUnity.UnityUtils; 6using OpenCVForUnity.ImgprocModule; 7 8using static OpenCVForUnity.CoreModule.Core; 9using static OpenCVForUnity.CoreModule.CvType; 10 11public class test3 : MonoBehaviour 12{ 13 Calculation calculation; 14 Mats vertices; 15 public GameObject field; 16 static float field_horizon; 17 static float field_vertice; 18 public Texture field_maintex; 19 20 void Start() 21 { 22 23 //fieldを呼び出し 24 field = GameObject.FindGameObjectWithTag("Field"); 25 //fieldサイズ取得 26 field_horizon = field.transform.localScale.x; 27 field_vertice = field.transform.localScale.z; 28 29 //fieldのmaintextureを呼び出し 30 field_maintex = field.GetComponent<Renderer>().material.mainTexture; 31 } 32 void Update() 33 { 34 35 if (Input.GetKey(KeyCode.T)) 36 { 37 //textureをmat型にする 38 Mat mat_tex = new Mat((int)field_horizon, (int)field_vertice, CV_8UC3); 39 Utils.texture2DToMat((Texture2D)field_maintex, mat_tex); 40 41 //mat_texに四角を描く 42 Point[] p_origin = new Point[4]; 43 p_origin[0] = new Point(field_horizon / 2 + 100, field_vertice / 2 + 100); 44 p_origin[1] = new Point(field_horizon / 2 - 100, field_vertice / 2 + 100); 45 p_origin[2] = new Point(field_horizon / 2 - 100, field_vertice / 2 - 100); 46 p_origin[3] = new Point(field_horizon / 2 + 100, field_vertice / 2 - 100); 47 Imgproc.fillConvexPoly(mat_tex, new MatOfPoint(p_origin), new Scalar(255, 255, 255)); 48 49 50 //mat_texをtextureにする. 51 Texture2D texture = new Texture2D(mat_tex.cols(), mat_tex.rows(), TextureFormat.ARGB32, false); 52 Utils.matToTexture2D(mat_tex, texture); 53 54 //maintextureを更新 55 field_maintex = texture; 56 } 57 } 58}
画像
追記
「Field_texture」マテリアルは緑単色のように見えますが、アルベドテクスチャはセットされているでしょうか?
Field_textureのインスペクタのスクリーンショットもあると手がかりになりそうです。
追記しました.
Maintextureを取得すれば,マテリアルのテクスチャが入手できると考えていたのですが,違うのでしょうか?
アルベドテクスチャはちゃんとありますね...
念のため確認したいのですが、「ArgumentNullException: Value cannot be null.Parameter name: texture2D == null」のメッセージが表示されるのはご質問文にあるとおり「field_maintex = field.GetComponent<Renderer>().material.mainTexture;」の行、つまりプレイモードにしただけでTキーを押さなくても発生するのでしょうか?
すみません.
Utils.texture2DToMat((Texture2D)field_maintex, mat_tex);
の部分でした.
たびたびすみません、動作の流れについてうかがいますが
・シーン編集中の段階ではtest3(実際のスクリプト名はDraw_Fieldでしょうか)の「Field」と「Field_maintex」はどちらもNoneにしておく
・プレイモードにするとStartが実行され、「Field」はご提示いただいた最初の画像のようにFieldオブジェクトが見つかってセットされるが、「Field_maintex」はNoneのままである
・そのあとTキーを押すと、field_maintexがないためArgumentNullExceptionが発生する
という状態でしょうか。
もう一点念のため確認しておきたい点として、シーンのヒエラルキー上に存在するオブジェクトの中で「Field」タグを持っているのはこの「Field」オブジェクトだけでしょうか。
あなたの回答
tips
プレビュー