-
Notifications
You must be signed in to change notification settings - Fork 0
シューティングゲーム改: シーン再構築へのオクルージョンの追加 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tomoto
wants to merge
1
commit into
step-up-base/version-1.0
Choose a base branch
from
step-up-occlusion
base: step-up-base/version-1.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // | ||
| // MeshResourceExtension.swift | ||
| // Chapter8 | ||
| // | ||
| // Created by 鷲尾友人 on 2024/07/06. | ||
| // | ||
|
|
||
| import RealityKit | ||
| import ARKit | ||
|
|
||
| // 下記サイトを参考に作成。 | ||
| // https://forums.developer.apple.com/forums/thread/749759 | ||
| // https://zenn.dev/shu223/articles/visionos_scenemesh | ||
| // https://github.com/XRealityZone/what-vision-os-can-do/blob/main/WhatVisionOSCanDo/Extension/GeometrySource.swift | ||
|
|
||
| extension MeshResource.Contents { | ||
| init(geom: MeshAnchor.Geometry) { | ||
| self.init() | ||
| self.instances = [MeshResource.Instance(id: "main", model: "model")] | ||
| var part = MeshResource.Part(id: "part", materialIndex: 0) | ||
| part.positions = MeshBuffer(geom.vertices.asSIMD3(ofType: Float.self)) | ||
| part.normals = MeshBuffer(geom.normals.asSIMD3(ofType: Float.self)) | ||
| part.triangleIndices = | ||
| MeshBuffer(geom.faces.asArray(ofType: UInt32.self)) | ||
| self.models = [MeshResource.Model(id: "model", parts: [part])] | ||
| } | ||
| } | ||
|
|
||
| extension GeometrySource { | ||
| func asArray<T>(ofType: T.Type) -> [T] { | ||
| assert(MemoryLayout<T>.stride == stride) | ||
| return (0 ..< self.count).map { | ||
| buffer.contents().advanced(by: offset + stride * Int($0)) | ||
| .assumingMemoryBound(to: T.self).pointee | ||
| } | ||
| } | ||
|
|
||
| func asSIMD3<T>(ofType: T.Type) -> [SIMD3<T>] { | ||
| return asArray(ofType: (T, T, T).self).map { .init($0.0, $0.1, $0.2) } | ||
| } | ||
| } | ||
|
|
||
| extension GeometryElement { | ||
| func asArray<T>(ofType: T.Type) -> [T] { | ||
| assert(MemoryLayout<T>.stride == bytesPerIndex) | ||
| return (0 ..< self.count * self.primitive.indexCount).map { | ||
| buffer.contents().advanced(by: bytesPerIndex * Int($0)) | ||
| .assumingMemoryBound(to: T.self).pointee | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,11 +33,17 @@ class SceneReconstruction: ObservableObject { | |
| // 障害物用のメッシュを作成 | ||
| 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()] | ||
| ) | ||
| entity.transform = Transform( | ||
| matrix: meshAnchor.originFromAnchorTransform | ||
| ) | ||
|
|
@@ -54,6 +60,7 @@ class SceneReconstruction: ObservableObject { | |
| case .updated: | ||
| // 物体の id を取得して形状と位置を更新 | ||
| guard let entity = meshEntities[meshAnchor.id] else { continue } | ||
| entity.model?.mesh = mesh | ||
|
Comment on lines
60
to
+63
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 認識される障害物のかたちは刻々と変化します。変化が検知されたらエンティティのかたちも忘れずに更新します。 |
||
| entity.transform = Transform( | ||
| matrix: meshAnchor.originFromAnchorTransform | ||
| ) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここが最も肝となる修正です。
SceneReconstructionProviderから得たMeshAnchorには現実の物体のかたちを表すメッシュ情報が格納されており、それをもとにShapeResourceとCollisionComponentを作成して衝突をシミュレートできるのは本誌で学んだ通りです。この修正ではさらに、このメッシュ情報を用いてMeshResourceを作成し、障害物を表すエンティティのかたち(ModelEntityのmesh引数)に設定します。またエンティティのマテリアルをOcclusionMaterial(背後のオブジェクトを隠す不可視のマテリアル)に設定します。MeshAnchorからMeshResourceへの変換は、ShapeResourceとは異なり簡単な手段がないので(visionOS 1.2時点)、MeshResource.swiftに自前の変換関数を用意して実現しています。