回答編集履歴

3

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

2019/10/17 13:38

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -586,9 +586,9 @@
586
586
 
587
587
  // マウスポインタの位置を狙う変換行列を作り...
588
588
 
589
- var screenScale = new Vector2(Screen.width, Screen.height) * 0.5f;
589
+ var screenScale = new Vector2(Screen.width, Screen.height);
590
-
590
+
591
- var pixelPosition = (Vector2)Input.mousePosition - screenScale;
591
+ var pixelPosition = ((Vector2)Input.mousePosition * 2.0f) - screenScale;
592
592
 
593
593
  var pixelMatrix = Matrix4x4.TRS(
594
594
 

2

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

2019/10/17 13:38

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -22,113 +22,113 @@
22
22
 
23
23
  {
24
24
 
25
- public Color32 color;
26
-
27
- private GetColorHelper helper;
28
-
29
-
30
-
31
- void Start()
32
-
33
- {
34
-
35
- // GetColorスクリプト自体をカメラにアタッチしてもいいなら、GetColorにUpdateと
36
-
37
- // OnPostRenderを両方実装して連携させることができるかと思いますが、GetColorは
38
-
39
- // 別のオブジェクトにアタッチしたい...といった事情がある場合は、GetColor本体と
40
-
41
- // カメラ用コンポーネントの2つに担当を分けて連携させることになるかと思います
42
-
43
- // 今回の例では補助コンポーネントとしてGetColorHelperを用意し、これをメインカメラに
44
-
45
- // アタッチしてOnPostRender部分を担当させることにしました
46
-
47
- helper = Camera.main.GetComponent<GetColorHelper>();
48
-
49
- if (helper == null)
50
-
51
- {
52
-
53
- helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
54
-
55
- }
56
-
57
- }
58
-
59
-
60
-
61
- void Update()
62
-
63
- {
64
-
65
- if (Input.GetMouseButtonDown(0))
66
-
67
- {
68
-
69
- color = helper.Tex.GetPixel(0, 0);
70
-
71
- Debug.Log(color.ToString());
72
-
73
- }
74
-
75
- }
76
-
77
-
78
-
79
- [RequireComponent(typeof(Camera))]
80
-
81
- private class GetColorHelper : MonoBehaviour
82
-
83
- {
84
-
85
- public Texture2D Tex { get; private set; }
86
-
87
-
88
-
89
- void Awake()
90
-
91
- {
92
-
93
- Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
94
-
95
- }
96
-
97
-
98
-
99
- void OnPostRender()
100
-
101
- {
102
-
103
- Vector2 pos = Input.mousePosition;
104
-
105
-
106
-
107
- // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
108
-
109
- if (SystemInfo.graphicsUVStartsAtTop)
110
-
111
- {
112
-
113
- pos.y = Screen.height - 1 - pos.y;
114
-
115
- }
116
-
117
-
118
-
119
- // マウスポインタが画面外にあるとReadPixelsに失敗するため、座標を範囲内におさめる
120
-
121
- float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
122
-
123
- float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
124
-
125
-
126
-
127
- Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
128
-
129
- }
130
-
131
- }
25
+ public Color32 color;
26
+
27
+ private GetColorHelper helper;
28
+
29
+
30
+
31
+ void Start()
32
+
33
+ {
34
+
35
+ // GetColorスクリプト自体をカメラにアタッチしてもいいなら、GetColorにUpdateと
36
+
37
+ // OnPostRenderを両方実装して連携させることができるかと思いますが、GetColorは
38
+
39
+ // 別のオブジェクトにアタッチしたい...といった事情がある場合は、GetColor本体と
40
+
41
+ // カメラ用コンポーネントの2つに担当を分けて連携させることになるかと思います
42
+
43
+ // 今回の例では補助コンポーネントとしてGetColorHelperを用意し、これをメインカメラに
44
+
45
+ // アタッチしてOnPostRender部分を担当させることにしました
46
+
47
+ helper = Camera.main.GetComponent<GetColorHelper>();
48
+
49
+ if (helper == null)
50
+
51
+ {
52
+
53
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
54
+
55
+ }
56
+
57
+ }
58
+
59
+
60
+
61
+ void Update()
62
+
63
+ {
64
+
65
+ if (Input.GetMouseButtonDown(0))
66
+
67
+ {
68
+
69
+ color = helper.Tex.GetPixel(0, 0);
70
+
71
+ Debug.Log(color.ToString());
72
+
73
+ }
74
+
75
+ }
76
+
77
+
78
+
79
+ [RequireComponent(typeof(Camera))]
80
+
81
+ private class GetColorHelper : MonoBehaviour
82
+
83
+ {
84
+
85
+ public Texture2D Tex { get; private set; }
86
+
87
+
88
+
89
+ void Awake()
90
+
91
+ {
92
+
93
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
94
+
95
+ }
96
+
97
+
98
+
99
+ void OnPostRender()
100
+
101
+ {
102
+
103
+ Vector2 pos = Input.mousePosition;
104
+
105
+
106
+
107
+ // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
108
+
109
+ if (SystemInfo.graphicsUVStartsAtTop)
110
+
111
+ {
112
+
113
+ pos.y = Screen.height - 1 - pos.y;
114
+
115
+ }
116
+
117
+
118
+
119
+ // マウスポインタが画面外にあるとReadPixelsに失敗するため、座標を範囲内におさめる
120
+
121
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
122
+
123
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
124
+
125
+
126
+
127
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
128
+
129
+ }
130
+
131
+ }
132
132
 
133
133
  }
134
134
 
@@ -160,107 +160,107 @@
160
160
 
161
161
  {
162
162
 
163
- public class GetColor : MonoBehaviour
164
-
165
- {
166
-
167
- public Color32 color;
168
-
169
- private GetColorHelper helper;
170
-
171
-
172
-
173
- void Start()
174
-
175
- {
176
-
177
- helper = Camera.main.GetComponent<GetColorHelper>();
178
-
179
- if (helper == null)
180
-
181
- {
182
-
183
- helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
184
-
185
- }
186
-
187
- }
188
-
189
-
190
-
191
- void Update()
192
-
193
- {
194
-
195
- if (Input.GetMouseButtonDown(0))
196
-
197
- {
198
-
199
- color = helper.Tex.GetPixel(0, 0);
200
-
201
- Debug.Log(color.ToString());
202
-
203
- }
204
-
205
- }
206
-
207
-
208
-
209
- [RequireComponent(typeof(Camera))]
210
-
211
- private class GetColorHelper : MonoBehaviour
212
-
213
- {
214
-
215
- public Texture2D Tex { get; private set; }
216
-
217
-
218
-
219
- void Awake()
220
-
221
- {
222
-
223
- Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
224
-
225
- }
226
-
227
-
228
-
229
- void OnPostRender()
230
-
231
- {
232
-
233
- Vector2 pos = Input.mousePosition;
234
-
235
-
236
-
237
- // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
238
-
239
- // Metalの場合、例外的に反転は行わないようにした
240
-
241
- if (SystemInfo.graphicsUVStartsAtTop && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Metal)
242
-
243
- {
244
-
245
- pos.y = Screen.height - 1 - pos.y;
246
-
247
- }
248
-
249
-
250
-
251
- float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
252
-
253
- float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
254
-
255
-
256
-
257
- Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
258
-
259
- }
260
-
261
- }
262
-
263
- }
163
+ public class GetColor : MonoBehaviour
164
+
165
+ {
166
+
167
+ public Color32 color;
168
+
169
+ private GetColorHelper helper;
170
+
171
+
172
+
173
+ void Start()
174
+
175
+ {
176
+
177
+ helper = Camera.main.GetComponent<GetColorHelper>();
178
+
179
+ if (helper == null)
180
+
181
+ {
182
+
183
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
184
+
185
+ }
186
+
187
+ }
188
+
189
+
190
+
191
+ void Update()
192
+
193
+ {
194
+
195
+ if (Input.GetMouseButtonDown(0))
196
+
197
+ {
198
+
199
+ color = helper.Tex.GetPixel(0, 0);
200
+
201
+ Debug.Log(color.ToString());
202
+
203
+ }
204
+
205
+ }
206
+
207
+
208
+
209
+ [RequireComponent(typeof(Camera))]
210
+
211
+ private class GetColorHelper : MonoBehaviour
212
+
213
+ {
214
+
215
+ public Texture2D Tex { get; private set; }
216
+
217
+
218
+
219
+ void Awake()
220
+
221
+ {
222
+
223
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
224
+
225
+ }
226
+
227
+
228
+
229
+ void OnPostRender()
230
+
231
+ {
232
+
233
+ Vector2 pos = Input.mousePosition;
234
+
235
+
236
+
237
+ // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
238
+
239
+ // Metalの場合、例外的に反転は行わないようにした
240
+
241
+ if (SystemInfo.graphicsUVStartsAtTop && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Metal)
242
+
243
+ {
244
+
245
+ pos.y = Screen.height - 1 - pos.y;
246
+
247
+ }
248
+
249
+
250
+
251
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
252
+
253
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
254
+
255
+
256
+
257
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
258
+
259
+ }
260
+
261
+ }
262
+
263
+ }
264
264
 
265
265
  }
266
266
 
@@ -288,108 +288,376 @@
288
288
 
289
289
  {
290
290
 
291
- public class GetColor : MonoBehaviour
292
-
293
- {
294
-
295
- public Color32 color;
296
-
297
- private GetColorHelper helper;
298
-
299
-
300
-
301
- void Start()
302
-
303
- {
304
-
305
- helper = Camera.main.GetComponent<GetColorHelper>();
306
-
307
- if (helper == null)
308
-
309
- {
310
-
311
- helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
312
-
313
- }
314
-
315
- }
316
-
317
-
318
-
319
- void Update()
320
-
321
- {
322
-
323
- if (Input.GetMouseButtonDown(0))
324
-
325
- {
326
-
327
- color = helper.Tex.GetPixel(0, 0);
328
-
329
- Debug.Log(color.ToString());
330
-
331
- }
332
-
333
- }
334
-
335
-
336
-
337
- [RequireComponent(typeof(Camera))]
338
-
339
- private class GetColorHelper : MonoBehaviour
340
-
341
- {
342
-
343
- public Texture2D Tex { get; private set; }
344
-
345
-
346
-
347
- void Awake()
348
-
349
- {
350
-
351
- Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
352
-
353
- }
354
-
355
-
356
-
357
- IEnumerator Start()
358
-
359
- {
360
-
361
- var waitForEndOfFrame = new WaitForEndOfFrame();
362
-
363
-
364
-
365
- while (true)
366
-
367
- {
368
-
369
- yield return waitForEndOfFrame;
370
-
371
-
372
-
373
- Vector2 pos = Input.mousePosition;
374
-
375
-
376
-
377
- float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
378
-
379
- float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
380
-
381
-
382
-
383
- Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
384
-
385
- }
386
-
387
- }
388
-
389
- }
390
-
391
- }
291
+ public class GetColor : MonoBehaviour
292
+
293
+ {
294
+
295
+ public Color32 color;
296
+
297
+ private GetColorHelper helper;
298
+
299
+
300
+
301
+ void Start()
302
+
303
+ {
304
+
305
+ helper = Camera.main.GetComponent<GetColorHelper>();
306
+
307
+ if (helper == null)
308
+
309
+ {
310
+
311
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
319
+ void Update()
320
+
321
+ {
322
+
323
+ if (Input.GetMouseButtonDown(0))
324
+
325
+ {
326
+
327
+ color = helper.Tex.GetPixel(0, 0);
328
+
329
+ Debug.Log(color.ToString());
330
+
331
+ }
332
+
333
+ }
334
+
335
+
336
+
337
+ [RequireComponent(typeof(Camera))]
338
+
339
+ private class GetColorHelper : MonoBehaviour
340
+
341
+ {
342
+
343
+ public Texture2D Tex { get; private set; }
344
+
345
+
346
+
347
+ void Awake()
348
+
349
+ {
350
+
351
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
352
+
353
+ }
354
+
355
+
356
+
357
+ IEnumerator Start()
358
+
359
+ {
360
+
361
+ var waitForEndOfFrame = new WaitForEndOfFrame();
362
+
363
+
364
+
365
+ while (true)
366
+
367
+ {
368
+
369
+ yield return waitForEndOfFrame;
370
+
371
+
372
+
373
+ Vector2 pos = Input.mousePosition;
374
+
375
+
376
+
377
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
378
+
379
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
380
+
381
+
382
+
383
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
384
+
385
+ }
386
+
387
+ }
388
+
389
+ }
390
+
391
+ }
392
392
 
393
393
  }
394
394
 
395
395
  ```
396
+
397
+
398
+
399
+ **追記**
400
+
401
+ まず、下記のようなメインテクスチャ・メインカラーで塗るだけのシンプルなUnlitシェーダーを用意しました。
402
+
403
+
404
+
405
+ ```ShaderLab
406
+
407
+ Shader "Unlit/UnlitAlbedo"
408
+
409
+ {
410
+
411
+ Properties
412
+
413
+ {
414
+
415
+ _MainTex ("Texture", 2D) = "white" {}
416
+
417
+ _Color ("Color", Color) = (1, 1, 1, 1)
418
+
419
+ }
420
+
421
+ SubShader
422
+
423
+ {
424
+
425
+ Tags { "RenderType"="Opaque" }
426
+
427
+ LOD 100
428
+
429
+
430
+
431
+ Pass
432
+
433
+ {
434
+
435
+ CGPROGRAM
436
+
437
+ #pragma vertex vert
438
+
439
+ #pragma fragment frag
440
+
441
+
442
+
443
+ #include "UnityCG.cginc"
444
+
445
+
446
+
447
+ struct appdata
448
+
449
+ {
450
+
451
+ float4 vertex : POSITION;
452
+
453
+ float2 uv : TEXCOORD0;
454
+
455
+ };
456
+
457
+
458
+
459
+ struct v2f
460
+
461
+ {
462
+
463
+ float2 uv : TEXCOORD0;
464
+
465
+ float4 vertex : SV_POSITION;
466
+
467
+ };
468
+
469
+
470
+
471
+ sampler2D _MainTex;
472
+
473
+ float4 _MainTex_ST;
474
+
475
+ fixed4 _Color;
476
+
477
+
478
+
479
+ v2f vert(appdata v)
480
+
481
+ {
482
+
483
+ v2f o;
484
+
485
+ o.vertex = UnityObjectToClipPos(v.vertex);
486
+
487
+ o.uv = TRANSFORM_TEX(v.uv, _MainTex);
488
+
489
+ return o;
490
+
491
+ }
492
+
493
+
494
+
495
+ fixed4 frag(v2f i) : SV_Target
496
+
497
+ {
498
+
499
+ return tex2D(_MainTex, i.uv) * _Color;
500
+
501
+ }
502
+
503
+ ENDCG
504
+
505
+ }
506
+
507
+ }
508
+
509
+ }
510
+
511
+ ```
512
+
513
+
514
+
515
+ 通常のレンダリングの場合下図のように見えるシーンを...
516
+
517
+
518
+
519
+ ![図1](57bdbdee30fc709c88702c89a1348f1b.png)
520
+
521
+
522
+
523
+ この代替シェーダーでシーンをレンダリングした場合、下図のように環境の映り込みや陰影がない単純な色塗り状態の映像が得られます。実際にはこの映像を画面上に表示するわけではなく、RenderTexture上に描画して色取り元とすることになりますね。
524
+
525
+
526
+
527
+ ![図2](fbf316d31b6a93341aad43e2abc77bcb.png)
528
+
529
+
530
+
531
+ また、この追加レンダリングはわざわざ画面全体について行う必要はなく、色を取りたい地点の1ピクセル分だけの大きさがあれば十分なはずです。そこで、このレンダリングの時だけ一時的に投影行列に手を加えて注目点だけを拡大し、それを1×1サイズのRenderTextureに描画することにしました。描画面積が大幅に節約でき、レンダリングコストを低減できると思われます。
532
+
533
+
534
+
535
+ 色取りスクリプトは下記のようにしてみました。補助スクリプトは不要になったため廃止しています。
536
+
537
+
538
+
539
+ ```C#
540
+
541
+ using System.Collections;
542
+
543
+ using UnityEngine;
544
+
545
+
546
+
547
+ public class AlbedoColorPicker : MonoBehaviour
548
+
549
+ {
550
+
551
+ public Color32 color;
552
+
553
+ public Renderer targetRenderer;
554
+
555
+
556
+
557
+ private Camera mainCamera;
558
+
559
+ private Shader alternativeShader;
560
+
561
+ private Texture2D pixelTexture;
562
+
563
+
564
+
565
+ private void Start()
566
+
567
+ {
568
+
569
+ this.mainCamera = Camera.main;
570
+
571
+ this.alternativeShader = Shader.Find("Unlit/UnlitAlbedo");
572
+
573
+ this.pixelTexture = new Texture2D(1, 1, TextureFormat.RGBA32, false);
574
+
575
+ }
576
+
577
+
578
+
579
+ private void Update()
580
+
581
+ {
582
+
583
+ if (Input.GetMouseButtonDown(0))
584
+
585
+ {
586
+
587
+ // マウスポインタの位置を狙う変換行列を作り...
588
+
589
+ var screenScale = new Vector2(Screen.width, Screen.height) * 0.5f;
590
+
591
+ var pixelPosition = (Vector2)Input.mousePosition - screenScale;
592
+
593
+ var pixelMatrix = Matrix4x4.TRS(
594
+
595
+ -pixelPosition,
596
+
597
+ Quaternion.identity,
598
+
599
+ new Vector3(screenScale.x, screenScale.y, 1.0f));
600
+
601
+
602
+
603
+ // projectionMatrixの後段に追加の変換を加え、alternativeShaderを使ってレンダリングする
604
+
605
+ var targetTexture = this.mainCamera.targetTexture;
606
+
607
+ var projectionMatrix = this.mainCamera.projectionMatrix;
608
+
609
+ var renderTexture = RenderTexture.GetTemporary(1, 1);
610
+
611
+ this.mainCamera.targetTexture = renderTexture;
612
+
613
+ this.mainCamera.projectionMatrix = pixelMatrix * projectionMatrix;
614
+
615
+ this.mainCamera.RenderWithShader(this.alternativeShader, null);
616
+
617
+ this.mainCamera.targetTexture = targetTexture;
618
+
619
+ this.mainCamera.ResetProjectionMatrix();
620
+
621
+
622
+
623
+ // renderTextureの内容をpixelTextureに読み取り、さらにpixelTextureから色を取り出す
624
+
625
+ var activeTexture = RenderTexture.active;
626
+
627
+ RenderTexture.active = renderTexture;
628
+
629
+ this.pixelTexture.ReadPixels(new Rect(0, 0, 1, 1), 0, 0);
630
+
631
+ RenderTexture.active = activeTexture;
632
+
633
+ RenderTexture.ReleaseTemporary(renderTexture);
634
+
635
+ this.color = this.pixelTexture.GetPixel(0, 0);
636
+
637
+
638
+
639
+ // 色設定対象がセットされていれば、それに取得された色をセットする
640
+
641
+ if (this.targetRenderer != null)
642
+
643
+ {
644
+
645
+ this.targetRenderer.material.color = this.color;
646
+
647
+ }
648
+
649
+ }
650
+
651
+ }
652
+
653
+ }
654
+
655
+ ```
656
+
657
+
658
+
659
+ 動かしてみると下図のようになりました。取った色に陰影付けの影響が及んでいないことをアピールするため、色の適用先はPlaneではなく画面右側にあるSphereにしています。
660
+
661
+
662
+
663
+ ![図3](62a27dba4a46a6c8e744c1e765c545fc.gif)

1

修正案を追記

2019/09/03 14:57

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -133,3 +133,263 @@
133
133
  }
134
134
 
135
135
  ```
136
+
137
+
138
+
139
+
140
+
141
+ **追記**
142
+
143
+ ご指摘ありがとうございます。当初のコードはWindows版でしか試していなかったのですが、確かにMac版でMetalを使用している場合は座標の上下が逆転してしまいますね...
144
+
145
+ その場しのぎ的ですみませんが、下記のようにMetalの場合に座標の上下反転を行わせないようにしてはどうでしょうか。
146
+
147
+ もし他にうまくいかない条件が見つかりましたらコメントください(とはいえMacとWindows以外にすぐ試せる環境を持っておらず、お力になれないかもしれませんが...)。
148
+
149
+
150
+
151
+ ```C#
152
+
153
+ using UnityEngine;
154
+
155
+ using UnityEngine.Rendering;
156
+
157
+
158
+
159
+ namespace GetPixelColor
160
+
161
+ {
162
+
163
+ public class GetColor : MonoBehaviour
164
+
165
+ {
166
+
167
+ public Color32 color;
168
+
169
+ private GetColorHelper helper;
170
+
171
+
172
+
173
+ void Start()
174
+
175
+ {
176
+
177
+ helper = Camera.main.GetComponent<GetColorHelper>();
178
+
179
+ if (helper == null)
180
+
181
+ {
182
+
183
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
184
+
185
+ }
186
+
187
+ }
188
+
189
+
190
+
191
+ void Update()
192
+
193
+ {
194
+
195
+ if (Input.GetMouseButtonDown(0))
196
+
197
+ {
198
+
199
+ color = helper.Tex.GetPixel(0, 0);
200
+
201
+ Debug.Log(color.ToString());
202
+
203
+ }
204
+
205
+ }
206
+
207
+
208
+
209
+ [RequireComponent(typeof(Camera))]
210
+
211
+ private class GetColorHelper : MonoBehaviour
212
+
213
+ {
214
+
215
+ public Texture2D Tex { get; private set; }
216
+
217
+
218
+
219
+ void Awake()
220
+
221
+ {
222
+
223
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
224
+
225
+ }
226
+
227
+
228
+
229
+ void OnPostRender()
230
+
231
+ {
232
+
233
+ Vector2 pos = Input.mousePosition;
234
+
235
+
236
+
237
+ // 座標系がOpenGL方式かDirectX方式かによって上下が変わるのを考慮する
238
+
239
+ // Metalの場合、例外的に反転は行わないようにした
240
+
241
+ if (SystemInfo.graphicsUVStartsAtTop && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Metal)
242
+
243
+ {
244
+
245
+ pos.y = Screen.height - 1 - pos.y;
246
+
247
+ }
248
+
249
+
250
+
251
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
252
+
253
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
254
+
255
+
256
+
257
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
258
+
259
+ }
260
+
261
+ }
262
+
263
+ }
264
+
265
+ }
266
+
267
+ ```
268
+
269
+
270
+
271
+ また、Planeに貼り付けた色が取れないという現象は私の試した限りでは再現させられなかったのですが(Planeであっても他の3Dオブジェクトと違いはないはずなので、Cubeで成功したのならPlaneでもいける気がするのですが...)、ImageなどUIオブジェクトの色を取れないという現象は確認できました。
272
+
273
+ おそらくCanvasの「Render Mode」が「Screen Space - Overlay」になっているんじゃないでしょうか?どうやらこのモードの場合、UIの描画は`OnPostRender`よりも後のタイミングになるため失敗したものと思われます。もしRender Modeを変えてもかまわないのなら、これを「Screen Space - Camera」や「World Space」 にすれば`OnPostRender`より先にUI描画が完了し、色を取ってくることができるでしょう。
274
+
275
+ あるいは、下記のように`WaitForEndOfFrame`のタイミングならScreen Space - Overlayでもいけそうな感じでした。どうしてもScreen Space - Overlayでないと困るようでしたらこちらも試してみてもいいかと思います。
276
+
277
+
278
+
279
+ ```C#
280
+
281
+ using System.Collections;
282
+
283
+ using UnityEngine;
284
+
285
+
286
+
287
+ namespace GetPixelColor
288
+
289
+ {
290
+
291
+ public class GetColor : MonoBehaviour
292
+
293
+ {
294
+
295
+ public Color32 color;
296
+
297
+ private GetColorHelper helper;
298
+
299
+
300
+
301
+ void Start()
302
+
303
+ {
304
+
305
+ helper = Camera.main.GetComponent<GetColorHelper>();
306
+
307
+ if (helper == null)
308
+
309
+ {
310
+
311
+ helper = Camera.main.gameObject.AddComponent<GetColorHelper>();
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
319
+ void Update()
320
+
321
+ {
322
+
323
+ if (Input.GetMouseButtonDown(0))
324
+
325
+ {
326
+
327
+ color = helper.Tex.GetPixel(0, 0);
328
+
329
+ Debug.Log(color.ToString());
330
+
331
+ }
332
+
333
+ }
334
+
335
+
336
+
337
+ [RequireComponent(typeof(Camera))]
338
+
339
+ private class GetColorHelper : MonoBehaviour
340
+
341
+ {
342
+
343
+ public Texture2D Tex { get; private set; }
344
+
345
+
346
+
347
+ void Awake()
348
+
349
+ {
350
+
351
+ Tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
352
+
353
+ }
354
+
355
+
356
+
357
+ IEnumerator Start()
358
+
359
+ {
360
+
361
+ var waitForEndOfFrame = new WaitForEndOfFrame();
362
+
363
+
364
+
365
+ while (true)
366
+
367
+ {
368
+
369
+ yield return waitForEndOfFrame;
370
+
371
+
372
+
373
+ Vector2 pos = Input.mousePosition;
374
+
375
+
376
+
377
+ float x = Mathf.Clamp(pos.x, 0.0f, Screen.width - 1);
378
+
379
+ float y = Mathf.Clamp(pos.y, 0.0f, Screen.height - 1);
380
+
381
+
382
+
383
+ Tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
384
+
385
+ }
386
+
387
+ }
388
+
389
+ }
390
+
391
+ }
392
+
393
+ }
394
+
395
+ ```