-
-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shaderlab pbr support refraction #2470
Changes from 34 commits
dd09252
1fc002e
adfdfec
62844aa
ffa2912
1fc63b2
bab9582
174e0e7
47fb197
f9b748c
4ef6387
76b40b1
3abe5c1
b81bbeb
bcc9daf
b9b9558
9d8a37e
84e2f0d
e977cbc
4040c26
420566d
4a3ce3e
6b19600
129ac02
1ff952d
1f011eb
76d9063
d2c26cf
9d9e4ae
02ae05e
a493e26
59e0da3
e062e18
aa434e4
82d9754
4c5eb18
b6c874a
0bf8d73
1f20667
d4b5d11
df27aef
1ba3440
09397f0
e7718de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,30 @@ | |
sampler2D camera_OpaqueTexture; | ||
vec3 evaluateRefraction(SurfaceData surfaceData, BRDFData brdfData) { | ||
RefractionModelResult ray; | ||
#ifdef REFRACTION_MODE | ||
refractionModelBox(-surfaceData.viewDir, surfaceData.position, surfaceData.normal, surfaceData.IOR, 0.005, ray); | ||
// '0' is refraction sphere, '1' is refraction plane, '2' is refraction thin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function named |
||
#if REFRACTION_MODE == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // RefractionMode.Plane |
||
refractionModelSphere(-surfaceData.viewDir, surfaceData.position, surfaceData.normal, surfaceData.IOR, surfaceData.thickness, ray); | ||
#elif REFRACTION_MODE == 1 | ||
refractionModelBox(-surfaceData.viewDir, surfaceData.position, surfaceData.normal, surfaceData.IOR, surfaceData.thickness, ray); | ||
#elif REFRACTION_MODE == 2 | ||
refractionModelBox(-surfaceData.viewDir, surfaceData.position, surfaceData.normal, surfaceData.IOR, surfaceData.thickness, ray); | ||
GuoLei1990 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif | ||
//TODO: support cubemap refraction. | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @todo: No need to process if the coord exceeds the screen? |
||
|
||
// Absorption coefficient from Disney: http://blog.selfshadow.com/publications/s2015-shading-course/burley/s2015_pbs_disney_bsdf_notes.pdf | ||
#ifdef MATERIAL_HAS_ABSORPTION | ||
GuoLei1990 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#ifdef MATERIAL_HAS_THICKNESS | ||
vec3 transmittance = min(vec3(1.0), exp(-surfaceData.absorptionCoefficient * ray.transmissionLength)); | ||
#else | ||
vec3 transmittance = 1.0 - surfaceData.absorptionCoefficient; | ||
#endif | ||
#endif | ||
|
||
// Sample the opaque texture to get the transmitted light | ||
vec4 getTransmission = texture2D(camera_OpaqueTexture, refractionCoords); | ||
vec3 refractionTransmitted = getTransmission.rgb; | ||
|
@@ -27,6 +41,10 @@ | |
vec3 specularDFG = brdfData.envSpecularDFG; | ||
|
||
refractionTransmitted *= (1.0 - specularDFG); | ||
|
||
GuoLei1990 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#ifdef MATERIAL_HAS_ABSORPTION | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @todo: camera distance |
||
refractionTransmitted *= transmittance; | ||
#endif | ||
|
||
return refractionTransmitted; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,27 @@ struct RefractionModelResult{ | |
// 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 refractionModelBox(vec3 V, vec3 positionWS, vec3 normalWS, float ior, float thickness, out RefractionModelResult ray){ | ||
// Refracted ray | ||
vec3 R = refract(V, normalWS, 1.0 / ior); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add macro cleanup and value validation.
The current implementation has potential issues:
Apply this diff to fix these issues:
📝 Committable suggestion