-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support transmission and refraction
- Loading branch information
Showing
8 changed files
with
152 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <refraction> | ||
|
||
#ifdef MATERIAL_ENABLE_TRANSMISSION | ||
uniform sampler2D camera_OpaqueTexture; | ||
vec3 evaluateTransmission(Geometry geometry, Material material) { | ||
RefractionModelResult ray; | ||
#if REFRACTION_MODE == 0 | ||
// RefractionMode.Sphere | ||
refractionModelSphere(-geometry.viewDir, geometry.position, geometry.normal, material.IOR, material.thickness, ray); | ||
#elif REFRACTION_MODE == 1 | ||
// RefractionMode.Planar | ||
refractionModelPlanar(-geometry.viewDir, geometry.position, geometry.normal, material.IOR, material.thickness, ray); | ||
#endif | ||
|
||
vec3 refractedRayExit = ray.positionExit; | ||
|
||
// We calculate the screen space position of the refracted point | ||
vec4 samplingPositionNDC = camera_ProjMat * camera_ViewMat * vec4( refractedRayExit, 1.0 ); | ||
vec2 refractionCoords = (samplingPositionNDC.xy / samplingPositionNDC.w) * 0.5 + 0.5; | ||
|
||
// Sample the opaque texture to get the transmitted light | ||
vec3 refractionTransmitted = texture2D(camera_OpaqueTexture, refractionCoords).rgb; | ||
refractionTransmitted *= material.diffuseColor; | ||
|
||
// Use specularFGD as an approximation of the fresnel effect | ||
// https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_slides_v2.pdf | ||
refractionTransmitted *= (1.0 - material.envSpecularDFG); | ||
|
||
#ifdef MATERIAL_HAS_THICKNESS | ||
// Absorption coefficient from Disney: http://blog.selfshadow.com/publications/s2015-shading-course/burley/s2015_pbs_disney_bsdf_notes.pdf | ||
vec3 transmittance = min(vec3(1.0), exp(-material.absorptionCoefficient * ray.transmissionLength)); | ||
refractionTransmitted *= transmittance; | ||
#endif | ||
|
||
return refractionTransmitted; | ||
} | ||
#endif |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifdef MATERIAL_ENABLE_TRANSMISSION | ||
struct RefractionModelResult { | ||
float transmissionLength; // length of the transmission during refraction through the shape | ||
vec3 positionExit; // out ray position | ||
// vec3 directionExit; // out ray direction | ||
}; | ||
|
||
//https://docs.unity3d.com/Packages/[email protected]/manual/refraction-models.html | ||
void refractionModelSphere(vec3 V, vec3 positionWS, vec3 normalWS, float ior, float thickness, out RefractionModelResult ray) { | ||
// Refracted ray | ||
vec3 R1 = refract(V, normalWS, 1.0 / ior); | ||
// Center of the tangent sphere | ||
// vec3 C = positionWS - normalWS * thickness * 0.5; | ||
|
||
// Second refraction (tangent sphere out) | ||
float dist = dot(-normalWS, R1) * thickness; | ||
// Out hit point in the tangent sphere | ||
vec3 P1 = positionWS + R1 * dist; | ||
// Out normal | ||
// vec3 N1 = safeNormalize(C - P1); | ||
// Out refracted ray | ||
// vec3 R2 = refract(R1, N1, ior); | ||
|
||
ray.transmissionLength = dist; | ||
ray.positionExit = P1; | ||
// ray.directionExit = R2; | ||
} | ||
|
||
void refractionModelPlanar(vec3 V, vec3 positionWS, vec3 normalWS, float ior, float thickness, out RefractionModelResult ray) { | ||
// Refracted ray | ||
vec3 R = refract(V, normalWS, 1.0 / ior); | ||
// Optical depth within the thin plane | ||
float dist = thickness / max(dot(-normalWS, R), 1e-5f); | ||
|
||
ray.transmissionLength = dist; | ||
ray.positionExit = vec3(positionWS + R * dist); | ||
// ray.directionExit = V; | ||
} | ||
|
||
#endif |