Cleanup: DRW: Move ModelMatrix declaration to common_view_lib
[blender.git] / source / blender / draw / engines / workbench / shaders / workbench_prepass_vert.glsl
1
2 #ifndef HAIR_SHADER
3 in vec3 pos;
4 in vec3 nor;
5 in vec2 u; /* active texture layer */
6 #  ifdef V3D_SHADING_VERTEX_COLOR
7 in vec3 c; /* active color */
8 #  endif
9 #  define uv u
10 #else /* HAIR_SHADER */
11 #  ifdef V3D_SHADING_TEXTURE_COLOR
12 uniform samplerBuffer u; /* active texture layer */
13 #  endif
14 flat out float hair_rand;
15 #endif /* HAIR_SHADER */
16
17 #ifdef NORMAL_VIEWPORT_PASS_ENABLED
18 out vec3 normal_viewport;
19 #endif
20
21 #ifdef V3D_SHADING_TEXTURE_COLOR
22 out vec2 uv_interp;
23 #endif
24 #ifdef V3D_SHADING_VERTEX_COLOR
25 out vec3 vertexColor;
26 #endif
27
28 /* From http://libnoise.sourceforge.net/noisegen/index.html */
29 float integer_noise(int n)
30 {
31   n = (n >> 13) ^ n;
32   int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
33   return (float(nn) / 1073741824.0);
34 }
35
36 #ifdef V3D_SHADING_VERTEX_COLOR
37 vec3 srgb_to_linear_attr(vec3 c)
38 {
39   c = max(c, vec3(0.0));
40   vec3 c1 = c * (1.0 / 12.92);
41   vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4));
42   return mix(c1, c2, step(vec3(0.04045), c));
43 }
44 #endif
45
46 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand)
47 {
48   /* To "simulate" anisotropic shading, randomize hair normal per strand. */
49   tan = normalize(tan);
50   vec3 nor = normalize(cross(binor, tan));
51   // nor = normalize(mix(nor, -tan, rand * 0.1));
52   // float cos_theta = (rand * 2.0 - 1.0) * 0.2;
53   // float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta));
54   // nor = nor * sin_theta + binor * cos_theta;
55   return nor;
56 }
57
58 void main()
59 {
60 #ifdef HAIR_SHADER
61 #  ifdef V3D_SHADING_TEXTURE_COLOR
62   vec2 uv = hair_get_customdata_vec2(u);
63 #  endif
64   float time, thick_time, thickness;
65   vec3 world_pos, tan, binor;
66   hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0),
67                               ModelMatrixInverse,
68                               ViewMatrixInverse[3].xyz,
69                               ViewMatrixInverse[2].xyz,
70                               world_pos,
71                               tan,
72                               binor,
73                               time,
74                               thickness,
75                               thick_time);
76
77   hair_rand = integer_noise(hair_get_strand_id());
78   vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand);
79 #else
80   vec3 world_pos = point_object_to_world(pos);
81 #endif
82   gl_Position = point_world_to_ndc(world_pos);
83
84 #ifdef V3D_SHADING_TEXTURE_COLOR
85   uv_interp = uv;
86 #endif
87
88 #ifdef V3D_SHADING_VERTEX_COLOR
89 #  ifndef HAIR_SHADER
90   vertexColor = srgb_to_linear_attr(c);
91 #  endif
92 #endif
93
94 #ifdef NORMAL_VIEWPORT_PASS_ENABLED
95   normal_viewport = normal_object_to_view(nor);
96 #  ifndef HAIR_SHADER
97   normal_viewport = normalize(normal_viewport);
98 #  endif
99 #endif
100
101 #ifdef USE_WORLD_CLIP_PLANES
102   world_clip_planes_calc_clip_distance(world_pos);
103 #endif
104 }