Projekt-GRK/shaders/shader_perlin_tex.frag
2018-06-20 21:34:24 +02:00

51 lines
1.4 KiB
GLSL

#version 430 core
uniform vec3 lightDir;
uniform vec2 resolution;
in vec3 interpNormal;
in vec3 localPos;
float random2d(in vec2 point) {
return fract(sin(dot(point.xy, vec2(12.9898, 78.233))) * 43758.5453123);
}
float perlinNoise(in vec2 point) {
vec2 i = floor(point);
vec2 f = fract(point);
float corn1 = random2d(i);
float corn2 = random2d(i + vec2(1.0, 0.0));
float corn3 = random2d(i + vec2(0.0, 1.0));
float corn4 = random2d(i + vec2(1.0, 1.0));
//interpolacja Hermite'a
vec2 u = f * f *(3.0 - 2.0 * f);
float p1 = mix(corn1, corn2, u.x);
float p2 = (corn3 - corn1) * u.y * (1.0 - u.x);
float p3 = (corn4 - corn2) * u.x * u.y;
return p1 + p2 + p3;
}
void main()
{
vec3 normal = normalize(interpNormal);
float diffuse = max(dot(normal, -lightDir), 0.0);
vec2 point = gl_FragCoord.xy / resolution.xy + localPos.xy;
vec2 pos = vec2(point * 0.3);
float noise = perlinNoise(pos);
vec3 color = (vec3(noise) * 2) * vec3(0.57, 0.55, 0.18);
//gl_FragColor = vec4(vec3(color), 1.0);
gl_FragColor = vec4(color.x * diffuse, color.y * diffuse, color.z * diffuse, 1.0);
//if(sin(localPos.y) > 0) gl_FragColor = vec4(positiveColor.x * diffuse, positiveColor.y * diffuse, positiveColor.z * diffuse, 1.0);
//else gl_FragColor = vec4(negativeColor.x * diffuse, negativeColor.y * diffuse, negativeColor.z * diffuse, 1.0);
}