Engineをスタンドアロンで使用する
PlayCanvas Engineでアプリケーションを構築する際に、エディタを使用せずに構築することが可能です。Engineを直接使用して構築されたアプリケーションの例をいくつか示します。
- glTF Viewer [GitHub]
- SuperSplat [GitHub]
- ...そしてもちろん、PlayCanvas Editorそのものもです!
このページでは、開始方法を説明します。
始める前に、Node.jsがインストールされていることを確認してください。
ViteとTypeScriptを使用するプロジェクトでは、公式のcreate-playcanvasツールを実行してEngine形式を選択します。
npm create playcanvas@latest my-app -- -f engine
生成されたプロジェクトには、実行可能なスターターと、対応するAIコーディングエージェント向けのPlayCanvas Skillsが含まれます。
プロジェクトをセットアップする際には、考慮すべき主要な2つのオプションがあります。
オプション1: ビルドツールとNPM
これは、ほとんどの開発者に適した推奨されるセットアップです。
ビルドツールは、アプリケーションを幅広いブラウザで実行できる最適化されたパッケージにバンドルできます。webpack、Rollup、esbuildなど、多くのビルドツールがあり、PlayCanvasはそれらすべてで動作します。ここでは、モダンなウェブプロジェクト向けに、より高速で無駄のない開発体験を提供することを目指す人気のビルドツールであるViteを使用します。
まず、JavaScriptとTypeScriptのどちらで開発するかを選択します。
- JavaScript
- TypeScript
-
ターミナル/コマンドプロンプトを開き、アプリ用のフォルダを作成してその中に
cdします。 -
playcanvasとviteをインストールします。npm install playcanvasnpm install vite --save-dev -
index.htmlを作成し、これを貼り付けます。index.html<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><style>body { margin: 0; overflow: hidden; }</style></head><body><script type="module" src="main.js"></script><canvas id='application'></canvas></body></html> -
main.jsを作成し、これを貼り付けます。main.jsimport * as pc from 'playcanvas';// create a graphics device, preferring WebGPU over WebGL 2.0// WebGPUを優先してグラフィックスデバイスを作成しますconst canvas = document.getElementById('application');const device = await pc.createGraphicsDevice(canvas, {deviceTypes: [pc.DEVICETYPE_WEBGPU]});// create an application with the systems and handlers it uses// 使用するシステムとハンドラーを指定してアプリケーションを作成しますconst options = new pc.AppOptions();options.graphicsDevice = device;options.componentSystems = [pc.RenderComponentSystem,pc.CameraComponentSystem,pc.LightComponentSystem];options.resourceHandlers = [pc.TextureHandler,pc.ContainerHandler];const app = new pc.AppBase(canvas);app.init(options);app.setCanvasResolution(pc.RESOLUTION_AUTO);app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);app.start();// resize the canvas when the window is resized// ウィンドウのサイズが変わったらキャンバスのサイズを変更しますwindow.addEventListener('resize', () => app.resizeCanvas());// create a camera// カメラを作成しますconst camera = new pc.Entity();camera.addComponent('camera', {clearColor: new pc.Color(0.3, 0.3, 0.7)});camera.setPosition(0, 0, 3);app.root.addChild(camera);// create a light// ライトを作成しますconst light = new pc.Entity();light.addComponent('light');light.setEulerAngles(45, 45, 0);app.root.addChild(light);// create a box// ボックスを作成しますconst box = new pc.Entity();box.addComponent('render', {type: 'box'});app.root.addChild(box);// rotate the box// ボックスを回転させますapp.on('update', (dt) => box.rotate(10 * dt, 20 * dt, 30 * dt)); -
Vite開発サーバーを実行します。
npx viteこれにより、
http://localhost:5173でサーバーが起動します。ブラウザでこのURLにアクセスすると、以下が表示されます。
ヒントソースファイルを保存するたびに、タブが自動的に再読み込みされます。
-
ターミナル/コマンドプロンプトを開き、アプリ用のフォルダを作成してその中に
cdします。 -
playcanvasとviteをインストールします。npm install playcanvasnpm install vite --save-dev -
index.htmlを作成し、これを貼り付けます。index.html<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><style>body { margin: 0; overflow: hidden; }</style></head><body><script type="module" src="main.ts"></script><canvas id='application'></canvas></body></html> -
main.tsを作成し、以下を貼り付けます。main.tsimport * as pc from 'playcanvas';// WebGPUを優先してグラフィックスデバイスを作成const canvas = document.getElementById('application') as HTMLCanvasElement;const device = await pc.createGraphicsDevice(canvas, {deviceTypes: [pc.DEVICETYPE_WEBGPU]});// 使用するシステムとハンドラーを指定してアプリケーションを作成const options = new pc.AppOptions();options.graphicsDevice = device;options.componentSystems = [pc.RenderComponentSystem,pc.CameraComponentSystem,pc.LightComponentSystem];options.resourceHandlers = [pc.TextureHandler,pc.ContainerHandler];const app = new pc.AppBase(canvas);app.init(options);app.setCanvasResolution(pc.RESOLUTION_AUTO);app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);app.start();// ウィンドウのサイズが変わったらキャンバスのサイズを変更window.addEventListener('resize', () => app.resizeCanvas());// カメラを作成const camera = new pc.Entity();camera.addComponent('camera', {clearColor: new pc.Color(0.3, 0.3, 0.7)});camera.setPosition(0, 0, 3);app.root.addChild(camera);// ライトを作成const light = new pc.Entity();light.addComponent('light');light.setEulerAngles(45, 45, 0);app.root.addChild(light);// ボックスを作成const box = new pc.Entity();box.addComponent('render', {type: 'box'});app.root.addChild(box);// ボックスを回転app.on('update', (dt: number) => box.rotate(10 * dt, 20 * dt, 30 * dt)); -
Vite開発サーバーを実行します。
npx viteこれにより、サーバーが
http://localhost:5173で起動します。ブラウザでこのURLにアクセスすると、以下が表示されます。
ヒントソースファイルを保存するたびに、タブが自動的にリロードされます。
オプション2:インポートマップとCDN
インポートマップは、JavaScriptモジュール内のモジュール指定子を解決できます。このインポートステートメントを考えてみましょう。
import * as pc from 'playcanvas';
インポートマップは、「playcanvas」をブラウザによって動的にロードできるCDNでホストされたエンジンのビルドに解決できます。これは、オプション1で説明したビルド手順をスキップできることを意味します。
まず、JavaScriptとTypeScriptのどちらで開発するかを選択します。
- JavaScript
- TypeScript
-
Terminal/Command Promptを開き、アプリのフォルダを作成し、その中に
cdします。 -
index.htmlを作成し、以下を貼り付けます。index.html<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><style>body { margin: 0; overflow: hidden; }</style><script type="importmap">{"imports": {"playcanvas": "https://cdn.jsdelivr.net/npm/playcanvas/+esm"}}</script></head><body><script type="module" src="main.js"></script><canvas id='application'></canvas></body></html> -
main.jsを作成し、以下を貼り付けます。main.jsimport * as pc from 'playcanvas';// WebGPUを優先してグラフィックスデバイスを作成const canvas = document.getElementById('application');const device = await pc.createGraphicsDevice(canvas, {deviceTypes: [pc.DEVICETYPE_WEBGPU]});// 使用するシステムとハンドラーを指定してアプリケーションを作成const options = new pc.AppOptions();options.graphicsDevice = device;options.componentSystems = [pc.RenderComponentSystem,pc.CameraComponentSystem,pc.LightComponentSystem];options.resourceHandlers = [pc.TextureHandler,pc.ContainerHandler];const app = new pc.AppBase(canvas);app.init(options);app.setCanvasResolution(pc.RESOLUTION_AUTO);app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);app.start();// ウィンドウのサイズが変わったらキャンバスのサイズを変更window.addEventListener('resize', () => app.resizeCanvas());// カメラを作成const camera = new pc.Entity();camera.addComponent('camera', {clearColor: new pc.Color(0.3, 0.3, 0.7)});camera.setPosition(0, 0, 3);app.root.addChild(camera);// ライトを作成const light = new pc.Entity();light.addComponent('light');light.setEulerAngles(45, 45, 0);app.root.addChild(light);// ボックスを作成const box = new pc.Entity();box.addComponent('render', {type: 'box'});app.root.addChild(box);// ボックスを回転させるapp.on('update', (dt) => box.rotate(10 * dt, 20 * dt, 30 * dt)); -
serveを実行します:npx serveこれにより、
http://localhost:3000でサーバーが起動します。ブラウザでこのURLにアクセスすると、次の表示が表示されます:
-
ターミナル/コマンドプロンプトを開き、アプリ用のフォルダーを作成してその中に
cdします。 -
index.htmlを作成し、これを貼り付けます:index.html<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><style>body { margin: 0; overflow: hidden; }</style><script type="importmap">{"imports": {"playcanvas": "https://cdn.jsdelivr.net/npm/playcanvas/+esm"}}</script></head><body><script type="module" src="main.js"></script><canvas id='application'></canvas></body></html> -
main.tsを作成し、これを貼り付けます:main.tsimport * as pc from 'playcanvas';// WebGPUを優先してグラフィックスデバイスを作成しますconst canvas = document.getElementById('application') as HTMLCanvasElement;const device = await pc.createGraphicsDevice(canvas, {deviceTypes: [pc.DEVICETYPE_WEBGPU]});// 使用するシステムとハンドラーを指定してアプリケーションを作成しますconst options = new pc.AppOptions();options.graphicsDevice = device;options.componentSystems = [pc.RenderComponentSystem,pc.CameraComponentSystem,pc.LightComponentSystem];options.resourceHandlers = [pc.TextureHandler,pc.ContainerHandler];const app = new pc.AppBase(canvas);app.init(options);app.setCanvasResolution(pc.RESOLUTION_AUTO);app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);app.start();// ウィンドウのサイズが変わったらキャンバスのサイズを変更しますwindow.addEventListener('resize', () => app.resizeCanvas());// カメラを作成しますconst camera = new pc.Entity();camera.addComponent('camera', {clearColor: new pc.Color(0.3, 0.3, 0.7)});camera.setPosition(0, 0, 3);app.root.addChild(camera);// ライトを作成しますconst light = new pc.Entity();light.addComponent('light');light.setEulerAngles(45, 45, 0);app.root.addChild(light);// ボックスを作成しますconst box = new pc.Entity();box.addComponent('render', {type: 'box'});app.root.addChild(box);// ボックスを回転させますapp.on('update', (dt: number) => box.rotate(10 * dt, 20 * dt, 30 * dt)); -
playcanvasとtypescriptをインストールします:npm install playcanvas typescript --save-dev -
main.tsをmain.jsにコンパイルします:npx tsc main.ts --target es2022 --lib esnext,dom --module esnext --moduleResolution bundler --types webxr -
serveを実行します:npx serveこれにより、
http://localhost:3000でサーバーが起動します。ブラウザでこのURLにアクセスすると、次の表示が表示されます:
アプリケーションの構成
サンプルは、2つの手順でアプリケーションを作成します。
createGraphicsDeviceでグラフィックスデバイスを作成します。deviceTypesにDEVICETYPE_WEBGPUを指定するとWebGPUが要求され、WebGPUが利用できない場合、EngineはWebGL 2.0にフォールバックします。- アプリケーションが使用するコンポーネントシステムとリソースハンドラーを列挙した
AppOptionsオブジェクトで、AppBaseを初期化します。それ以外は登録されないため、ビルドツールは残りの部分をバンドルから除外できます。
アプリケーションの機能を増やすときは、必要なものを登録します。
- コンポーネント: コンポーネントの種類ごとに、そのコンポーネントシステムが必要です。たとえば、
scriptコンポーネントを使用するにはpc.ScriptComponentSystemを追加します。Applicationのコンストラクターに、すべてのコンポーネントの種類とそのシステムが記載されています。システムが登録されていない場合、addComponentはnullを返し、Engineのデバッグビルドはエラーをログに出力します。 - アセット: アセットの種類ごとに、そのリソースハンドラーが必要です。サンプルでは、テクスチャ用の
pc.TextureHandlerとGLBモデル用のpc.ContainerHandlerを登録しています。 - その他の機能: サウンド、バッチ処理、ライトマップ、XRは、
AppOptionsの他のプロパティで有効にします。たとえば、soundコンポーネントにはoptions.soundManager = new pc.SoundManager()も必要です。
Applicationクラスは、これらをすべて自動的に設定します。ただし、コンストラクターではWebGPUデバイスを作成できず、すべてのコンポーネントシステムとリソースハンドラーがバンドルに含まれます。また、将来のリリースで非推奨になる予定です。