回答編集履歴

1

ステンシルシャドウによる方法を追記

2021/01/01 20:25

投稿

Bongo
Bongo

スコア10811

test CHANGED
@@ -18,53 +18,53 @@
18
18
 
19
19
  {
20
20
 
21
- private Mesh cubeMesh;
21
+ private Mesh cubeMesh;
22
-
23
-
24
-
22
+
23
+
24
+
25
- private void Awake()
25
+ private void Awake()
26
-
26
+
27
- {
27
+ {
28
-
28
+
29
- this.cubeMesh = this.GetComponent<MeshFilter>().mesh;
29
+ this.cubeMesh = this.GetComponent<MeshFilter>().mesh;
30
-
30
+
31
- var normals = this.cubeMesh.normals;
31
+ var normals = this.cubeMesh.normals;
32
-
32
+
33
- var uv = this.cubeMesh.uv;
33
+ var uv = this.cubeMesh.uv;
34
-
34
+
35
- var scale = new Vector2(0.25f, 0.5f);
35
+ var scale = new Vector2(0.25f, 0.5f);
36
-
36
+
37
- for (var vertexIndex = 0; vertexIndex < normals.Length; vertexIndex++)
37
+ for (var vertexIndex = 0; vertexIndex < normals.Length; vertexIndex++)
38
-
38
+
39
- {
39
+ {
40
-
40
+
41
- var normal = normals[vertexIndex];
41
+ var normal = normals[vertexIndex];
42
-
42
+
43
- var absNormalYz = new Vector2(Mathf.Abs(normal.y), Mathf.Abs(normal.z));
43
+ var absNormalYz = new Vector2(Mathf.Abs(normal.y), Mathf.Abs(normal.z));
44
-
44
+
45
- var x = (int)Vector2.Dot(absNormalYz, new Vector2(1, 2));
45
+ var x = (int)Vector2.Dot(absNormalYz, new Vector2(1, 2));
46
-
46
+
47
- var y = (int)Mathf.Clamp01(-(normal.x + normal.y + normal.z));
47
+ var y = (int)Mathf.Clamp01(-(normal.x + normal.y + normal.z));
48
-
48
+
49
- uv[vertexIndex] = (uv[vertexIndex] + new Vector2(x, y)) * scale;
49
+ uv[vertexIndex] = (uv[vertexIndex] + new Vector2(x, y)) * scale;
50
-
50
+
51
- }
51
+ }
52
-
52
+
53
- this.cubeMesh.uv = uv;
53
+ this.cubeMesh.uv = uv;
54
-
54
+
55
- this.cubeMesh.UploadMeshData(true);
55
+ this.cubeMesh.UploadMeshData(true);
56
-
56
+
57
- }
57
+ }
58
-
59
-
60
-
58
+
59
+
60
+
61
- private void OnDestroy()
61
+ private void OnDestroy()
62
-
62
+
63
- {
63
+ {
64
-
64
+
65
- Destroy(this.cubeMesh);
65
+ Destroy(this.cubeMesh);
66
-
66
+
67
- }
67
+ }
68
68
 
69
69
  }
70
70
 
@@ -109,3 +109,417 @@
109
109
 
110
110
 
111
111
  ![図5](c8dd4d990e0457384d66e415089de67d.gif)
