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

回答編集履歴

3

ピクセル拡大行列の拡大量を修正

2019/10/17 13:38

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -292,8 +292,8 @@
292
292
  if (Input.GetMouseButtonDown(0))
293
293
  {
294
294
  // マウスポインタの位置を狙う変換行列を作り...
295
- var screenScale = new Vector2(Screen.width, Screen.height) * 0.5f;
295
+ var screenScale = new Vector2(Screen.width, Screen.height);
296
- var pixelPosition = (Vector2)Input.mousePosition - screenScale;
296
+ var pixelPosition = ((Vector2)Input.mousePosition * 2.0f) - screenScale;
297
297
  var pixelMatrix = Matrix4x4.TRS(
298
298
  -pixelPosition,
299
299
  Quaternion.identity,

2

代替シェーダー案について追記

2019/10/17 13:38

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -10,60 +10,60 @@
10
10
 
11
11
  public class GetColor : MonoBehaviour
12
12
  {
13
- public Color32 color;
13
+ public Color32 color;
14
- private GetColorHelper helper;
14
+ private GetColorHelper helper;
15
15
 
16
- void Start()
16
+ void Start()
17
- {
17
+ {
18
- // GetColorスクリプト自体をカメラにアタッチしてもいいなら、GetColorにUpdateと
18
+ // GetColorスクリプト自体をカメラにアタッチしてもいいなら、GetColorにUpdateと
19
- // OnPostRenderを両方実装して連携させることができるかと思いますが、GetColorは
19
+ // OnPostRenderを両方実装して連携させることができるかと思いますが、GetColorは
20
- // 別のオブジェクトにアタッチしたい...といった事情がある場合は、GetColor本体と
20
+ // 別のオブジェクトにアタッチしたい...といった事情がある場合は、GetColor本体と
21
- // カメラ用コンポーネントの2つに担当を分けて連携させることになるかと思います
21
+ // カメラ用コンポーネントの2つに担当を分けて連携させることになるかと思います
22
- // 今回の例では補助コンポーネントとしてGetColorHelperを用意し、これをメインカメラに
22
+ // 今回の例では補助コンポーネントとしてGetColorHelperを用意し、これをメインカメラに
23
- // アタッチしてOnPostRender部分を担当させることにしました
23
+ // アタッチしてOnPostRender部分を担当させることにしました
24
- helper = Camera.main.GetComponent<GetColorHelper>();
24
+ helper = Camera.main.GetComponent<GetColorHelper>();
25
- if (helper == null)
25
+ if (helper == null)
26
- {
26
+ {
27
- helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
27
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
28
- }
28
+ }
29
- }
29
+ }
30
30
 
31
- void Update()
31
+ void Update()
32
- {
32
+ {
33
- if (Input.GetMouseButtonDown(0))
33
+ if (Input.GetMouseButtonDown(0))
34
- {
34
+ {
35
- color = helper.Tex.GetPixel(0, 0);
35
+ color = helper.Tex.GetPixel(0, 0);
36
- Debug.Log(color.ToString());
36
+ Debug.Log(color.ToString());
37
- }
37
+ }
38
- }
38
+ }
39
39
 
40
- [RequireComponent(typeof(Camera))]
40
+ [RequireComponent(typeof(Camera))]
41
- private class GetColorHelper : MonoBehaviour
41
+ private class GetColorHelper : MonoBehaviour
42
- {
42
+ {
43
- public Texture2D Tex { get; private set; }
43
+ public Texture2D Tex { get; private set; }
44
44
 
45
- void Awake()
45
+ void Awake()
46
- {
46
+ {
47
- Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
47
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
48
- }
48
+ }
49
49
 
50
- void OnPostRender()
50
+ void OnPostRender()
51
- {
51
+ {
52
- Vector2 pos = Input.mousePosition;
52
+ Vector2 pos = Input.mousePosition;
53
-
53
+
54
- // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
54
+ // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
55
- if (SystemInfo.graphicsUVStartsAtTop)
55
+ if (SystemInfo.graphicsUVStartsAtTop)
56
- {
56
+ {
57
- pos.y = Screen.height - 1 - pos.y;
57
+ pos.y = Screen.height - 1 - pos.y;
58
- }
58
+ }
59
59
 
60
- // マウスポインタが画面外にあるとReadPixelsに失敗するため、座標を範囲内におさめる
60
+ // マウスポインタが画面外にあるとReadPixelsに失敗するため、座標を範囲内におさめる
61
- float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
61
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
62
- float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
62
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
63
-
63
+
64
- Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
64
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
65
- }
65
+ }
66
- }
66
+ }
67
67
  }
68
68
  ```
69
69
 
@@ -79,57 +79,57 @@
79
79
 
80
80
  namespace GetPixelColor
81
81
  {
82
- public class GetColor : MonoBehaviour
82
+ public class GetColor : MonoBehaviour
83
- {
83
+ {
84
- public Color32 color;
84
+ public Color32 color;
85
- private GetColorHelper helper;
85
+ private GetColorHelper helper;
86
86
 
87
- void Start()
87
+ void Start()
88
- {
88
+ {
89
- helper = Camera.main.GetComponent<GetColorHelper>();
89
+ helper = Camera.main.GetComponent<GetColorHelper>();
90
- if (helper == null)
90
+ if (helper == null)
91
- {
91
+ {
92
- helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
92
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
93
- }
93
+ }
94
- }
94
+ }
95
95
 
96
- void Update()
96
+ void Update()
97
- {
97
+ {
98
- if (Input.GetMouseButtonDown(0))
98
+ if (Input.GetMouseButtonDown(0))
99
- {
99
+ {
100
- color = helper.Tex.GetPixel(0, 0);
100
+ color = helper.Tex.GetPixel(0, 0);
101
- Debug.Log(color.ToString());
101
+ Debug.Log(color.ToString());
102
- }
102
+ }
103
- }
103
+ }
104
104
 
105
- [RequireComponent(typeof(Camera))]
105
+ [RequireComponent(typeof(Camera))]
106
- private class GetColorHelper : MonoBehaviour
106
+ private class GetColorHelper : MonoBehaviour
107
- {
107
+ {
108
- public Texture2D Tex { get; private set; }
108
+ public Texture2D Tex { get; private set; }
109
109
 
110
- void Awake()
110
+ void Awake()
111
- {
111
+ {
112
- Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
112
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
113
- }
113
+ }
114
114
 
115
- void OnPostRender()
115
+ void OnPostRender()
116
- {
116
+ {
117
- Vector2 pos = Input.mousePosition;
117
+ Vector2 pos = Input.mousePosition;
118
-
118
+
119
- // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
119
+ // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
120
- // Metalの場合、例外的に反転は行わないようにした
120
+ // Metalの場合、例外的に反転は行わないようにした
121
- if (SystemInfo.graphicsUVStartsAtTop && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Metal)
121
+ if (SystemInfo.graphicsUVStartsAtTop && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Metal)
122
- {
122
+ {
123
- pos.y = Screen.height - 1 - pos.y;
123
+ pos.y = Screen.height - 1 - pos.y;
124
- }
124
+ }
125
125
 
126
- float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
126
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
127
- float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
127
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
128
-
128
+
129
- Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
129
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
130
- }
130
+ }
131
- }
131
+ }
132
- }
132
+ }
133
133
  }
134
134
  ```
135
135
 
@@ -143,56 +143,190 @@
143
143
 
144
144
  namespace GetPixelColor
145
145
  {
146
- public class GetColor : MonoBehaviour
146
+ public class GetColor : MonoBehaviour
147
- {
147
+ {
148
- public Color32 color;
148
+ public Color32 color;
149
- private GetColorHelper helper;
149
+ private GetColorHelper helper;
150
150
 
151
- void Start()
151
+ void Start()
152
- {
152
+ {
153
- helper = Camera.main.GetComponent<GetColorHelper>();
153
+ helper = Camera.main.GetComponent<GetColorHelper>();
154
- if (helper == null)
154
+ if (helper == null)
155
- {
155
+ {
156
- helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
156
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
157
- }
157
+ }
158
- }
158
+ }
159
159
 
160
- void Update()
160
+ void Update()
161
- {
161
+ {
162
- if (Input.GetMouseButtonDown(0))
162
+ if (Input.GetMouseButtonDown(0))
163
- {
163
+ {
164
- color = helper.Tex.GetPixel(0, 0);
164
+ color = helper.Tex.GetPixel(0, 0);
165
- Debug.Log(color.ToString());
165
+ Debug.Log(color.ToString());
166
- }
166
+ }
167
- }
167
+ }
168
168
 
169
- [RequireComponent(typeof(Camera))]
169
+ [RequireComponent(typeof(Camera))]
170
- private class GetColorHelper : MonoBehaviour
170
+ private class GetColorHelper : MonoBehaviour
171
- {
171
+ {
172
- public Texture2D Tex { get; private set; }
172
+ public Texture2D Tex { get; private set; }
173
173
 
174
- void Awake()
174
+ void Awake()
175
- {
175
+ {
176
- Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
176
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
177
- }
177
+ }
178
178
 
179
- IEnumerator Start()
179
+ IEnumerator Start()
180
- {
180
+ {
181
- var waitForEndOfFrame = new WaitForEndOfFrame();
181
+ var waitForEndOfFrame = new WaitForEndOfFrame();
182
182
 
183
- while (true)
183
+ while (true)
184
- {
184
+ {
185
- yield return waitForEndOfFrame;
185
+ yield return waitForEndOfFrame;
186
186
 
187
- Vector2 pos = Input.mousePosition;
187
+ Vector2 pos = Input.mousePosition;
188
188
 
189
- float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
189
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
190
- float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
190
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
191
191
 
192
- Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
192
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
193
- }
193
+ }
194
- }
194
+ }
195
- }
195
+ }
196
- }
196
+ }
197
197
  }
198
- ```
198
+ ```
199
+
200
+ **追記**
201
+ まず、下記のようなメインテクスチャ・メインカラーで塗るだけのシンプルなUnlitシェーダーを用意しました。
202
+
203
+ ```ShaderLab
204
+ Shader "Unlit/UnlitAlbedo"
205
+ {
206
+ Properties
207
+ {
208
+ _MainTex ("Texture", 2D) = "white" {}
209
+ _Color ("Color", Color) = (1, 1, 1, 1)
210
+ }
211
+ SubShader
212
+ {
213
+ Tags { "RenderType"="Opaque" }
214
+ LOD 100
215
+
216
+ Pass
217
+ {
218
+ CGPROGRAM
219
+ #pragma vertex vert
220
+ #pragma fragment frag
221
+
222
+ #include "UnityCG.cginc"
223
+
224
+ struct appdata
225
+ {
226
+ float4 vertex : POSITION;
227
+ float2 uv : TEXCOORD0;
228
+ };
229
+
230
+ struct v2f
231
+ {
232
+ float2 uv : TEXCOORD0;
233
+ float4 vertex : SV_POSITION;
234
+ };
235
+
236
+ sampler2D _MainTex;
237
+ float4 _MainTex_ST;
238
+ fixed4 _Color;
239
+
240
+ v2f vert(appdata v)
241
+ {
242
+ v2f o;
243
+ o.vertex = UnityObjectToClipPos(v.vertex);
244
+ o.uv = TRANSFORM_TEX(v.uv, _MainTex);
245
+ return o;
246
+ }
247
+
248
+ fixed4 frag(v2f i) : SV_Target
249
+ {
250
+ return tex2D(_MainTex, i.uv) * _Color;
251
+ }
252
+ ENDCG
253
+ }
254
+ }
255
+ }
256
+ ```
257
+
258
+ 通常のレンダリングの場合下図のように見えるシーンを...
259
+
260
+ ![図1](57bdbdee30fc709c88702c89a1348f1b.png)
261
+
262
+ この代替シェーダーでシーンをレンダリングした場合、下図のように環境の映り込みや陰影がない単純な色塗り状態の映像が得られます。実際にはこの映像を画面上に表示するわけではなく、RenderTexture上に描画して色取り元とすることになりますね。
263
+
264
+ ![図2](fbf316d31b6a93341aad43e2abc77bcb.png)
265
+
266
+ また、この追加レンダリングはわざわざ画面全体について行う必要はなく、色を取りたい地点の1ピクセル分だけの大きさがあれば十分なはずです。そこで、このレンダリングの時だけ一時的に投影行列に手を加えて注目点だけを拡大し、それを1×1サイズのRenderTextureに描画することにしました。描画面積が大幅に節約でき、レンダリングコストを低減できると思われます。
267
+
268
+ 色取りスクリプトは下記のようにしてみました。補助スクリプトは不要になったため廃止しています。
269
+
270
+ ```C#
271
+ using System.Collections;
272
+ using UnityEngine;
273
+
274
+ public class AlbedoColorPicker : MonoBehaviour
275
+ {
276
+ public Color32 color;
277
+ public Renderer targetRenderer;
278
+
279
+ private Camera mainCamera;
280
+ private Shader alternativeShader;
281
+ private Texture2D pixelTexture;
282
+
283
+ private void Start()
284
+ {
285
+ this.mainCamera = Camera.main;
286
+ this.alternativeShader = Shader.Find("Unlit/UnlitAlbedo");
287
+ this.pixelTexture = new Texture2D(1, 1, TextureFormat.RGBA32, false);
288
+ }
289
+
290
+ private void Update()
291
+ {
292
+ if (Input.GetMouseButtonDown(0))
293
+ {
294
+ // マウスポインタの位置を狙う変換行列を作り...
295
+ var screenScale = new Vector2(Screen.width, Screen.height) * 0.5f;
296
+ var pixelPosition = (Vector2)Input.mousePosition - screenScale;
297
+ var pixelMatrix = Matrix4x4.TRS(
298
+ -pixelPosition,
299
+ Quaternion.identity,
300
+ new Vector3(screenScale.x, screenScale.y, 1.0f));
301
+
302
+ // projectionMatrixの後段に追加の変換を加え、alternativeShaderを使ってレンダリングする
303
+ var targetTexture = this.mainCamera.targetTexture;
304
+ var projectionMatrix = this.mainCamera.projectionMatrix;
305
+ var renderTexture = RenderTexture.GetTemporary(1, 1);
306
+ this.mainCamera.targetTexture = renderTexture;
307
+ this.mainCamera.projectionMatrix = pixelMatrix * projectionMatrix;
308
+ this.mainCamera.RenderWithShader(this.alternativeShader, null);
309
+ this.mainCamera.targetTexture = targetTexture;
310
+ this.mainCamera.ResetProjectionMatrix();
311
+
312
+ // renderTextureの内容をpixelTextureに読み取り、さらにpixelTextureから色を取り出す
313
+ var activeTexture = RenderTexture.active;
314
+ RenderTexture.active = renderTexture;
315
+ this.pixelTexture.ReadPixels(new Rect(0, 0, 1, 1), 0, 0);
316
+ RenderTexture.active = activeTexture;
317
+ RenderTexture.ReleaseTemporary(renderTexture);
318
+ this.color = this.pixelTexture.GetPixel(0, 0);
319
+
320
+ // 色設定対象がセットされていれば、それに取得された色をセットする
321
+ if (this.targetRenderer != null)
322
+ {
323
+ this.targetRenderer.material.color = this.color;
324
+ }
325
+ }
326
+ }
327
+ }
328
+ ```
329
+
330
+ 動かしてみると下図のようになりました。取った色に陰影付けの影響が及んでいないことをアピールするため、色の適用先はPlaneではなく画面右側にあるSphereにしています。
331
+
332
+ ![図3](62a27dba4a46a6c8e744c1e765c545fc.gif)

1

修正案を追記

2019/09/03 14:57

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -65,4 +65,134 @@
65
65
  }
66
66
  }
