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
use crate::quad_ins::*;
use crate::*;
#[derive(Clone, Copy)]
#[repr(C)]
pub struct ImageIns {
base: QuadIns,
pt1: Vec2,
pt2: Vec2,
alpha: f32,
}
static SHADER: Shader = Shader {
build_geom: Some(QuadIns::build_geom),
code_to_concatenate: &[
Cx::STD_SHADER,
QuadIns::SHADER,
code_fragment!(
r#"
texture texture: texture2D;
instance pt1: vec2;
instance pt2: vec2;
instance alpha: float;
varying tc: vec2;
varying v_pixel: vec2;
//let dpi_dilate: float<Uniform>;
fn vertex() -> vec4 {
// return vec4(geom.x-0.5, geom.y, 0., 1.);
let shift: vec2 = -draw_scroll;
let clipped: vec2 = clamp(
geom * rect_size + rect_pos + shift,
draw_clip.xy,
draw_clip.zw
);
let pos = (clipped - shift - rect_pos) / rect_size;
tc = mix(pt1, pt2, pos);
v_pixel = clipped;
// only pass the clipped position forward
return camera_projection * vec4(clipped.x, clipped.y, draw_depth, 1.);
}
fn pixel() -> vec4 {
return vec4(sample2d(texture, tc.xy).rgb * alpha, alpha);
}"#
),
],
..Shader::DEFAULT
};
impl Default for ImageIns {
fn default() -> Self {
Self { base: Default::default(), pt1: vec2(0., 0.), pt2: vec2(1., 1.), alpha: 1.0 }
}
}
impl ImageIns {
pub fn draw(cx: &mut Cx, rect: Rect, texture_handle: TextureHandle) -> Area {
let area = cx.add_instances(&SHADER, &[ImageIns { base: QuadIns::from_rect(rect), ..Default::default() }]);
area.write_texture_2d(cx, "texture", texture_handle);
area
}
}