Asset Hooks
PlayCanvas React provides hooks for loading and managing different assets. These hooks simplify the process of loading assets and allow you to handle loading and error states.
The general signature of the hooks is:
const { asset, loading, error } = useModel(src, props);
Where src
is the source URL of the asset and props
are additional properties to pass to the asset loader.
import { useModel } from '@playcanvas/react/hooks';
export function RenderAsset() {
const { asset, loading, error } = useModel('model.glb');
if (loading) return <LoadingSpinner />;
if (error) return <ErrorMessage message={error} />;
if (!asset) return null;
return <Render asset={asset} />;
}
Every hooks returns an AssetResult
with the following properties
All Hooks
The following hooks are available:
useModel
- Load a 3D model assetuseSplat
- Load a Gaussian Splat assetuseTexture
- Load a texture assetuseEnvAtlas
- Load an environment atlas textureuseAsset
- Generic hook for loading any type of asset
useModel
A specialized hook for loading 3D model assets (GLB/GLTF). Pass the source URL of the model file and any additional properties to pass to the asset loader and use the resulting asset in the <Container/>
component.
import { useModel } from '@playcanvas/react/hooks';
export function RenderModel() {
const { asset, loading, error } = useModel('model.glb');
if (loading) return <LoadingSpinner />;
if (error) return <ErrorMessage message={error} />;
if (!asset) return null;
return <Render asset={asset} />;
}
useSplat
A specialized hook for loading Gaussian Splat assets. Pass the source URL of the splat file and any additional properties to pass to the asset loader and use the resulting asset in the <GSplat/>
component.
import { useSplat } from '@playcanvas/react/hooks';
export function RenderSplat() {
const { asset, loading, error } = useSplat('splat.ply');
if (loading) return <LoadingSpinner />;
if (error) return <ErrorMessage message={error} />;
if (!asset) return null;
return (<Entity>
<GSplat asset={asset} />
</Entity>);
}
See the <GSplat/>
component for more information, and the Gaussian Splat demo for a working example.
useTexture
A specialized hook for loading texture assets. Pass the source URL of the texture file and any additional properties to pass to the asset loader and use the resulting asset in any component that accepts a texture.
import { useTexture } from '@playcanvas/react/hooks';
import { useMaterial } from '@playcanvas/react/hooks';
export function RenderTexture() {
const { asset, loading, error } = useTexture('texture.jpg');
const material = useMaterial({ map: asset.resource });
if (loading) return <LoadingSpinner />;
if (error) return <ErrorMessage message={error} />;
if (!asset) return null;
return <Render type="box" material={material} />;
}
See the useMaterial hook and Render component for more information.
useEnvAtlas
A specialized hook for loading environment atlas textures. Pass the source URL of the texture file and any additional properties to pass to the asset loader and use the resulting asset in the <EnvAtlas/>
component.
import { useEnvAtlas } from '@playcanvas/react/hooks';
export function RenderEnvAtlas() {
const { asset, loading, error } = useEnvAtlas('env.jpg');
if (loading) return <LoadingSpinner />;
if (error) return <ErrorMessage message={error} />;
if (!asset) return null;
return <EnvAtlas asset={asset} />;
}
See the EnvAtlas component for more information.
useAsset
This is a generic hook for loading any type of asset. You can use it to load any asset type that PlayCanvas supports which is sometimes useful if you need to load an asset dynamically.
import { useAsset } from '@playcanvas/react/hooks';
const assetTypeToLoadType = {
'ply': useSplat,
'jpg': useTexture,
'png': useTexture,
'glb': useModel,
'gltf': useModel,
}
export function useDynamicAsset(src: string, props: Record<string, unknown> = {}) {
const mimeType = src.split('.').pop();
const loadType = assetTypeToLoadType[mimeType];
if (!loadType) {
throw new Error(`Unsupported asset type: ${mimeType}`);
}
return useAsset(src, loadType, props);
}