[2829] | 1 | //////////////////// |
---|
| 2 | // Screen Spaced Ambient Occlusion shader |
---|
| 3 | // mainly based on Kustls shader |
---|
| 4 | |
---|
[2826] | 5 | #define NUM_SAMPLES 8 |
---|
| 6 | #define SAMPLE_INTENSITY 0.5f |
---|
[2825] | 7 | //#define SAMPLE_INTENSITY 0.7f |
---|
[2822] | 8 | #define AREA_SIZE 5e-1f |
---|
[2821] | 9 | |
---|
[2810] | 10 | // kustls magic sample positions |
---|
[2820] | 11 | /*static const float2 samples[NUM_SAMPLES] = |
---|
[2810] | 12 | { |
---|
| 13 | {-0.326212f, -0.405805f}, |
---|
| 14 | {-0.840144f, -0.07358f}, |
---|
| 15 | {-0.695914f, 0.457137f}, |
---|
| 16 | {-0.203345f, 0.620716}, |
---|
| 17 | {0.96234f, -0.194983f}, |
---|
| 18 | {0.473434f, -0.480026f}, |
---|
| 19 | {0.519456, 0.767022f}, |
---|
| 20 | {0.185461f, -0.893124f}, |
---|
| 21 | {0.507431f, 0.064425f}, |
---|
| 22 | {0.89642f, 0.412458f}, |
---|
| 23 | {-0.32194f, -0.932615f}, |
---|
| 24 | {-0.791559f, -0.597705f}, |
---|
| 25 | {0.326212f, 0.405805f}, |
---|
| 26 | {0.840144f, 0.07358f}, |
---|
| 27 | {0.695914f, -0.457137f}, |
---|
| 28 | {0.203345f, -0.620716}, |
---|
| 29 | {-0.96234f, 0.194983f}, |
---|
| 30 | {-0.473434f, 0.480026f}, |
---|
| 31 | {-0.519456, -0.767022f}, |
---|
| 32 | {-0.185461f, 0.893124f}, |
---|
| 33 | {-0.507431f, -0.064425f}, |
---|
| 34 | {-0.89642f, -0.412458f}, |
---|
| 35 | {0.32194f, 0.932615f}, |
---|
| 36 | {0.791559f, 0.597705f} |
---|
[2820] | 37 | };*/ |
---|
[2810] | 38 | |
---|
| 39 | |
---|
| 40 | struct fragment |
---|
| 41 | { |
---|
| 42 | float4 pos: WPOS; // normalized screen position |
---|
| 43 | float4 texCoord: TEXCOORD0; |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | struct pixel |
---|
| 48 | { |
---|
| 49 | float4 color: COLOR0; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | |
---|
[2814] | 53 | float2 reflect(float2 pt, float2 n) |
---|
| 54 | { |
---|
| 55 | // distance to plane |
---|
| 56 | float d = dot(n, pt); |
---|
| 57 | // reflect around plane |
---|
| 58 | float2 rpt = pt - d * 2.0f * n; |
---|
| 59 | |
---|
| 60 | //return pt; |
---|
| 61 | return rpt; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
[2821] | 65 | float2 rotate(float2 pt, float2 n) |
---|
| 66 | { |
---|
| 67 | float2 ptTransformed; |
---|
| 68 | ptTransformed.x = n.r * pt.x - n.g * pt.y; |
---|
| 69 | ptTransformed.y = n.g * pt.x + n.r * pt.y; |
---|
| 70 | |
---|
| 71 | return ptTransformed; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
[2810] | 75 | //based on kustls shader |
---|
| 76 | float ssao(fragment IN, |
---|
| 77 | uniform sampler2D positions, |
---|
[2820] | 78 | uniform sampler2D noiseTexture, |
---|
[2822] | 79 | uniform float2 samples[NUM_SAMPLES], |
---|
| 80 | uniform float3 currentNormal |
---|
[2810] | 81 | ) |
---|
| 82 | { |
---|
| 83 | // the current world position |
---|
[2818] | 84 | float4 centerPosition = tex2D(positions, IN.texCoord.xy); |
---|
| 85 | // the w coordinate from the persp. projection |
---|
| 86 | float w = centerPosition.w; |
---|
[2810] | 87 | |
---|
| 88 | // Check in a circular area around the current position. |
---|
| 89 | // Shoot vectors to the positions there, and check the angle to these positions. |
---|
| 90 | // Summing up these angles gives an estimation of the occlusion at the current position. |
---|
| 91 | |
---|
| 92 | float total_ao = 0.0; |
---|
| 93 | |
---|
[2826] | 94 | const float areaSize = 5e-1f; |
---|
[2820] | 95 | //const float areaSize = 3e-1f; |
---|
| 96 | //const float sampleIntensity = 0.2f; |
---|
[2810] | 97 | |
---|
[2820] | 98 | for (int i = 0; i < NUM_SAMPLES; i ++) { |
---|
[2810] | 99 | float2 offset = samples[i]; |
---|
| 100 | |
---|
| 101 | //sample noisetex; r stores costheta, g stores sintheta |
---|
[2823] | 102 | float2 noise = tex2D(noiseTexture, IN.texCoord.xy * 7.0f).xy * 2.0f - 1.0f; |
---|
[2818] | 103 | |
---|
| 104 | // rotation |
---|
| 105 | //float2 offsetTransformed = offset; |
---|
[2821] | 106 | float2 offsetTransformed = rotate(offset, noise); |
---|
| 107 | //float2 offsetTransformed = reflect(offset, noise); |
---|
[2825] | 108 | |
---|
[2813] | 109 | // weight with projected coordinate to reach similar kernel size for near and far |
---|
[2820] | 110 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; |
---|
[2810] | 111 | |
---|
| 112 | float3 sample_position = tex2D(positions, texcoord).xyz; |
---|
| 113 | |
---|
[2818] | 114 | float3 vector_to_sample = sample_position - centerPosition.xyz; |
---|
[2810] | 115 | float length_to_sample = length(vector_to_sample); |
---|
[2820] | 116 | float3 direction_to_sample = vector_to_sample / length_to_sample; |
---|
[2810] | 117 | |
---|
| 118 | // Angle between current normal and direction to sample controls AO intensity. |
---|
[2822] | 119 | float cos_angle = dot(direction_to_sample, currentNormal); |
---|
[2810] | 120 | cos_angle = max(cos_angle, 0.0f); |
---|
[2825] | 121 | cos_angle *= cos_angle; |
---|
| 122 | |
---|
[2810] | 123 | // distance between current position and sample position controls AO intensity. |
---|
[2826] | 124 | //const float maxdist = 2e-1f; |
---|
| 125 | //const float maxdist = 5e-1f; |
---|
| 126 | const float distanceScale = 1e-6f; |
---|
| 127 | //float distance_intensity = maxdist - length_to_sample; |
---|
| 128 | float distance_intensity = (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample); |
---|
| 129 | //distance_intensity = max(distance_intensity, 0.0f); |
---|
[2821] | 130 | // quadratic influence |
---|
[2826] | 131 | //distance_intensity *= distance_intensity; |
---|
[2810] | 132 | |
---|
[2826] | 133 | total_ao += cos_angle * distance_intensity; |
---|
[2810] | 134 | } |
---|
| 135 | |
---|
| 136 | return (1.0f - total_ao); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
[2820] | 140 | float4 shade(fragment IN, |
---|
| 141 | uniform sampler2D colors, |
---|
| 142 | uniform sampler2D positions, |
---|
[2822] | 143 | uniform float3 normal, |
---|
| 144 | uniform float amb) |
---|
[2810] | 145 | { |
---|
[2817] | 146 | float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f); |
---|
[2815] | 147 | float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f); |
---|
[2810] | 148 | |
---|
| 149 | float4 color = tex2D(colors, IN.texCoord.xy); |
---|
[2822] | 150 | |
---|
[2810] | 151 | float4 position = tex2D(positions, IN.texCoord.xy); |
---|
| 152 | |
---|
[2822] | 153 | float4 ambient = 0.3f; |
---|
[2810] | 154 | |
---|
| 155 | // float3 L = normalize(lightPosition - position); |
---|
| 156 | float3 light = normalize(lightDir.xyz); |
---|
[2817] | 157 | float3 light2 = normalize(lightDir2.xyz); |
---|
[2818] | 158 | |
---|
[2810] | 159 | float diffuseLight = max(dot(normal, light), 0.0f); |
---|
[2815] | 160 | float diffuseLight2 = max(dot(normal, light2), 0.0f); |
---|
[2810] | 161 | |
---|
[2815] | 162 | float diffuse = diffuseLight + diffuseLight2; |
---|
| 163 | |
---|
[2822] | 164 | return (ambient + diffuse) * color * (1.0f - amb) + amb * color; |
---|
[2820] | 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
[2821] | 168 | pixel main_ssao(fragment IN, |
---|
| 169 | uniform sampler2D colors, |
---|
| 170 | uniform sampler2D positions, |
---|
| 171 | uniform sampler2D normals, |
---|
| 172 | uniform sampler2D noiseTexture, |
---|
| 173 | uniform float2 samples[NUM_SAMPLES]) |
---|
| 174 | { |
---|
| 175 | pixel OUT; |
---|
| 176 | |
---|
[2822] | 177 | float4 normal = tex2D(normals, IN.texCoord.xy); |
---|
| 178 | float amb = normal.w; |
---|
| 179 | |
---|
| 180 | // expand normal |
---|
| 181 | normal = normalize(normal * 2.0f - 1.0f); |
---|
| 182 | |
---|
| 183 | float4 col = shade(IN, colors, positions, normal, amb); |
---|
| 184 | float ao = ssao(IN, positions, noiseTexture, samples, normal); |
---|
[2821] | 185 | |
---|
[2829] | 186 | //OUT.color = ao; |
---|
| 187 | OUT.color = ao * col; |
---|
[2821] | 188 | |
---|
| 189 | return OUT; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
[2820] | 193 | pixel main(fragment IN, |
---|
| 194 | uniform sampler2D colors, |
---|
| 195 | uniform sampler2D positions, |
---|
[2821] | 196 | uniform sampler2D normals) |
---|
[2820] | 197 | { |
---|
| 198 | pixel OUT; |
---|
[2822] | 199 | |
---|
| 200 | float4 normal = tex2D(normals, IN.texCoord.xy); |
---|
| 201 | float amb = normal.w; |
---|
[2820] | 202 | |
---|
[2822] | 203 | // expand normal |
---|
| 204 | normal = normalize(normal * 2.0f - 1.0f); |
---|
| 205 | |
---|
| 206 | float4 col = shade(IN, colors, positions, normal.xyz, amb); |
---|
[2821] | 207 | OUT.color = col; |
---|
| 208 | |
---|
[2810] | 209 | return OUT; |
---|
| 210 | } |
---|