Skip to content

Commit e4e9013

Browse files
authored
Refactor GSplat component to simplify asset handling and remove unused props. Updated to use useComponent hook instead of useParent and useLayoutEffect, streamlining the component's functionality. (#160)
1 parent 22d607e commit e4e9013

File tree

1 file changed

+8
-56
lines changed

1 file changed

+8
-56
lines changed
Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use client"
22

3-
import { FC, useLayoutEffect, useRef } from "react";
4-
import { useParent } from "../hooks";
5-
import { Asset, Entity, GSplatComponent, GSplatResource } from "playcanvas";
3+
import { FC } from "react";
4+
import { useComponent } from "../hooks";
5+
import { Entity, GSplatComponent } from "playcanvas";
66
import { PublicProps } from "../utils/types-utils";
7-
import { Schema, validatePropsWithDefaults, createComponentDefinition, getStaticNullApplication } from "../utils/validation";
7+
import { validatePropsWithDefaults, createComponentDefinition, getStaticNullApplication } from "../utils/validation";
88
/**
99
* The GSplat component allows an entity to render a Gaussian Splat.
1010
* @param {GSplatProps} props - The props to pass to the GSplat component.
@@ -17,64 +17,16 @@ export const GSplat: FC<GSplatProps> = (props) => {
1717

1818
const safeProps = validatePropsWithDefaults<GSplatProps, GSplatComponent>(props, componentDefinition);
1919

20-
const { asset, vertex, fragment } = safeProps;
21-
const parent: Entity = useParent();
22-
const assetRef = useRef<Entity | null>(null);
20+
useComponent("gsplat", safeProps, componentDefinition.schema);
21+
return null
2322

24-
useLayoutEffect(() => {
25-
if (asset) {
26-
const resource = asset.resource as GSplatResource;
27-
assetRef.current = resource.instantiate({ vertex, fragment });
28-
if (assetRef.current) parent.addChild(assetRef.current);
29-
}
30-
31-
return () => {
32-
if (!assetRef.current) return;
33-
parent.removeChild(assetRef.current);
34-
};
35-
}, [asset]);
36-
37-
return null;
3823
};
3924

40-
interface GSplatProps extends Partial<PublicProps<GSplatComponent>> {
41-
/**
42-
* The asset to use for the GSplat.
43-
*/
44-
asset: Asset;
45-
/**
46-
* The vertex shader to use for the GSplat.
47-
*/
48-
vertex?: string;
49-
/**
50-
* The fragment shader to use for the GSplat.
51-
*/
52-
fragment?: string;
53-
}
25+
type GSplatProps = Partial<PublicProps<GSplatComponent>>
5426

5527
const componentDefinition = createComponentDefinition(
5628
"GSplat",
5729
() => new Entity("mock-gsplat", getStaticNullApplication()).addComponent('gsplat') as GSplatComponent,
5830
(component) => (component as GSplatComponent).system.destroy(),
5931
"GSplatComponent"
60-
)
61-
62-
// include additional props
63-
componentDefinition.schema = {
64-
...componentDefinition.schema,
65-
asset: {
66-
validate: (value: unknown) => value instanceof Asset,
67-
errorMsg: (value: unknown) => `Invalid value for prop "asset": ${value}. Expected an Asset.`,
68-
default: undefined
69-
},
70-
vertex: {
71-
validate: (value: unknown) => !value || typeof value === 'string',
72-
errorMsg: (value: unknown) => `Vertex shader must be a string, received ${value}`,
73-
default: null // Allows engine to handle the default shader
74-
},
75-
fragment: {
76-
validate: (value: unknown) => !value || typeof value === 'string',
77-
errorMsg: (value: unknown) => `Fragment shader must be a string, received ${value}`,
78-
default: null // Allows engine to handle the default shader
79-
}
80-
} as Schema<GSplatComponent, GSplatComponent>
32+
)

0 commit comments

Comments
 (0)