Skip to content

Commit f0c7443

Browse files
committed
Update README
Better readme with example usage
1 parent 4af7687 commit f0c7443

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

README.md

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
[![GNU license v3.0](https://img.shields.io/badge/License-GNU-green.svg)](https://github.com/svey-xyz/simple-shader-component/LICENSE)
77
[![Bundle size](https://img.shields.io/bundlejs/size/simple-shader-component)](https://github.com/svey-xyz/simple-shader-component)
88

9-
109
## Description
1110
This library provides a simple to use webgl shader component, with included framework wrappers and strong typing. Shaders are powerful tools and can be used to add a lot of visual interest to your web project. However, setting-up many shader tools can be complicated and add a large bundle to your project, this library provides a simple core package to make rendering a shader in your DOM incredibly easy.
1211

@@ -16,7 +15,61 @@ While libraries like [three.js](https://threejs.org/) are amazing, not every pro
1615

1716
## Installation & Usage
1817
```zsh
19-
npm i simple-shader-component
18+
bun i simple-shader-component
19+
```
20+
21+
The base class and types are all exported from the root of the package.
22+
```ts
23+
import { Shader } from 'simple-shader-component'
24+
```
25+
26+
Framework wrappers are available under sub exports.
27+
```ts
28+
import { SimpleShaderCanvas } from 'simple-shader-component/react'
29+
```
30+
***Note**: Right now only the React wrapper is included. If there's interest, or if my workflow requires it, I will add additional wrappers.*
31+
32+
### Basic Usage
33+
'use client' directive is required to set hooks. If you're not using hooks it can be done in a server component.
34+
35+
```tsx
36+
'use client'
37+
38+
import { SimpleShaderCanvas } from 'simple-shader-component/react'
39+
import { Shader, UniformValue } from 'simple-shader-component'
40+
41+
// Define shaders as strings, or import from a glsl file with a loader
42+
import { frag, vert } from '.your-custom-shaders'
43+
44+
// Example uniform to track elapsed time in the shader
45+
const uniforms: Array<UniformValue> = [
46+
{
47+
name: 'u_time',
48+
type: "float",
49+
value: 0.0
50+
}
51+
]
52+
53+
export const Test = () => {
54+
// Simple hook to update a uniform
55+
const loopLogic = (shader: Shader) => {
56+
const uTime: UniformValue = {
57+
name: 'u_time',
58+
type: 'float',
59+
value: shader.getElapsedTime() // Uses exposed method of shader
60+
}
61+
shader.setUniform(uTime)
62+
}
63+
64+
// Define which method to attach the hook to
65+
const loopHook = {
66+
methodName: MethodName.LOOP,
67+
hook: loopLogic
68+
}
69+
70+
// Render component with args
71+
return <SimpleShaderCanvas args={{ uniforms: uniforms, fragShader: frag, vertShader: vert, hooks: [loopHook] }} />
72+
}
2073
```
21-
*Coming soon*
2274

75+
*More coming soon*

0 commit comments

Comments
 (0)