Debian Codegraph @

tests/manual/wave/shaders/ribbonwireframe.frag

raw
1 #version 330 core
2
3 in WireframeVertex {
4 vec3 position;
5 vec3 normal;
6 noperspective vec4 edgeA;
7 noperspective vec4 edgeB;
8 flat int configuration;
9 } fs_in;
10
11 out vec4 fragColor;
12
13 uniform struct LightInfo {
14 vec4 position;
15 vec3 intensity;
16 } light;
17
18 uniform struct LineInfo {
19 float width;
20 vec4 color;
21 } line;
22
23 uniform vec3 ka; // Ambient reflectivity
24 uniform vec3 kd; // Diffuse reflectivity
25
26 vec3 rimLightModel( const in vec3 pos, const in vec3 n )
27 {
28 // Calculate the vector from the light to the fragment
29 vec3 s = normalize( vec3( light.position ) - pos );
30
31 // Calculate the vector from the fragment to the eye position (the
32 // origin since this is in "eye" or "camera" space
33 vec3 v = normalize( -pos );
34
35 // Refleft the light beam using the normal at this fragment
36 vec3 r = reflect( -s, n );
37
38 // Calculate the diffuse component, which for rim lighting it 1 minus s dot n
39 // rather than s dot n as for standard diffuse lighting
40 float sDotN = dot( s, n );
41 vec3 diffuse = vec3( 1.0 - max( sDotN, 0.0 ) );
42
43 // Combine the ambient, diffuse and specular contributions
44 return light.intensity * ( ka + kd * diffuse );
45 }
46
47 vec4 shadeLine( const in vec4 color )
48 {
49 // Find the smallest distance between the fragment and a triangle edge
50 float d;
51 if ( fs_in.configuration == 0 ) {
52 // Common configuration
53 d = min( fs_in.edgeA.x, fs_in.edgeA.y );
54 d = min( d, fs_in.edgeA.z );
55 } else {
56 // Handle configuration where screen space projection breaks down
57 // Compute and compare the squared distances
58 vec2 AF = gl_FragCoord.xy - fs_in.edgeA.xy;
59 float sqAF = dot( AF, AF );
60 float AFcosA = dot( AF, fs_in.edgeA.zw );
61 d = abs( sqAF - AFcosA * AFcosA );
62
63 vec2 BF = gl_FragCoord.xy - fs_in.edgeB.xy;
64 float sqBF = dot( BF, BF );
65 float BFcosB = dot( BF, fs_in.edgeB.zw );
66 d = min( d, abs( sqBF - BFcosB * BFcosB ) );
67
68 // Only need to care about the 3rd edge for some configurations.
69 if ( fs_in.configuration == 1 ||
70 fs_in.configuration == 2 ||
71 fs_in.configuration == 4 ) {
72 float AFcosA0 = dot( AF, normalize( fs_in.edgeB.xy - fs_in.edgeA.xy ) );
73 d = min( d, abs( sqAF - AFcosA0 * AFcosA0 ) );
74 }
75
76 d = sqrt( d );
77 }
78
79 // Blend between line color and shaded color
80 float mixVal;
81 if ( d < line.width - 1.0 ) {
82 mixVal = 1.0;
83 } else if ( d > line.width + 1.0 ) {
84 mixVal = 0.0;
85 } else {
86 float x = d - ( line.width - 1.0 );
87 mixVal = exp2( -2.0 * ( x * x ) );
88 }
89
90 return mix( color, line.color, mixVal );
91 }
92
93 void main()
94 {
95 vec3 n = gl_FrontFacing ? fs_in.normal : -fs_in.normal;
96 vec4 color = vec4( rimLightModel( fs_in.position, normalize( n ) ), 1.0 );
97 fragColor = shadeLine( color );
98 }