回答編集履歴

4

画像ファイルからのキューブマップ復元について追記

2019/10/11 02:27

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -515,3 +515,33 @@
515
515
  ```
516
516
 
517
517
  保存された画像からキューブマップを作る場合、書き出し時と逆に[Cubemap.SetPixels](https://docs.unity3d.com/jp/560/ScriptReference/Cubemap.SetPixels.html)の`face`をそれぞれの[CubemapFace](https://docs.unity3d.com/jp/560/ScriptReference/CubemapFace.html)に切り替えながら計6回セットしてやることになるかと思います。
518
+
519
+
520
+
521
+ **画像ファイルからキューブマップを作る件について追記**
522
+
523
+
524
+
525
+ 提示いたしましたコードの`cubemapLeft`や`cubemapRight`は[Cubemap](https://docs.unity3d.com/560/Documentation/ScriptReference/Cubemap.html)型ではなく「[dimension](https://docs.unity3d.com/560/Documentation/ScriptReference/RenderTexture-dimension.html)を[Cube](https://docs.unity3d.com/560/Documentation/ScriptReference/Rendering.TextureDimension.Cube.html)にした`RenderTexture`型」です。これは`Texture2D`と普通の`RenderTexture`との関係と同様で、前者は画像ファイルなどCPU側のデータをGPU側でのテクスチャとして使えるように送り込む用途、後者はGPU側でテクスチャ上にレンダリングを行う用途に適していると言えるでしょう。
526
+
527
+ 今回のケースでは画像ロード先のキューブマップが`RenderTexture`である必要はないので、`Cubemap`に変更してはいかがでしょうか。
528
+
529
+ ```C#
530
+
531
+ private Cubemap cubemapLeft;
532
+
533
+ private Cubemap cubemapRight;
534
+
535
+ ```
536
+
537
+ そして初期化時も`Cubemap`を作るようにしてやれば、`cubemapLeft`や`cubemapRight`に対して`SetPixels`が使えるようになるはずです。
538
+
539
+ ```C#
540
+
541
+ this.cubemapLeft = new Cubemap(1024, TextureFormat.RGB24, false);
542
+
543
+ this.cubemapRight = new Cubemap(1024, TextureFormat.RGB24, false);
544
+
545
+ ```
546
+
547
+ それ以降の円筒投影する`Graphics.Blit(this.cubemapRight, this.equirectangularTexture, this.equirectangularMaterial);`の部分では`cubemapLeft`や`cubemapRight`が`RenderTexture`であっても`Cubemap`であっても区別せず扱えるはずですので、コードはそのままでいいかと思います。

3

保存した画像からキューブマップを復元することについて追記

2019/10/11 02:27

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -513,3 +513,5 @@
513
513
  }
514
514
 
515
515
  ```
