SHINY REFLECTIONS YAY

This commit is contained in:
dingusreal 2025-08-28 18:42:33 +08:00 committed by m-doescode
parent e2054a51a8
commit 5b5006eb90
4 changed files with 37 additions and 5 deletions

View file

@ -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);
}

View file

@ -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;

View file

@ -137,6 +137,7 @@ static void renderPart(std::shared_ptr<BasePart> 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<float, std::shared_ptr<BasePart>> sorted;

View file

@ -22,6 +22,7 @@ Skybox::Skybox(std::array<std::string, 6> 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<std::string, 6> 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);
}