-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathViewTransform.tsx
More file actions
150 lines (130 loc) · 3.72 KB
/
ViewTransform.tsx
File metadata and controls
150 lines (130 loc) · 3.72 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
import React, { useEffect, useRef } from "react";
import { StyleSheet, View, PixelRatio, Text } from "react-native";
import Animated, {
useAnimatedStyle,
useSharedValue,
} from "react-native-reanimated";
import type { CanvasRef } from "react-native-wgpu";
import { Canvas } from "react-native-wgpu";
import { redFragWGSL, triangleVertWGSL } from "../Triangle/triangle";
export function ViewTransform() {
const ref = useRef<CanvasRef>(null);
const rotation = useSharedValue(0);
const id = useRef<ReturnType<typeof setInterval> | null>(null);
useEffect(() => {
const _id = setInterval(() => {
rotation.value += 0.05 % (Math.PI * 2);
}, 30);
id.current = _id;
return () => {
if (id.current) {
clearInterval(id.current);
}
};
}, [rotation]);
const animatedStyle = useAnimatedStyle(() => {
const angle = rotation.value * 10;
return {
transform: [{ perspective: 1000 }, { rotateY: `${angle}deg` }],
};
});
useEffect(() => {
(async () => {
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
throw new Error("No adapter");
}
const device = await adapter.requestDevice();
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
const context = ref.current!.getContext("webgpu")!;
const canvas = context.canvas as HTMLCanvasElement;
canvas.width = canvas.clientWidth * PixelRatio.get();
canvas.height = canvas.clientHeight * PixelRatio.get();
if (!context) {
throw new Error("No context");
}
context.configure({
device,
format: presentationFormat,
alphaMode: "premultiplied",
});
const pipeline = device.createRenderPipeline({
layout: "auto",
vertex: {
module: device.createShaderModule({
code: triangleVertWGSL,
}),
entryPoint: "main",
},
fragment: {
module: device.createShaderModule({
code: redFragWGSL,
}),
entryPoint: "main",
targets: [
{
format: presentationFormat,
},
],
},
primitive: {
topology: "triangle-list",
},
});
const commandEncoder = device.createCommandEncoder();
const textureView = context.getCurrentTexture().createView();
const renderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: textureView,
clearValue: [0, 0, 0, 0],
loadOp: "clear",
storeOp: "store",
},
],
};
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(pipeline);
passEncoder.draw(3);
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);
context.present();
})();
}, [ref]);
return (
<View style={styles.container}>
{/* @ts-expect-error React 19 + Reanimated typing issue */}
<Animated.View style={[styles.canvasContainer, animatedStyle]}>
<Canvas ref={ref} style={styles.canvas} transparent />
<Text style={styles.text}>AAAAAAAAAAAAAAAABBBBBBBBBBBBB</Text>
</Animated.View>
<View style={styles.redBackground} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: "#2c2c2cCC",
},
canvasContainer: {
width: 300,
height: 300,
},
canvas: {
width: 300,
height: 300,
},
redBackground: {
position: "absolute",
width: 300,
height: 300,
backgroundColor: "#ff0000cc",
},
text: {
color: "#FFFFFF",
fontSize: 20,
},
});