@playcanvas/react
Version:
A React renderer for PlayCanvas – build interactive 3D applications using React's declarative paradigm.
74 lines • 3.06 kB
JavaScript
"use client";
import { useComponent } from "../hooks/index.js";
import { Asset, Entity, GSplatInstance, ShaderMaterial } from "playcanvas";
import { validatePropsWithDefaults, createComponentDefinition, getStaticNullApplication } from "../utils/validation.js";
/**
* 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 = (props) => {
const safeProps = validatePropsWithDefaults(props, componentDefinition);
useComponent("gsplat", safeProps, componentDefinition.schema);
return null;
};
const componentDefinition = createComponentDefinition("GSplat", () => new Entity("mock-gsplat", getStaticNullApplication()).addComponent('gsplat'), (component) => component.system.destroy(), { apiName: "GSplatComponent" });
componentDefinition.schema = {
...componentDefinition.schema,
asset: {
validate: (val) => val instanceof Asset,
errorMsg: (val) => `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) => typeof val === 'boolean',
errorMsg: (val) => `Invalid value for prop "unified": "${JSON.stringify(val)}". Expected a boolean.`,
default: false,
apply: (instance, props, key) => {
const value = props[key];
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) => val === null || val instanceof ShaderMaterial,
errorMsg: (val) => `Invalid value for prop "material": "${val}". Expected a ShaderMaterial or null.`,
default: null,
apply: (instance, props, key) => {
const value = props[key];
if (instance.material === value) {
return;
}
if (value) {
instance.material = value;
}
}
},
instance: {
validate: (val) => val === null || val instanceof GSplatInstance,
errorMsg: (val) => `Invalid value for prop "instance": "${val}". Expected a GSplatInstance or null.`,
default: null,
apply: (instance, props, key) => {
const value = props[key];
if (instance.instance === value) {
return;
}
instance.instance = value;
}
}
};
//# sourceMappingURL=GSplat.js.map