| 1 |
// WS30 FRAGMENT SHADER |
| 2 |
|
| 3 |
// -*-C++-*- |
| 4 |
#version 130 |
| 5 |
#extension GL_EXT_texture_array : enable |
| 6 |
|
| 7 |
varying vec3 normal; |
| 8 |
varying vec4 ecPosition; |
| 9 |
|
| 10 |
uniform sampler2D landclass; |
| 11 |
uniform sampler2DArray atlas; |
| 12 |
uniform sampler2D perlin; |
| 13 |
|
| 14 |
// Passed from VPBTechnique, not the Effect |
| 15 |
uniform float fg_tileWidth; |
| 16 |
uniform float fg_tileHeight; |
| 17 |
uniform bool fg_photoScenery; |
| 18 |
uniform vec4 fg_dimensionsArray[128]; |
| 19 |
uniform vec4 fg_ambientArray[128]; |
| 20 |
uniform vec4 fg_diffuseArray[128]; |
| 21 |
uniform vec4 fg_specularArray[128]; |
| 22 |
uniform vec4 fg_textureLookup1[128]; |
| 23 |
uniform vec4 fg_textureLookup2[128]; |
| 24 |
uniform mat4 fg_zUpTransform; |
| 25 |
uniform vec3 fg_modelOffset; |
| 26 |
|
| 27 |
// See include_fog.frag |
| 28 |
uniform int fogType; |
| 29 |
vec3 fog_Func(vec3 color, int type); |
| 30 |
|
| 31 |
// See Shaders/shadows-include.frag |
| 32 |
float getShadowing(); |
| 33 |
|
| 34 |
// See Shaders/clustered-include.frag |
| 35 |
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel); |
| 36 |
|
| 37 |
void main() |
| 38 |
{ |
| 39 |
float NdotL, NdotHV, fogFactor; |
| 40 |
vec3 lightDir = gl_LightSource[0].position.xyz; |
| 41 |
vec3 halfVector = gl_LightSource[0].halfVector.xyz; |
| 42 |
vec4 texel; |
| 43 |
vec4 fragColor; |
| 44 |
vec4 specular = vec4(0.0); |
| 45 |
|
| 46 |
// Material properties. |
| 47 |
// Material properties. |
| 48 |
vec4 mat_diffuse, mat_ambient, mat_specular; |
| 49 |
float mat_shininess; |
| 50 |
|
| 51 |
if (fg_photoScenery) { |
| 52 |
mat_ambient = vec4(1.0,1.0,1.0,1.0); |
| 53 |
mat_diffuse = vec4(1.0,1.0,1.0,1.0); |
| 54 |
mat_specular = vec4(0.1, 0.1, 0.1, 1.0); |
| 55 |
mat_shininess = 1.2; |
| 56 |
|
| 57 |
texel = texture(landclass, vec2(gl_TexCoord[0].s, 1.0 - gl_TexCoord[0].t)); |
| 58 |
} else { |
| 59 |
// The Landclass for this particular fragment. This can be used to |
| 60 |
// index into the atlas textures. |
| 61 |
int lc = int(texture2D(landclass, gl_TexCoord[0].st).g * 255.0 + 0.5); |
| 62 |
uint tex1 = uint(fg_textureLookup1[lc].r * 255.0 + 0.5); |
| 63 |
|
| 64 |
// Color Mode is always AMBIENT_AND_DIFFUSE, which means |
| 65 |
// using a base colour of white for ambient/diffuse, |
| 66 |
// rather than the material color from ambientArray/diffuseArray. |
| 67 |
mat_ambient = vec4(1.0,1.0,1.0,1.0); |
| 68 |
mat_diffuse = vec4(1.0,1.0,1.0,1.0); |
| 69 |
mat_specular = fg_specularArray[lc]; |
| 70 |
mat_shininess = fg_dimensionsArray[lc].z; |
| 71 |
|
| 72 |
// Different textures have different have different dimensions. |
| 73 |
vec2 atlas_dimensions = fg_dimensionsArray[lc].st; |
| 74 |
vec2 atlas_scale = vec2(fg_tileWidth / atlas_dimensions.s, fg_tileHeight / atlas_dimensions.t ); |
| 75 |
vec2 st = atlas_scale * gl_TexCoord[0].st; |
| 76 |
|
| 77 |
// Rotate texture using the perlin texture as a mask to reduce tiling |
| 78 |
if (step(0.5, texture(perlin, atlas_scale * gl_TexCoord[0].st / 8.0).r) == 1.0) { |
| 79 |
st = vec2(atlas_scale.s * gl_TexCoord[0].t, atlas_scale.t * gl_TexCoord[0].s); |
| 80 |
} |
| 81 |
|
| 82 |
if (step(0.5, texture(perlin, - atlas_scale * gl_TexCoord[0].st / 16.0).r) == 1.0) { |
| 83 |
st = -st; |
| 84 |
} |
| 85 |
|
| 86 |
texel = texture(atlas, vec3(st, tex1)); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
vec4 color = mat_ambient * (gl_LightModel.ambient + gl_LightSource[0].ambient); |
| 91 |
|
| 92 |
// If gl_Color.a == 0, this is a back-facing polygon and the |
| 93 |
// normal should be reversed. |
| 94 |
vec3 n = (2.0 * gl_Color.a - 1.0) * normal; |
| 95 |
n = normalize(n); |
| 96 |
NdotL = dot(n, lightDir); |
| 97 |
|
| 98 |
if (NdotL > 0.0) { |
| 99 |
float shadowmap = getShadowing(); |
| 100 |
color += mat_diffuse * NdotL * shadowmap; |
| 101 |
NdotHV = max(dot(n, halfVector), 0.0); |
| 102 |
if (mat_shininess > 0.0) |
| 103 |
specular.rgb = (mat_specular.rgb |
| 104 |
* gl_LightSource[0].specular.rgb |
| 105 |
* pow(NdotHV, mat_shininess) |
| 106 |
* shadowmap); |
| 107 |
} |
| 108 |
color.a = mat_diffuse.a; |
| 109 |
|
| 110 |
// This shouldn't be necessary, but our lighting becomes very |
| 111 |
// saturated. Clamping the color before modulating by the texture |
| 112 |
// is closer to what the OpenGL fixed function pipeline does. |
| 113 |
color = clamp(color, 0.0, 1.0); |
| 114 |
|
| 115 |
|
| 116 |
fragColor = color * texel + specular; |
| 117 |
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb); |
| 118 |
|
| 119 |
fragColor.rgb = fog_Func(fragColor.rgb, fogType); |
| 120 |
gl_FragColor = fragColor; |
| 121 |
} |