-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathscreen_texture.rs
84 lines (65 loc) · 1.9 KB
/
screen_texture.rs
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
use macroquad::prelude::*;
#[macroquad::main("Texture")]
async fn main() {
let texture: Texture2D = load_texture("examples/chess.png").await.unwrap();
let lens_material = load_material(
ShaderSource::Glsl {
vertex: LENS_VERTEX_SHADER,
fragment: LENS_FRAGMENT_SHADER,
},
MaterialParams {
uniforms: vec![UniformDesc::new("Center", UniformType::Float2)],
..Default::default()
},
)
.unwrap();
loop {
clear_background(WHITE);
draw_texture_ex(
&texture,
0.0,
0.0,
WHITE,
DrawTextureParams {
dest_size: Some(vec2(screen_width(), screen_height())),
..Default::default()
},
);
let lens_center = mouse_position();
lens_material.set_uniform("Center", lens_center);
gl_use_material(&lens_material);
draw_circle(lens_center.0, lens_center.1, 250.0, RED);
gl_use_default_material();
next_frame().await
}
}
const LENS_FRAGMENT_SHADER: &'static str = r#"#version 100
precision lowp float;
varying vec2 uv;
varying vec2 uv_screen;
varying vec2 center;
uniform sampler2D _ScreenTexture;
void main() {
float gradient = length(uv);
vec2 uv_zoom = (uv_screen - center) * gradient + center;
gl_FragColor = texture2D(_ScreenTexture, uv_zoom);
}
"#;
const LENS_VERTEX_SHADER: &'static str = "#version 100
attribute vec3 position;
attribute vec2 texcoord;
varying lowp vec2 center;
varying lowp vec2 uv;
varying lowp vec2 uv_screen;
uniform mat4 Model;
uniform mat4 Projection;
uniform vec2 Center;
void main() {
vec4 res = Projection * Model * vec4(position, 1);
vec4 c = Projection * Model * vec4(Center, 0, 1);
uv_screen = res.xy / 2.0 + vec2(0.5, 0.5);
center = c.xy / 2.0 + vec2(0.5, 0.5);
uv = texcoord;
gl_Position = res;
}
";