R2 Core Shaders
R2Refraction.h
Go to the documentation of this file.
1 #ifndef R2_REFRACTION_H
2 #define R2_REFRACTION_H
3 
4 /// \file R2Refraction.h
5 /// \brief Functions and types to render generic refraction.
6 
8  /// A scaling values for sampled refraction values
9  float scale;
10  /// A color by which to multiply the final refracted image
11  vec3 color;
12  /// An image to act as the source for refraction; typically an image of the
13  /// rendered scene.
14  sampler2D scene;
15  /// An image to mask sampling to avoid bleeding
16  sampler2D mask;
17 };
18 
19 ///
20 /// Calculate masked refraction using a given delta vector.
21 ///
22 /// @param refraction Basic refraction parameters
23 /// @param position_here_clip The clip-space coordinates of the surface
24 /// @param delta The delta vector
25 ///
26 /// @return A refracted sample
27 ///
28 
29 vec4
31  const R2_refraction_t refraction,
32  const vec4 position_here_clip,
33  const vec3 delta)
34 {
35  vec3 delta_scaled = delta * refraction.scale;
36 
37  vec3 position_here_ndc = vec3 (position_here_clip.xyz / position_here_clip.w);
38  vec2 position_here_uv = (position_here_ndc.xy + 1.0) * 0.5;
39 
40  vec3 position_there_ndc = position_here_ndc + delta_scaled;
41  vec2 position_there_uv = (position_there_ndc.xy + 1.0) * 0.5;
42 
43  vec4 scene_there = texture (refraction.scene, position_there_uv);
44  float mask = texture (refraction.mask, position_there_uv).x;
45  vec4 scene_here = texture (refraction.scene, position_here_uv);
46 
47  return mix (scene_here, scene_there, mask) * vec4 (refraction.color, 1.0);
48 }
49 
50 #endif // R2_REFRACTION_H
sampler2D mask
An image to mask sampling to avoid bleeding.
Definition: R2Refraction.h:16
float scale
A scaling values for sampled refraction values.
Definition: R2Refraction.h:9
vec3 color
A color by which to multiply the final refracted image.
Definition: R2Refraction.h:11
sampler2D scene
Definition: R2Refraction.h:14
vec4 R2_refractionMasked(const R2_refraction_t refraction, const vec4 position_here_clip, const vec3 delta)
Definition: R2Refraction.h:30