Created
September 16, 2023 16:44
-
-
Save sjhalayka/a65862c696e6f3cd03437b8340f6b39e to your computer and use it in GitHub Desktop.
Reflection, scattering, and refraction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vec3 o_reflect = hitPos + rayPayload.normal * 0.01; | |
vec3 d_reflect = reflect(d, rayPayload.normal); | |
vec3 temp_o = o_reflect; | |
vec3 temp_d = d_reflect; | |
if(rayPayload.reflector < 1.0) // if less than fully reflective, do scattering | |
{ | |
vec3 o_scatter = hitPos + rayPayload.normal * 0.01; | |
vec3 d_scatter = cosWeightedRandomHemisphereDirection(rayPayload.normal, prng_state); | |
temp_o = mix(temp_o, o_scatter, 1.0 - rayPayload.reflector); | |
temp_d = mix(temp_d, d_scatter, 1.0 - rayPayload.reflector); | |
} | |
if(rayPayload.opacity < 1.0) // if partially transparent, do refraction | |
{ | |
vec3 o_transparent = vec3(0.0); | |
vec3 d_transparent = vec3(0.0); | |
// Incoming | |
if(dot(d, rayPayload.normal) <= 0.0) | |
{ | |
o_transparent = hitPos.xyz - rayPayload.normal * 0.01f; | |
d_transparent = refract(d, rayPayload.normal, eta); | |
} | |
else // Outgoing | |
{ | |
vec3 temp_dir = refract(d, -rayPayload.normal, 1.0 / eta); | |
if(temp_dir != vec3(0.0)) | |
{ | |
o_transparent = hitPos.xyz + rayPayload.normal * 0.01f; | |
d_transparent = temp_dir; | |
} | |
else | |
{ | |
o_transparent = hitPos.xyz - rayPayload.normal * 0.01f; | |
d_transparent = reflect(d, -rayPayload.normal); | |
} | |
} | |
temp_o = mix(temp_o, o_transparent, 1.0 - rayPayload.opacity); | |
temp_d = mix(temp_d, d_transparent, 1.0 - rayPayload.opacity); | |
} | |
o = temp_o; | |
d = temp_d; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment