WWDC23: Build spatial experiences with RealityKit | Apple

Apple Developer
6 Jun 202427:32

Summary

TLDRこのビデオスクリプトでは、Appleプラットフォームの新しいRealityKitフレームワークが紹介されています。RealityKitを使用して、3Dモデルとエフェクトをリアルにレンダリング、アニメーション化、シミュレートすることができます。スピーカーは、RealityViewやSwiftUIと組み合わせて3Dコンテンツを追加し、ジェスチャー、アニメーション、空間音を活用してインタラクティブな体験を作り出す方法を解説しています。カスタムコンポーネントやシステムを通じて、アプリの特定のニーズに合わせた3D体験を拡張する方法も紹介されています。

Takeaways

  • 🎨 RealityKitは、3Dモデルとエフェクトをリアルにレンダリング、アニメーション化、シミュレートするためのフレームワークです。
  • 📅 RealityKitは2019年に導入され、その後多くの新機能が追加されました。
  • 🌐 RealityKitを使用すると、アプリの2Dウィンドウに3Dコンテンツを追加したり、3Dコンテンツを別のウィンドウに中央に表示したり、自己と環境を没入型体験に持ち込むことができます。
  • 📱 RealityKitはAppleプラットフォームのコア3Dフレームワークであり、特にxrOSでは多くの機能を提供しています。
  • 🔑 このプレゼンテーションでは、エンティティ、コンポーネント、RealityViewなどの重要な機能に焦点を当て、RealityKitの使い方を紹介します。
  • 🌍 'Hello World'体験の3D要素を作成するために、SwiftUI、RealityKit、Reality Composer Proを使用する方法を紹介します。
  • 🛠️ RealityKitとSwiftUIを組み合わせることで、アプリに3D要素を追加し、次の次元へと発展させることができます。
  • 🌌 RealityViewは、SwiftUIのビューで、RealityKitのエンティティを用いて3Dコンテンツを構成することができます。
  • 👀 RealityKitのエンティティはコンポーネントを通じて特定の動作を実装し、モデルコンポーネントや変形コンポーネントなどがあります。
  • 🤖 RealityKitのカスタムコンポーネントとシステムを定義することで、アプリの特定のニーズに合わせた機能を構築できます。
  • 🎧 RealityKitのSpatial Audioを使用すると、3D空間内で音がリアルなように聞こえる効果を作り出せます。

Q & A

  • RealityKitは何のためのフレームワークですか?

    -RealityKitは、3Dモデルとエフェクトをリアルにレンダリング、アニメーション化、シミュレートするためのフレームワークです。

  • RealityKitはいつ導入されたのか、またその後でどのような新機能が追加されましたか?

    -RealityKitは2019年に導入され、その後多くの新機能が追加されています。

  • RealityKitを使用することで、どのような3Dコンテンツの体験を作成できますか?

    -RealityKitを使用することで、アプリの2Dウィンドウに3Dコンテンツを追加したり、独立したウィンドウで3Dコンテンツを前面にしたり、自己と環境を没入型体験にすることができます。

  • RealityKitはどのようにしてAppleプラットフォームのコアフレームワークとなっていますか?

    -RealityKitはAppleプラットフォーム上で3Dコンテンツを作成するための主要なフレームワークであり、特にxrOSでは多くの機能を提供しています。

  • RealityViewとは何で、どのような機能を提供していますか?

    -RealityViewは新しいSwiftUIビューで、3Dモデルとエフェクトを表示するために使用されます。これはRealityKitのエンティティを使用して3Dコンテンツを構成することができます。

  • RealityKitで扱える3Dコンテンツの形式として、何が挙げられますか?

    -RealityKitで扱える3Dコンテンツの形式として、USDZファイルなどが挙げられます。

  • RealityKitでエンティティに追加できるコンポーネントの例をいくつか挙げてください。

    -RealityKitでエンティティに追加できるコンポーネントの例として、モデルコンポーネント、トランスフォームコンポーネント、入力ターゲットコンポーネント、コリジョンコンポーネントなどがあります。

  • RealityKitにおける「エンティティ」と「コンポーネント」の違いは何ですか?

    -エンティティはレンダリングや動作を持たせるためのコンテナーオブジェクトであり、コンポーネントはエンティティに特定の動作を提供するものです。

  • RealityKitでカスタムコンポーネントを作成するにはどうすればよいですか?

    -カスタムコンポーネントを作成するには、コンポーネントプロトコルに準拠し、必要なデータを含めます。Codableプロトコルに準拠することで、Reality Composer Proのインターフェイスで直接追加できるようになります。

  • RealityKitにおける「システム」とは何で、どのような役割を果たしますか?

    -システムはエンティティとコンポーネントに対してコードを実行する仕組みで、3D体験の外観や動作をモデル化するECS(Entity Component System)の一部分です。

  • RealityKitでの実空間音声を扱うためにはどのようなタイプのオーディオコンポーネントがありますか?

    -RealityKitでの実空間音声を扱うためには、空間オーディオコンポーネント、環境オーディオコンポーネント、チャンネルオーディオコンポーネントがあります。

Outlines

00:00

😀 リアリティキットの紹介と基本機能

ジョンはRealityKitチームのエンジニアとして、新しいRealityKitフレームワークを紹介します。これは3Dモデルとエフェクトをリアルにレンダリング、アニメーション化、シミュレートするためのツールです。2019年に発表され、その後多くの新機能が追加されました。RealityKitを使用して他のデバイスでアプリを構築していた場合、共通点が多く見られるでしょう。RealityKitはAppleプラットフォームのコア3Dフレームワークであり、xrOSでは特に多くの機能を提供しています。このプレゼンテーションでは、エンティティ、コンポーネント、RealityViewなどの主要な機能に焦点を当て、RealityKitの使い方を紹介します。

05:01

🌐 3DコンテンツとSwiftUIの統合

RealityKitとSwiftUIを組み合わせることで、2Dウィンドウに3Dコンテンツを追加し、アプリを次の次元へと引き上げることができます。SwiftUIはビューとウィンドウを定義するのに使用され、RealityKitは3D要素を追加するのに使われます。例えば、WorldアプリではSwiftUIを使って標準の2Dウィンドウとボタンを表示していますが、Planet Earthボタンをタップすることで3D地球儀を表示する詳細ビューに移動できます。RealityKitのModel 3D Viewを使用して2Dウィンドウに3Dコンテンツを追加する方法や、ボリュームウィンドウを使用して3Dコンテンツを前面にする方法について説明します。

10:01

🎨 RealityViewを使った3Dコンテンツの作成

RealityViewはRealityKitのエンティティを使用して3Dコンテンツを作成する新しいSwiftUIビューです。エンティティはコンテナオブジェクトであり、コンポーネントを通じて特定の動作を実装します。モデルコンポーネントとトランスフォームコンポーネントがEarthエンティティに追加され、3D空間に配置されます。RealityKitはARKitと同じ3D座標規約を使用しており、RealityViewのコンテンツインスタンスには座標空間を変換する機能があります。RealityViewはエンティティを追加し、レンダリング、アニメーション化、シミュレーションを行うためのシンプルな方法を提供します。

15:03

🤖 エンティティとコンポーネントの詳細

エンティティはRealityKitの基本的なオブジェクトであり、コンポーネントを通じて振る舞いが定義されます。モデルコンポーネントはメッシュとマテリアルをエンティティに提供し、トランスフォームコンポーネントは3D空間内の位置、向き、スケールを制御します。エンティティは複数の子エンティティから成り立ち、それぞれが独自のコンポーネントセットを持っています。RealityKitには様々なコンポーネントがあり、コリジョン、入力ターゲット、ホバーエフェクトなどがあります。さらに独自のコンポーネントを作成する方法も紹介されています。

20:06

🎭 リアリティビューとインタラクティブな3Dエクスペリエンスの作り方

RealityViewを使用して3D空間にエンティティを配置し、ジェスチャーやアニメーション、スペシャルオーディオでインタラクティブな3Dエクスペリエンスを作成する方法について説明します。RealityViewはSwiftUIビューにジェスチャーを追加し、エンティティに入力ターゲットコンポーネントとコリジョンコンポーネントを追加することで、インタラクションに対応させます。さらに、ホバーエフェクトコンポーネントを追加して、エンティティがインタラクティブであることを示します。アニメーションとスペシャルオーディオを用いて、シーンをより生き生きとさせ、リアルな3D体験を作り出します。

25:07

🛰️ カスタムコンポーネントとシステムの活用

RealityKitでは、既存の機能を組み合わせて独自のコンポーネントやシステムを作成し、3Dエクスペリエンスを構築することができます。コンポーネントは3D体験の1つの側面を制御するデータであり、エンティティにグループ化されています。カスタムコンポーネントの例として、トレースメッシュを持つコンポーネントが紹介され、その実装方法について説明します。システムはエンティティとコンポーネントに作用するコードであり、ECSと呼ばれるツールを使用して3D体験の外観と振る舞いをモデル化します。トレースシステムの例として、衛星エンティティの後を追うラインメッシュを更新するシステムが紹介されています。

🎉 RealityKitの機能紹介と今後の展望

RealityKitの紹介を締めくくり、RealityKitとRealityViewを使用してSwiftUIで定義されたビュー、ウィンドウ、イマーシブスペースに3D要素を追加する方法について要約説明します。USDファイルを読み込み、ジェスチャーを処理し、アニメーションとスペシャルオーディオを再生する機能があります。さらに、カスタムコンポーネントとシステムを定義してアプリの特定のニーズに応じた機能を構築できます。他のセッションも参照され、RealityKitの豊富な機能を使用してアプリを構築する際の参考になります。

Mindmap

Keywords

💡RealityKit

RealityKitはAppleのフレームワークで、3Dモデルとエフェクトをリアルにレンダリング、アニメーション化、シミュレートするためのツールです。2019年に導入され、その後多くの新機能が追加されました。このビデオでは、RealityKitを使ってスペーシャル体験を作成する方法が紹介されており、そのコアとなる3Dフレームワークとしての役割を果たしています。

💡3Dモデル

3Dモデルとは、三次元空間に存在する物体やキャラクターをコンピュータ上で再現したものです。ビデオでは、RealityKitを使って3Dモデルを扱い、それらを2Dウィンドウに追加したり、別のウィンドウで前面に表示することができると説明されています。

💡スペーシャル体験

スペーシャル体験とは、ユーザーを没入させた感覚を提供するタイプのデジタル体験です。ビデオでは、RealityKitを使って2Dウィンドウに3Dコンテンツを追加したり、完全に没入した体験を作成したりする方法が紹介されています。

💡SwiftUI

SwiftUIはAppleのUIフレームワークで、iOS、macOS、tvOS、watchOSのアプリケーション開発に使用されます。ビデオでは、SwiftUIとRealityKitを組み合わせて、3D要素を2Dウィンドウに追加する方法が説明されています。

💡エンティティ

エンティティは、RealityKitにおけるコンテナーオブジェクトであり、レンダリングや動作をさせるにはコンポーネントが必要です。ビデオでは、Earthモデルやサテライトなどのエンティティにモデルコンポーネントやトランスフォームコンポーネントが追加され、3D空間に配置される様子が説明されています。

💡コンポーネント

コンポーネントは、エンティティに特定の動作を提供するものです。ビデオでは、モデルコンポーネントやトランスフォームコンポーネント、入力ターゲットコンポーネントなどが挙げられ、それらがエンティティの動作にどのように影響を与えるかが説明されています。

💡RealityView

RealityViewは、SwiftUIのビューで、RealityKitのエンティティを含む3Dコンテンツを表示するために使用されます。ビデオでは、RealityViewを使って3DコンテンツをSwiftUIのウィンドウに追加する方法が紹介されています。

💡アニメーション

アニメーションは、オブジェクトの動きや状態の変化を時間に沿って連続的に変化させることで、没入感やリアルさを提供します。ビデオでは、RealityKitのアニメーション機能を使って、例えば月が地球をorbitするアニメーションを作成する方法が説明されています。

💡スペーシャルオーディオ

スペーシャルオーディオは、音源が空間内のある場所から発せられるかのように聞こえる音です。ビデオでは、RealityKitのスペーシャルオーディオコンポーネントを使って、オーディオを空間的に配置し、シーンに没入感を加える方法が紹介されています。

💡カスタムコンポーネント

カスタムコンポーネントは、開発者が独自の機能を実装するためのRealityKitのコンポーネントです。ビデオでは、traceMeshオブジェクトを含むカスタムコンポーネントの例が示され、独自のアニメーション効果を作成する方法が説明されています。

Highlights

RealityKitは、3Dモデルとエフェクトをリアルにレンダリング、アニメーション化、シミュレートするためのフレームワークです。

RealityKitは2019年に導入され、その後多くの新機能が追加されました。

RealityKitを使用すると、2Dウィンドウに3Dコンテンツを追加したり、独立したウィンドウで3Dコンテンツを前面に表示したり、没入型体験に自分と環境を持ち込むことができます。

RealityKitはAppleプラットフォームのコア3Dフレームワークであり、xrOSでは特に多くの機能を提供しています。

プレゼンテーションでは、エンティティ、コンポーネント、RealityViewなどの主要な機能に焦点を当て、RealityKitの使用方法を紹介します。

「Hello World」体験を用いて3D要素の構築方法をSwiftUI、RealityKit、Reality Composer Proで説明します。

RealityKitとSwiftUIを組み合わせることで、アプリを次の次元に持ち上げることができます。

RealityKitのビルディングブロックであるエンティティとコンポーネントについて学びます。

RealityViewは、3Dモデルとエフェクトのための新しいSwiftUIビューで、RealityKitエンティティを使用して3Dコンテンツを組合わせることができます。

入力の処理やアニメーション、Spatial Audioでアプリを活性化する方法について説明します。

カスタムコンポーネントとシステムでRealityKitの可能性を解き放す方法について話します。

SwiftUIはビューとウィンドウを定義する方法で、RealityKitは3D要素を追加することができます。

2Dウィンドウに3Dコンテンツを追加するには、RealityKitのModel 3D Viewを使用して簡単に実現できます。

3Dコンテンツを別々のウィンドウで前面に表示するために、体積ウィンドウを使用することができます。

Immersive Spaceを使用すると、アプリはウィンドウの範囲を超え、没入型体験を提供できます。

RealityKitのエンティティはコンテナオブジェクトであり、コンポーネントを通じて特定の振る舞いを実装します。

RealityViewはRealityKitエンティティを含むSwiftUIビューで、エンティティをレンダリング、アニメーション化、シミュレートするために使用されます。

RealityKitのカスタムコンポーネントとシステムを使用して、アプリの特定のニーズに合わせた機能を構築できます。

RealityKitとReality Composer Proを使用して、3Dコンテンツを作成し、XcodeでSwiftUIと組み合わせて没入型アプリを開発できます。

Transcripts

play00:00

♪ Mellow instrumental hip-hop ♪

play00:03

play00:10

Hello. I'm John, and I'm an engineer

play00:12

on the RealityKit team.

play00:14

