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" ;
66import { 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
5527const 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