| 1 |
// WS30 FRAGMENT SHADER |
| 2 |
|
| 3 |
// -*-C++-*- |
| 4 |
#version 130 |
| 5 |
#extension GL_EXT_texture_array : enable |
| 6 |
// written by Thorsten Renk, Oct 2011, based on default.frag |
| 7 |
|
| 8 |
|
| 9 |
////////////////////////////////////////////////////////////////// |
| 10 |
// TEST PHASE TOGGLES AND CONTROLS |
| 11 |
// |
| 12 |
|
| 13 |
// Development tools: |
| 14 |
// Reduce haze to almost zero, while preserving lighting. Useful for observing distant tiles. |
| 15 |
// Keeps the calculation overhead. This can be used for profiling. |
| 16 |
// Possible values: 0:Normal, 1:Reduced haze. |
| 17 |
const int reduce_haze_without_removing_calculation_overhead = 0; |
| 18 |
|
| 19 |
// Remove haze and lighting and shows just the texture. |
| 20 |
// Useful for checking texture rendering and scenery. |
| 21 |
// The compiler will likely optimise out the haze and lighting calculations. |
| 22 |
// Possible values: 0:Normal, 1:Just the texture. |
| 23 |
const int remove_haze_and_lighting = 0; |
| 24 |
|
| 25 |
// Use built-in water shader. Use for testing impact of ws30-water.frag |
| 26 |
const int water_shader = 1; |
| 27 |
|
| 28 |
// |
| 29 |
// End of test phase controls |
| 30 |
////////////////////////////////////////////////////////////////// |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
// Ambient term comes in gl_Color.rgb. |
| 46 |
varying vec4 light_diffuse_comp; |
| 47 |
varying vec3 normal; |
| 48 |
varying vec3 relPos; |
| 49 |
varying vec2 ground_tex_coord; |
| 50 |
|
| 51 |
uniform sampler2D landclass; |
| 52 |
uniform sampler2DArray textureArray; |
| 53 |
|
| 54 |
varying float yprime_alt; |
| 55 |
varying float mie_angle; |
| 56 |
varying vec4 ecPosition; |
| 57 |
|
| 58 |
uniform float visibility; |
| 59 |
uniform float avisibility; |
| 60 |
uniform float scattering; |
| 61 |
uniform float terminator; |
| 62 |
uniform float terrain_alt; |
| 63 |
uniform float hazeLayerAltitude; |
| 64 |
uniform float overcast; |
| 65 |
uniform float eye_alt; |
| 66 |
uniform float cloud_self_shading; |
| 67 |
|
| 68 |
// Passed from VPBTechnique, not the Effect |
| 69 |
// Passed from VPBTechnique, not the Effect |
| 70 |
uniform float fg_tileWidth; |
| 71 |
uniform float fg_tileHeight; |
| 72 |
uniform bool fg_photoScenery; |
| 73 |
uniform vec4 fg_dimensionsArray[128]; |
| 74 |
uniform vec4 fg_ambientArray[128]; |
| 75 |
uniform vec4 fg_diffuseArray[128]; |
| 76 |
uniform vec4 fg_specularArray[128]; |
| 77 |
uniform vec4 fg_textureLookup1[128]; |
| 78 |
uniform vec4 fg_textureLookup2[128]; |
| 79 |
uniform vec4 fg_materialParams3[128]; |
| 80 |
|
| 81 |
#define MAX_TEXTURES 8 |
| 82 |
uniform mat4 fg_zUpTransform; |
| 83 |
uniform vec3 fg_modelOffset; |
| 84 |
|
| 85 |
const float EarthRadius = 5800000.0; |
| 86 |
const float terminator_width = 200000.0; |
| 87 |
|
| 88 |
float alt; |
| 89 |
float eShade; |
| 90 |
|
| 91 |
float fog_func (in float targ, in float alt); |
| 92 |
vec3 get_hazeColor(in float light_arg); |
| 93 |
vec3 filter_combined (in vec3 color) ; |
| 94 |
|
| 95 |
float shadow_func (in float x, in float y, in float noise, in float dist); |
| 96 |
float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density); |
| 97 |
float Noise2D(in vec2 coord, in float wavelength); |
| 98 |
float Noise3D(in vec3 coord, in float wavelength); |
| 99 |
float SlopeLines2D(in vec2 coord, in vec2 gradDir, in float wavelength, in float steepness); |
| 100 |
float Strata3D(in vec3 coord, in float wavelength, in float variation); |
| 101 |
float fog_func (in float targ, in float alt); |
| 102 |
float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt); |
| 103 |
float alt_factor(in float eye_alt, in float vertex_alt); |
| 104 |
float light_distance_fading(in float dist); |
| 105 |
float fog_backscatter(in float avisibility); |
| 106 |
|
| 107 |
vec3 rayleigh_out_shift(in vec3 color, in float outscatter); |
| 108 |
vec3 get_hazeColor(in float light_arg); |
| 109 |
vec3 searchlight(); |
| 110 |
vec3 landing_light(in float offset, in float offsetv); |
| 111 |
vec3 filter_combined (in vec3 color) ; |
| 112 |
|
| 113 |
float getShadowing(); |
| 114 |
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel); |
| 115 |
vec4 generateWaterTexel(); |
| 116 |
|
| 117 |
// Not used |
| 118 |
float luminance(vec3 color) |
| 119 |
{ |
| 120 |
return dot(vec3(0.212671, 0.715160, 0.072169), color); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
////////////////////////// |
| 125 |
// Test-phase code: |
| 126 |
|
| 127 |
// These should be sent as uniforms |
| 128 |
|
| 129 |
// Tile dimensions in meters |
| 130 |
// vec2 tile_size = vec2(tile_width , tile_height); |
| 131 |
// Testing: texture coords are sent flipped right now: |
| 132 |
|
| 133 |
// Note tile_size is defined in the shader include: ws30-landclass-search-functions.frag. |
| 134 |
// vec2 tile_size = vec2(tile_height , tile_width); |
| 135 |
|
| 136 |
// From noise.frag |
| 137 |
float rand2D(in vec2 co); |
| 138 |
|
| 139 |
// Create random landclasses without a texture lookup to stress test. |
| 140 |
// Each square of square_size in m is assigned a random landclass value. |
| 141 |
int get_random_landclass(in vec2 co, in vec2 tile_size); |
| 142 |
|
| 143 |
// End Test-phase code |
| 144 |
//////////////////////// |
| 145 |
|
| 146 |
// These functions, and other function they depend on, are defined |
| 147 |
// in ws30-ALS-landclass-search.frag. |
| 148 |
|
| 149 |
// Lookup a ground texture at a point based on the landclass at that point, without visible |
| 150 |
// seams at coordinate discontinuities or at landclass boundaries where texture are switched. |
| 151 |
// The partial derivatives of the tile_coord at the fragment is needed to adjust for |
| 152 |
// the stretching of different textures, so that the correct mip-map level is looked |
| 153 |
// up and there are no seams. |
| 154 |
// Texture types: 0: base texture, 1: grain texture, 2: gradient texture, 3 dot texture, |
| 155 |
// 4: mix texture, 5: detail texture. |
| 156 |
|
| 157 |
vec4 lookup_ground_texture_array(in int texture_type, in vec2 ground_texture_coord, in int landclass_id, |
| 158 |
in vec4 dFdx_and_dFdy); |
| 159 |
|
| 160 |
|
| 161 |
// Look up the landclass id [0 .. 255] for this particular fragment. |
| 162 |
// Lookup id of any neighbouring landclass that is within the search distance. |
| 163 |
// Searches are performed in upto 4 directions right now, but only one landclass is looked up |
| 164 |
// Create a mix factor werighting the influences of nearby landclasses |
| 165 |
void get_landclass_id(in vec2 tile_coord, in vec4 dFdx_and_dFdy, |
| 166 |
out int landclass_id, out ivec4 neighbor_landclass_ids, |
| 167 |
out int num_unique_neighbors,out vec4 mix_factor |
| 168 |
); |
| 169 |
|
| 170 |
|
| 171 |
// Look up the texel of the specified texture type (e.g. grain or detail textures) for this fragment |
| 172 |
// and any neighbor texels, then mix. |
| 173 |
|
| 174 |
vec4 get_mixed_texel(in int texture_type, in vec2 g_texture_coord, |
| 175 |
in int landclass_id, in int num_unique_neighbors, |
| 176 |
in ivec4 neighbor_texel_landclass_ids, in vec4 neighbor_mix_factors, |
| 177 |
in vec4 dFdx_and_dFdy |
| 178 |
); |
| 179 |
|
| 180 |
// Determine the texel and material parameters for a particular fragment, |
| 181 |
// Taking into account photoscenery etc. |
| 182 |
void get_material(in int landclass, |
| 183 |
in vec2 ground_tex_coord, |
| 184 |
in vec4 dxdy_gc, |
| 185 |
out float mat_shininess, |
| 186 |
out vec4 mat_ambient, |
| 187 |
out vec4 mat_diffuse, |
| 188 |
out vec4 mat_specular, |
| 189 |
out vec4 dxdy, |
| 190 |
out vec2 st |
| 191 |
); |
| 192 |
|
| 193 |
// Apply the ALS haze model to a specific fragment |
| 194 |
vec4 applyHaze(inout vec4 fragColor, |
| 195 |
inout vec3 hazeColor, |
| 196 |
in vec3 secondary_light, |
| 197 |
in float ct, |
| 198 |
in float hazeLayerAltitude, |
| 199 |
in float visibility, |
| 200 |
in float avisibility, |
| 201 |
in float dist, |
| 202 |
in float lightArg, |
| 203 |
in float mie_angle); |
| 204 |
|
| 205 |
// Procedurally generate a water texel for this fragment |
| 206 |
vec4 generateWaterTexel(); |
| 207 |
|
| 208 |
void main() |
| 209 |
{ |
| 210 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 211 |
// this is taken from default.frag |
| 212 |
float NdotL, NdotHV, fogFactor; |
| 213 |
vec3 n = normalize(normal); |
| 214 |
vec3 lightDir = gl_LightSource[0].position.xyz; |
| 215 |
vec3 halfVector = gl_LightSource[0].halfVector.xyz; |
| 216 |
vec4 texel; |
| 217 |
vec4 fragColor; |
| 218 |
vec4 specular = vec4(0.0); |
| 219 |
float intensity; |
| 220 |
|
| 221 |
// Material/texel properties |
| 222 |
float mat_shininess; |
| 223 |
vec2 st; |
| 224 |
vec4 mat_ambient, mat_diffuse, mat_specular, dxdy; |
| 225 |
|
| 226 |
// This is a water fragment, so calculate the fragment color procedurally |
| 227 |
fragColor = generateWaterTexel(); |
| 228 |
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, fragColor.rgb); |
| 229 |
|
| 230 |
// angle with horizon |
| 231 |
float dist = length(relPos); |
| 232 |
float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist; |
| 233 |
|
| 234 |
float lightArg = (terminator-yprime_alt)/100000.0; |
| 235 |
vec3 hazeColor = get_hazeColor(lightArg); |
| 236 |
gl_FragColor = applyHaze(fragColor, hazeColor, vec3(0.0), ct, hazeLayerAltitude, visibility, avisibility, dist, lightArg, mie_angle); |
| 237 |
|
| 238 |
// Testing phase controls: |
| 239 |
if (remove_haze_and_lighting == 1) |
| 240 |
{ |
| 241 |
gl_FragColor = texel; |
| 242 |
} |
| 243 |
} |