Today, I'm delighted to introduce you

play00:16

to the new RealityKit for creating spatial experiences.

play00:20

RealityKit is a framework for realistically rendering,

play00:24

animating, and simulating 3D models and effects.

play00:27

We introduced RealityKit in 2019

play00:30

and have added a lot of new features since then.

play00:33

If you've already used RealityKit

play00:35

for building apps on other devices,

play00:37

you'll find that there's a lot in common.

play00:40

With RealityKit, you can augment your app's 2D windows

play00:43

with 3D content, bring 3D content front and center

play00:47

in a separate window, or bring yourself and your environment

play00:51

into an immersive experience.

play00:54

RealityKit is a core 3D framework on Apple platforms.

play00:58

And especially on xrOS, it offers a lot of features.

play01:02

In this presentation, I'll focus on some key features,

play01:05

like entities, components, and RealityView,

play01:09

which will introduce you to RealityKit

play01:11

and show you how to use it.

play01:13

I'll also mention sessions that cover other concepts

play01:16

or go into more detail.

play01:18

Let's get started.

play01:20

I'll use the Hello World experience to explain

play01:22

the concepts in this presentation.

play01:25

But before I get to those concepts,

play01:27

let me take you through the three different modules

play01:29

that are a part of this experience.

play01:32

The first module, Planet Earth,

play01:34

presents a 3D globe in its own window

play01:36

that you can interact with from any angle.

play01:40

The second module, Objects in Orbit,

play01:42

immerses you in a model of the Earth, the Moon,

play01:45

and a satellite, demonstrating animations,

play01:48

Spatial Audio, and custom behaviors,

play01:50

like the trace which follows the satellite.

play01:53

The third module, the Solar System,

play01:56

contains a fully immersive experience,

play01:58

which you can learn more about in other sessions.

play02:02

I'm going to show you how to build the 3D elements

play02:04

of this Hello World experience using SwiftUI, RealityKit,

play02:08

and Reality Composer Pro.

play02:11

Let's dive in.

play02:12

I'll get started by talking about how you can use RealityKit

play02:16

together with SwiftUI to bring your app

play02:18

into the next dimension.

play02:20

Next, I'll examine the building blocks of RealityKit,

play02:23

entities like the Earth model and the components

play02:26

which implement its behaviors.

play02:28

You'll learn about the features of RealityView,

play02:31

which is a new SwiftUI view for 3D models and effects.

play02:35

Then I'll explain how you can handle input

play02:37

and bring your app to life with animation and Spatial Audio.

play02:42

Finally, I'll talk about unlocking the potential

play02:45

of RealityKit with custom components and systems.

play02:49

Let's begin by exploring how RealityKit

play02:51

and SwiftUI work together.

play02:54

SwiftUI is how you define your views and windows,

play02:57

and RealityKit lets you add 3D elements.

play03:01

For example, the World app uses SwiftUI to display

play03:05

a standard 2D window with a few buttons.

play03:09

Tapping the Planet Earth button on the left

play03:11

navigates to a detail view,

play03:13

which shows a 2D illustration of the Earth.

play03:16

But what if I want to replace that 2D image with a 3D globe?

play03:21

Adding 3D content to a 2D window is easy

play03:24

using the model 3D view in RealityKit.

play03:27

Let's go over the code.

play03:29

Here's the SwiftUI view, which displays that globe image.

play03:33

I'll change it to display a 3D globe by importing RealityKit

play03:38

and changing the image to a model 3D view,

play03:42

referencing a USD file in my project called Globe.

play03:47

We can customize the loaded model before displaying it

play03:50

by adding two pieces of code:

play03:52

a content closure for the model that is returned

play03:56

and a placeholder view builder to specify a view

play03:59

that's displayed while the model is loading.

play04:03

In the content closure,

play04:04

I'll add resizable and scaledToFit modifiers

play04:08

to make sure that the model fits into the available space

play04:11

in my UI.

play04:13

And because Model 3D loads its model asynchronously,

play04:17

there is a placeholder view builder,

play04:19

which you can use to customize the view

play04:20

that's displayed during the loading process.

play04:23

In this case, I'm just using the built-in ProgressView

play04:26

to display a spinner.

play04:28

Finally, I need to add the globe 3D model as a resource

play04:31

to the app or Swift package.

play04:46

And now the model appears in line in the app

play04:49

with the same appearance that it has in Quick Look

play04:51

or Reality Composer Pro.

play04:54

Placing 3D content in a 2D window is great,

play04:57

but I want my 3D model to be front and center.

play05:01

To accomplish that,

play05:02

I'm going to put the globe in a separate window.

play05:05

I'll add a button to the app's detail view

play05:07

to open that new window.

play05:09

And instead of using a regular window,

play05:11

which displays its contents against a 2D background,

play05:14

I'll use a new window style

play05:16

which places its contents in a volume.

play05:19

This is called a volumetric window,

play05:21

and it's great for viewing 3D content.

play05:25

Unlike a 2D window,

play05:26

a volumetric window is meant to be used from any angle,

play05:30

so it's ideal for 3D models.

play05:32

