-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (88 loc) · 3.36 KB
/
index.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { fetchShader } from './src/ShaderUtils.js';
import Material from './src/Material.js';
class Main {
constructor() {
this.canvas = document.querySelector('canvas');
this.scaleValue = 250;
this.offset = { x: 0, y: 0 };
this.moving = false;
this.start = { x: 0, y: 0 };
}
async initialize() {
if (!navigator.gpu) {
console.error("WebGPU not supported on this browser.");
return;
}
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const context = this.canvas.getContext('webgpu');
const format = navigator.gpu.getPreferredCanvasFormat();
context.configure({
device,
format,
alphaMode: 'opaque',
});
this.device = device;
this.context = context;
this.format = format;
await this.setupShaders();
this.createMaterial();
this.setupEventListeners();
this.resize();
}
async setupShaders() {
this.vertexShaderCode = await fetchShader('./src/shader/vertex.wgsl');
this.fragmentShaderCode = await fetchShader('./src/shader/fragment.wgsl');
}
createMaterial() {
this.material = new Material(this.device, this.vertexShaderCode, this.fragmentShaderCode, {
0: { size: 8, usage: GPUBufferUsage.UNIFORM }, // resolution
1: { size: 4, usage: GPUBufferUsage.UNIFORM }, // scale
2: { size: 8, usage: GPUBufferUsage.UNIFORM }, // offset
});
this.material.createPipeline(this.format);
}
setupEventListeners() {
window.addEventListener('resize', () => this.resize());
this.canvas.addEventListener('mousedown', (event) => this.onMouseDown(event));
this.canvas.addEventListener('mousemove', (event) => this.onMouseMove(event));
this.canvas.addEventListener('mouseup', () => this.onMouseUp());
this.canvas.addEventListener('wheel', (event) => this.onWheel(event));
}
resize() {
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.material.updateUniform('0', new Float32Array([this.canvas.width, this.canvas.height]));
this.material.updateUniform('1', new Float32Array([this.scaleValue]));
this.render();
}
onMouseDown(event) {
this.start.x = event.pageX;
this.start.y = event.pageY;
this.moving = true;
}
onMouseMove(event) {
if (this.moving) {
this.offset.x += event.movementX / this.scaleValue;
this.offset.y += event.movementY / this.scaleValue;
this.material.updateUniform('2', new Float32Array([this.offset.x, this.offset.y]));
this.render();
}
}
onMouseUp() {
this.moving = false;
}
onWheel(event) {
this.scaleValue -= (event.deltaY / 100) * (this.scaleValue / 5);
this.material.updateUniform('1', new Float32Array([this.scaleValue]));
this.resize();
}
render() {
const commandEncoder = this.device.createCommandEncoder();
const textureView = this.context.getCurrentTexture().createView();
this.material.render(commandEncoder, textureView);
this.device.queue.submit([commandEncoder.finish()]);
}
}
const app = new Main();
app.initialize();