Lack of total internal reflection in refract #50549

Closed
opened 2017-01-29 05:38:08 +01:00 by Chris Cook · 11 comments

System Information

Blender Version
Broken: 2.78 4bb1e22

Short description of error
Currently, for k values less than 0 in the refract function, a vector of 0,0,0. This should return a reflection. In combination with fixing the Fresnel node with values under 1, this would allow for accurate total internal reflection.

Exact steps for others to reproduce the error

Create icosphere, set shading to smooth, assign refraction BSDF, view from inside sphere.

found at line 193 in stdosl.h

  vector refract (vector I, vector N, float eta) {
      float IdotN = dot (I, N);
      float k = 1 - eta*eta * (1 - IdotN*IdotN);
      return (k < 0) ? vector(0,0,0) : (eta*I - N * (eta*IdotN + sqrt(k)));
  }

correction should be something like

  return (k < 0) ? reflect(I,N) : (eta*I - N * (eta*IdotN + sqrt(k)));
**System Information** **Blender Version** Broken: 2.78 4bb1e22 **Short description of error** Currently, for k values less than 0 in the refract function, a vector of 0,0,0. This should return a reflection. In combination with fixing the Fresnel node with values under 1, this would allow for accurate total internal reflection. **Exact steps for others to reproduce the error** Create icosphere, set shading to smooth, assign refraction BSDF, view from inside sphere. found at line 193 in stdosl.h ``` vector refract (vector I, vector N, float eta) { float IdotN = dot (I, N); float k = 1 - eta*eta * (1 - IdotN*IdotN); return (k < 0) ? vector(0,0,0) : (eta*I - N * (eta*IdotN + sqrt(k))); } ``` correction should be something like ``` return (k < 0) ? reflect(I,N) : (eta*I - N * (eta*IdotN + sqrt(k))); ```
Author

Changed status to: 'Open'

Changed status to: 'Open'
Author

Added subscriber: @smilebags

Added subscriber: @smilebags

Added subscriber: @SteffenD

Added subscriber: @SteffenD
Member

Added subscribers: @LukasStockner, @Blendify

Added subscribers: @LukasStockner, @Blendify
Member

@LukasStockner said that he would take a look.

@LukasStockner said that he would take a look.
Member

I don't really agree that it should return a reflection - after all, it's explicitly called refract, not bounce_off_glass. So, when refracting isn't possible, the function shouldn't attempt to guess what you might want instead.
Another consideration is that you'll probably want to use fresnel weighting between reflection and refraction anyways, and in the case of TIR the refraction weight will be zero.

And even if it was wrong behavior, we can't just change functions that have been in releases in the past since it would mess up shaders that have been written with the old behavior in mind.

If you really want the behavior you're describing, you could just wrap refract() with a helper function that checks the result and returns reflect() instead.

I don't really agree that it should return a reflection - after all, it's explicitly called refract, not bounce_off_glass. So, when refracting isn't possible, the function shouldn't attempt to guess what you might want instead. Another consideration is that you'll probably want to use fresnel weighting between reflection and refraction anyways, and in the case of TIR the refraction weight will be zero. And even if it was wrong behavior, we can't just change functions that have been in releases in the past since it would mess up shaders that have been written with the old behavior in mind. If you really want the behavior you're describing, you could just wrap refract() with a helper function that checks the result and returns reflect() instead.
Author

I understand your point of view and agree that this function shouldn't be changed after reading your comment.

Should, then, the Fresnel node be corrected to produce the right value for rays coming out of a material? I feel like the physically correct way of rendering refractive materials (especially when it is computationally not much more expensive) shouldn't require coding for the user.

Currently the Fresnel node doesn't give correct values, and therefore can't be used by its-self to create Total Internal Reflection. All other aspects of this node are physically accurate, and I don't see this change being detrimental to existing materials.

I understand your point of view and agree that this function shouldn't be changed after reading your comment. Should, then, the Fresnel node be corrected to produce the right value for rays coming out of a material? I feel like the physically correct way of rendering refractive materials (especially when it is computationally not much more expensive) shouldn't require coding for the user. Currently the Fresnel node doesn't give correct values, and therefore can't be used by its-self to create Total Internal Reflection. All other aspects of this node are physically accurate, and I don't see this change being detrimental to existing materials.

Added subscriber: @brecht

Added subscriber: @brecht

The Fresnel node was primarily intended for mixing diffuse and glossy, since the glass BSDF already includes fresnel. For correct handling of multiple scattering and roughness you also need the Fresnel to be baked into the BSDF anyway, doing it using custom nodes is not physically correct.

The problem is that those two cases require different handling of normals, for glass you want to invert the IOR when exiting the object, for mixing diffuse and glossy you don't want that.

So you'd need to do something like adding an option to the Fresnel node to choose the behavior. But really I would prefer to eventually remove the Refractive BSDF node, since it's not possible to use it in a physically correct way.

The Fresnel node was primarily intended for mixing diffuse and glossy, since the glass BSDF already includes fresnel. For correct handling of multiple scattering and roughness you also need the Fresnel to be baked into the BSDF anyway, doing it using custom nodes is not physically correct. The problem is that those two cases require different handling of normals, for glass you want to invert the IOR when exiting the object, for mixing diffuse and glossy you don't want that. So you'd need to do something like adding an option to the Fresnel node to choose the behavior. But really I would prefer to eventually remove the Refractive BSDF node, since it's not possible to use it in a physically correct way.
Author

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Chris Cook self-assigned this 2017-02-05 15:47:27 +01:00
Author

Thanks for the insight, @brecht, it hadn't occurred to me that it would be used for that, but it makes sense, as its current values mean a diffuse/glossy mix looks the same on both sides.

I would prefer to eventually remove the Refractive BSDF node, since it's not possible to use it in a physically correct way.

Okay. I have been playing with adding dispersion to refractive materials, both with node groups and with OSL, and both methods rely on the Refraction BSDF to work. It has been a goal of mine to get glass looking better in cycles (at the expense of samples)

As for Total Internal Reflection, adding a 'refractive' boolean option to the Fresnel node would be sufficient for purely smooth glass, but as you said, won't fix the issue for rough surfaces.

I will close this case as I've realised the issue is not in the refraction, but would like to continue discussing a backwards-compatible solution to this, as I believe it would be a great addition to a reasonably visible problem. I'm happy to help implement the changes.

Thanks for the insight, @brecht, it hadn't occurred to me that it would be used for that, but it makes sense, as its current values mean a diffuse/glossy mix looks the same on both sides. > I would prefer to eventually remove the Refractive BSDF node, since it's not possible to use it in a physically correct way. Okay. I have been playing with adding dispersion to refractive materials, both with node groups and with OSL, and both methods rely on the Refraction BSDF to work. It has been a goal of mine to get glass looking better in cycles (at the expense of samples) As for Total Internal Reflection, adding a 'refractive' boolean option to the Fresnel node would be sufficient for purely smooth glass, but as you said, won't fix the issue for rough surfaces. I will close this case as I've realised the issue is not in the refraction, but would like to continue discussing a backwards-compatible solution to this, as I believe it would be a great addition to a reasonably visible problem. I'm happy to help implement the changes.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
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
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
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
EEVEE & Viewport
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
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
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
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#50549
No description provided.