Hi there!
I am trying to create an OSL shader that simulates realistic diffraction grating - like you would find on CDs or holographic stickers. I would love help creating an OSL shader for this as I have very limited coding experience.
I have tried a lot of various approaches manually (without OSL shaders) to create the specific iridescent grating effect with some success, but unfortunately it is not accurate under all lighting conditions and breaks when viewed from certain angles. I have seen users of other 3D programs share diffraction grating OSL shaders, however they unfortunately don't work inside of Octane / C4D. I can share those OSL shaders if it would be a helpful jumping off point.
I imagine the OSL shader would plug into the "film" slot with black to white float values to control the effect.
Here are some examples of my faux grating effect I created using manual methods.
Any help would be greatly appreciated!
Thanks
Diffraction grating OSL Shader
Moderators: ChrisHekman, aoktar
Hi,
if you search for BGnoise in node editor, you can find a very old osl script I have written, that could be useful in this case: You need to change the mode to Vertical, and a NoiseType different from Cell, then create a Projection node in Spherical mode, and play with BGnoise values to obtain the effect you want.
Sorry, the sliders are not correct, so better to use numbers.
ciao,
Beppe
if you search for BGnoise in node editor, you can find a very old osl script I have written, that could be useful in this case: You need to change the mode to Vertical, and a NoiseType different from Cell, then create a Projection node in Spherical mode, and play with BGnoise values to obtain the effect you want.
Sorry, the sliders are not correct, so better to use numbers.
ciao,
Beppe
- adzrozanski
- Posts: 10
- Joined: Wed Apr 12, 2017 9:30 pm
Thanks for the reply. That is a great little shader!
Unfortunately it doesn't imitate the properties of diffraction grating in motion. What I'm hoping to brainstorm is a method to achieve the shifting of colors across the surface to imitate real diffraction grating caused by a surface with microfacets.
This is a diagram I pulled off the internet that gets across how the Angle of Incidence affects what part of the wavelength is visible. I tried doing this using a Falloff / Fresnel on the film channel and a very tiny radial bump, which achieved the effect partly, still not entirely convincing though.
Unfortunately it doesn't imitate the properties of diffraction grating in motion. What I'm hoping to brainstorm is a method to achieve the shifting of colors across the surface to imitate real diffraction grating caused by a surface with microfacets.
This is a diagram I pulled off the internet that gets across how the Angle of Incidence affects what part of the wavelength is visible. I tried doing this using a Falloff / Fresnel on the film channel and a very tiny radial bump, which achieved the effect partly, still not entirely convincing though.
- Attachments
-
- DifGrating.png (7.03 KiB) Viewed 987 times
- adzrozanski
- Posts: 10
- Joined: Wed Apr 12, 2017 9:30 pm
Quick update:
I have been refining my initial shader and I think this is getting much closer to the actual effect. I don't think it is still physically accurate but I tried to use the science behind iridescent materials to get this result.
What I have done and why:
- Colorful Diffraction: I added a Falloff to the Film channel because color should be dependent on viewing angle of each point on the surface. Film IOR is set to 1. I kept the falloff type to eye ray and moved up the minimum value (here it is 0.5) to bring some color repetition onto the surface.
- Gratings / Ridges: In the Bump channel I use a Sinewave node set to spherical projection to get concentric rings (to imitate the microfacets/grooves of a CD) and set the scale in the transform to something really small so there are many concentric rings to the point that they are no longer visible / distinguishable. I the Color Correction node to this and set the gamma to 2.2.
Overall it's getting there. Would love to see some other approaches that achieve a more physically accurate outcome!
I have been refining my initial shader and I think this is getting much closer to the actual effect. I don't think it is still physically accurate but I tried to use the science behind iridescent materials to get this result.
What I have done and why:
- Colorful Diffraction: I added a Falloff to the Film channel because color should be dependent on viewing angle of each point on the surface. Film IOR is set to 1. I kept the falloff type to eye ray and moved up the minimum value (here it is 0.5) to bring some color repetition onto the surface.
- Gratings / Ridges: In the Bump channel I use a Sinewave node set to spherical projection to get concentric rings (to imitate the microfacets/grooves of a CD) and set the scale in the transform to something really small so there are many concentric rings to the point that they are no longer visible / distinguishable. I the Color Correction node to this and set the gamma to 2.2.
Overall it's getting there. Would love to see some other approaches that achieve a more physically accurate outcome!