fragmentcolor 0.12.5 → 0.12.7

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # FragmentColor for JavaScript (WASM)
2
2
 
3
- [FragmentColor](https://fragmentcolor.org) is a cross‑platform GPU programming library implemented in Rust and wgpu, compiled to WebAssembly for JavaScript.
3
+ [FragmentColor](https://fragmentcolor.org) is a universal GPU abstraction for Rust, JavaScript, Python, Swift, and Kotlin. This package is the JavaScript binding, compiled to WebAssembly.
4
4
 
5
5
  This README is specific to the npm package. For Rust usage, see the repository README.md. For Python, see `platforms/python/README.md` in the repository.
6
6
 
@@ -23,19 +23,16 @@ yarn add fragmentcolor
23
23
  import init, { Renderer, Shader, Pass } from "fragmentcolor";
24
24
 
25
25
  async function start() {
26
- // Initializes the WASM module
26
+ // Loads the WebAssembly module
27
27
  await init();
28
28
 
29
- // Initializes a renderer and a target compatible with the given canvas
29
+ // A renderer and a target that draws into the canvas
30
30
  const canvas = document.getElementById("my-canvas");
31
31
  const renderer = new Renderer();
32
32
  const target = await renderer.createTarget(canvas);
33
33
 
34
- // You can pass the shader as a source string, a registry slug, or an
35
- // https URL pointing at a .wgsl file.
36
- const circle = await Shader.fetch("./path/to/circle.wgsl");
37
- const triangle = await Shader.fetch("https://fragmentcolor.org/shaders/sdf2d/circle.wgsl");
38
- const shader = new Shader(`
34
+ // A shader from WGSL source. Its uniforms are ready to set by name.
35
+ const background = new Shader(`
39
36
  struct VertexOutput {
40
37
  @builtin(position) coords: vec4<f32>,
41
38
  }
@@ -47,17 +44,10 @@ async function start() {
47
44
  @group(0) @binding(0)
48
45
  var<uniform> my_struct: MyStruct;
49
46
 
50
- @group(0) @binding(1)
51
- var<uniform> my_vec2: vec2<f32>;
52
-
53
47
  @vertex
54
- fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> VertexOutput {
55
- const vertices = array(
56
- vec2( -1., -1.),
57
- vec2( 3., -1.),
58
- vec2( -1., 3.)
59
- );
60
- return VertexOutput(vec4<f32>(vertices[in_vertex_index], 0.0, 1.0));
48
+ fn vs_main(@builtin(vertex_index) index: u32) -> VertexOutput {
49
+ let vertices = array(vec2(-1., -1.), vec2(3., -1.), vec2(-1., 3.));
50
+ return VertexOutput(vec4<f32>(vertices[index], 0.0, 1.0));
61
51
  }
62
52
 
63
53
  @fragment
@@ -65,32 +55,49 @@ async function start() {
65
55
  return vec4<f32>(my_struct.my_field, 1.0);
66
56
  }
67
57
  `);
68
-
69
- // The library binds and updates the uniforms automatically
70
- shader.set("my_struct.my_field", [0.1, 0.8, 0.9]);
71
- shader.set("my_vec2", [1.0, 1.0]);
58
+ background.set("my_struct.my_field", [0.1, 0.8, 0.9]);
72
59
 
73
60
  // One shader is all you need to render
74
- renderer.render(circle, target);
75
-
76
- // But you can also combine multiple shaders in a render Pass
77
- const rpass = new Pass("single pass");
78
- rpass.addShader(circle);
79
- rpass.addShader(triangle);
80
- rpass.addShader(shader);
81
- renderer.render(rpass, target);
82
-
83
- // Finally, you can render an array of passes in order. No extra type needed.
84
- const passes = [rpass, new Pass("GUI pass")];
85
- renderer.render(passes, target);
86
-
87
- // To animate, simply update the uniforms in a loop
88
- function animate() {
89
- circle.set("position", [0.0, 0.0]);
61
+ renderer.render(background, target);
62
+
63
+ // Library functions join your own source, which calls them.
64
+ // On the web, Shader.fetch loads them from fragmentcolor.org.
65
+ const circle = await Shader.fetch([
66
+ "sdf2d/circle",
67
+ `
68
+ @group(0) @binding(0)
69
+ var<uniform> center: vec2<f32>;
70
+
71
+ @vertex
72
+ fn vs_main(@builtin(vertex_index) index: u32) -> @builtin(position) vec4<f32> {
73
+ let vertices = array(vec2(-1., -1.), vec2(3., -1.), vec2(-1., 3.));
74
+ return vec4<f32>(vertices[index], 0.0, 1.0);
75
+ }
76
+
77
+ @fragment
78
+ fn fs_main(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
79
+ if circle(pos.xy - center, 100.0) > 0.0 {
80
+ discard;
81
+ }
82
+ return vec4<f32>(1.0, 0.4, 0.2, 1.0);
83
+ }
84
+ `,
85
+ ]);
86
+
87
+ // Passes render in order, and the second one keeps what the first drew.
88
+ const circlePass = Pass.fromShader("circle", circle);
89
+ circlePass.loadPrevious();
90
+ const passes = [Pass.fromShader("background", background), circlePass];
91
+
92
+ // To animate, update the uniforms and render again on each frame
93
+ function animate(time) {
94
+ const size = target.size();
95
+ const x = size.width / 2 + Math.cos(time / 1000) * 100;
96
+ circle.set("center", [x, size.height / 2]);
90
97
  renderer.render(passes, target);
91
98
  requestAnimationFrame(animate);
92
99
  }
93
- animate();
100
+ requestAnimationFrame(animate);
94
101
  }
95
102
 
96
103
  start();
@@ -103,7 +110,7 @@ start();
103
110
  ./fc build_web # add --debug for a debug build
104
111
 
105
112
  # Run JS demos (Vite dev server) and open browser
106
- ./fc run_web repl # or: ./fc run_web multipass | ./fc run_web headless
113
+ ./fc run_web repl # or: ./fc run_web gallery | ./fc run_web visual
107
114
 
108
115
  # Manual alternative
109
116
  pnpm --dir examples/javascript install