@playcanvas/react
Version:
A React renderer for PlayCanvas – build interactive 3D applications using React's declarative paradigm.
109 lines (108 loc) • 3.61 kB
TypeScript
import { Asset } from "playcanvas";
/**
* Result of an asset loading operation
*/
export interface AssetResult {
/** The loaded asset, or null if not loaded or failed */
asset: Asset | null;
/** Whether the asset is currently loading */
loading: boolean;
/** Error message if loading failed, or null if successful */
error: string | null;
}
/**
* Simple hook to fetch an asset from the asset registry.
*
* @param src - The source URL of the asset.
* @param type - The type of the asset (must be one of: texture, gsplat, container, model).
* @param props - Additional properties to pass to the asset loader.
* @returns An object containing the asset, loading state, and any error.
*
* @example
* ```tsx
* const { asset, loading, error } = useAsset('model.glb', 'container');
*
* if (loading) return <LoadingSpinner />;
* if (error) return <ErrorMessage message={error} />;
* if (!asset) return null;
*
* return <Render asset={asset} />;
* ```
*/
export declare const useAsset: (src: string, type: string, props?: Record<string, unknown>) => AssetResult;
/**
* Simple hook to fetch a splat asset from the asset registry.
*
* @param src - The source URL of the splat asset.
* @param props - Additional properties to pass to the asset loader.
* @returns An object containing the asset, loading state, and any error.
*
* @example
* ```tsx
* const { asset, loading, error } = useSplat('splat.ply');
*
* if (loading) return <LoadingSpinner />;
* if (error) return <ErrorMessage message={error} />;
* if (!asset) return null;
*
* return <GSplat asset={asset} />;
* ```
*/
export declare const useSplat: (src: string, props?: Record<string, unknown>) => AssetResult;
/**
* Simple hook to fetch a texture asset from the asset registry.
*
* @param src - The source URL of the texture asset.
* @param props - Additional properties to pass to the asset loader.
* @returns An object containing the asset, loading state, and any error.
*
* @example
* ```tsx
* const { asset, loading, error } = useTexture('texture.jpg');
*
* if (loading) return <LoadingSpinner />;
* if (error) return <ErrorMessage message={error} />;
* if (!asset) return null;
*
* return <Material map={asset.resource} />;
* ```
*/
export declare const useTexture: (src: string, props?: Record<string, unknown>) => AssetResult;
/**
* Simple hook to load an environment atlas texture asset.
*
* @param src - The source URL of the environment atlas texture.
* @param props - Additional properties to pass to the asset loader.
* @returns An object containing the asset, loading state, and any error.
*
* @example
* ```tsx
* 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} />;
* ```
*/
export declare const useEnvAtlas: (src: string, props?: Record<string, unknown>) => AssetResult;
/**
* Simple hook to load a 3D model asset (GLB/GLTF).
*
* @param src - The source URL of the 3D model.
* @param props - Additional properties to pass to the asset loader.
* @returns An object containing the asset, loading state, and any error.
*
* @example
* ```tsx
* const { asset, loading, error } = useModel('model.glb');
*
* if (loading) return <LoadingSpinner />;
* if (error) return <ErrorMessage message={error} />;
* if (!asset) return null;
*
* return <Container asset={asset} />;
* ```
*/
export declare const useModel: (src: string, props?: Record<string, unknown>) => AssetResult;