Spaces:
Runtime error
Runtime error
Hariganesh Srinivasan
commited on
webgl
Browse files- .gitignore +2 -0
- test.py +112 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
flagged/
|
2 |
+
.python-version
|
test.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
_HTML = """
|
4 |
+
<html>
|
5 |
+
<body>
|
6 |
+
<canvas id="canvas" width="500" height="500"></canvas>
|
7 |
+
|
8 |
+
<script id="vertex" type="x-shader/x-vertex">
|
9 |
+
#version 300 es
|
10 |
+
|
11 |
+
in vec4 vertexPosition;
|
12 |
+
|
13 |
+
void main() {
|
14 |
+
gl_Position = vertexPosition;
|
15 |
+
}
|
16 |
+
</script>
|
17 |
+
|
18 |
+
<script id="fragment" type="x-shader/x-fragment">
|
19 |
+
#version 300 es
|
20 |
+
precision highp float;
|
21 |
+
|
22 |
+
uniform vec2 canvasSize;
|
23 |
+
uniform float time;
|
24 |
+
out vec4 fragColor;
|
25 |
+
|
26 |
+
void main() {
|
27 |
+
vec2 coord = gl_FragCoord.xy/canvasSize.xy;
|
28 |
+
coord = 2.*coord - 1.;
|
29 |
+
float scale = (sin(time) + 1.)/2.;
|
30 |
+
coord /= scale;
|
31 |
+
if (abs(coord.x) < 1. && abs(coord.y) < 1.) {
|
32 |
+
coord = (coord + 1.)/2.;
|
33 |
+
fragColor = vec4(coord.x, coord.y, 1.-coord.x, 1);
|
34 |
+
} else {
|
35 |
+
fragColor = vec4(1,1,1,1);
|
36 |
+
}
|
37 |
+
}
|
38 |
+
</script>
|
39 |
+
|
40 |
+
<script>
|
41 |
+
const canvas = document.getElementById("canvas");
|
42 |
+
const vertexCode = document.getElementById("vertex").textContent;
|
43 |
+
const fragmentCode = document.getElementById("fragment").textContent;
|
44 |
+
|
45 |
+
const gl = canvas.getContext("webgl2");
|
46 |
+
if (!gl) throw "WebGL2 not supported";
|
47 |
+
|
48 |
+
function createShader(shaderType, sourceCode) {
|
49 |
+
const shader = gl.createShader(shaderType);
|
50 |
+
gl.shaderSource(shader, sourceCode.trim());
|
51 |
+
gl.compileShader(shader);
|
52 |
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
53 |
+
throw gl.getShaderInfoLog(shader);
|
54 |
+
}
|
55 |
+
return shader;
|
56 |
+
}
|
57 |
+
|
58 |
+
const program = gl.createProgram();
|
59 |
+
gl.attachShader(program, createShader(gl.VERTEX_SHADER, vertexCode));
|
60 |
+
gl.attachShader(program, createShader(gl.FRAGMENT_SHADER, fragmentCode));
|
61 |
+
gl.linkProgram(program);
|
62 |
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
63 |
+
throw gl.getProgramInfoLog(program);
|
64 |
+
}
|
65 |
+
gl.useProgram(program);
|
66 |
+
|
67 |
+
const vertices = [
|
68 |
+
[-1, -1],
|
69 |
+
[1, -1],
|
70 |
+
[-1, 1],
|
71 |
+
[1, 1],
|
72 |
+
];
|
73 |
+
const vertexData = new Float32Array(vertices.flat());
|
74 |
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
|
75 |
+
gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.STATIC_DRAW);
|
76 |
+
|
77 |
+
const vertexPosition = gl.getAttribLocation(program, "vertexPosition");
|
78 |
+
gl.enableVertexAttribArray(vertexPosition);
|
79 |
+
gl.vertexAttribPointer(vertexPosition, 2, gl.FLOAT, false, 0, 0);
|
80 |
+
|
81 |
+
const canvasSizeUniform = gl.getUniformLocation(program, 'canvasSize');
|
82 |
+
gl.uniform2f(canvasSizeUniform, canvas.width, canvas.height);
|
83 |
+
|
84 |
+
const timeUniform = gl.getUniformLocation(program, 'time');
|
85 |
+
|
86 |
+
function draw() {
|
87 |
+
gl.uniform1f(timeUniform, performance.now() / 1000);
|
88 |
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, vertices.length);
|
89 |
+
requestAnimationFrame(draw);
|
90 |
+
}
|
91 |
+
draw();
|
92 |
+
</script>
|
93 |
+
</body>
|
94 |
+
</html>
|
95 |
+
"""
|
96 |
+
|
97 |
+
HTML = """
|
98 |
+
|
99 |
+
"""
|
100 |
+
|
101 |
+
|
102 |
+
def webgl():
|
103 |
+
return ("" + HTML + "")
|
104 |
+
|
105 |
+
|
106 |
+
demo = gr.Interface(
|
107 |
+
webgl,
|
108 |
+
None,
|
109 |
+
["html"],
|
110 |
+
)
|
111 |
+
|
112 |
+
demo.launch()
|