Use.GPU is run via the Live run-time, similar to React.
There are two ways to use it:
Note: If you just want to edit the existing demos, see @use-gpu/app.
Use.GPU embedded in a basic HTML page.
Examples
/examples/ts-vite - Empty canvas with vite./examples/ts-webpack - Empty canvas with webpack and babel.Steps
@use-gpu/workbench, @use-gpu/webgpu, @use-gpu/wgsl-loader).wgsl using @use-gpu/wgsl-loader.wasm modules onReact shim to make <JSX> work correctlyrender() on your <App />e.g.
import React, { render } from '@use-gpu/live';
window.onload = async () => {
const { App } = await import('./app');
render(<App />);
The await import(…) on the outside allows synchronous import * from '.wasm' to work on the inside.
Use.GPU has convenient React ↔︎ Live portals in both directions. See @use-gpu/react for more info.
Steps
@use-gpu/react, @use-gpu/workbench, @use-gpu/webgpu, @use-gpu/wgsl-loader)..wgsl to JS using @use-gpu/wgsl-loader.wasm imports<LiveCanvas> to insert a <canvas>. Will make a portal to Live.<AutoCanvas canvas={canvas} />).On the React side:
import React, { FC } from 'react';
import { LiveCanvas } from '@use-gpu/react';
import { Component } from './component';
// This is a React component
const MyCanvas: FC = () => {
return (
<LiveCanvas>
// These are Live children
{(canvas) => <Component canvas={canvas} />}
</LiveCanvas>
);
}
On the Live side:
import type { LC, PropsWithChildren } from '@use-gpu/live';
type ComponentProps = {
canvas: HTMLCanvasElement,
};
// This is a Live component
export const Component: LC<ComponentProps> = (props: PropsWithChildren<ComponentProps>) => {
const {canvas} = props;
// ...
return null;
};
To use custom .wgsl files as shaders, your build process must convert them to JS. This can be done using the included @use-gpu/wgsl-loader, if your build system is compatible (webpack, node or rollup). The built-in shaders in @use-gpu/wgsl are pre-transpiled to JS.
e.g. webpack config:
{
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: [/node_modules/],
loader: 'babel-loader',
},
{
test: /\.wgsl$/i,
use: ['@use-gpu/wgsl-loader'],
},
],
},
}
Live has support for hot module reload with both Webpack (via webpack-dev-server) and Vite.
In your webpack.config.js:
entry point:{
entry: [
'./src/index.tsx',
'webpack-dev-server/client/index.js?hot=true&live-reload=true',
],
}
{
devServer: {
// ...
hot: true,
},
}
App component in hot(..., module) if using Webpack, or hot(..., import.meta) if using Vite:import React, { hot } from '@use-gpu/live';
export const App = hot(() => {
// ...
}, module); // or `import.meta` if using Vite