A volumetric window also keeps a fixed size

play05:35

that is true to life.

play05:37

You can scale a model in a volumetric window

play05:39

to be one meter across,

play05:41

and it will always be displayed at exactly that size.

play05:44

I think that's a great fit for the globe in Hello World.

play05:48

Let's go through the process of adding a volumetric window.

play05:51

First, I'll add a window group to my app.

play05:55

Window groups act as a template that an app can use

play05:57

to open new windows.

play06:00

I'll give the new window an identifier to distinguish it

play06:03

from this app's main window.

play06:05

Next, I'll add a windowStyle volumetric modifier

play06:08

to the window group.

play06:10

I'll also add a defaultSize modifier

play06:13

to give this window a size in meters.

play06:16

Finally, I'll add a button to the detail view.

play06:19

To make the button open the window I've just added,

play06:22

I'll add a property that gives me access

play06:24

to the open window action from the SwiftUI environment.

play06:35

Then I'll call that action from my button.

play06:42

Let's run this app in the simulator.

play06:45

When I press the View Globe button,

play06:47

a volumetric window appears.

play06:51

Now I can interact with the globe from multiple angles,

play06:54

not just the front.

play06:59

But sometimes, the key to unlocking the experience

play07:02

you want to create is immersion.

play07:05

In the Objects in Orbit module of the World app,

play07:08

you're immersed in an animated model of the Earth

play07:11

and its satellites that demonstrates their orbits.

play07:14

This uses an immersive space, a new scene type

play07:18

which allows your app to place 3D elements anywhere in space.

play07:22

When you open an Immersive Space,

play07:24

your app can go beyond the bounds of a window

play07:26

and provide a magical experience.

play07:30

Adding an Immersive Space

play07:31

is similar to adding a window group.

play07:34

It's a new scene in the body of my app.

play07:37

Here I'm using RealityView,

play07:39

which will give me more control over the scene than Model 3D.

play07:43

I'll go over RealityView in more detail in a few minutes.

play07:46

Like before, I'll add a button to the app's detail view.

play07:53

I'll get the openImmersiveSpace action from the environment

play07:57

and call it with the identifier of the scene I've defined.

play08:06

Note that the openImmersiveSpace action is asynchronous.

play08:09

It completes once the space has finished opening.

play08:14

When I press the View Orbits button,

play08:16

an Immersive Space appears.

play08:19

This is already stunning,

play08:21

but you can make it more engaging

play08:23

by adding interactivity, animation,

play08:25

and audio with RealityKit.

play08:28

Whether you're working with 2D windows containing 3D content,

play08:31

or volumetric windows that emphasize your 3D models,

play08:35

I encourage you to check out these SwiftUI sessions

play08:37

if you haven't already.

play08:39

The "Meet SwiftUI for spatial computing" session

play08:42

is an overview of what's new with SwiftUI on this platform.

play08:46

The "Take SwiftUI to the next dimension" session

play08:50

demonstrates how to get the most out of 3D content in a window.

play08:55

There are also multiple styles of immersion.

play08:58

The solar system module of Hello World

play09:01

uses a fully immersive space that hides passthrough

play09:04

and displays its own backdrop.

play09:07

The "Go beyond the window with SwiftUI" session

play09:10

goes into detail about all styles of Immersive Spaces.

play09:14

If you're thinking about creating

play09:15

an immersive experience, I highly recommend this talk.

play09:19

You've encountered two ways to use RealityKit

play09:22

in your SwiftUI views,

play09:23

the easy-to-use Model 3D and RealityView.

play09:28

RealityView is what I'll use for the rest of this session

play09:30

because it allows you to compose your 3D content

play09:33

using RealityKit entities.

play09:36

So what is a RealityKit entity?

play09:39

An entity is a container object.

play09:41

If you create an empty entity from code,

play09:43

it won't do anything.

play09:44

To make an entity render or give a behavior,

play09:47

it must have components.

play09:49

Each component enables some specific behavior for an entity.

play09:54

Here are some examples.

play09:57

The Earth entity in this app is implemented with two components:

play10:01

a model component, which gives the entity a mesh and materials,

play10:04

and a transform component,

play10:06

which places the entity in 3D space.

play10:09

The same is true of the satellite entity.

play10:13

The model component renders a mesh

play10:15

and applies materials to it.

play10:18

These Earth and satellite models

play10:19

were created in a digital content creation tool,

play10:22

exported to a USDZ file, and loaded into RealityKit.

play10:27

These meshes have a physically-based material

play10:30

applied to them to give them their final appearance.

play10:33

A material uses a set of textures and shaders

play10:37

to describe how the surface of a mesh responds to light.

play10:41

To learn more about materials,

play10:42

I recommend watching Niels' session,

play10:45

"Explore materials in Reality Composer Pro."

play10:49

In addition to a model,

play10:50

these entities have a transform component.

play10:53

The transform component places an entity in 3D space.

play10:57

You can control an entity's position, orientation,

play11:00

and scale by setting properties on its transform component,

play11:04

as well as by setting the entity's parent.

play11:08

RealityKit uses the same 3D coordinate conventions as ARKit

play11:12

