-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathgolwebcam.tsx
More file actions
133 lines (124 loc) · 3.03 KB
/
golwebcam.tsx
File metadata and controls
133 lines (124 loc) · 3.03 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
import React, { useState, useRef, useCallback, useEffect, useContext } from "react";
import { Bus, Uniform, Node, Shaders, GLSL } from "gl-react";
import { Surface } from "gl-react-dom";
import { Video, VideoContext } from "./video";
import { golShaders } from "./gol";
import { useTimeLoop } from "../hooks/useTimeLoop";
const extraShaders = Shaders.create({
Display: {
frag: GLSL`
precision highp float;
varying vec2 uv;
uniform sampler2D gol, webcam;
uniform float webcamRatio;
void main () {
vec2 aspect = vec2(max(1.0, 1.0/webcamRatio), max(1.0, webcamRatio));
vec2 p = uv * aspect + (1.0 - aspect) / 2.0;
if (0.0>p.x||1.0<p.x||0.0>p.y||1.0<p.y) {
gl_FragColor = vec4(0.0);
}
else {
vec3 webcamC = texture2D(webcam, p).rgb;
gl_FragColor = vec4(
vec3(1.0) * texture2D(gol, p).r +
webcamC * mix(step(0.5, webcamC.r), 0.9, 0.2),
1.0);
}
}
`,
},
});
const Display = ({ gol, webcam }: { gol: any; webcam: any }) => (
<Node
shader={extraShaders.Display}
uniformsOptions={{ gol: { interpolation: "nearest" } }}
uniforms={{
gol,
webcam,
webcamRatio: Uniform.textureSizeRatio(webcam),
}}
/>
);
function GameOfLifeWebcam({
size,
reset,
resetTexture,
}: {
size: number;
reset: boolean;
resetTexture: any;
}) {
const { tick } = useTimeLoop(4);
return (
<Node
shader={golShaders.GameOfLife}
width={size}
height={size}
backbuffering
sync
uniforms={{
t: reset ? resetTexture : Uniform.Backbuffer,
size,
}}
/>
);
}
const WebCamSource = () => {
const videoRef = useContext(VideoContext);
useEffect(() => {
let stream: MediaStream | null = null;
if (videoRef?.current && navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({ video: true }).then((s) => {
stream = s;
if (videoRef.current) {
videoRef.current.srcObject = stream;
}
}).catch(() => {});
}
return () => {
if (stream) {
stream.getTracks().forEach((track) => track.stop());
}
};
}, [videoRef]);
return null;
};
export default function GOLWebcam() {
const [reset, setReset] = useState(false);
const [size, setSize] = useState(32);
const webcamRef = useRef<any>(null);
const onMouseDown = useCallback(() => {
setReset(true);
setSize(Math.floor(10 + 200 * Math.random() * Math.random()));
}, []);
const onMouseUp = useCallback(() => {
setReset(false);
}, []);
return (
<Surface
style={{ cursor: "pointer" }}
width={400}
height={400}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
>
<Bus ref={webcamRef}>
{(redraw: any) => (
<Video onFrame={redraw} autoPlay>
<WebCamSource />
</Video>
)}
</Bus>
<Display
gol={
<GameOfLifeWebcam
reset={reset}
size={size}
resetTexture={() => webcamRef.current}
/>
}
webcam={() => webcamRef.current}
/>
</Surface>
);
}