Repository navigation
Expand file tree
/
Copy pathGSplat.tsx
More file actions
92 lines (84 loc) · 3.73 KB
/
Copy pathGSplat.tsx
File metadata and controls
92 lines (84 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use client';
import type { GSplatComponent } from 'playcanvas';
import { Asset, Entity, GSplatInstance, ShaderMaterial } from 'playcanvas';
import type { FC } from 'react';
import { useComponent } from '../hooks/index.ts';
import type { PublicProps } from '../utils/types-utils.ts';
import { validatePropsWithDefaults, createComponentDefinition, getStaticNullApplication } from '../utils/validation.ts';
/**
* The GSplat component allows an entity to render a Gaussian Splat.
* @param {GSplatProps} props - The props to pass to the GSplat component.
* @see https://api.playcanvas.com/engine/classes/GSplatComponent.html
* @example
* const { data: splat } = useSplat('./splat.ply')
* <GSplat asset={splat} />
*/
export const GSplat: FC<GSplatProps> = (props) => {
const safeProps = validatePropsWithDefaults<GSplatProps, GSplatComponent>(props, componentDefinition);
useComponent('gsplat', safeProps, componentDefinition.schema);
return null;
};
type GSplatProps = Partial<PublicProps<GSplatComponent>>;
const componentDefinition = createComponentDefinition(
'GSplat',
() => new Entity('mock-gsplat', getStaticNullApplication()).addComponent('gsplat') as GSplatComponent,
(component) => (component as GSplatComponent).system.destroy(),
// `id` is a getter-only property and `lodDistances`/`splatBudget` are removed
// in engine 2.18+, so they must not become settable schema props
{ apiName: 'GSplatComponent', exclude: ['id', 'lodDistances', 'splatBudget'] }
);
componentDefinition.schema = {
...componentDefinition.schema,
asset: {
validate: (val: unknown) => val instanceof Asset,
errorMsg: (val: unknown) => `Invalid value for prop "asset": "${JSON.stringify(val)}". Expected an Asset.`,
default: null
},
// `unified` is a special property that can not be modified while the component is enabled
unified: {
validate: (val: unknown) => typeof val === 'boolean',
errorMsg: (val: unknown) => `Invalid value for prop "unified": "${JSON.stringify(val)}". Expected a boolean.`,
default: false,
apply: (instance: GSplatComponent, props: Record<string, unknown>, key: string) => {
const value = props[key] as boolean;
if (instance.unified === value) {
return;
}
// If not enabled, just set directly
if (!instance.enabled) {
instance.unified = value;
return;
}
// Temporarily disable component, set value, re-enable
instance.enabled = false;
instance.unified = value;
instance.enabled = true;
}
},
material: {
validate: (val: unknown) => val === null || val instanceof ShaderMaterial,
errorMsg: (val: unknown) => `Invalid value for prop "material": "${val}". Expected a ShaderMaterial or null.`,
default: null,
apply: (instance: GSplatComponent, props: Record<string, unknown>, key: string) => {
const value = props[key] as ShaderMaterial | null;
if (instance.material === value) {
return;
}
if (value) {
instance.material = value;
}
}
},
instance: {
validate: (val: unknown) => val === null || val instanceof GSplatInstance,
errorMsg: (val: unknown) => `Invalid value for prop "instance": "${val}". Expected a GSplatInstance or null.`,
default: null,
apply: (instance: GSplatComponent, props: Record<string, unknown>, key: string) => {
const value = props[key] as GSplatInstance | null;
if (instance.instance === value) {
return;
}
instance.instance = value;
}
}
};