and many other 3D engines.

play11:15

The origin is at the center of the RealityView.

play11:18

The y-axis points upward, the z-axis points towards you,

play11:22

and the x-axis points to your right.

play11:25

One unit is one meter.

play11:28

Note that these conventions are different

play11:30

from SwiftUI's conventions.

play11:33

There are functions on RealityView's content instance

play11:36

that make it easy to convert back and forth

play11:38

between the RealityKit and SwiftUI coordinate spaces.

play11:42

Every entity has a transform,

play11:44

but not every entity has a model.

play11:47

Sometimes an entity is assembled out of multiple child entities,

play11:51

each with their own set of components.

play11:54

This gives you more programmatic control.

play11:56

For example, you could play individual animations

play11:59

on the transforms of child entities.

play12:03

RealityKit contains a variety of components

play12:05

depending on what you want to do.

play12:07

I'll talk about some specific components today,

play12:10

collision, input target, and hover effect

play12:13

just to name a few.

play12:14

I'll also demonstrate how to create your own components.

play12:19

Now that we've got a sense of how entities

play12:20

and components work, let's use RealityView

play12:23

to place those entities in your app.

play12:27

RealityView is a SwiftUI view

play12:29

that contains any number of entities.

play12:31

Entities need to be added to a RealityView

play12:34

in order to be rendered, animated, and simulated.

play12:37

So, how does a RealityView work?

play12:40

RealityView provides a content instance

play12:43

which lets you add entities to the view.

play12:46

This is an easy way to get started

play12:48

if you have already loaded an entity

play12:49

or if you want to create an entity programmatically.

play12:53

But this closure is asynchronous,

play12:55

so it's simple to load an entity from a file

play12:58

and display it in your view.

play13:00

Here I asynchronously load an Earth model from a USD file

play13:04

and add it to the content instance

play13:06

once the load has completed.

play13:10

You can also load more than one model

play13:12

and place both of them in your RealityView.

play13:15

Instead of being laid out next to each other,

play13:17

these models will coincide in space.

play13:20

If that's not what you want,

play13:21

you can change the position of the entities added to the view.

play13:24

This example positions the moon entity

play13:27

half a meter to the right.

play13:30

Once you've set up your RealityView,

play13:32

you may want to connect state in your app to properties

play13:35

stored on RealityKit components.

play13:38

RealityView lets you express the connections

play13:40

between SwiftUI managed state and the entities

play13:43

in a RealityView with an update closure.

play13:47

This makes it easy to drive the behavior of 3D models

play13:50

with a source of truth from your app's data model.

play13:54

This view loads a model and applies a rotation

play13:57

controlled by whoever uses the view.

play14:00

Note that the code in the update section only runs

play14:04

when the values that it depends on change.

play14:07

If you're building a UI with a combination of 2D

play14:10

and 3D elements, you'll sometimes need to convert

play14:13

coordinates between views and entities.

play14:16

RealityView provides coordinate conversion functions

play14:19

between SwiftUI view coordinate spaces

play14:22

and RealityKit entity coordinate spaces.

play14:25

The RealityView's content instance

play14:28

provides a convert function that converts points,

play14:31

bounding boxes, and transforms from a SwiftUI coordinate space

play14:35

to an entity's local space or vice versa.

play14:39

Here, I get the minimum length of any of the view's dimensions

play14:43

and scale the loaded entity to fit in the available space.

play14:48

RealityView also provides a mechanism

play14:51

for subscribing to events published by entities

play14:53

and components.

play14:56

In this example, I play an animation

play14:58

authored in the loaded USD file after the load completes.

play15:02

The content instance has a subscribe to: function,

play15:06

which adds an event handler.

play15:08

This example runs some code when an animation completes.

play15:12

There are RealityKit events published

play15:14

for all kinds of things, from animation to physics to audio.

play15:19

You can also attach SwiftUI views to entities.

play15:23

The attachments feature of RealityView makes it easy

play15:26

to position views in 3D space.

play15:29

Check out Yujin’s session,

play15:31

"Enhance your spatial computing app with RealityKit,"

play15:34

to learn more.

play15:36

There's a lot you can do with RealityView.

play15:39

But let's get back to our celestial bodies

play15:41

and bring them to life.

play15:43

First, I'll show you how to add a drag gesture

play15:46

so that you can reposition the Earth entity.

play15:49

And then I'll explain animations and Spatial Audio.

play15:52

Here's an example RealityView containing three entities.

play15:56

You can add a gesture to a RealityView,

play15:58

like any other SwiftUI view,

play16:00

and it will hit test against entities in that view.

play16:04

To receive input, the entity must have both

play16:07

an input target component and a collision component.

play16:10

When a touch event is handled by RealityView,

play16:13

it will ignore any entities that don't have both collision

play16:17

and an input target.

play16:19

Only this last entity has both components,

play16:21

so gestures added to this RealityView

play16:24

will only react to input directed at this entity.

play16:29

To make the Earth entity draggable,

play16:31

I'll give it an input target component

play16:33

and a collision component, and add a drag gesture

play16:36

to the RealityView.

play16:38

To add the components, I'll use Reality Composer Pro.

play16:43

Reality Composer Pro is a new developer tool

play16:45

that lets you compose, edit, and preview 3D content.

play16:49

I'll just use it to add a few components to an entity.

play16:53

To learn more about what you can do with Reality Composer Pro,

play16:56

check out Eric's session, "Meet Reality Composer Pro."

play17:01

The World app already has a World Assets package set up

play17:05

containing the USD files that this experience uses.

play17:09

I'll open that package in Reality Composer Pro.

play17:20

The Earth model is in a USDZ archive,

play17:23

which is self-contained and not meant to be modified.

play17:27

Instead of modifying that asset, I'll create a new USD scene file

play17:31

and reference the Earth asset.

play17:34

USD files can reference other USDs and modify them in place

play17:39

without actually changing the referenced file.

play17:42

Nondestructive editing like this is really handy

play17:44

when you need to make small changes to a USD file

play17:47

that someone else is working on.

play17:50

I'll create a new scene named DraggableGlobe

play17:54

and drag in the globe file to create a reference to it.

play18:02

Now I can add components to it.

play18:04

I'll add an input target component

play18:06

and also a collision component.

play18:11

The default shape for the collision component

play18:14

is a cube.

play18:15

I'll change it to a sphere so it better matches the model.

play18:21

It's important for the collision shape to be

play18:23

a reasonable approximation of the visual model.

play18:26

The closer the match,

play18:27

the more intuitive interactions with the model will be.

play18:31

I want to be able to move the Earth model around,

play18:34

so I'll add a drag gesture to the RealityView.

play18:37

A standard SwiftUI drag gesture will work,

play18:40

but I can enable the gesture to manipulate specific entities

play18:43

rather than the entire view

play18:45

by adding a targetedToEntity modifier on the gesture.

play18:50

When the gesture's value changes,

play18:52

I'll change the entity's position to match.

play18:55

There's one critical detail though.

play18:56

The gesture's value is in SwiftUI's coordinate space,

play19:00

so I have to convert it to RealityKit's coordinate space

play19:03

in order to change the entity's position.

play19:06

All the pieces are now in place.

play19:08

So in the Objects in Orbit module,

play19:11

I can now pinch and drag to move the Earth around.

play19:15

Great, our app is now interactive.

play19:18

But I'd like my Earth entity

play19:19

to indicate that it's interactive.

play19:22

There's a RealityKit component we can use for this,

play19:25

the HoverEffectComponent.

play19:27

Hover effects provided by SwiftUI and RealityKit

play19:30

are the only way to make your app react

play19:33

to where you're looking.

play19:34

This effect is applied outside of your app's process

play19:38

in a privacy-preserving manner.

play19:41

I'll add a hover effect component to the Earth entity

play19:43

when it's added to the RealityView.

play19:46

Now, the Earth model lights up when the pointer is over it

play19:50

to indicate that I am able to interact with it.

play19:54

Next, let's move on to animations.

play19:56

RealityKit has a number of built-in animation types,

play19:59

such as from-to-by animations,

play20:02

which animate a property from an initial value to a final value,

play20:06

orbit animations that cause an entity to revolve

play20:08

around its parent, and time-sampled animations

play20:12

that progress frame by frame through a series of values.

play20:16

I'll set up an orbit animation on the Moon.

play20:20

The Moon will take 30 seconds to complete a full orbit

play20:23

and the orbital axis will be the y-axis.

play20:27

And I'll make sure that the orbit starts

play20:29

from the Moon's current position.

play20:32

Once I've defined the properties of this orbit animation,

play20:35

I'll generate an animation resource for it

play20:38

and play that animation on the Moon entity.

play20:42

And now the Moon orbits the Earth.

play20:46

To me, this is the magical moment.

play20:48

With an animation in place, the scene feels alive.

play20:51

But while animation helps bring your 3D content to life,

play20:55

Spatial Audio makes your model feel like it's really there.

play21:00

There are three types of audio in RealityKit:

play21:02

spatial, ambient, and channel.

play21:05

Let's look at each one of them in more detail.

play21:08

RealityKit sounds are spatial by default,

play21:11

so audio sources sound like they actually exist

play21:13

in your surroundings.

play21:16

Spatial Audio Component lets you customize how your objects

play21:19

emit sound into your space to make them even more realistic

play21:23

or more artistic.

play21:26

Use directivity to emit sounds in all directions

play21:29

or project sound in a specific direction.

play21:34

Ambient Audio Component is great for multichannel files,

play21:37

which capture the sound of an environment.

play21:40

No additional reverb is added to ambient sources.

play21:43

Each channel of the ambience is played from a fixed direction.

play21:49

And finally, Channel Audio Component

play21:51

sends the audio files channels directly to the speakers

play21:54

without any spatial effects.

play21:56

This is ideal for background music

play21:58

which is not connected to any visual element.

play22:01

You can add audio to your scene in Reality Composer Pro

play22:04

and interface with it using RealityKit.

play22:07

Or you can hook up audio in code.

play22:10

Let's take a look.

play22:11

I'll add a bit of looping audio to an orbiting satellite.

