|
| 1 | +import { CUSTOM_ELEMENTS_SCHEMA, Component, Input } from '@angular/core'; |
| 2 | +import { NgtArgs, injectNgtRef, type NgtPointsMaterial } from 'angular-three'; |
| 3 | +import * as THREE from 'three'; |
| 4 | + |
| 5 | +const opaque_fragment = parseInt(THREE.REVISION.replace(/\D+/g, '')) >= 154 ? 'opaque_fragment' : 'output_fragment'; |
| 6 | + |
| 7 | +export class PointMaterial extends THREE.PointsMaterial { |
| 8 | + constructor(parameters: THREE.PointsMaterialParameters) { |
| 9 | + super(parameters); |
| 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 <${parseInt(THREE.REVISION.replace(/\D+/g, '')) >= 154 ? 'colorspace_fragment' : 'encodings_fragment'}> |
| 27 | + `, |
| 28 | + ); |
| 29 | + }; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +declare global { |
| 34 | + interface HTMLElementTagNameMap { |
| 35 | + /** |
| 36 | + * @extends ngt-points-material |
| 37 | + */ |
| 38 | + 'ngt-point-material': NgtPointsMaterial; |
| 39 | + /** |
| 40 | + * @extends ngt-points-material |
| 41 | + */ |
| 42 | + 'ngts-point-material': NgtPointsMaterial; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +@Component({ |
| 47 | + selector: 'ngts-point-material', |
| 48 | + standalone: true, |
| 49 | + template: `<ngt-primitive ngtCompound [ref]="pointMaterialRef" *args="[material]" attach="material" />`, |
| 50 | + imports: [NgtArgs], |
| 51 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 52 | +}) |
| 53 | +export class NgtsPointMaterial { |
| 54 | + @Input() pointMaterialRef = injectNgtRef<PointMaterial>(); |
| 55 | + |
| 56 | + material = new PointMaterial({}); |
| 57 | +} |
0 commit comments