|
| 1 | +--- |
| 2 | +id: routing |
| 3 | +title: Routing |
| 4 | +sidebar_label: Routing |
| 5 | +--- |
| 6 | + |
| 7 | +import Tabs from '@theme/Tabs'; |
| 8 | +import TabItem from '@theme/TabItem'; |
| 9 | + |
| 10 | +Sometimes, we might have use-cases where we need to load different Scene graph while keeping the Canvas alive on the route level. |
| 11 | +To help with this, `angular-three` provides the component `NgtRoutedScene` |
| 12 | + |
| 13 | +To start, we can create two Scene components: `RedScene` and `BlueScene` |
| 14 | + |
| 15 | +<Tabs> |
| 16 | +<TabItem value="red" label="red-scene.component.ts" default> |
| 17 | + |
| 18 | +```ts |
| 19 | +import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, ViewChild } from '@angular/core'; |
| 20 | +import { injectBeforeRender } from 'angular-three'; |
| 21 | + |
| 22 | +@Component({ |
| 23 | + standalone: true, |
| 24 | + template: ` |
| 25 | + <ngt-mesh #cube> |
| 26 | + <ngt-box-geometry /> |
| 27 | + <ngt-mesh-basic-material color="red" /> |
| 28 | + </ngt-mesh> |
| 29 | + `, |
| 30 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 31 | +}) |
| 32 | +export default class RedScene { |
| 33 | + @ViewChild('cube', { static: true }) cube!: ElementRef<THREE.Mesh>; |
| 34 | + |
| 35 | + constructor() { |
| 36 | + injectBeforeRender(({ clock }) => { |
| 37 | + this.cube.nativeElement.rotation.x = clock.elapsedTime; |
| 38 | + this.cube.nativeElement.rotation.y = clock.elapsedTime; |
| 39 | + }); |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +</TabItem> |
| 45 | +<TabItem value="blue" label="blue-scene.component.ts"> |
| 46 | + |
| 47 | +```ts |
| 48 | +import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, ViewChild } from '@angular/core'; |
| 49 | +import { injectBeforeRender } from 'angular-three'; |
| 50 | + |
| 51 | +@Component({ |
| 52 | + standalone: true, |
| 53 | + template: ` |
| 54 | + <ngt-mesh #cube> |
| 55 | + <ngt-box-geometry /> |
| 56 | + <ngt-mesh-basic-material color="blue" /> |
| 57 | + </ngt-mesh> |
| 58 | + `, |
| 59 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 60 | +}) |
| 61 | +export default class BlueScene { |
| 62 | + @ViewChild('cube', { static: true }) cube!: ElementRef<THREE.Mesh>; |
| 63 | + |
| 64 | + constructor() { |
| 65 | + injectBeforeRender(({ clock }) => { |
| 66 | + this.cube.nativeElement.rotation.x = clock.elapsedTime; |
| 67 | + this.cube.nativeElement.rotation.y = clock.elapsedTime; |
| 68 | + }); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +</TabItem> |
| 74 | +</Tabs> |
| 75 | + |
| 76 | +Next, we'll provide `RedScene` and `BlueScene` as lazy-load component in our route configuration. |
| 77 | + |
| 78 | +```ts title="main.ts" |
| 79 | +import { bootstrapApplication } from '@angular/platform-browser'; |
| 80 | +import { provideRouter } from '@angular/router'; |
| 81 | +import { AppComponent } from './app/app.component'; |
| 82 | + |
| 83 | +bootstrapApplication(AppComponent, { |
| 84 | + providers: [ |
| 85 | + provideRouter([ |
| 86 | + { |
| 87 | + path: '', |
| 88 | + loadComponent: () => import('./app/red-scene.component'), |
| 89 | + }, |
| 90 | + { |
| 91 | + path: 'blue', |
| 92 | + loadComponent: () => import('./app/blue-scene.component'), |
| 93 | + }, |
| 94 | + ]), |
| 95 | + ], |
| 96 | +}).catch((err) => console.error(err)); |
| 97 | +``` |
| 98 | + |
| 99 | +Finally, we'll use `NgtRoutedScene` as the `[sceneGraph]` |
| 100 | + |
| 101 | +```ts title="app.component.ts" |
| 102 | +import { Component } from '@angular/core'; |
| 103 | +import { RouterLink } from '@angular/router'; |
| 104 | +import { extend, NgtCanvas, NgtRoutedScene } from 'angular-three'; |
| 105 | +import * as THREE from 'three'; |
| 106 | + |
| 107 | +extend(THREE); |
| 108 | + |
| 109 | +@Component({ |
| 110 | + standalone: true, |
| 111 | + selector: 'angular-three-root', |
| 112 | + template: ` |
| 113 | + <ul> |
| 114 | + <li> |
| 115 | + <a routerLink="/">Red</a> |
| 116 | + </li> |
| 117 | + <li> |
| 118 | + <a routerLink="/blue">Blue</a> |
| 119 | + </li> |
| 120 | + </ul> |
| 121 | + <ngt-canvas [sceneGraph]="scene" /> |
| 122 | + `, |
| 123 | + imports: [NgtCanvas, RouterLink], |
| 124 | +}) |
| 125 | +export class AppComponent { |
| 126 | + readonly scene = NgtRoutedScene; |
| 127 | +} |
| 128 | +``` |
0 commit comments