-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBasic.ts
More file actions
72 lines (64 loc) · 1.45 KB
/
Basic.ts
File metadata and controls
72 lines (64 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const vertShader = `
struct Output {
@builtin(position) pos: vec4f,
@location(0) uv: vec2f,
}
@vertex
fn main(
@builtin(vertex_index) vertexIndex: u32,
) -> Output {
var pos = array<vec2f, 4>(
vec2(1, 1), // top-right
vec2(-1, 1), // top-left
vec2(1, -1), // bottom-right
vec2(-1, -1) // bottom-left
);
var uv = array<vec2f, 4>(
vec2(1., 1.), // top-right
vec2(0., 1.), // top-left
vec2(1., 0.), // bottom-right
vec2(0., 0.) // bottom-left
);
var out: Output;
out.pos = vec4f(pos[vertexIndex], 0.0, 1.0);
out.uv = uv[vertexIndex];
return out;
}
`
const fragShader = `
@fragment
fn main(
@location(0) uv: vec2f,
) -> @location(0) vec4f {
let red = floor(uv.x * f32(span.x)) / f32(span.x);
let green = floor(uv.y * f32(span.y)) / f32(span.y);
return vec4(red, green, blue, 1.0);
}
`
import {
d,
Lil,
uniform,
vertex,
fragment,
} from "../../browser.ts"
class Basic extends Lil {
@vertex v = vertShader
@fragment f = fragShader
@uniform(d.struct({
x: d.u32,
y: d.u32,
}))
accessor span = { x: 10, y: 20 }
@uniform(d.f32)
accessor blue = 0.5
}
const basic = new Basic()
const g = await basic.init(document.querySelector("canvas")!)
g.draw(4)
const tick = () => new Promise(requestAnimationFrame)
while (true) {
await tick()
basic.blue = Math.sin(Date.now()/1000)/2+0.5
g.draw(4)
}