glre 0.21.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +150 -80
  2. package/dist/index.d.ts +524 -7
  3. package/dist/index.js +46 -13
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +46 -13
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/native.d.ts +6 -64
  8. package/dist/native.js +46 -13
  9. package/dist/native.js.map +1 -1
  10. package/dist/native.mjs +46 -13
  11. package/dist/native.mjs.map +1 -1
  12. package/dist/react.d.ts +7 -11
  13. package/dist/react.js +46 -13
  14. package/dist/react.js.map +1 -1
  15. package/dist/react.mjs +46 -13
  16. package/dist/react.mjs.map +1 -1
  17. package/dist/solid.d.ts +6 -63
  18. package/dist/solid.js +46 -13
  19. package/dist/solid.js.map +1 -1
  20. package/dist/solid.mjs +46 -13
  21. package/dist/solid.mjs.map +1 -1
  22. package/package.json +25 -60
  23. package/src/code/glsl.ts +186 -0
  24. package/src/code/wgsl.ts +170 -0
  25. package/src/index.ts +105 -0
  26. package/src/native.ts +24 -0
  27. package/src/node/cache.ts +67 -0
  28. package/src/node/const.ts +147 -0
  29. package/src/node/conv.ts +122 -0
  30. package/src/node/index.ts +96 -0
  31. package/src/node/node.ts +114 -0
  32. package/src/node/types.ts +101 -0
  33. package/src/node/uniform.ts +99 -0
  34. package/src/react.ts +18 -0
  35. package/src/solid.ts +15 -0
  36. package/src/types.ts +90 -0
  37. package/src/utils.ts +53 -0
  38. package/src/webgl/buffer.ts +78 -0
  39. package/src/webgl/index.ts +79 -0
  40. package/src/webgl/program.ts +61 -0
  41. package/src/webgl/shader.ts +60 -0
  42. package/src/webgl/texture.ts +93 -0
  43. package/src/webgpu/buffer.ts +96 -0
  44. package/src/webgpu/device.ts +91 -0
  45. package/src/webgpu/index.ts +40 -0
  46. package/src/webgpu/pipeline.ts +94 -0
  47. package/src/webgpu/texture.ts +139 -0
  48. package/dist/types-f429c8b1.d.ts +0 -79
package/README.md CHANGED
@@ -95,44 +95,30 @@ npm install glre
95
95
  </td>
96
96
  </table>
97
97
 
98
- ## PRs
99
-
100
- ###### welcome✨
101
-
102
98
  ## What does it look like?
103
99
 
104
100
  <table>
105
- <tr>
106
- <td width="7500px" align="center" valign="center">
107
- glre simplifies glsl programming via TypeScript, React, Solid and more (<a href="https://codesandbox.io/s/glre-basic-demo-ppzo3d">live demo</a>).
108
- </td>
109
- <td width="2500px" valign="top">
110
- <a href="https://codesandbox.io/s/glre-basic-demo-ppzo3d">
111
- <img alt="4" src="https://i.imgur.com/Lb3h9fs.jpg"></img>
112
- </a>
113
- </td>
114
- </tr>
101
+ <tbody>
102
+ <tr>
103
+ <td width="7500px" align="center" valign="center">
104
+ glre simplifies glsl programming via TypeScript, React, Solid and more (<a href="https://codesandbox.io/s/glre-basic-demo-ppzo3d">live demo</a>).
105
+ </td>
106
+ <td width="2500px" valign="top">
107
+ <a href="https://codesandbox.io/s/glre-basic-demo-ppzo3d">
108
+ <img alt="4" src="https://i.imgur.com/Lb3h9fs.jpg"></img>
109
+ </a>
110
+ </td>
111
+ </tr>
112
+ </tbody>
115
113
  </table>
116
114
 