112
+
113
+
114
+
115
+ ##ステンシルシャドウによる方法(その1)
116
+
117
+
118
+
119
+ まずプロジェクト内に、以前別の方の「[影のできる位置を求めてコライダーをつけたい](https://teratail.com/questions/235539)」への回答の際に作成した`ShadowMeshAssembler`を入れました。
120
+
121
+ そして上述の`CubeMeshUnwrapper`内の`this.cubeMesh.UploadMeshData(true);`を`this.cubeMesh.UploadMeshData(false);`に変更し、メッシュ加工後もメッシュデータを読み書きできるようにしました。
122
+
123
+ ターゲットには`CubeMeshUnwrapper`に加えて、各面への頂点変換を保持するための下記スクリプトをアタッチしました。
124
+
125
+
126
+
127
+ ```C#
128
+
129
+ using System.Linq;
130
+
131
+ using UnityEngine;
132
+
133
+
134
+
135
+ [RequireComponent(typeof(MeshFilter))]
136
+
137
+ public class FaceDecomposer : MonoBehaviour
138
+
139
+ {
140
+
141
+ public int FaceCount { get; private set; }
142
+
143
+ public Matrix4x4[] UvMatrices { get; private set; }
144
+
145
+ public Matrix4x4[] ObjectToUvMatrices { get; private set; }
146
+
147
+
148
+
149
+ private void Start()
150
+
151
+ {
152
+
153
+ var mesh = this.GetComponent<MeshFilter>().mesh;
154
+
155
+ var vertices = mesh.vertices;
156
+
157
+ var uv = mesh.uv;
158
+
159
+ var triangles = mesh.triangles;
160
+
161
+ this.FaceCount = triangles.Length / 3;
162
+
163
+ var matrices = Enumerable.Range(0, this.FaceCount).Select(
164
+
165
+ faceIndex =>
166
+
167
+ {
168
+
169
+ var k0 = faceIndex * 3;
170
+
171
+ var k1 = k0 + 1;
172
+
173
+ var k2 = k0 + 2;
174
+
175
+ var i0 = triangles[k0];
176
+
177
+ var i1 = triangles[k1];
178
+
179
+ var i2 = triangles[k2];
180
+
181
+ var v0 = vertices[i0];
182
+
183
+ var v1 = vertices[i1];
184
+
185
+ var v2 = vertices[i2];
186
+
187
+ var t0 = uv[i0];
188
+
189
+ var t1 = uv[i1];
190
+
191
+ var t2 = uv[i2];
192
+
193
+ var uvMatrix = new Matrix4x4(
194
+
195
+ t2 - t0,
196
+
197
+ t1 - t0,
198
+
199
+ new Vector4(0, 0, 1, 0),
200
+
201
+ new Vector4(t0.x, t0.y, 0, 1));
202
+
203
+ var objectMatrix = new Matrix4x4(
204
+
205
+ v2 - v0,
206
+
207
+ v1 - v0,
208
+
209
+ Vector3.Cross(v2 - v0, v1 - v0).normalized,
210
+
211
+ new Vector4(v0.x, v0.y, v0.z, 1));
212
+
213
+ var objectToUvMatrix = uvMatrix * objectMatrix.inverse;
214
+
215
+ return (uvMatrix, objectToUvMatrix);
216
+
217
+ }).ToArray();
218
+
219
+ this.UvMatrices = matrices.Select(m => m.uvMatrix).ToArray();
220
+
221
+ this.ObjectToUvMatrices = matrices.Select(m => m.objectToUvMatrix).ToArray();
222
+
223
+ }
224
+
225
+ }
226
+
227
+ ```
228
+
229
+
230
+
231
+ 影の撮影はこれまでの`ShadowCaptor`に代わって下記`ShadowVolumeCaptor`を使いました。
232
+
233
+
234
+
235
+ ```C#
236
+
237
+ using System;
238
+
239
+ using System.Collections.Generic;
240
+
241
+ using System.IO;
242
+
243
+ using UnityEngine;
244
+
245
+
246
+
247
+ public class ShadowVolumeCaptor : MonoBehaviour
248
+
249
+ {
250
+
251
+ private static readonly int LightDirectionId = Shader.PropertyToID("_LightDirection");
252
+
253
+ private static readonly int WorldToUvId = Shader.PropertyToID("_WorldToUv");
254
+
255
+
256
+
257
+ [SerializeField] private Shader faceMarkerShader;
258
+
259
+ [SerializeField] private Shader shadowMarkerShader;
260
+
261
+ [SerializeField] private Shader shadowDrawerShader;
262
+
263
+ [SerializeField] private Vector2Int size = new Vector2Int(1024, 1024);
264
+
265
+ [SerializeField] private Transform lightTransform;
266
+
267
+
268
+
269
+ private readonly Dictionary<MeshFilter, Mesh> shadowVolumes = new Dictionary<MeshFilter, Mesh>();
270
+
271
+ private Material faceMarkerMaterial;
272
+
273
+ private Material shadowMarkerMaterial;
274
+
275
+ private Material shadowDrawerMaterial;
276
+
277
+ private Mesh face;
278
+
279
+ private Mesh quad;
280
+
281
+ private RenderTexture resultRenderTexture;
282
+
283
+ private Texture2D resultTexture;
284
+
285
+
286
+
287
+ private void Awake()
288
+
289
+ {
290
+
291
+ if (this.faceMarkerShader == null)
292
+
293
+ {
294
+
295
+ Debug.LogError("Face marker shader is not set.");
296
+
297
+ Destroy(this);
298
+
299
+ return;
300
+
301
+ }
302
+
303
+ if (this.shadowMarkerShader == null)
304
+
305
+ {
306
+
307
+ Debug.LogError("Shadow marker shader is not set.");
308
+
309
+ Destroy(this);
310
+
311
+ return;
312
+
313
+ }
314
+
315
+ if (this.shadowDrawerShader == null)
316
+
317
+ {
318
+
319
+ Debug.LogError("Shadow drawer shader is not set.");
320
+
321
+ Destroy(this);
322
+
323
+ return;
324
+
325
+ }
326
+
327
+
328
+
329
+ this.faceMarkerMaterial = new Material(this.faceMarkerShader);
330
+
331
+ this.shadowMarkerMaterial = new Material(this.shadowMarkerShader);
332
+
333
+ this.shadowDrawerMaterial = new Material(this.shadowDrawerShader);
334
+
335
+ this.resultTexture = new Texture2D(this.size.x, this.size.y);
336
+
337
+ this.resultRenderTexture = new RenderTexture(this.size.x, this.size.y, 24);
338
+
339
+ this.face = new Mesh
340
+
341
+ {
342
+
343
+ name = "Face",
344
+
345
+ vertices = new[] {Vector3.zero, Vector3.up, Vector3.right},
346
+
347
+ triangles = new[] {0, 1, 2}
348
+
349
+ };
350
+
351
+ this.quad = new Mesh
352
+
353
+ {
354
+
355
+ name = "Quad",
356
+
357
+ vertices = new[] {new Vector3(-1, -1, 0), new Vector3(-1, 1, 0), new Vector3(1, -1, 0), new Vector3(1, 1, 0)},
358
+
359
+ triangles = new[] {0, 1, 2, 3, 2, 1}
360
+
361
+ };
362
+
363
+ Shader.WarmupAllShaders();
364
+
365
+ }
366
+
367
+
368
+
369
+ private void OnDestroy()
370
+
371
+ {
372
+
373
+ Destroy(this.faceMarkerMaterial);
374
+
375
+ Destroy(this.shadowMarkerMaterial);
376
+
377
+ Destroy(this.shadowDrawerMaterial);
378
+
379
+ Destroy(this.resultTexture);
380
+
381
+ Destroy(this.resultRenderTexture);
382
+
383
+ Destroy(this.face);
384
+
385
+ Destroy(this.quad);
386
+
387
+ foreach (var shadowVolume in this.shadowVolumes.Values)
388
+
389
+ {
390
+
391
+ Destroy(shadowVolume);
392
+
393
+ }
394
+
395
+ }
396
+
397
+
398
+
399
+ public void Capture(FaceDecomposer target, string path)
400
+
401
+ {
402
+
403
+ if (target == null)
404
+
405
+ {
406
+
407
+ throw new ArgumentNullException(nameof(target));
408
+
409
+ }
410
+
411
+
412
+
413
+ if (string.IsNullOrWhiteSpace(path))
414
+
415
+ {
416
+
417
+ throw new ArgumentException("Invalid path.");
418
+
419
+ }
420
+
421
+
422
+
423
+ // レンダーターゲットを設定し...
424
+
425
+ var activeTexture = RenderTexture.active;
426
+
427
+ RenderTexture.active = this.resultRenderTexture;
428
+
429
+ GL.Clear(true, true, Color.white);
430
+
431
+
432
+
433
+ // ライト方向を設定し...
434
+
435
+ this.shadowMarkerMaterial.SetVector(LightDirectionId, this.lightTransform.forward);
436
+
437
+
438
+
439
+ // シーン上の各MeshFilterについて...
440
+
441
+ var worldToTargetMatrix = target.transform.worldToLocalMatrix;
442
+
443
+ foreach (var meshFilter in FindObjectsOfType<MeshFilter>())
444
+
445
+ {
446
+
447
+ var shadowToWorldMatrix = meshFilter.transform.localToWorldMatrix;
448
+
449
+
450
+
451
+ // シャドウボリュームメッシュを取得し(なければ生成し)...
452
+
453
+ if (!this.shadowVolumes.TryGetValue(meshFilter, out var shadowVolume))
454
+
455
+ {
456
+
457
+ shadowVolume = ShadowMeshAssembler.AssembleShadowMesh(meshFilter.sharedMesh);
458
+
459
+ this.shadowVolumes[meshFilter] = shadowVolume;
460
+
461
+ }
462
+
463
+
464
+
465
+ // ターゲットを構成する各面について...
466
+
467
+ var faceCount = target.FaceCount;
468
+
469
+ for (var i = 0; i < faceCount; i++)
470
+
471
+ {
472
+
473
+ // まず面が占めるピクセルをマークする
474
+
475
+ this.faceMarkerMaterial.SetPass(0);
476
+
477
+ Graphics.DrawMeshNow(this.face, target.UvMatrices[i]);
478
+
479
+
480
+
481
+ // マークされたピクセルにシャドウボリュームをレンダリングする
482
+
483
+ this.shadowMarkerMaterial.SetMatrix(WorldToUvId, target.ObjectToUvMatrices[i] * worldToTargetMatrix);
484
+
485
+ this.shadowMarkerMaterial.SetPass(0);
486
+
487
+ Graphics.DrawMeshNow(shadowVolume, shadowToWorldMatrix);
488
+
489
+
490
+
491
+ // マーキングを解除する
492
+
493
+ this.faceMarkerMaterial.SetPass(1);
494
+
495
+ Graphics.DrawMeshNow(this.face, target.UvMatrices[i]);
496
+
497
+ }
498
+
499
+ }
500
+
501
+
502
+
503
+ // 影の中にいると判定された部分に黒を塗る
504
+
505
+ this.shadowDrawerMaterial.SetPass(0);
506
+
507
+ Graphics.DrawMeshNow(this.quad, Matrix4x4.identity);
508
+
509
+
510
+
511
+ // 結果をTexture2Dに読み取らせてPNG形式にエンコードして保存する
512
+
513
+ this.resultTexture.ReadPixels(new Rect(0, 0, this.size.x, this.size.y), 0, 0);
514
+
515
+ RenderTexture.active = activeTexture;
516
+
517
+ var resultData = this.resultTexture.EncodeToPNG();
518
+
519
+ File.WriteAllBytes(path, resultData);
520
+
521
+ }
522
+
523
+ }
524
+
525
+ ```