-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathComputeToy.tsx
More file actions
182 lines (157 loc) · 4.99 KB
/
Copy pathComputeToy.tsx
File metadata and controls
182 lines (157 loc) · 4.99 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { useCallback, useEffect, useRef, useState } from "react";
import type { CanvasRef } from "react-native-webgpu";
import { Canvas } from "react-native-webgpu";
import { Platform, useWindowDimensions } from "react-native";
import { useSharedValue } from "react-native-reanimated";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
import { ComputeEngine } from "./engine";
export interface ComputeToy {
shader: string;
uniforms: Record<string, number>;
}
// Use a CORS proxy for web to bypass CORS restrictions
const getCorsProxyUrl = (url: string) => {
return Platform.OS === "web"
? `https://corsproxy.io/?${encodeURIComponent(url)}`
: url;
};
export const useComputeToy = (toyId: number) => {
const [props, setProps] = useState<ComputeToy | null>(null);
useEffect(() => {
(async () => {
const shaderURL = getCorsProxyUrl(
`https://compute.toys/view/${toyId}/wgsl`,
);
const uniformsURL = getCorsProxyUrl(
`https://compute.toys/view/${toyId}/json`,
);
// Execute both fetch requests in parallel
const [shaderResponse, uniformsResponse] = await Promise.all([
fetch(shaderURL),
fetch(uniformsURL),
]);
// Process the responses in parallel
const [shader, uniformsJSON] = await Promise.all([
shaderResponse.text(),
uniformsResponse.json(),
]);
const uniforms: Record<string, number> = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
uniformsJSON.body.uniforms.forEach((uniform: any) => {
uniforms[uniform.name] = uniform.value;
});
setProps({ shader, uniforms });
})();
}, [toyId]);
return props;
};
export interface ComputeToyProps {
toy: ComputeToy;
}
export const ComputeToy = ({ toy: { shader, uniforms } }: ComputeToyProps) => {
const ref = useRef<CanvasRef>(null);
const mouse = useSharedValue({ pos: { x: 0, y: 0 }, click: false });
const { width, height } = useWindowDimensions();
const [engine, setEngine] = useState<ComputeEngine | null>(null);
const animationRef = useRef<number | null>(null);
const lastTimeRef = useRef<number>(0);
// Initialize WebGPU and the compute engine
useEffect(() => {
const initWebGPU = async () => {
try {
// Create the compute engine
await ComputeEngine.create();
const eng = ComputeEngine.getInstance();
// Set the canvas surface
if (ref.current) {
eng.setSurface(ref.current!);
// Set the canvas size based on your CANVAS constants
// TODO: use PixelRatio.get()?
eng.resize(width, height, 1);
// Initialize render state
eng.reset();
// Set callbacks for shader compilation
eng.onSuccess((entryPoints) => {
console.log(
"Shader compiled successfully with entry points:",
entryPoints,
);
});
eng.onError((message, row, col) => {
console.error(`Shader error at ${row}:${col} - ${message}`);
});
eng.setCustomFloats(
Object.keys(uniforms),
new Float32Array(Object.values(uniforms)),
);
// Process and compile the shader
const preprocessed = await eng.preprocess(shader);
if (preprocessed) {
await eng.compile(preprocessed);
}
setEngine(eng);
}
} catch (error) {
console.error("Failed to initialize WebGPU:", error);
}
};
initWebGPU();
return () => {
if (animationRef.current !== null) {
cancelAnimationFrame(animationRef.current);
}
};
}, [height, ref, shader, uniforms, width]);
// Animation/render loop
const renderLoop = useCallback(
(timestamp: number) => {
if (!engine) {
return;
}
// Calculate time delta
const delta = lastTimeRef.current
? (timestamp - lastTimeRef.current) / 1000
: 0;
lastTimeRef.current = timestamp;
// Update time uniforms
engine.setTimeElapsed(timestamp / 1000);
engine.setTimeDelta(delta);
if (mouse) {
engine.setMousePos(mouse.value.pos.x, mouse.value.pos.y);
engine.setMouseClick(mouse.value.click);
}
// Render frame
engine.render();
// Schedule next frame
animationRef.current = requestAnimationFrame(renderLoop);
},
[engine, mouse],
);
useEffect(() => {
renderLoop(new Date().getTime());
}, [renderLoop]);
const gesture = Gesture.Pan()
.onChange((e) => {
mouse.value = {
pos: {
x: e.absoluteX / width,
y: e.absoluteY / height,
},
click: true,
};
})
.onEnd((e) => {
mouse.value = {
pos: {
x: e.absoluteX / width,
y: e.absoluteY / height,
},
click: false,
};
});
return (
<GestureDetector gesture={gesture}>
<Canvas ref={ref} style={{ width, height }} />
</GestureDetector>
);
};