Skip to content
Snippets Groups Projects
Commit 3e2bbe52 authored by JonShard's avatar JonShard
Browse files

Added terrain shaders. They were ignored by git before.

parent 501dd1d8
No related branches found
No related tags found
No related merge requests found
#shader vertex
#version 410
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 uv;
layout(location = 3) in vec4 vertex_color;
out vec2 texCoord;
out vec4 vertex_color_out;
out vec4 pos;
uniform mat4 m2w;
uniform float time = 0;
layout(std140) uniform OK_Matrices{
mat4 projection;
mat4 view;
vec4 view_position;
};
mat4 rotate(float x, float y, float z) {
return mat4(
(cos(y + z) + cos(y - z)) / 2, (-sin(y + z) + sin(y - z)) / 2, -sin(y), 0,
(cos(x + y + z) - cos(x - y + z) + cos(x + y - z) - cos(x - y - z) + 2 * sin(x + z) - 2 * sin(x - z)) / 4, (2 * cos(x + z) + 2 * cos(x - z) - sin(x + y + z) + sin(x - y + z) + sin(x + y - z) - sin(x - y - z)) / 4, (-sin(x + y) - sin(x - y)) / 2, 0,
(-2 * cos(x + z) + 2 * cos(x - z) + sin(x + y + z) - sin(x - y + z) + sin(x + y - z) - sin(x - y - z)) / 4, (cos(x + y + z) - cos(x - y + z) - cos(x + y - z) + cos(x - y - z) + 2 * sin(x + z) + 2 * sin(x - z)) / 4, (cos(x + y) + cos(x - y)) / 2, 0,
0, 0, 0, 1
);
}
out vec3 fragVert;
out vec3 fragNormal;
out float vertexHeight;
vec4 MVP(in vec4 position) {
return projection * view * m2w * position;
}
void main() {
float F = sqrt(position.x*position.x + position.y*position.y + position.z*position.z) * 0.01;
mat4 rot = rotate(0, time*F, 0);
vec4 rotatedNormal = rot * vec4(normal, 1);
// Pass some variables to the fragment shader
//fragNormal = vec3(rotatedNormal);
vertex_color_out = rotatedNormal;
texCoord = uv;
fragNormal = mat3(transpose(inverse(m2w))) * normal; //http://www.lighthouse3d.com/tutorials/glsl-12-tutorial/the-normal-matrix/
vec4 out_position = MVP(position);
gl_Position = out_position;
vertexHeight = position.y;
fragVert = vec3(m2w * position);
}
#shader fragment
#version 410
#define MAX_LIGHTS 8
#define EPSILON 0.0000000000000001
in vec4 gl_FragCoord;
in vec2 texCoord;
in vec3 fragNormal;
in vec3 fragVert;
in float vertexHeight;
out vec4 out_color;
uniform float time = 0;
uniform float opacity = 1;
uniform float specularity = 0.4;
uniform float intensity = 0.3;
uniform mat4 m2w;
layout(std140) uniform OK_Matrices{
mat4 projection;
mat4 view;
vec4 view_position;
};
struct OK_Light_Directional {
vec4 direction;
vec4 intensities;
};
struct OK_Light_Point {
vec4 position;
vec4 intensities;
float constant;
float linear;
float quadratic;
float alignment;
};
layout(std140) uniform OK_Lights{
OK_Light_Point light[MAX_LIGHTS];
OK_Light_Directional sun;
};
vec3 OK_PointLight(in vec3 position, in vec3 intensities, in float constant, in float linear, in float quadratic) {
//Ambience
float ambientStrength = 0.02;
vec3 ambient = ambientStrength * intensities;
// Diffussion
vec3 norm = normalize(fragNormal);
vec3 lightDir = normalize(position - fragVert);
float diffusion = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diffusion * intensities;
// Specularity
vec3 viewDir = normalize(view_position.xyz - fragVert);
vec3 reflectDir = reflect(-lightDir, norm);
float specPower = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularity * specPower * intensities;
// Attenuation
float distance = length(position - fragVert);
float attenuation = 1.0 / (constant + linear * distance + quadratic * distance * distance +EPSILON);
return (ambient + diffuse + specular) * attenuation;
}
vec3 OK_DirectionalLight(in vec3 lightDir, in vec3 intensities) {
//Ambience
float ambientStrength = 0.04;
vec3 ambient = ambientStrength * intensities;
//Diffuse
vec3 norm = normalize(fragNormal);
lightDir = -normalize(lightDir);
float diffusion = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diffusion * intensities;
//Specularity
vec3 viewDir = normalize(view_position.xyz - fragVert);
vec3 reflectDir = reflect(-lightDir, norm);
float specPower = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularity * specPower * intensities;
return (ambient + diffuse + specular);
}
void main() {
vec3 lights = OK_DirectionalLight(sun.direction.xyz, sun.intensities.rgb);
for(int i = 0; i < MAX_LIGHTS; i++)
{
lights += OK_PointLight(
light[i].position.xyz,
light[i].intensities.rgb,
light[i].constant,
light[i].linear,
light[i].quadratic
);
}
float height = vertexHeight + 0.5; // Not sure why the vertexHeight in modelspace is ofset by 0.5, but this is a hotfix.
float multiplier = 0.35; // To scale the all the heights down. In case max height on map is lower than 1.
vec2 waterRange = vec2(0 * multiplier, 0.2 * multiplier);
vec2 beachRange = vec2(0.20001 * multiplier, 0.25 * multiplier);
vec2 forrestRange = vec2(0.250001 * multiplier, 0.46 * multiplier);
vec2 snowRange = vec2(0.8 * multiplier, 1.000001);
float water = int(height < waterRange.y);
float beach = int(height > beachRange.x && height < beachRange.y);
float forrest = int(height >forrestRange.x && height <forrestRange.y);
float snow = int(height > snowRange.x && height < snowRange.y);
int redLine = 0; // INSERT UNIFORM FOR RED LINE HERE!! 1 or 0!!
float thickness = 0.003; // How thick the red line marking the zones are.
out_color = vec4(clamp( lights + // Color: // Color enabled yes/no
water * vec3(0.06, 0.41, 0.7) * float(redLine == 0) +
beach * vec3(0.57, 0.51, 0.27) * float(redLine == 0) +
forrest * vec3(0.1, 0.31, 0.05) * float(redLine == 0) +
snow * vec3(0.27, 0.37, 0.37) * float(redLine == 0) +
redLine * vec3(1.0, 0.0, 1.0) * float((height > snowRange.x && height < snowRange.x + thickness) || (height < snowRange.y && height > snowRange.y - thickness)) +
redLine * vec3(0.0, 1.0, 0.0) * float((height > forrestRange.x && height < forrestRange.x + thickness) || (height < forrestRange.y && height > forrestRange.y - thickness)) +
redLine * vec3(1.0, 0.87, 0.0) * float((height > beachRange.x && height < beachRange.x + thickness) || (height < beachRange.y && height > beachRange.y - thickness)) +
redLine * vec3(0.0, 1.0, 0.97) * float((height > waterRange.x && height < waterRange.x + thickness) || (height < waterRange.y && height > waterRange.y - thickness))
, 0, 1), 1);
// out_color = vec4(height, 0, 0, 1);
}
#shader vertex
#version 410
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 uv;
layout(location = 3) in vec4 vertex_color;
out vec2 texCoord;
out vec4 vertex_color_out;
out vec4 pos;
uniform mat4 m2w;
uniform float time = 0;
layout(std140) uniform OK_Matrices{
mat4 projection;
mat4 view;
vec4 view_position;
};
mat4 rotate(float x, float y, float z) {
return mat4(
(cos(y + z) + cos(y - z)) / 2, (-sin(y + z) + sin(y - z)) / 2, -sin(y), 0,
(cos(x + y + z) - cos(x - y + z) + cos(x + y - z) - cos(x - y - z) + 2 * sin(x + z) - 2 * sin(x - z)) / 4, (2 * cos(x + z) + 2 * cos(x - z) - sin(x + y + z) + sin(x - y + z) + sin(x + y - z) - sin(x - y - z)) / 4, (-sin(x + y) - sin(x - y)) / 2, 0,
(-2 * cos(x + z) + 2 * cos(x - z) + sin(x + y + z) - sin(x - y + z) + sin(x + y - z) - sin(x - y - z)) / 4, (cos(x + y + z) - cos(x - y + z) - cos(x + y - z) + cos(x - y - z) + 2 * sin(x + z) + 2 * sin(x - z)) / 4, (cos(x + y) + cos(x - y)) / 2, 0,
0, 0, 0, 1
);
}
out vec3 fragVert;
out vec3 fragNormal;
vec4 MVP(in vec4 position) {
return projection * view * m2w * position;
}
void main() {
float F = sqrt(position.x*position.x + position.y*position.y + position.z*position.z) * 0.01;
mat4 rot = rotate(0, time*F, 0);
vec4 rotatedNormal = rot * vec4(normal, 1);
// Pass some variables to the fragment shader
//fragNormal = vec3(rotatedNormal);
vertex_color_out = rotatedNormal;
texCoord = uv;
fragNormal = mat3(transpose(inverse(m2w))) * normal; //http://www.lighthouse3d.com/tutorials/glsl-12-tutorial/the-normal-matrix/
vec4 out_position = MVP(position);
gl_Position = out_position;
fragVert = vec3(m2w * position);
}
#shader fragment
#version 410
#define MAX_LIGHTS 8
#define EPSILON 0.0000000000000001
in vec4 gl_FragCoord;
in vec2 texCoord;
in vec3 fragNormal;
in vec3 fragVert;
out vec4 out_color;
uniform float time = 0;
uniform float opacity = 1;
uniform float specularity = 1;
uniform float intensity = 1;
uniform mat4 m2w;
layout(std140) uniform OK_Matrices{
mat4 projection;
mat4 view;
vec4 view_position;
};
struct OK_Light_Directional {
vec4 direction;
vec4 intensities;
};
struct OK_Light_Point {
vec4 position;
vec4 intensities;
float constant;
float linear;
float quadratic;
float alignment;
};
layout(std140) uniform OK_Lights{
OK_Light_Point light[MAX_LIGHTS];
OK_Light_Directional sun;
};
vec3 OK_PointLight(in vec3 position, in vec3 intensities, in float constant, in float linear, in float quadratic) {
//Ambience
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * intensities;
// Diffussion
vec3 norm = normalize(fragNormal);
vec3 lightDir = normalize(position - fragVert);
float diffusion = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diffusion * intensities;
// Specularity
vec3 viewDir = normalize(view_position.xyz - fragVert);
vec3 reflectDir = reflect(-lightDir, norm);
float specPower = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularity * specPower * intensities;
// Attenuation
float distance = length(position - fragVert);
float attenuation = 1.0 / (constant + linear * distance + quadratic * distance * distance +EPSILON);
return (ambient + diffuse + specular) * attenuation;
}
vec3 OK_DirectionalLight(in vec3 lightDir, in vec3 intensities) {
//Ambience
float ambientStrength = 0.7;
vec3 ambient = ambientStrength * intensities;
//Diffuse
vec3 norm = normalize(fragNormal);
lightDir = -normalize(lightDir);
float diffusion = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diffusion * intensities;
//Specularity
vec3 viewDir = normalize(view_position.xyz - fragVert);
vec3 reflectDir = reflect(-lightDir, norm);
float specPower = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularity * specPower * intensities;
return (ambient + diffuse + specular);
}
void main() {
vec3 lights = OK_DirectionalLight(sun.direction.xyz, sun.intensities.rgb);
for(int i = 0; i < MAX_LIGHTS; i++)
{
lights += OK_PointLight(
light[i].position.xyz,
light[i].intensities.rgb,
light[i].constant,
light[i].linear,
light[i].quadratic
);
}
// out_color = vec4( normalize(lights + vec3((sin(fragVert.y * 20) / 2 + 0.5) * (2 * ((sin(10 * fragVert.y) / 2) + 0.5)),
// 0,
// 0)), 1);
out_color = vec4( normalize(lights + vec3((sin(fragVert.y * 10) / 2 + 0.5) *
((cos(fragVert.y * 30) / 2) + 0.5),
0,
0)), 1);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment