diff options
| author | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:02:32 +0100 |
|---|---|---|
| committer | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:02:32 +0100 |
| commit | 6b8af9cf83851c075c6c9514b1deaa931c2b19a4 (patch) | |
| tree | 428986b49c32e21d3f7a3c2dfa41858ae0153209 /07-july/resources/shaders/terrainShader.frag | |
Initial commit
Diffstat (limited to '07-july/resources/shaders/terrainShader.frag')
| -rw-r--r-- | 07-july/resources/shaders/terrainShader.frag | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/07-july/resources/shaders/terrainShader.frag b/07-july/resources/shaders/terrainShader.frag new file mode 100644 index 0000000..be9c773 --- /dev/null +++ b/07-july/resources/shaders/terrainShader.frag @@ -0,0 +1,73 @@ +#version 420 + +in vec2 Fragment_UV; +in vec3 World_Normal; +in vec4 Fragment_Color; +in vec3 toLightVector[4]; +in vec3 toEyeVector; + +out vec4 out_color; + +uniform vec4 lightColor[4]; +uniform vec3 attenuation[4]; +uniform vec3 World_eyePosition; + +uniform sampler2D Texture_Background; +uniform sampler2D Texture_R; +uniform sampler2D Texture_G; +uniform sampler2D Texture_B; +uniform sampler2D Texture_BlendMap; + +void main() +{ + vec4 totalDiffuse = vec4(0.0, 0.0, 0.0, 1.0); + vec4 totalSpecular = vec4(0.0, 0.0, 0.0, 1.0); + + for(int i = 0; i < 4; i++) + { + /*Light Attenuation*/ + float dist = length(toLightVector[i]); + float attFactor = attenuation[i].x + (attenuation[i].y * dist) + (attenuation[i].z * dist * dist); + vec3 unitToLightVector = normalize(toLightVector[i]); + /*Diffuse lighting*/ + + /*La intensidad es el cos entre la normal y el vector hacia la luz*/ + float brightness = dot(unitToLightVector, normalize(World_Normal)); + /*No queremos luz negativa ni mayor a 1*/ + brightness = max(brightness, 0.0); + totalDiffuse = totalDiffuse + (brightness * lightColor[i]) / attFactor; + + /* Specular lighting */ + + /* Reflejamos el vector hacia la luz (su inverso) en la normal */ + vec3 Vector_ReflectedLight = reflect(-unitToLightVector, World_Normal); + + /* La luz especular es el cos del angulo entre el vector hacia el ojo y la luz reflectada en la normal */ + float specularity = clamp( dot(Vector_ReflectedLight, toEyeVector), 0, 1 ); + float reflectivity = 1; + /* Que tan grande es el specular highlight */ + specularity = pow(specularity, 10); + totalSpecular = totalSpecular + clamp( reflectivity * specularity * lightColor[i], 0, 1) / attFactor; + } + + /* Ambient Light */ + totalDiffuse = max(totalDiffuse, 0.2); + + /*Terrain color*/ + /*The color of the current fragment from the blendmap*/ + vec4 blendMapColor = texture(Texture_BlendMap, Fragment_UV); + /*We want the background color when we have black in the blend map*/ + float backTextureAmount = 1 - (blendMapColor.r + blendMapColor.g + blendMapColor.b); + /*So we dont lose image quality from the tiles textures (Fragment_UV make the terrain look less HD)*/ + vec2 Tiled_UV = Fragment_UV * 50.0f; + /*We get the color of each map tile (soil1, soil2, etc)*/ + vec4 backgroundTextureColor = texture(Texture_Background, Tiled_UV) * backTextureAmount; + vec4 rTextureColor = texture(Texture_R, Tiled_UV) * blendMapColor.r; + vec4 gTextureColor = texture(Texture_G, Tiled_UV) * blendMapColor.g; + vec4 bTextureColor = texture(Texture_B, Tiled_UV) * blendMapColor.b; + /*We mix them corresponding with the blendmap*/ + vec4 totalColor = backgroundTextureColor + rTextureColor + gTextureColor + bTextureColor; + + /*Our final color for the fragment*/ + out_color = totalDiffuse * totalColor + totalSpecular; +} |
