Skip to content

Commit 7eff3e2

Browse files
committed
docs: add vertex colors instances
1 parent 4a7198c commit 7eff3e2

File tree

7 files changed

+195
-28
lines changed

7 files changed

+195
-28
lines changed

apps/demo/src/app/app.routes.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ export const routes: Routes = [
2121
asset: 'assets/demo/view-cube',
2222
},
2323
},
24+
{
25+
path: 'vertex-colors-instance',
26+
loadComponent: () => import('./vertex-colors-instance/vertex-colors-instance.component'),
27+
data: {
28+
description: 'Three.js vertex colors with instances',
29+
link: '/vertex-colors-instance',
30+
asset: 'assets/demo/vertex-colors-instances',
31+
},
32+
},
2433
];

apps/demo/src/app/cubes/cubes.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, inject, Input, ViewChild } from '@angular/core';
2-
import { extend, injectBeforeRender, NgtArgs, NgtCanvas, NgtStore } from 'angular-three';
2+
import { injectBeforeRender, NgtArgs, NgtCanvas, NgtStore } from 'angular-three';
33
import * as THREE from 'three';
4-
import { OrbitControls } from 'three-stdlib';
5-
6-
extend({ OrbitControls });
74

85
@Component({
96
selector: 'demo-cube',
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, inject, ViewChild } from '@angular/core';
2+
import { NgtArgs, NgtCanvas, NgtStore } from 'angular-three';
3+
// @ts-expect-error no type def for nice-color-palettes
4+
import niceColors from 'nice-color-palettes';
5+
import * as THREE from 'three';
6+
const niceColor = niceColors[Math.floor(Math.random() * niceColors.length)];
7+
8+
@Component({
9+
selector: 'demo-colors-instances',
10+
standalone: true,
11+
template: `
12+
<ngt-instanced-mesh #instanced *args="[undefined, undefined, length]">
13+
<ngt-box-geometry *args="[0.15, 0.15, 0.15]">
14+
<ngt-instanced-buffer-attribute attach="attributes.color" *args="[colors, 3]" />
15+
</ngt-box-geometry>
16+
<ngt-mesh-lambert-material [vertexColors]="true" [toneMapped]="false" />
17+
</ngt-instanced-mesh>
18+
`,
19+
imports: [NgtArgs],
20+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
21+
})
22+
export class ColorsInstances {
23+
readonly length = 125000;
24+
25+
private readonly o = new THREE.Object3D();
26+
private readonly c = new THREE.Color();
27+
private readonly colorsArr = Array.from({ length: this.length }, () => niceColor[Math.floor(Math.random() * 5)]);
28+
29+
readonly colors = Float32Array.from(
30+
Array.from({ length: this.length }, (_, index) =>
31+
this.c.set(this.colorsArr[index]).convertSRGBToLinear().toArray()
32+
).flat()
33+
);
34+
35+
@ViewChild('instanced') set instanced({ nativeElement }: ElementRef<THREE.InstancedMesh>) {
36+
let i = 0;
37+
for (let x = 0; x < 50; x++) {
38+
for (let y = 0; y < 50; y++) {
39+
for (let z = 0; z < 50; z++) {
40+
const id = i++;
41+
this.o.position.set(25 - x, 25 - y, 25 - z);
42+
this.o.updateMatrix();
43+
nativeElement.setMatrixAt(id, this.o.matrix);
44+
}
45+
}
46+
}
47+
}
48+
}
49+
50+
@Component({
51+
standalone: true,
52+
template: `
53+
<ngt-ambient-light />
54+
<ngt-directional-light [intensity]="0.55" [position]="150" />
55+
<demo-colors-instances />
56+
<ngt-orbit-controls
57+
*args="[camera, glDom]"
58+
[enableDamping]="true"
59+
[autoRotate]="true"
60+
(beforeRender)="$any($event).object.update()"
61+
/>
62+
`,
63+
imports: [NgtArgs, ColorsInstances],
64+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
65+
})
66+
export class Scene {
67+
private readonly store = inject(NgtStore);
68+
readonly camera = this.store.get('camera');
69+
readonly glDom = this.store.get('gl', 'domElement');
70+
}
71+
72+
@Component({
73+
standalone: true,
74+
template: ` <ngt-canvas [sceneGraph]="SceneGraph" [camera]="{ position: [0, 0, 1] }" /> `,
75+
imports: [NgtCanvas],
76+
})
77+
export default class DemoVertexColorsInstances {
78+
readonly SceneGraph = Scene;
79+
}

apps/demo/src/app/view-cube/view-cube.component.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, inject, ViewChild } from '@angular/core';
22
import {
3-
extend,
43
injectBeforeRender,
54
NgtArgs,
65
NgtCanvas,
@@ -13,9 +12,6 @@ import {
1312
prepare,
1413
} from 'angular-three';
1514
import * as THREE from 'three';
16-
import { OrbitControls } from 'three-stdlib';
17-
18-
extend({ OrbitControls });
1915

2016
@Component({
2117
selector: 'view-cube',

apps/demo/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { bootstrapApplication } from '@angular/platform-browser';
22
import { provideRouter } from '@angular/router';
33
import { extend } from 'angular-three';
44
import * as THREE from 'three';
5+
import { OrbitControls } from 'three-stdlib';
56
import { AppComponent } from './app/app.component';
67
import { routes } from './app/app.routes';
78

89
extend(THREE);
10+
extend({ OrbitControls });
911

1012
bootstrapApplication(AppComponent, { providers: [provideRouter(routes)] }).catch((err) => console.error(err));

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@rx-angular/state": "^1.7.0",
6767
"@swc/helpers": "~0.4.14",
6868
"ngx-resize": "^1.0.5",
69+
"nice-color-palettes": "^3.0.0",
6970
"rxjs": "~7.8.0",
7071
"three": "^0.148.0",
7172
"three-stdlib": "^2.21.6",

0 commit comments

Comments
 (0)