-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
262 additions
and
4 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
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,53 @@ | ||
#ifndef BTDF_INCLUDED | ||
#define BTDF_INCLUDED | ||
|
||
#include "Refraction.glsl" | ||
|
||
vec4 renderer_texelSize; // x: 1/width, y: 1/height, z: width, w: height | ||
sampler2D camera_OpaqueTexture; | ||
|
||
#ifdef MATERIAL_ENABLE_SS_REFRACTION | ||
vec3 evaluateRefraction(Varyings varyings, SurfaceData surfaceData, BRDFData brdfData, vec3 speculaColor) { | ||
RefractionModel ray; | ||
#if REFRACTION_SPHERE | ||
RefractionModelSphere(surfaceData.viewDir, varyings.positionWS, varyings.normalWS, surfaceData.IOR, surfaceData.thickness, ray); | ||
#elif REFRACTION_PLANE | ||
RefractionModelBox(surfaceData.viewDir, varyings.positionWS, varyings.normalWS, surfaceData.IOR, surfaceData.thickness, ray); | ||
#elif REFRACTION_THIN | ||
RefractionModelBox(surfaceData.viewDir, varyings.positionWS, varyings.normalWS, surfaceData.IOR, surfaceData.thickness, ray); | ||
#endif | ||
|
||
vec3 refractedRayExit = ray.position; //ray.position + ray.direction; | ||
|
||
// Project refracted vector on the framebuffer, while mapping to normalized device coordinates. | ||
vec4 ndcPos = camera_ProjMat * camera_ViewMat * vec4( refractedRayExit, 1.0 ); | ||
vec2 refractionCoords = ndcPos.xy / ndcPos.w; | ||
refractionCoords *= 0.5; | ||
refractionCoords += 0.5; | ||
|
||
// compute transmission | ||
vec3 absorptionCoefficient = max(vec3(0), -log(surfaceData.attenuationColor)/surfaceData.attenuationDistance); | ||
#ifdef MATERIAL_HAS_ABSORPTION | ||
vec3 transmittance = min(vec3(1.0), exp(-absorptionCoefficient * ray.dist)); | ||
#else | ||
vec3 transmittance = 1.0 - absorptionCoefficient; | ||
#endif | ||
|
||
// vec4 transmittedLight = texture2D(camera_OpaqueTexture, varyings.uv); | ||
vec4 transmittedLight = sampleTexture2DBicubic(camera_OpaqueTexture, refractionCoords, renderer_texelSize); | ||
// vec3 transmittanceColor = brdfData.diffuseColor * transmittance; | ||
vec3 E = envBRDFApprox(speculaColor, brdfData.roughness, surfaceData.dotNV); | ||
|
||
vec3 ft = transmittedLight.rgb; | ||
ft *= brdfData.diffuseColor; | ||
ft *= 1.0 - E; | ||
|
||
#ifdef MATERIAL_HAS_ABSORPTION | ||
ft *= transmittance; | ||
#endif | ||
|
||
return ft; | ||
} | ||
#endif | ||
|
||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#ifndef REFRACTION_INCLUDED | ||
#define REFRACTION_INCLUDED | ||
|
||
#ifdef MATERIAL_ENABLE_SS_REFRACTION | ||
|
||
struct RefractionModel{ | ||
float dist; // length of the transmission during refraction through the shape | ||
vec3 position; // out ray position | ||
vec3 direction; // out ray direction | ||
}; | ||
|
||
void RefractionModelSphere(vec3 V, vec3 positionWS, vec3 normalWS, float ior, float thickness, out RefractionModel ray){ | ||
// Sphere shape model: | ||
// We approximate locally the shape of the object as sphere, that is tangent to the shape. | ||
// The sphere has a diameter of {thickness} | ||
// The center of the sphere is at {positionWS} - {normalWS} * {thickness} * 0.5 | ||
// So the light is refracted twice: in and out of the tangent sphere | ||
|
||
// First refraction (tangent sphere in) | ||
// 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 NoR1 = dot(normalWS, R1); | ||
// Optical depth within the sphere | ||
float dist = -NoR1 * thickness; | ||
// Out hit point in the tangent sphere | ||
vec3 P1 = positionWS + R1 * dist; | ||
// Out normal | ||
vec3 N1 = normalize(C - P1); | ||
// Out refracted ray | ||
vec3 R2 = refract(R1, N1, ior); | ||
|
||
ray.dist = dist; | ||
ray.position = P1; | ||
ray.direction = R2; | ||
} | ||
|
||
void RefractionModelBox(vec3 V, vec3 positionWS, vec3 normalWS, float ior, float thickness, out RefractionModel ray){ | ||
// Plane shape model: | ||
// We approximate locally the shape of the object as a plane with normal {normalWS} at {positionWS} | ||
// with a thickness {thickness} | ||
// Refracted ray | ||
vec3 R = refract(V, normalWS, 1.0 / ior); | ||
|
||
// Optical depth within the thin plane | ||
float dist = thickness / max(dot(R, -normalWS), 0.001); | ||
|
||
ray.dist = dist; | ||
ray.position = positionWS + R * dist; | ||
ray.direction = V; | ||
} | ||
#endif | ||
#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