Merge pull request 'noise' (#8) from noise into master

Reviewed-on: s473581/dgrk-2023-planeta#8
This commit is contained in:
Oliwia Michalik 2024-02-04 13:03:08 +01:00
commit 43ab04e7eb
3 changed files with 85 additions and 3 deletions

View File

@ -19,6 +19,11 @@ uniform vec3 lightColor;
uniform bool atmosphereCheck; uniform bool atmosphereCheck;
uniform float u_time;
uniform float cloudLight;
uniform float cloudIntensity;
uniform float cloudSpeed;
in vec3 vecNormal; in vec3 vecNormal;
in vec3 worldPos; in vec3 worldPos;
in vec2 vtc; in vec2 vtc;
@ -103,8 +108,69 @@ vec3 toneMapping(vec3 color)
return mapped; return mapped;
} }
void main() float random (in vec2 st) {
{ return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
float noise (in vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
// Four corners in 2D of a tile
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}
#define OCTAVES 6
float fbm (in vec2 st) {
// Initial values
float value = 0.0;
float amplitude = .5;
float frequency = 0.;
//
// Loop of octaves
for (int i = 0; i < OCTAVES; i++) {
value += amplitude * noise(st);
st *= 2.;
amplitude *= .5;
}
return value;
}
vec4 noiseColor() {
vec2 st = vtc.xy;
vec2 mirroredSt = vec2(1.0 - st.x, st.y);
float timeOffset = u_time * cloudSpeed;
st.x -= timeOffset;
mirroredSt.x += timeOffset; // Inverse direction for mirrored effect
st.x = fract(st.x);
mirroredSt.x = fract(mirroredSt.x);
vec3 color = vec3(fbm(st * cloudIntensity));
vec3 mirroredColor = vec3(fbm(mirroredSt * cloudIntensity));
float blend = smoothstep(0.45, 0.55, st.x);
vec3 finalColor = mix(color, mirroredColor, blend);
vec4 noiseColor = vec4(finalColor, 1.0);
return noiseColor;
}
void main() {
vec3 normal = normalize(vecNormal); vec3 normal = normalize(vecNormal);
vec3 viewDir = normalize(cameraPos - worldPos); vec3 viewDir = normalize(cameraPos - worldPos);
vec3 lightDir = normalize(lightPos - worldPos); vec3 lightDir = normalize(lightPos - worldPos);
@ -132,5 +198,9 @@ void main()
vec3 pbrColor = vec3(1.0) - exp(-illumination * exposition); vec3 pbrColor = vec3(1.0) - exp(-illumination * exposition);
outColor = vec4(pbrColor, 1); vec4 noiseColor = noiseColor() * min(1, AMBIENT + diffuse) * vec4(lightColor, 0.0) / cloudLight;
vec3 mixedColor = mix(pbrColor.rgb, noiseColor.rgb, noiseColor.r);
outColor = vec4(mixedColor, 1.0);
} }

View File

@ -11,6 +11,7 @@ uniform mat4 modelMatrix;
out vec3 vecNormal; out vec3 vecNormal;
out vec3 worldPos; out vec3 worldPos;
out vec2 vtc; out vec2 vtc;
uniform vec3 lightPos; uniform vec3 lightPos;
@ -25,6 +26,7 @@ void main()
{ {
worldPos = (modelMatrix * vec4(vertexPosition, 1)).xyz; worldPos = (modelMatrix * vec4(vertexPosition, 1)).xyz;
vecNormal = (modelMatrix * vec4(vertexNormal, 0)).xyz; vecNormal = (modelMatrix * vec4(vertexNormal, 0)).xyz;
gl_Position = transformation * vec4(vertexPosition, 1.0); gl_Position = transformation * vec4(vertexPosition, 1.0);
vtc = vec2(vertexTexCoord.x, 1.0 - vertexTexCoord.y); vtc = vec2(vertexTexCoord.x, 1.0 - vertexTexCoord.y);

View File

@ -74,6 +74,10 @@ int HDR_HEIGHT = 1024;
float lightPower = 8.f; float lightPower = 8.f;
glm::vec3 lightColor = glm::vec3(lightPower, lightPower, lightPower); glm::vec3 lightColor = glm::vec3(lightPower, lightPower, lightPower);
float cloudLight = 20.f;
float cloudIntensity = 15.f;
float cloudSpeed = 0.07f;
glm::mat4 createCameraMatrix() { glm::mat4 createCameraMatrix() {
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f))); glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, cameraDir)); glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, cameraDir));
@ -191,6 +195,12 @@ void drawPlanetPbr(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint t
glUniform3f(glGetUniformLocation(programPbr, "lightPos"), sunPos.x, sunPos.y, sunPos.z); glUniform3f(glGetUniformLocation(programPbr, "lightPos"), sunPos.x, sunPos.y, sunPos.z);
glUniform3f(glGetUniformLocation(programPbr, "lightColor"), lightColor.x, lightColor.y, lightColor.z); glUniform3f(glGetUniformLocation(programPbr, "lightColor"), lightColor.x, lightColor.y, lightColor.z);
float time = glfwGetTime();
glUniform1f(glGetUniformLocation(programPbr, "u_time"), time);
glUniform1f(glGetUniformLocation(programPbr, "cloudLight"), cloudLight);
glUniform1f(glGetUniformLocation(programPbr, "cloudIntensity"), cloudIntensity);
glUniform1f(glGetUniformLocation(programPbr, "cloudSpeed"), cloudSpeed);
glUniform1i(glGetUniformLocation(programPbr, "atmosphereCheck"), atmosphereCheck); glUniform1i(glGetUniformLocation(programPbr, "atmosphereCheck"), atmosphereCheck);
Core::DrawContext(context); Core::DrawContext(context);