Created
September 4, 2025 00:33
-
-
Save TAATHub/b36f10a7b9b7868c5a9a3236de6d3437 to your computer and use it in GitHub Desktop.
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
| import SwiftUI | |
| import RealityKit | |
| import RealityKitContent | |
| import FoundationModels | |
| struct ImmersiveView: View { | |
| @State private var voxelModel: VoxelModel? | |
| var body: some View { | |
| RealityView { content in | |
| if let immersiveContentEntity = try? await Entity(named: "Immersive", in: realityKitContentBundle) { | |
| content.add(immersiveContentEntity) | |
| } | |
| } update: { content in | |
| guard let voxelModel else { return } | |
| for voxel in voxelModel.voxels { | |
| let entity = ModelEntity(mesh: .generateBox(size: 0.1), materials: [ | |
| SimpleMaterial(color: .init(red: voxel.color.red/255, | |
| green: voxel.color.green/255, | |
| blue: voxel.color.blue/255, | |
| alpha: 1.0), isMetallic: false) | |
| ]) | |
| entity.position = .init(x: Float(voxel.position.x), | |
| y: Float(voxel.position.y), | |
| z: Float(voxel.position.z)) | |
| content.add(entity) | |
| } | |
| } | |
| .task { | |
| let instructions = """ | |
| あなたはプロの3Dモデラーです。 | |
| サイズ0.1x0.1x0.1のvoxelを3D空間内に配置して、指示された条件で複数のvoxelを組み合わせてボクセルモデルを作ってください。 | |
| """ | |
| let session = LanguageModelSession(model: .default, instructions: instructions) | |
| let prompt = """ | |
| 以下の条件で「赤い球体」のVoxelModelを作ってください。 | |
| - ボクセルモデルのサイズは1x1x1 | |
| - 使えるvoxelの数は制限なし | |
| - それぞれのvoxelが重ならないように3D空間内に配置すること | |
| - VoxelのカラーはRGBで任意の値を指定できる | |
| """ | |
| let response = try? await session.respond(to: prompt, generating: VoxelModel.self) | |
| voxelModel = response?.content | |
| print("response: \(response)") | |
| } | |
| } | |
| } | |
| @Generable(description: "3D model with voxels") | |
| struct VoxelModel { | |
| @Guide(description: "Array of voxels") | |
| var voxels: [Voxel] | |
| } | |
| @Generable(description: "Voxel composing the 3D model") | |
| struct Voxel { | |
| @Guide(description: "Position of voxel in 3D space") | |
| var position: VoxelPosition | |
| @Guide(description: "Color of voxel") | |
| var color: VoxelColor | |
| } | |
| @Generable(description: "Position of voxel in 3D space") | |
| struct VoxelPosition { | |
| var x: Float | |
| var y: Float | |
| var z: Float | |
| } | |
| @Generable(description: "Color of voxel") | |
| struct VoxelColor { | |
| var red: Double | |
| var green: Double | |
| var blue: Double | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment