Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
docs(demo): adjust level of details demo
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Dec 9, 2021
1 parent 03baa05 commit bce4199
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { Component } from '@angular/core';
@Component({
selector: 'ngt-root',
template: `
<ngt-simple-cube></ngt-simple-cube>
<!-- <ngt-simple-cube></ngt-simple-cube>-->
<!-- <ngt-kinematic-cube></ngt-kinematic-cube>-->
<!-- <ngt-compound-body></ngt-compound-body>-->
<!-- <ngt-keen-bloom></ngt-keen-bloom>-->
<!-- <ngt-instances></ngt-instances>-->
<ngt-level-of-details></ngt-level-of-details>
`,
})
export class AppComponent {}
2 changes: 2 additions & 0 deletions packages/demo/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CompoundBodyComponentModule } from './compound-body/compound-body.compo
import { InstancesComponentModule } from './instances/instances.component';
import { KeenComponentModule } from './keen-bloom/keen-bloom.component';
import { KinematicCubeComponentModule } from './kinematic-cube/kinematic-cube.component';
import { LevelOfDetailsModule } from './level-of-details/level-of-details.component';
import { SimpleCubeComponentModule } from './simple-cube.component';

@NgModule({
Expand All @@ -17,6 +18,7 @@ import { SimpleCubeComponentModule } from './simple-cube.component';
CompoundBodyComponentModule,
KeenComponentModule,
InstancesComponentModule,
LevelOfDetailsModule,
],
providers: [],
bootstrap: [AppComponent],
Expand Down
148 changes: 148 additions & 0 deletions packages/demo/src/app/level-of-details/level-of-details.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { NgtCoreModule, NgtRepeatModule } from '@angular-three/core';
import { NgtIcosahedronGeometryModule } from '@angular-three/core/geometries';
import {
NgtDirectionalLightModule,
NgtPointLightModule,
} from '@angular-three/core/lights';
import { NgtMeshLambertMaterialModule } from '@angular-three/core/materials';
import { NgtMeshModule } from '@angular-three/core/meshes';
import { NgtStatsModule } from '@angular-three/core/stats';
import {
NgtSobaFlyControls,
NgtSobaFlyControlsModule,
} from '@angular-three/soba/controls';
import { NgtSobaDetailedModule } from '@angular-three/soba/performances';
import {
ChangeDetectionStrategy,
Component,
NgModule,
ViewChild,
} from '@angular/core';
import * as THREE from 'three';

@Component({
selector: 'ngt-level-of-details',
template: `
<ngt-canvas
[linear]="true"
[camera]="{ fov: 45, near: 1, far: 15000, position: [0, 0, 1000] }"
[scene]="{ fog }"
(created)="
$event.camera.updateProjectionMatrix();
$event.renderer.setClearColor('black')
"
>
<ngt-point-light [position]="[0, 0, 0]" color="#ff2200"></ngt-point-light>
<ngt-directional-light
[position]="[0, 0, 1]"
color="#ffffff"
></ngt-directional-light>
<ngt-stats></ngt-stats>
<ngt-mesh-lambert-material
#lambertMaterial="ngtMeshLambertMaterial"
[parameters]="{ color: '#ffffff', wireframe: true }"
></ngt-mesh-lambert-material>
<ngt-icosahedron-geometry
#geometry16="ngtIcosahedronGeometry"
[args]="[100, 16]"
></ngt-icosahedron-geometry>
<ngt-icosahedron-geometry
#geometry8="ngtIcosahedronGeometry"
[args]="[100, 8]"
></ngt-icosahedron-geometry>
<ngt-icosahedron-geometry
#geometry4="ngtIcosahedronGeometry"
[args]="[100, 4]"
></ngt-icosahedron-geometry>
<ngt-icosahedron-geometry
#geometry2="ngtIcosahedronGeometry"
[args]="[100, 2]"
></ngt-icosahedron-geometry>
<ngt-icosahedron-geometry
#geometry1="ngtIcosahedronGeometry"
[args]="[100, 1]"
></ngt-icosahedron-geometry>
<ngt-soba-detailed
#lod="ngtSobaDetailed"
*repeat="let _ of 1000"
[position]="[
10000 * (0.5 - (1 | mathConst: 'random')),
7500 * (0.5 - (1 | mathConst: 'random')),
10000 * (0.5 - (1 | mathConst: 'random'))
]"
[distances]="[50, 300, 1000, 2000, 8000]"
>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry16.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry8.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry4.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry2.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry1.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
</ngt-soba-detailed>
<ngt-soba-fly-controls></ngt-soba-fly-controls>
</ngt-canvas>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LevelOfDetailsComponent {
fog = new THREE.Fog('#000000', 1, 15000);

@ViewChild(NgtSobaFlyControls, { static: true })
flyControls!: NgtSobaFlyControls;

ngAfterViewInit() {
this.flyControls.controls.movementSpeed = 1000;
this.flyControls.controls.rollSpeed = Math.PI / 10;
}
}

@NgModule({
declarations: [LevelOfDetailsComponent],
exports: [LevelOfDetailsComponent],
imports: [
NgtCoreModule,
NgtStatsModule,
NgtSobaFlyControlsModule,
NgtPointLightModule,
NgtDirectionalLightModule,
NgtSobaDetailedModule,
NgtRepeatModule,
NgtMeshModule,
NgtIcosahedronGeometryModule,
NgtMeshLambertMaterialModule,
],
})
export class LevelOfDetailsModule {}
156 changes: 156 additions & 0 deletions packages/demo/src/app/level-of-details/level-of-details.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
This example shows the performance aspect of the LOD feature. This is ported from the original [THREE.js LOD example](https://threejs.org/examples/#webgl_lod)

The code for this example is straightforward. There is only one strange place which is `<ngt-mesh [disabled]="true"></ngt-mesh>`. `ngt-mesh` is not the same as a mere `THREE.Mesh`, there are a bunch of other things going on to ensure all the props of a Mesh gets passed over in a declarative manner. However, in the case of LOD, Mesh becomes a special case where they're not on the scene as themselves but as LOD's levels. With this in mind, we do not need the extra functionality of `ngt-mesh` and therefore can `[disabled]` the controller underneath of `ngt-mesh`.

Note that there will be no Events handled when `disabled` is true.

```ts
import { NgtCoreModule, NgtRepeatModule } from '@angular-three/core';
import { NgtIcosahedronGeometryModule } from '@angular-three/core/geometries';
import {
NgtDirectionalLightModule,
NgtPointLightModule,
} from '@angular-three/core/lights';
import { NgtMeshLambertMaterialModule } from '@angular-three/core/materials';
import { NgtMeshModule } from '@angular-three/core/meshes';
import { NgtStatsModule } from '@angular-three/core/stats';
import {
NgtSobaFlyControls,
NgtSobaFlyControlsModule,
} from '@angular-three/soba/controls';
import { NgtSobaDetailedModule } from '@angular-three/soba/performances';
import {
ChangeDetectionStrategy,
Component,
NgModule,
ViewChild,
} from '@angular/core';
import * as THREE from 'three';

@Component({
selector: 'ngt-level-of-details',
template: `
<ngt-canvas
[linear]="true"
[camera]="{ fov: 45, near: 1, far: 15000, position: [0, 0, 1000] }"
[scene]="{ fog }"
(created)="
$event.camera.updateProjectionMatrix();
$event.renderer.setClearColor('black')
"
>
<ngt-point-light [position]="[0, 0, 0]" color="#ff2200"></ngt-point-light>
<ngt-directional-light
[position]="[0, 0, 1]"
color="#ffffff"
></ngt-directional-light>
<ngt-stats></ngt-stats>
<ngt-mesh-lambert-material
#lambertMaterial="ngtMeshLambertMaterial"
[parameters]="{ color: '#ffffff', wireframe: true }"
></ngt-mesh-lambert-material>
<ngt-icosahedron-geometry
#geometry16="ngtIcosahedronGeometry"
[args]="[100, 16]"
></ngt-icosahedron-geometry>
<ngt-icosahedron-geometry
#geometry8="ngtIcosahedronGeometry"
[args]="[100, 8]"
></ngt-icosahedron-geometry>
<ngt-icosahedron-geometry
#geometry4="ngtIcosahedronGeometry"
[args]="[100, 4]"
></ngt-icosahedron-geometry>
<ngt-icosahedron-geometry
#geometry2="ngtIcosahedronGeometry"
[args]="[100, 2]"
></ngt-icosahedron-geometry>
<ngt-icosahedron-geometry
#geometry1="ngtIcosahedronGeometry"
[args]="[100, 1]"
></ngt-icosahedron-geometry>
<ngt-soba-detailed
#lod="ngtSobaDetailed"
*repeat="let _ of 1000"
[position]="[
10000 * (0.5 - (1 | mathConst: 'random')),
7500 * (0.5 - (1 | mathConst: 'random')),
10000 * (0.5 - (1 | mathConst: 'random'))
]"
[distances]="[50, 300, 1000, 2000, 8000]"
>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry16.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry8.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry4.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry2.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
<ngt-mesh
#ngtMesh="ngtMesh"
[disabled]="true"
[geometry]="geometry1.geometry"
[material]="lambertMaterial.material"
[scale]="[1.5, 1.5, 1.5]"
></ngt-mesh>
</ngt-soba-detailed>
<ngt-soba-fly-controls></ngt-soba-fly-controls>
</ngt-canvas>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LevelOfDetailsComponent {
fog = new THREE.Fog('#000000', 1, 15000);

@ViewChild(NgtSobaFlyControls, { static: true })
flyControls!: NgtSobaFlyControls;

ngAfterViewInit() {
this.flyControls.controls.movementSpeed = 1000;
this.flyControls.controls.rollSpeed = Math.PI / 10;
}
}

@NgModule({
declarations: [LevelOfDetailsComponent],
exports: [LevelOfDetailsComponent],
imports: [
NgtCoreModule,
NgtStatsModule,
NgtSobaFlyControlsModule,
NgtPointLightModule,
NgtDirectionalLightModule,
NgtSobaDetailedModule,
NgtRepeatModule,
NgtMeshModule,
NgtIcosahedronGeometryModule,
NgtMeshLambertMaterialModule,
],
})
export class LevelOfDetailsModule {}
```
23 changes: 23 additions & 0 deletions packages/demo/src/app/level-of-details/level-of-details.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Meta, moduleMetadata } from '@storybook/angular';
import { LevelOfDetailsModule } from './level-of-details.component';
// @ts-ignore
import levelOfDetailsDocs from './level-of-details.mdx';

export default {
title: 'Examples/Level of Details',
decorators: [
moduleMetadata({
imports: [LevelOfDetailsModule],
}),
],
parameters: {
docs: { page: levelOfDetailsDocs },
viewMode: 'story',
},
} as Meta;

export const Default = () => ({
template: `
<ngt-level-of-details></ngt-level-of-details>
`,
});

0 comments on commit bce4199

Please sign in to comment.