Skip to content

Commit c57dcea

Browse files
committed
feat(soba): point material
1 parent 7a9c2be commit c57dcea

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

Diff for: libs/soba/materials/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './lib/mesh-reflector-material';
44
export * from './lib/mesh-refraction-material';
55
export * from './lib/mesh-transmission-material';
66
export * from './lib/mesh-wobble-material';
7+
export * from './lib/point-material';

Diff for: libs/soba/materials/src/lib/point-material.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, input } from '@angular/core';
2+
import { NgtArgs, NgtAttachable } from 'angular-three';
3+
import { PointMaterial } from 'angular-three-soba/shaders';
4+
import { PointsMaterialParameters } from 'three';
5+
6+
@Component({
7+
selector: 'ngts-point-material',
8+
standalone: true,
9+
template: `
10+
<ngt-primitive *args="[material]" [attach]="attach()" [parameters]="options()">
11+
<ng-content />
12+
</ngt-primitive>
13+
`,
14+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
15+
changeDetection: ChangeDetectionStrategy.OnPush,
16+
imports: [NgtArgs],
17+
})
18+
export class NgtsPointMaterial {
19+
attach = input<NgtAttachable>('material');
20+
options = input({} as PointsMaterialParameters);
21+
22+
material = new PointMaterial(this.options());
23+
}

Diff for: libs/soba/shaders/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './lib/grid-material';
22
export * from './lib/mesh-refraction-material';
3+
export * from './lib/point-material';

Diff for: libs/soba/shaders/src/lib/point-material.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { NgtMaterial } from 'angular-three';
2+
import { getVersion } from 'angular-three-soba/misc';
3+
import { PointsMaterial, PointsMaterialParameters } from 'three';
4+
5+
const opaque_fragment = getVersion() >= 154 ? 'opaque_fragment' : 'output_fragment';
6+
7+
export class PointMaterial extends PointsMaterial {
8+
constructor(props: PointsMaterialParameters) {
9+
super(props);
10+
this.onBeforeCompile = (shader, renderer) => {
11+
const { isWebGL2 } = renderer.capabilities;
12+
shader.fragmentShader = shader.fragmentShader.replace(
13+
`#include <${opaque_fragment}>`,
14+
`
15+
${
16+
!isWebGL2
17+
? `#extension GL_OES_standard_derivatives : enable\n#include <${opaque_fragment}>`
18+
: `#include <${opaque_fragment}>`
19+
}
20+
vec2 cxy = 2.0 * gl_PointCoord - 1.0;
21+
float r = dot(cxy, cxy);
22+
float delta = fwidth(r);
23+
float mask = 1.0 - smoothstep(1.0 - delta, 1.0 + delta, r);
24+
gl_FragColor = vec4(gl_FragColor.rgb, mask * gl_FragColor.a );
25+
#include <tonemapping_fragment>
26+
#include <${getVersion() >= 154 ? 'colorspace_fragment' : 'encodings_fragment'}>
27+
`,
28+
);
29+
};
30+
}
31+
}
32+
33+
export type NgtPointMaterial = NgtMaterial<PointMaterial, [PointsMaterialParameters]>;
34+
35+
declare global {
36+
interface HTMLElementTagNameMap {
37+
/**
38+
* @extends ngt-points-material
39+
*/
40+
'ngt-point-material': NgtPointMaterial;
41+
}
42+
}

0 commit comments

Comments
 (0)