Skip to content

Commit fb55b62

Browse files
committed
Add a points example
1 parent e8e1d1b commit fb55b62

File tree

8 files changed

+438
-0
lines changed

8 files changed

+438
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
struct Vertex {
2+
@location(0) position: vec4f,
3+
};
4+
5+
struct Uniforms {
6+
matrix: mat4x4f,
7+
resolution: vec2f,
8+
size: f32,
9+
};
10+
11+
struct VSOutput {
12+
@builtin(position) position: vec4f,
13+
@location(0) texcoord: vec2f,
14+
};
15+
16+
@group(0) @binding(0) var<uniform> uni: Uniforms;
17+
18+
@vertex fn vs(
19+
vert: Vertex,
20+
@builtin(vertex_index) vNdx: u32,
21+
) -> VSOutput {
22+
let points = array(
23+
vec2f(-1, -1),
24+
vec2f( 1, -1),
25+
vec2f(-1, 1),
26+
vec2f(-1, 1),
27+
vec2f( 1, -1),
28+
vec2f( 1, 1),
29+
);
30+
var vsOut: VSOutput;
31+
let pos = points[vNdx];
32+
let clipPos = uni.matrix * vert.position;
33+
let pointPos = vec4f(pos * uni.size / uni.resolution, 0, 0);
34+
vsOut.position = clipPos + pointPos;
35+
vsOut.texcoord = pos * 0.5 + 0.5;
36+
return vsOut;
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
struct Vertex {
2+
@location(0) position: vec4f,
3+
};
4+
5+
struct Uniforms {
6+
matrix: mat4x4f,
7+
resolution: vec2f,
8+
size: f32,
9+
};
10+
11+
struct VSOutput {
12+
@builtin(position) position: vec4f,
13+
@location(0) texcoord: vec2f,
14+
};
15+
16+
@group(0) @binding(0) var<uniform> uni: Uniforms;
17+
18+
@vertex fn vs(
19+
vert: Vertex,
20+
@builtin(vertex_index) vNdx: u32,
21+
) -> VSOutput {
22+
let points = array(
23+
vec2f(-1, -1),
24+
vec2f( 1, -1),
25+
vec2f(-1, 1),
26+
vec2f(-1, 1),
27+
vec2f( 1, -1),
28+
vec2f( 1, 1),
29+
);
30+
var vsOut: VSOutput;
31+
let pos = points[vNdx];
32+
let clipPos = uni.matrix * vert.position;
33+
let pointPos = vec4f(pos * uni.size / uni.resolution * clipPos.w, 0, 0);
34+
vsOut.position = clipPos + pointPos;
35+
vsOut.texcoord = pos * 0.5 + 0.5;
36+
return vsOut;
37+
}

sample/points/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<title>webgpu-samples: points</title>
7+
<style>
8+
html, body {
9+
margin: 0; /* remove default margin */
10+
height: 100%; /* make body fill the browser window */
11+
display: flex;
12+
place-content: center center;
13+
}
14+
canvas {
15+
width: 600px;
16+
height: 600px;
17+
max-width: 100%;
18+
display: block;
19+
}
20+
</style>
21+
<script defer src="main.js" type="module"></script>
22+
<script defer type="module" src="../../js/iframe-helper.js"></script>
23+
</head>
24+
<body>
25+
<canvas></canvas>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)