516
+
517
+ 保存された画像からキューブマップを作る場合、書き出し時と逆に[Cubemap.SetPixels](https://docs.unity3d.com/jp/560/ScriptReference/Cubemap.SetPixels.html)の`face`をそれぞれの[CubemapFace](https://docs.unity3d.com/jp/560/ScriptReference/CubemapFace.html)に切り替えながら計6回セットしてやることになるかと思います。

2

キューブマップのファイル出力について追記

2019/10/09 20:11

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -451,3 +451,65 @@
451
451
 
452
452
 
453
453
  ![図](30eb4042c900fe89326bf2b7fe1f005d.gif)
454
+
455
+
456
+
457
+ **キューブマップをファイルに保存する件について追記**
458
+
459
+ 以前申し上げましたように、キューブマップには6つの面があります。保存メソッドを下記のようにしてみるとどうでしょうか?
460
+
461
+ ```C#
462
+
463
+ void savePng(string filename, int count, RenderTexture saveRT)
464
+
465
+ {
466
+
467
+ CubemapFace[] cubemapFaces =
468
+
469
+ {
470
+
471
+ CubemapFace.PositiveX, CubemapFace.NegativeX,
472
+
473
+ CubemapFace.PositiveY, CubemapFace.NegativeY,
474
+
475
+ CubemapFace.PositiveZ, CubemapFace.NegativeZ
476
+
477
+ };
478
+
479
+ Texture2D tex = new Texture2D(saveRT.width, saveRT.height, TextureFormat.RGB24, false);
480
+
481
+
482
+
483
+ foreach (CubemapFace face in cubemapFaces)
484
+
485
+ {
486
+
487
+ Graphics.SetRenderTarget(saveRT, 0, face);
488
+
489
+ tex.ReadPixels(new Rect(0, 0, saveRT.width, saveRT.height), 0, 0);
490
+
491
+ tex.Apply();
492
+
493
+
494
+
495
+ // Encode texture into PNG
496
+
497
+ byte[] bytes = tex.EncodeToPNG();
498
+
499
+
500
+
501
+ var name = @"C:\Users\Username\Desktop\unity\Captures\" + filename + count.ToString("0") + face.ToString() + ".png";
502
+
503
+ //Write to a file in the project folder
504
+
505
+ File.WriteAllBytes(name, bytes);
506
+
507
+ }
508
+
509
+
510
+
511
+ UnityEngine.Object.Destroy(tex);
512
+
513
+ }
514
+
515
+ ```

1

代替案を追記

2019/10/09 20:01

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -1,3 +1,453 @@
1
1
  では、インスペクター上で参照をセットしておくことは可能でしょうか?
2
2
 
3
3
  スクリプトに`[SerializeField] private Shader equirectangularShader;`のようにフィールドを追加して、インスペクター上の欄にプロジェクトビューからシェーダーファイルをドラッグ&ドロップし、マテリアル作成部分では`this.equirectangularMaterial = new Material(this.equirectangularShader);`という風にするのではどうでしょう。
4
+
5
+
6
+
7
+ **追記**
8
+
9
+ すみませんが、独自のシェーダーを持ち込む方法はわかりませんでした...
10
+
11
+ アプローチを変えて、「[Unity5.6.2f1でメインカメラの両目の映像をレンダリングしたい](https://teratail.com/questions/215439)」で申し上げた正距円筒図法展開をビルトインシェーダーのみで行えないか検討してみました。
12
+
13
+ デフォルトの状態ですとAlways Included Shadersに`Hidden/CubeCopy`が入っています。もしそのゲームでそこがいじられていなければ、それを使うことができるんじゃないかともくろみました。
14
+
15
+
16
+
17
+ `CubeCopy`は単純に入力されたUV座標からキューブマップをサンプリングして出力するだけのシェーダーですので、円筒図法展開に利用するには入力するメッシュをちょっと工夫してやることになるでしょう。
18
+
19
+ そこで頂点座標は四角い展開図の形、UV座標は3次元の球体の形のメッシュを作り、それを[DrawMeshNow](https://docs.unity3d.com/jp/560/ScriptReference/Graphics.DrawMeshNow.html)で描画することにしました。
20
+
21
+ 前回の`StereoEquirectangularTextureUpdater`をベースにしており相変わらず`SerializeField`やらを使っていますので、ご質問者さんの用途には直接利用できないかもしれません。使えそうな部分だけご参考にしていただきたく思います。
22
+
23
+
24
+
25
+ ```C#
26
+
27
+ using System;
28
+
29
+ using System.Collections.Generic;
30
+
31
+ using UnityEngine;
32
+
33
+
34
+
35
+ public class StereoEquirectangularTextureUpdaterBuiltinShader : MonoBehaviour
36
+
37
+ {
38
+
39
+ public enum Arrangement
40
+
41
+ {
42
+
43
+ TopAndBottom,
44
+
45
+ SideBySide
46
+
47
+ }
48
+
49
+
50
+
51
+ public enum FieldOfView
52
+
53
+ {
54
+
55
+ Full,
56
+
57
+ Half
58
+
59
+ }
60
+
61
+
62
+
63
+ [SerializeField] private StereoCubemapUpdater stereoCubemapUpdater;
64
+
65
+ [SerializeField] private RenderTexture equirectangularTexture;
66
+
67
+ [SerializeField] private bool useLocalDirection;
68
+
69
+ [SerializeField] private Arrangement arrangement;
70
+
71
+ [SerializeField] private FieldOfView fieldOfView;
72
+
73
+ [SerializeField] private int sphereNetDivision = 16;
74
+
75
+
76
+
77
+ private RenderTexture cubemapLeft;
78
+
79
+ private RenderTexture cubemapRight;
80
+
81
+ private Material equirectangularMaterial;
82
+
83
+ private Mesh sphereNet;
84
+
85
+ private List<Vector3> sphereNetUvs;
86
+
87
+ private List<Vector3> rotatedSphereNetUvs;
88
+
89
+ private Mesh hemisphereNet;
90
+
91
+ private List<Vector3> hemisphereNetUvs;
92
+
93
+ private List<Vector3> rotatedHemisphereNetUvs;
94
+
95
+
96
+
97
+ // 球面の展開図を表すメッシュを生成するメソッド
98
+
99
+ // divisionを大きくするほど正確に展開できるはずだが、その分頂点数が増えるので
100
+
101
+ // ローカル展開の場合のUV更新にかかるコストが大きくなると思われる
102
+
103
+ private Mesh CreateSphereNet(int division, bool asHemisphere, out List<Vector3> uvs)
104
+
105
+ {
106
+
107
+ var deltaAngle = Mathf.PI / division;
108
+
109
+ var divisionX = (asHemisphere ? 1 : 2) * division;
110
+
111
+ var divisionY = division;
112
+
113
+ var divisionXPlus1 = divisionX + 1;
114
+
115
+ var divisionYPlus1 = divisionY + 1;
116
+
117
+ var deltaX = 2.0f / divisionX;
118
+
119
+ var deltaY = 2.0f / divisionY;
120
+
121
+ var x = new float[divisionXPlus1];
122
+
123
+ var phi0 = -(asHemisphere ? 0.5f : 1.0f) * Mathf.PI;
124
+
125
+ var sinPhi = new float[x.Length];
126
+
127
+ var cosPhi = new float[x.Length];
128
+
129
+ var y = new float[divisionYPlus1];
130
+
131
+ var theta0 = -0.5f * Mathf.PI;
132
+
133
+ var sinTheta = new float[y.Length];
134
+
135
+ var cosTheta = new float[y.Length];
136
+
137
+ for (var i = 0; i <= divisionX; i++)
138
+
139
+ {
140
+
141
+ var angle = phi0 + (deltaAngle * i);
142
+
143
+ sinPhi[i] = Mathf.Sin(angle);
144
+
145
+ cosPhi[i] = Mathf.Cos(angle);
146
+
147
+ x[i] = (deltaX * i) - 1.0f;
148
+
149
+ }
150
+
151
+
152
+
153
+ for (var i = 0; i <= divisionY; i++)
154
+
155
+ {
156
+
157
+ var angle = theta0 + (deltaAngle * i);
158
+
159
+ sinTheta[i] = Mathf.Sin(angle);
160
+
161
+ cosTheta[i] = Mathf.Cos(angle);
162
+
163
+ y[i] = (deltaY * i) - 1.0f;
164
+
165
+ }
166
+
167
+
168
+
169
+ var vertices = new Vector3[divisionXPlus1 * divisionYPlus1];
170
+
171
+ uvs = new List<Vector3>(vertices.Length);
172
+
173
+ for (var j = 0; j <= divisionY; j++)
174
+
175
+ {
176
+
177
+ var o = j * divisionXPlus1;
178
+
179
+ for (var i = 0; i <= divisionX; i++)
180
+
181
+ {
182
+
183
+ vertices[o + i] = new Vector3(x[i], y[j], 0.0f);
184
+
185
+ uvs.Add(new Vector3(sinPhi[i] * cosTheta[j], sinTheta[j], cosPhi[i] * cosTheta[j]));
186
+
187
+ }
188
+
189
+ }
190
+
191
+
192
+
193
+ var triangles = new int[divisionX * divisionY * 6];
194
+
195
+ for (var j = 0; j < divisionY; j++)
196
+
197
+ {
198
+
199
+ var o = j * divisionX;
200
+
201
+ for (var i = 0; i < divisionX; i++)
202
+
203
+ {
204
+
205
+ var k = (o + i) * 6;
206
+
207
+ triangles[k] = (divisionXPlus1 * j) + i;
208
+
209
+ triangles[k + 1] = (divisionXPlus1 * (j + 1)) + i;
210
+
211
+ triangles[k + 2] = triangles[k] + 1;
212
+
213
+ triangles[k + 3] = triangles[k + 1] + 1;
214
+
215
+ triangles[k + 4] = triangles[k + 2];
216
+
217
+ triangles[k + 5] = triangles[k + 1];
218
+
219
+ }
220
+
221
+ }
222
+
223
+
224
+
225
+ var net = new Mesh();
226
+
227
+ net.vertices = vertices;
228
+
229
+ net.SetUVs(0, uvs);
230
+
231
+ net.triangles = triangles;
232
+
233
+ net.MarkDynamic();
234
+
235
+ return net;
236
+
237
+ }
238
+
239
+
240
+
241
+ private void Start()
242
+
243
+ {
244
+
245
+ this.cubemapLeft = this.stereoCubemapUpdater.CubemapLeft;
246
+
247
+ this.cubemapRight = this.stereoCubemapUpdater.CubemapRight;
248
+
249
+ this.equirectangularMaterial = new Material(Shader.Find("Hidden/CubeCopy"));
250
+
251
+ this.sphereNet = this.CreateSphereNet(this.sphereNetDivision, false, out this.sphereNetUvs);
252
+
253
+ this.hemisphereNet = this.CreateSphereNet(this.sphereNetDivision, true, out this.hemisphereNetUvs);
254
+
255
+ this.rotatedSphereNetUvs = new List<Vector3>(this.sphereNetUvs);
256
+
257
+ this.rotatedHemisphereNetUvs = new List<Vector3>(this.hemisphereNetUvs);
258
+
259
+ }
260
+
261
+
262
+
263
+ private void LateUpdate()
264
+
265
+ {
266
+
267
+ // 描画先座標の移動・伸縮はMatrix4x4で表現する
268
+
269
+ Matrix4x4 leftVertexTransform;
270
+
271
+ Matrix4x4 rightVertexTransform;
272
+
273
+ switch (this.arrangement)
274
+
275
+ {
276
+
277
+ case Arrangement.TopAndBottom:
278
+
279
+ leftVertexTransform = Matrix4x4.TRS(
280
+
281
+ new Vector3(0.0f, 0.5f, 0.0f),
282
+
283
+ Quaternion.identity,
284
+
285
+ new Vector3(1.0f, 0.5f, 1.0f));
286
+
287
+ rightVertexTransform = Matrix4x4.TRS(
288
+
289
+ new Vector3(0.0f, -0.5f, 0.0f),
290
+
291
+ Quaternion.identity,
292
+
293
+ new Vector3(1.0f, 0.5f, 1.0f));
294
+
295
+ break;
296
+
297
+ case Arrangement.SideBySide:
298
+
299
+ leftVertexTransform = Matrix4x4.TRS(
300
+
301
+ new Vector3(-0.5f, 0.0f, 0.0f),
302
+
303
+ Quaternion.identity,
304
+
305
+ new Vector3(0.5f, 1.0f, 1.0f));
306
+
307
+ rightVertexTransform = Matrix4x4.TRS(
308
+
309
+ new Vector3(0.5f, 0.0f, 0.0f),
310
+
311
+ Quaternion.identity,
312
+
313
+ new Vector3(0.5f, 1.0f, 1.0f));
314
+
315
+ break;
316
+
317
+ default:
318
+
319
+ throw new ArgumentOutOfRangeException();
320
+
321
+ }
322
+
323
+
324
+
325
+ // ローカル展開の場合はUV座標を回転しメッシュに再設定する
326
+
327
+ var rotation = this.stereoCubemapUpdater.transform.rotation;
328
+
329
+ switch (this.fieldOfView)
330
+
331
+ {
332
+
333
+ case FieldOfView.Full:
334
+
335
+ if (this.useLocalDirection)
336
+
337
+ {
338
+
339
+ for (var i = 0; i < this.sphereNetUvs.Count; i++)
340
+
341
+ {
342
+
343
+ this.rotatedSphereNetUvs[i] = rotation * this.sphereNetUvs[i];
344
+
345
+ }
346
+
347
+
348
+
349
+ this.sphereNet.SetUVs(0, this.rotatedSphereNetUvs);
350
+
351
+ }
352
+
353
+ else
354
+
355
+ {
356
+
357
+ this.sphereNet.SetUVs(0, this.sphereNetUvs);
358
+
359
+ }
360
+
361
+
362
+
363
+ this.sphereNet.RecalculateBounds();
364
+
365
+ break;
366
+
367
+ case FieldOfView.Half:
368
+
369
+ if (this.useLocalDirection)
370
+
371
+ {
372
+
373
+ for (var i = 0; i < this.hemisphereNetUvs.Count; i++)
374
+
375
+ {
376
+
377
+ this.rotatedHemisphereNetUvs[i] = rotation * this.hemisphereNetUvs[i];
378
+
379
+ }
380
+
381
+
382
+
383
+ this.hemisphereNet.SetUVs(0, this.rotatedHemisphereNetUvs);
384
+
385
+ }
386
+
387
+ else
388
+
389
+ {
390
+
391
+ this.hemisphereNet.SetUVs(0, this.hemisphereNetUvs);
392
+
393
+ }
394
+
395
+
396
+
397
+ this.hemisphereNet.RecalculateBounds();
398
+
399
+ break;
400
+
401
+ default:
402
+
403
+ throw new ArgumentOutOfRangeException();
404
+
405
+ }
406
+
407
+
408
+
409
+ // メッシュをレンダーテクスチャ上にレンダリング
410
+
411
+ if (this.equirectangularMaterial.SetPass(0))
412
+
413
+ {
414
+
415
+ var activeTexture = RenderTexture.active;
416
+
417
+ RenderTexture.active = this.equirectangularTexture;
418
+
419
+ var net = this.fieldOfView == FieldOfView.Full ? this.sphereNet : this.hemisphereNet;
420
+
421
+ GL.PushMatrix();
422
+
423
+ GL.LoadProjectionMatrix(Matrix4x4.identity);
424
+
425
+ this.equirectangularMaterial.mainTexture = this.cubemapLeft;
426
+
427
+ Graphics.DrawMeshNow(net, leftVertexTransform);
428
+
429
+ this.equirectangularMaterial.mainTexture = this.cubemapRight;
430
+
431
+ Graphics.DrawMeshNow(net, rightVertexTransform);
432
+
433
+ GL.PopMatrix();
434
+
435
+ RenderTexture.active = activeTexture;
436
+
437
+ }
438
+
439
+ }
440
+
441
+ }
442
+
443
+ ```
444
+
445
+
446
+
447
+ 注意点として、前回の独自シェーダーによる方法ではピクセル単位の正確さで展開されるのに対し、今回の方法では展開に使うメッシュをある程度細かくしないとゆがみが目立ってしまう弱点があります。
448
+
449
+ たとえば`sphereNetDivision`を4に下げると、こんな風になってしまいます...
450
+
451
+
452
+
453
+ ![図](30eb4042c900fe89326bf2b7fe1f005d.gif)