67
67
  }
68
+ ```
69
+
70
+
71
+ **追記**
72
+ ご指摘ありがとうございます。当初のコードはWindows版でしか試していなかったのですが、確かにMac版でMetalを使用している場合は座標の上下が逆転してしまいますね...
73
+ その場しのぎ的ですみませんが、下記のようにMetalの場合に座標の上下反転を行わせないようにしてはどうでしょうか。
74
+ もし他にうまくいかない条件が見つかりましたらコメントください(とはいえMacとWindows以外にすぐ試せる環境を持っておらず、お力になれないかもしれませんが...)。
75
+
76
+ ```C#
77
+ using UnityEngine;
78
+ using UnityEngine.Rendering;
79
+
80
+ namespace GetPixelColor
81
+ {
82
+ public class GetColor : MonoBehaviour
83
+ {
84
+ public Color32 color;
85
+ private GetColorHelper helper;
86
+
87
+ void Start()
88
+ {
89
+ helper = Camera.main.GetComponent<GetColorHelper>();
90
+ if (helper == null)
91
+ {
92
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
93
+ }
94
+ }
95
+
96
+ void Update()
97
+ {
98
+ if (Input.GetMouseButtonDown(0))
99
+ {
100
+ color = helper.Tex.GetPixel(0, 0);
101
+ Debug.Log(color.ToString());
102
+ }
103
+ }
104
+
105
+ [RequireComponent(typeof(Camera))]
106
+ private class GetColorHelper : MonoBehaviour
107
+ {
108
+ public Texture2D Tex { get; private set; }
109
+
110
+ void Awake()
111
+ {
112
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
113
+ }
114
+
115
+ void OnPostRender()
116
+ {
117
+ Vector2 pos = Input.mousePosition;
118
+
119
+ // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
120
+ // Metalの場合、例外的に反転は行わないようにした
121
+ if (SystemInfo.graphicsUVStartsAtTop && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Metal)
122
+ {
123
+ pos.y = Screen.height - 1 - pos.y;
124
+ }
125
+
126
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
127
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
128
+
129
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
130
+ }
131
+ }
132
+ }
133
+ }
134
+ ```
135
+
136
+ また、Planeに貼り付けた色が取れないという現象は私の試した限りでは再現させられなかったのですが(Planeであっても他の3Dオブジェクトと違いはないはずなので、Cubeで成功したのならPlaneでもいける気がするのですが...)、ImageなどUIオブジェクトの色を取れないという現象は確認できました。
137
+ おそらくCanvasの「Render Mode」が「Screen Space - Overlay」になっているんじゃないでしょうか?どうやらこのモードの場合、UIの描画は`OnPostRender`よりも後のタイミングになるため失敗したものと思われます。もしRender Modeを変えてもかまわないのなら、これを「Screen Space - Camera」や「World Space」 にすれば`OnPostRender`より先にUI描画が完了し、色を取ってくることができるでしょう。
138
+ あるいは、下記のように`WaitForEndOfFrame`のタイミングならScreen Space - Overlayでもいけそうな感じでした。どうしてもScreen Space - Overlayでないと困るようでしたらこちらも試してみてもいいかと思います。
139
+
140
+ ```C#
141
+ using System.Collections;
142
+ using UnityEngine;
143
+
144
+ namespace GetPixelColor
145
+ {
146
+ public class GetColor : MonoBehaviour
147
+ {
148
+ public Color32 color;
149
+ private GetColorHelper helper;
150
+
151
+ void Start()
152
+ {
153
+ helper = Camera.main.GetComponent<GetColorHelper>();
154
+ if (helper == null)
155
+ {
156
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
157
+ }
158
+ }
159
+
160
+ void Update()
161
+ {
162
+ if (Input.GetMouseButtonDown(0))
163
+ {
164
+ color = helper.Tex.GetPixel(0, 0);
165
+ Debug.Log(color.ToString());
166
+ }
167
+ }
168
+
169
+ [RequireComponent(typeof(Camera))]
170
+ private class GetColorHelper : MonoBehaviour
171
+ {
172
+ public Texture2D Tex { get; private set; }
173
+
174
+ void Awake()
175
+ {
176
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
177
+ }
178
+
179
+ IEnumerator Start()
180
+ {
181
+ var waitForEndOfFrame = new WaitForEndOfFrame();
182
+
183
+ while (true)
184
+ {
185
+ yield return waitForEndOfFrame;
186
+
187
+ Vector2 pos = Input.mousePosition;
188
+
189
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
190
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
191
+
192
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
193
+ }
194
+ }
195
+ }
196
+ }
197
+ }
68
198
  ```