117
115
  ```ts
118
116
  import { createRoot } from 'react-dom/client'
119
- import { useGL, useFrame } from 'glre/react'
120
-
121
- const fragment = `
122
- precision highp float;
123
- uniform vec2 iResolution;
124
- void main() {
125
- gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
126
- }
127
- `
117
+ import { useGL, vec4, fract, gl_FragCoord, iResolution } from 'glre/react'
118
+ const fragment = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1)
128
119
 
129
120
  const App = () => {
130
121
  const gl = useGL({ fragment })
131
- useFrame(() => {
132
- gl.clear()
133
- gl.viewport()
134
- gl.drawArrays()
135
- })
136
122
  return <canvas ref={gl.ref} />
137
123
  }
138
124
 
@@ -148,27 +134,19 @@ react-native supported ([codesandbox demo](https://codesandbox.io/p/sandbox/glre
148
134
 
149
135
  ```ts
150
136
  import { GLView } from 'expo-gl'
151
- import { useGL, useFrame } from 'glre/native'
152
137
  import { registerRootComponent } from 'expo'
153
-
154
- const fragment = `
155
- precision highp float;
156
- uniform vec2 iResolution;
157
- void main() {
158
- gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
159
- }
160
- `
138
+ import { useGL, vec4, fract, fragCoord, iResolution } from 'glre/native'
139
+ const fragment = vec4(fract(fragCoord.xy / iResolution), 0, 1)
161
140
 
