シューティングゲーム改: シーン再構築へのオクルージョンの追加 #1
Draft
tomoto wants to merge 1 commit intostep-up-base/version-1.0from
Draft
Conversation
tomoto
commented
Aug 21, 2024
Comment on lines
34
to
+46
| guard let shape = try? await ShapeResource | ||
| .generateStaticMesh(from: meshAnchor) else { continue } | ||
| guard let mesh = try? await MeshResource( | ||
| from: MeshResource.Contents(geom: meshAnchor.geometry) | ||
| ) else { continue } | ||
| switch update.event { | ||
| // 障害物が新規に追加された場合 | ||
| case .added: | ||
| // 追加された物体から障害物 Entity を作成、物理演算機能を追加 | ||
| let entity = ModelEntity() | ||
| // 追加された物体から障害物 Entity を作成、隠面処理と物理演算機能を追加 | ||
| let entity = ModelEntity( | ||
| mesh: mesh, | ||
| materials: [OcclusionMaterial()] | ||
| ) |
Collaborator
Author
There was a problem hiding this comment.
ここが最も肝となる修正です。
SceneReconstructionProvider から得た MeshAnchor には現実の物体のかたちを表すメッシュ情報が格納されており、それをもとに ShapeResource と CollisionComponent を作成して衝突をシミュレートできるのは本誌で学んだ通りです。この修正ではさらに、このメッシュ情報を用いて MeshResource を作成し、障害物を表すエンティティのかたち(ModelEntity の mesh 引数)に設定します。またエンティティのマテリアルを OcclusionMaterial (背後のオブジェクトを隠す不可視のマテリアル)に設定します。
MeshAnchor から MeshResource への変換は、ShapeResource とは異なり簡単な手段がないので(visionOS 1.2時点)、MeshResource.swift に自前の変換関数を用意して実現しています。
tomoto
commented
Aug 21, 2024
Comment on lines
60
to
+63
| case .updated: | ||
| // 物体の id を取得して形状と位置を更新 | ||
| guard let entity = meshEntities[meshAnchor.id] else { continue } | ||
| entity.model?.mesh = mesh |
Collaborator
Author
There was a problem hiding this comment.
認識される障害物のかたちは刻々と変化します。変化が検知されたらエンティティのかたちも忘れずに更新します。
a9fa5e3 to
1fca32e
Compare
e4b6caa to
c09cf66
Compare
1fca32e to
a9fa5e3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
注: このDraft PRは「さらにステップアップ」の説明用です。実際にマージを要求するものではありません。
概要
Chapter 8で作成した「シューティングゲーム改」では、シーン再構築により現実世界の物体が障害物となり、弾が跳ね返ったり転がったりといった振る舞いをします。しかし、弾やターゲットが障害物の背後にあっても透けて見えてしまい、不自然な体験になることがあります。これは、障害物が物体の運動にのみ影響を与えるように設定されており、背後にある物体を隠す設定(オクルージョンまたは陰面処理と呼ばれる)がなされていないためです。本ステップアップでは、オクルージョンを追加してこの問題を解決します。
差分の確認とコードの修正
Files changedタブを開き、差分を確認してください。これらの差分に従って、Chapter 8のコードを修正してください。差分があるのは下記3ファイルです。
project.pbxprojMeshResourceExtension.swiftSceneReconstruction.swiftrunメソッド内のシーン再構築処理を変更します。+で始まる行を追加、-で始まる行を削除してください。動作確認
修正後、Vision Pro実機でアプリを実行してください。障害物の背後にある弾やターゲットが隠れて見えなくなることが確認できます。
うまく動かないなど、答え合わせが必要な人はこのリンクから本修正適用済みのアーカイブをダウンロードし、
Chapter8フォルダを参照してください。解説
ソース差分上のコメントを参照してください。