play22:16

First, I'll add a Spatial Audio Component to an empty entity

play22:19

that will act as the audio source.

play22:22

A directivity of 0.75 creates a tight beam of sound

play22:26

in a particular direction.

play22:28

I'll rotate that audio source entity around its y-axis

play22:32

so that the audio is projected in the direction I want.

play22:36

I'll then load a looping audio clip from a resource

play22:39

and play it on the audioSource entity by calling playAudio.

play22:44

Let's see this in action.

play22:46

[DISTANT BEEPING]

play22:48

Since the Spatial Audio source is configured

play22:51

with a tightly focused directivity,

play22:53

the audio can be heard clearly on my side of the Earth,

play22:55

but it is quieter when the satellite is on the other side.

play23:00

That was input, animation, and audio.

play23:04

You can build more functionality in RealityKit by combining

play23:07

its existing functionality in different ways.

play23:11

There are two primary tools you can use for this purpose,

play23:14

defining your own components and defining your own systems.

play23:18

A component contains the data controlling one aspect

play23:21

of a 3D experience.

play23:24

Components are grouped into entities.

play23:26

Without components, an entity does nothing.

play23:29

Each component supplies a single element

play23:31

of an entity's implementation.

play23:33

You've learned that a transform component

play23:35

positions an entity and that a model component

play23:38

renders a 3D model.

play23:40

In addition to the predefined components

play23:42

that RealityKit provides,

play23:44

you can define your own components.

play23:46

Here's an example custom component

play23:48

that contains a traceMesh object

play23:51

that my coworker Paul has created.

play23:53

The trace component type

play23:55

conforms to the component protocol,

play23:58

so you can get and set this component

play24:00

on any entity at runtime.

play24:03

You can also adopt a data-driven workflow

play24:05

by defining a component in a Swift package

play24:08

and conforming it to the Codable protocol.

play24:12

Codable components will appear

play24:13

in the Reality Composer Pro interface

play24:16

and can be directly added to entities at design time.

play24:20

You can learn more about custom components in the talk

play24:24

"Work with Reality Composer Pro content in Xcode."

play24:28

I already went through entities earlier in this talk,

play24:31

and I just covered components.

play24:32

Next, let's talk about systems.

play24:35

Systems contain code that act on entities and components.

play24:40

Taken together, entities, components, and systems, or ECS,

play24:45

are a tool for modeling the appearance

play24:48

and behavior of your 3D experience.

play24:51

Systems are a way to structure the code

play24:53

that implement your app's behaviors.

play24:56

Code in a system runs at a regular interval,

play24:59

acting upon your component's current state.

play25:02

For example, this TraceSystem updates a line mesh

play25:06

that's traced behind the satellite entity

play25:09

as it orbits the earth.

play25:10

Each update, it adds the entity's current position

play25:13

to the trace.

play25:15

Once a system is registered,

play25:17

it automatically applies everywhere in your app

play25:19

that you use RealityKit.

play25:22

Registering the trace system in my app's initializer

play25:25

causes it to update for all relevant entities.

play25:29

But what entities are relevant, and when does the system update?

play25:34

This system only wants to update entities with a trace component,

play25:38

so I create an entity query that filters to entities

play25:42

which have a trace component.

play25:44

In the update function, the system passes in

play25:47

the entity query and also specifies

play25:49

that it wants to update entities when rendering.

play25:53

The rendering condition means that this system will update

play25:56

at an appropriate rate for smooth animations.

play26:00

Here's the trace system in action,

play26:02

adding the entity's position to the line mesh

play26:05

in order to produce a fluid custom animation.

play26:09

Systems are a really effective way to implement

play26:11

a variety of effects and behaviors.

play26:15

RealityKit has a lot of features that make it easy

play26:17

to build 3D apps.

play26:19

You can use RealityKit and RealityView to add 3D elements

play26:23

to views, windows, and immersive spaces defined with SwiftUI.

play26:28

You can load USD files, handle gestures, and play animation

play26:32

and Spatial Audio, all using RealityKit.

play26:35

RealityKit provides many predefined components,

play26:38

but you can also define custom components and systems

play26:41

for your app's specific needs.

play26:45

With that, I've covered the concepts you need

play26:47

to get started with RealityKit.

play26:50

Yujin’s session,

play26:51

"Enhance Your spatial computing app with RealityKit"

play26:53

will take you through more features of RealityKit,

play26:56

like portals, particle emitters, attachments, and more.

play27:00

And Amanda's session

play27:02

"Work with Reality Composer Pro content in Xcode"

play27:05

takes you through the process of building an immersive app

play27:08

using Reality Composer Pro, RealityKit,

play27:11

Xcode previews, and the simulator.

play27:14

There are a lot of exciting features in RealityKit

play27:16

that you can use in your app.

play27:18

I'm really excited to see the wonderful experiences

play27:21

you'll create.

play27:23

Thanks for watching.

play27:24

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
リアリティキットSwiftUI空間体験3Dモデルアニメーションスペーシャルオーディオカスタムコンポーネントリアリティビューリアリティコンポージター開発ツール
Besoin d'un résumé en anglais ?