162
141
  const App = () => {
163
- const self = useGL({ fragment })
164
- useFrame(() => {
165
- self.clear()
166
- self.viewport()
167
- self.drawArrays()
168
- self.gl.flush()
169
- self.gl.endFrameEXP()
142
+ const { gl, ref } = useGL({
143
+ fragment,
144
+ render() {
145
+ gl.flush()
146
+ gl.endFrameEXP()
147
+ },
170
148
  })
171
- return <GLView style={{ flex: 1 }} onContextCreate={self.ref} />
149
+ return <GLView style={{ flex: 1 }} onContextCreate={ref} />
172
150
  }
173
151
 
174
152
  registerRootComponent(App)
@@ -184,23 +162,11 @@ solid js supported ([codesandbox demo](https://codesandbox.io/p/sandbox/glre-sol
184
162
 
185
163
  ```ts
186
164
  import { render } from 'solid-js/web'
187
- import { onGL, onFrame } from 'glre/solid'
188
-
189
- const fragment = `
190
- precision highp float;
191
- uniform vec2 iResolution;
192
- void main() {
193
- gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
194
- }
195
- `
165
+ import { onGL, vec4, fract, fragCoord, iResolution } from 'glre/solid'
166
+ const fragment = vec4(fract(fragCoord.xy / iResolution), 0, 1)
196
167
 
197
168
  const App = () => {
198
169
  const gl = onGL({ fragment })
199
- onFrame(() => {
200
- gl.clear()
201
- gl.viewport()
202
- gl.drawArrays()
203
- })
204
170
  return <canvas ref={gl.ref} />
205
171
  }
206
172
 
@@ -218,33 +184,137 @@ pure js supported ([codesandbox demo](https://codesandbox.io/s/glre-basic-demo3-
218
184
  ```html
219
185
  <canvas id="id" style="top: 0; left: 0; position: fixed" />
220
186
  <script type="module">
221
- import self from 'https://cdn.skypack.dev/glre@latest'
222
- const fragment = `
223
- precision highp float;
224
- uniform vec2 iResolution;
225
- void main() {
226
- gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
227
- }
228
- `
229
- function setup() {
187
+ import createGL from 'https://cdn.skypack.dev/glre@latest'
188
+ import { vec4, fract, fragCoord, iResolution } from 'glre'
189
+ const fragment = vec4(fract(fragCoord.xy / iResolution), 0, 1)
190
+
191
+ function App() {
230
192
  const el = document.getElementById('id')
231
193
  const gl = el.getContext('webgl2')
232
- self({ el, gl, fragment })
233
- self.init()
234
- self.resize()
235
- draw()
194
+ createGL({ el, gl, fragment }).mount()
236
195
  }
237
- function draw() {
238
- requestAnimationFrame(draw)
239
- self.render()
240
- self.clear()
241
- self.viewport()
242
- self.drawArrays()
243
- }
244
- document.addEventListener('DOMContentLoaded', setup)
196
+ document.addEventListener('DOMContentLoaded', App)
245
197
  </script>
246
198
  ```
247
199
 
248
200
  </details>
249
201
  </samp>
250
202
  </strong>
203
+
204
+ ## Node System
205
+
206
+ glre now features a powerful node-based shader system inspired by Three.js Shading Language (TSL). This system allows you to write shaders using TypeScript-like syntax and automatically handles the conversion to both WebGL and WebGPU shaders.
207
+
208
+ The node system provides a declarative approach to shader creation, making your code more readable, maintainable, and portable across different rendering backends.
209
+
210
+ ### Node Types and Functions
211
+
212
+ The node system provides various types and functions that mirror GLSL functionality:
213
+
214
+ ```ts
215
+ // Basic types
216
+ import { float, int, vec2, vec3, vec4, mat3, mat4 } from 'glre'
217
+
218
+ // Built-in variables
219
+ import { gl_FragCoord, gl_Position, iResolution, iTime } from 'glre'
220
+
221
+ // Math functions
222
+ import { sin, cos, abs, pow, mix, clamp, normalize } from 'glre'
223
+
224
+ // Texture functions
225
+ import { texture, textureCube, sampler2D } from 'glre'
226
+ ```
227
+
228
+ ### Creating Custom Functions
229
+
230
+ You can define reusable shader functions using the `Fn` constructor:
231
+
232
+ ```ts
233
+ import { Fn, vec3, sin, cos, float } from 'glre'
234
+
235
+ // Define a function that creates a rotation matrix
236
+ const rotateY = Fn(([angle = float(0)]) => {
237
+ const s = sin(angle)
238
+ const c = cos(angle)
239
+ return mat3(c, 0, s, 0, 1, 0, -s, 0, c)
240
+ })
241
+
242
+ // Use the function in your shader
243
+ const rotatedPosition = rotateY(iTime).mul(position)
244
+ ```
245
+
246
+ ### Conditional Logic
247
+
248
+ The node system supports conditional operations:
249
+
250
+ ```ts
251
+ import { If, vec4, lessThan } from 'glre'
252
+
253
+ // Create a conditional color output
254
+ const color = vec4(1, 0, 0, 1).toVar()
255
+
256
+ If(position.y.lessThan(0.5), () => {
257
+ color.assign(vec4(0, 1, 0, 1))
258
+ })
259
+
260
+ // Use the color in your shader
261
+ const fragment = color
262
+ ```
263
+
264
+ ### Uniforms
265
+
266
+ The node system provides a powerful way to define and manage uniform values in your shaders:
267
+
268
+ ```ts
269
+ import { createRoot } from 'react-dom/client'
270
+ import { useGL } from 'glre/react'
271
+ import { uniform, vec3, vec4 } from 'glre'
272
+
273
+ const uRand = uniform(1.0)
274
+
275
+ // Create a simple pulsing color shader
276
+ const App = () => {
277
+ const gl = useGL({
278
+ fragment: vec4(vec3(uRand), 1.0),
279
+ loop() {
280
+ pulse.set(0.5 + 0.5 * Math.random())
281
+ },
282
+ })
283
+ return <canvas ref={gl.ref} />
284
+ }
285
+
286
+ createRoot(document.getElementById('root')).render(<App />)
287
+ ```
288
+
289
+ ### Attributes
290
+
291
+ Attributes allow you to define per-vertex data for your shaders:
292
+
293
+ ```ts
294
+ import { createRoot } from 'react-dom/client'
295
+ import { useGL } from 'glre/react'
296
+ import { attribute, vec3, vec4 } from 'glre'
297
+
298
+ // Define vertex positions
299
+ const positions = attribute(-1.0, -1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, 0.0)
300
+
301
+ // Create a shader that uses attributes
302
+ const App = () => {
303
+ const gl = useGL({ vertex: positions })
304
+ return <canvas ref={gl.ref} />
305
+ }
306
+
307
+ createRoot(document.getElementById('root')).render(<App />)
308
+ ```
309
+
310
+ ### WebGPU Support
311
+
312
+ The node system is designed to work with both WebGL and WebGPU, providing a seamless transition path as browsers adopt the new standard. Your shader code written with the node system will automatically compile to the appropriate shading language based on the available renderer.
313
+
314
+ ## PRs
315
+
316
+ ###### welcome✨
317
+
318
+ ## LICENSE
319
+
320
+ ###### MIT⚾️