Cycles: Add thin film iridescence to Principled BSDF #118477
No reviewers
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
7 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#118477
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "LukasStockner/blender:iridescence"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This is an implementation of thin film iridescence in the Principled BSDF based on "A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence".
There are still several open topics that are left for future work:
As an example, here's a picture of a soap bubble, using the same parameters (IOR 1.7, 400nm) as in the paper:
I've also compared the results to the great comparison by @PascalSchon from here and the paper supplemental material, and it matches at least in terms of overall hue.
EEVEE rendering of the Principled BSDF is currently broken. This is of how the node inputs are being passed to
node_shader_gpu_bsdf_principled
. A simple fix is to just add the extra inputs to that function and leave them unused. (Your main focus seems to be Cycles, so leaving these unused at the moment is fine). Something as simple as:f666e9d034
will do at the moment.Sorry for "reviewing" minor features while you probably want feedback on the more important stuff.
4418a204f3
tod665174879
Updates:
d665174879
to0dcf334003
WIP: Cycles: Add thin film iridescence to Principled BSDFto Cycles: Add thin film iridescence to Principled BSDFUpdates:
main
I'm not aware of any remaining issues, so I think this is ready for review. There's things that could be improved later (the color adaption method, adding it as a standalone node etc.), but the basics are there.
I did not test this PR thoroughly, just had a first pass. I believe some points in you PR description is outdated, please update them.
I set up a soap bubble and it looks beatiful. I wonder what parameters should I use for coated metal?
@ -383,6 +419,7 @@ ccl_device Spectrum bsdf_microfacet_estimate_albedo(KernelGlobals kg,
if (!is_zero(reflectance) && bsdf->fresnel_type == MicrofacetFresnel::GENERALIZED_SCHLICK) {
ccl_private FresnelGeneralizedSchlick *fresnel = (ccl_private FresnelGeneralizedSchlick *)
bsdf->fresnel;
/* TODO: Iridescence */
What is this TODO?
The code here wasn't accounting for the thin film yet, this is fixed now.
@ -238,0 +272,4 @@
}
float R123 = R12 * R23;
float r123 = sqrt(R123);
Why not
sqrtf()
here?Thanks, fixed.
@ -238,0 +288,4 @@
/* Computes reflectivity (R) and phase shift (phi) for perpendicular (.x) and parallel (.y)
* polarized light. */
ccl_device_inline void fresnel_dielectric_polarized(const float cosThetaI,
This function is almost identical as
fresnel_dielectric()
except for the phase computation, would it be practical to reuse that function? The phase computation could then be:The existing function also returns
cos_theta_t
, which is needed for computing the OPD.If it turns out to be impractical, I would then prefer that these two functions have the same structure, notation and comment, eg.
snake_case
, a comment indicating total internal reflection, mentioning s and p polarization (or computer_s
andr_p
first, then square the result).Good point, thanks, those are indeed very similar. I'll merge them together.
Comparing the two, I think I found a sign error in the current code. So far it wouldn't have mattered because we only use squared values, but it does matter for the phase.
Do you mean the sign of
cos_theta_t
? Infresnel_dielectric()
it is the angle between the surface normal and the refracted ray, hence negative, and the sign is used forrefract_angle()
. The result should be the same as yourfresnel_dielectric_polarized()
, i.e.cos_theta_i + eta * cos_theta_t < 0
is equivalent toeta1 * cosThetaI < eta2 * cosThetaT
.Yes, I've noticed that and updated the code accordingly.
What I meant is that the current code does
but I think it should be
(note the sign swap in the denominator). This only sign-swaps
r_p
, which doesn't matter currently since the code only returnssqr(r_p)
.Yes, you are right, I was not paying attention to the denominator. I remember switching the order was only to make the two lines have the same structure because the sign didn't matter. Sorry for the confusion 😄
@ -238,0 +331,4 @@
/* Compute angle inside the thin film (after refraction at the top interface). */
float cosTheta2_sq = 1.0f - sqr(eta1 / eta2) * (1.0f - sqr(cosTheta));
if (cosTheta2_sq < 0.0f) {
/* TIR at the top interface. */
I believe we should trust
fresnel_dielectric_polarized()
instead of checking TIR before calling the function.@ -261,0 +261,4 @@
/* Panel for Thin Film settings. */
PanelDeclarationBuilder &film = b.add_panel("Thin Film").default_closed(true);
film.add_input<decl::Float>("Thin Film Thickness").default_value(0.0).min(0.0f).max(100000.0f);
Specify the unit (nm).
Good point. If we're already handling units, might as well do it properly: I've created #120900 for this, depending on what PR gets merged first I'll just update the other to tag this input correctly.
Default value for float socket is
0.0f
by default, no reason to make this explicit.Max value also does looks like meaningless (what for such soft max?)
I think it's easier to understand the code with the default value explicit.
For soft max indeed
FLT_MAX
should generally be used if there is no real upper bound. But this can sometimes cause numerical precision problems, so not sure it works here.I mean, at some point such declaration can be cleaned due to the fact this is not really necessary.
@ -261,0 +263,4 @@
PanelDeclarationBuilder &film = b.add_panel("Thin Film").default_closed(true);
film.add_input<decl::Float>("Thin Film Thickness").default_value(0.0).min(0.0f).max(100000.0f);
#define SOCK_THIN_FILM_THICKNESS_ID 28
film.add_input<decl::Float>("Thin Film IOR").default_value(1.3f).min(1.0f).max(1000.0f);
I think we could default to the IOR of water, which is 1.33.
0dcf334003
to5921fdfc56
@ -390,3 +425,1 @@
float z = sqrtf(fabsf((bsdf->ior - 1.0f) / (bsdf->ior + 1.0f)));
s = lookup_table_read_3D(
kg, rough, cos_NI, z, kernel_data.tables.ggx_gen_schlick_ior_s, 16, 16, 16);
/* Precomputing LUTs for thin-film iridescence isn't viable, so fall back to the specular
For cognitive ease I would prefer to add an empty
if (fresnel->thin_film.thickness > 0.1f)
branch and put the comment there, the following block is then put in theelse
branch@ -39,0 +56,4 @@
}
/* Return squared amplitude to get the fraction of reflected energy. */
return make_float2(sqr(r_s), sqr(r_p));
The newly added
sqr()
is not used, I assume you wanted to use it here?At first I thought that this needs to return un-squared coefficients, so I used
average(sqr(fresnel_dielectric_polarized(...)))
infresnel_dielectric
. But with the code as it is now, I don't thinksqr(make_float2(r_s, r_p))
is much nicer thanmake_float2(sqr(r_s), sqr(r_p))
, so we might as well not add it at all I guess.@ -39,0 +64,4 @@
float eta,
ccl_private float *r_cos_theta_t)
{
return average(fresnel_dielectric_polarized(cos_theta_i, eta, r_cos_theta_t, NULL));
We use
nullptr
now.Also,
average()
forfloat2
doesn't seem to be defined on metal 😕Ah, okay, I wasn't sure if that's supported on GPU but looks like it is.
I think the kernel is all C++ now, but not feature complete on some GPUs, not sure what exactly is supported but if buildbot doesn't complain it should be fine.
@ -500,2 +542,3 @@
template<MicrofacetType m_type>
ccl_device Spectrum bsdf_microfacet_eval(ccl_private const ShaderClosure *sc,
ccl_device Spectrum bsdf_microfacet_eval(KernelGlobals kg,
const ShaderClosure *sc,
This does not compile on Metal. Add
ccl_private
.@blender-bot build +gpu
The GPU tests are known to be failing currently, for everything except Cycles. So the reported failures from the latest run can be ignored.
4453b67e5c
tod149ae8550
Looks beautiful!
Should we have a test file for this?
Yes, I can think of a few cases:
The first 5 can probably be combined into one file. I'll put something together.
I've been looking forward to this for a week now -- still working on it?
The test files won't be particularly useful from a user perspective, they're just for development/CI use.
There's really not much to using the feature - you just set the main IOR as usual, then you set the IOR of the thin film, then you set the thickness of the film. From there, it's just a matter of tweaking the values to get the desired look.
If the usage doesn't seem straightforward, we can maybe list some example phenomena and the value ranges in the manual? Or even provide a .blend file, we do have it for Glossy BSDF.
@KickAir8p I would imagine it's more like there is a known phenomenon the user wants to model, then they could go search for the required values for base IOR, thin film IOR and film thickness. For example, if you want to model a soap bubble, the base IOR is 1.0 (air), the film IOR is 1.33 (water), and the film thickness is between 10 and 1000nm; for rock dove neck feathers the film IOR is 1.55 (keratin), the thickness is somewhere between 400 and 600nm, then you plug in some absorbing volume inside. There are also iridescent phenomena not explained by thin-film interference but can be approximated nonetheless, so the user just tweak the IORs and thickness until the material has roughly the desired look.
Or the new Ray Portal BSDF -- I've found the test .blend files provided for that to be particularly helpful.
I'm not asking for devs to demo every conceivable use. But it seems like there's frequently a disconnect between the technical explanations and translating those into boots-on-the-ground usage that a few basic examples in blend files can go a long way to bridge. On your soap bubble point, the top post has a picture (photo?) of a soap bubble with the assertion that this node replicates some aspects of the effect. A blend file accomplishing that would be a substantial adjunct to giving numbers in a post, especially since the blend would (of necessity) include info on the requirements of the mesh that the node is intended to shade, recommended lighting (hello Blender's forest.exr), etc.
Maybe this seems so intuitively obvious to the people who created the thing that they think only a blithering idiot would need an example blend file to get the point. Please note my blithering in this post, and this idiot will be happy to provide more if it would help.
@KickAir8p Here's a guide on how to create a scene using thin films, focused around the bubble.
Here is the .blend file I created: Thin film bubble example.blend
When you start playing around, you will notice some things.
You might be wondering, why do I use a IOR of 1.33 while the original pull request uses a IOR of 1.7 for the soap bubble. The original pull request based their example image on the example image provided in a paper about thin film rendering. And the paper probably just used an IOR of 1.7 to exaggerate the effect. I used a IOR of 1.33 because that's water. Maybe the IOR of a soapy water is 1.7 due to the soap?
Appreciate it, details here.
Edited to add: test render with blend file (CC0 Public Domain) that near-replicates real-world effect is up here if it'd help.
Very nice work @LukasStockner! I just today stumbled upon this feature, but already tried it out on an example image from my Blender Conference 2022 presentation (linked as you might remember :) ).
For reference, the result of my node implementation:
And here's the result of the Principled BSDF implementation:
I think they're very close! What impresses me even more is that your implementation finished rendering 9.4 times quicker. I'm looking forward to seeing this supported on metals as well, but very impressed so far!
The only thing I can't figure out, is the seemingly absent second 'hot spot' (reflection of the sun) from the inside of the bubble when rendered with the Principled shader.
I double-checked by turning off the HDRI that illuminates the scene, and replaced it with a sun lamp. I set the sun lamp to a power where it saturates the camera. In that case, the nodes implementation yields a significantly brighter reflection (~1000x) from the inside of the surface than the Principled implementation. See here (values of bottom highlight shown in screen shot):
Nodes:
Principled:
Is this what we should expect, or might there be something going?
In case it's useful for anyone, here's the file with the HDRI and sun lamp + both thin film materials
Cheers,
Robert