From 5b5006eb90f302e20cab8c9d21d62b54df9a80a0 Mon Sep 17 00:00:00 2001 From: dingusreal <148672705+dingusreal@users.noreply.github.com> Date: Thu, 28 Aug 2025 18:42:33 +0800 Subject: [PATCH] SHINY REFLECTIONS YAY --- assets/shaders/phong.fs | 32 +++++++++++++++++++++++++++++--- core/src/objects/part/basepart.h | 1 + core/src/rendering/renderer.cpp | 4 ++++ core/src/rendering/skybox.cpp | 5 +++-- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/assets/shaders/phong.fs b/assets/shaders/phong.fs index 965919b..dca543f 100644 --- a/assets/shaders/phong.fs +++ b/assets/shaders/phong.fs @@ -54,11 +54,14 @@ uniform int numPointLights; uniform DirLight sunLight; uniform Material material; uniform sampler2DArray studs; +uniform samplerCube skybox; uniform float transparency; +uniform float reflectance; uniform vec3 texScale; -// Functions +// Functions +vec3 calculateReflection(); vec3 calculateDirectionalLight(DirLight light); vec3 calculatePointLight(PointLight light); mat3 lookAlong(vec3 pos, vec3 forward, vec3 up); @@ -68,6 +71,7 @@ mat3 lookAlong(vec3 pos, vec3 forward, vec3 up); void main() { vec3 result = vec3(0.0); + result += calculateReflection(); result += calculateDirectionalLight(sunLight); for (int i = 0; i < numPointLights; i++) { @@ -95,6 +99,25 @@ mat3 lookAlong(vec3 pos, vec3 forward, vec3 up) { return mat3(s, u, f); } + +vec3 sampleSkybox() +{ + vec3 norm = normalize(vNormal); + vec3 viewDir = normalize(viewPos - vPos); + vec3 reflectDir = reflect(-viewDir, norm); + + return textureLod(skybox,reflectDir, 5.0 * (1.0-material.shininess)).rgb; +} + +vec3 calculateReflection() { + vec3 norm = normalize(vNormal); + vec3 viewDir = normalize(viewPos - vPos); + vec3 reflectDir = reflect(viewDir, norm); + float fresnel = (pow(1.0-max(dot(viewDir, norm), 0.0), 5.0)); + vec3 result = sampleSkybox() * mix(fresnel * material.specular, vec3(1.0), reflectance); + return result; +} + vec3 calculateDirectionalLight(DirLight light) { // Calculate diffuse vec3 norm = normalize(vNormal); @@ -105,10 +128,13 @@ vec3 calculateDirectionalLight(DirLight light) { vec3 viewDir = normalize(viewPos - vPos); vec3 reflectDir = reflect(-lightDir, norm); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + // float fresnel = (pow(1.0-max(dot(viewDir, norm), 0.0), 5.0)); - vec3 ambient = light.ambient * material.diffuse; - vec3 diffuse = light.diffuse * diff * material.diffuse; + + vec3 ambient = light.ambient * (material.diffuse * (1.0-reflectance)); + vec3 diffuse = light.diffuse * diff * (material.diffuse * (1.0-reflectance)); vec3 specular = light.specular * spec * material.specular; + // specular += sampleSkybox() * fresnel * material.specular; return (ambient + diffuse + specular); } diff --git a/core/src/objects/part/basepart.h b/core/src/objects/part/basepart.h index 39c700b..6dbe4da 100644 --- a/core/src/objects/part/basepart.h +++ b/core/src/objects/part/basepart.h @@ -83,6 +83,7 @@ public: DEF_PROP_CATEGORY(APPEARANCE) DEF_PROP Color3 color; DEF_PROP float transparency = 0.f; + DEF_PROP float reflectance = 0.f; DEF_PROP_CATEGORY(BEHAVIOR) DEF_PROP_(on_update=onUpdated) bool anchored = false; diff --git a/core/src/rendering/renderer.cpp b/core/src/rendering/renderer.cpp index 76cb06b..35f94d8 100644 --- a/core/src/rendering/renderer.cpp +++ b/core/src/rendering/renderer.cpp @@ -137,6 +137,7 @@ static void renderPart(std::shared_ptr part) { shader->set("normalMatrix", normalMatrix); shader->set("texScale", size); shader->set("transparency", part->transparency); + shader->set("reflectance", part->reflectance); shader->set("surfaces[" + std::to_string(NormalId::Right) + "]", (int)part->rightSurface); shader->set("surfaces[" + std::to_string(NormalId::Top) + "]", (int)part->topSurface); @@ -183,11 +184,14 @@ void renderParts() { shader->set("numPointLights", 0); studsTexture->activate(0); shader->set("studs", 0); + skyboxTexture->activate(1); + shader->set("skybox", 1); // Pre-calculate the normal matrix for the shader // Pass in the camera position shader->set("viewPos", camera.cameraPos); + // Sort by nearest std::map> sorted; diff --git a/core/src/rendering/skybox.cpp b/core/src/rendering/skybox.cpp index d7dbd93..8aa5fb5 100644 --- a/core/src/rendering/skybox.cpp +++ b/core/src/rendering/skybox.cpp @@ -22,6 +22,7 @@ Skybox::Skybox(std::array faces, unsigned int format) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_CUBE_MAP); stbi_image_free(data); } @@ -31,7 +32,7 @@ Skybox::Skybox(std::array faces, unsigned int format) { glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); // Interpolation - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } @@ -41,5 +42,5 @@ Skybox::~Skybox() { void Skybox::activate(unsigned int textureIdx) { glActiveTexture(GL_TEXTURE0 + textureIdx); - glBindTexture(GL_TEXTURE_2D, this->ID); + glBindTexture(GL_TEXTURE_CUBE_MAP, this->ID); }