Creating a Dispersion Shader in Cycles (CGC Weekly #17)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this week on CGC weekly will be improving the glass shader tenfold by adding a little touch called dispersion hello everybody and welcome back to another episode of CGC weekly here on the CG cookie blender training YouTube channel as stated in a little teaser segment before the intro we are going to be creating a dispersion shader in blender but you might be wondering what is a dispersion shader and how does it differ from a standard glass shader let's first look at what dispersion is odds are at some point in your life you've dealt with some sort of glass prism whether that be like an actual we'll just straight-up cube that's glass or something more like the edge of a window that might be beveled or something like that odds are you might have noticed something cool as well when you look at the corner of say a beveled piece of glass you may notice that light when it passes through kind of gets broken up into a rainbow this effect is called dispersion by default cycles and all of its default glass or transparent shaders don't support dispersion because it's something that's very intensive but it can get you some really cool results before we start creating the shader though I want to first talk about why dispersion occurs that way while we're creating the shader you guys actually understand what's going on however if you just want to just make the shader and you don't want to really learn about it I mean I guess you can click the time or skip to the time stamp that's displayed right here but I'd strongly advise sticking around through the little it'll be like a two minute physics lesson and that's all anyway I'm gonna hop over the whiteboard to get started oh hello whiteboard how kind of you to greet us today so basically this cube is glass we'll assume and an incident light ray or a light ray that hasn't been affected by anything comes in to our cube like so just like that this light ray just enters and when it reaches this boundary right between air and glass something occurs because glass allows light to be transmitted through it the light refracts so a fraction is something that occurs when light reaches a boundary between two different materials or mediums of different indices of refraction in this case air being you know very non optically dense that it is has an iowa or n of one point zero zero zero three which we can pretty much just assume is one point zero zero so we'll just ignore that zero three part at the end the cube of glass however has an iowa of say one point four five zero and i know it's gonna be kind of hard to see from where you are cuz you guys are kind of far away but that's what it says n equals one point four five zero so basically this light travels from one medium to another and when light transfers through different mediums it's speed changes that's right the speed of light isn't exactly what you think of it actually isn't constant at all light can actually be slowed down quite a bit by changing the medium that it's passing through now the speed of light that most of you probably know which is three hundred kilometers per second is the speed of light through a vacuum where the IOR is equal to one a little tidbit of information that's kind of important here is that iya waar which will use the letter n for is equal to c the universal constant or the speed of light divided by the velocity of light in whatever medium it's in so I'll use VM there as our variable for those of you who are math savvy you know that means that IOR is a ratio of the speed of light to the speed of light through whatever medium it's going through so since the speed of light in a vacuum is the speed of light the ratio is one point zero zero or you know an IO R r1 but when light is traveling through whatever medium this is which has an IO R of 1.45 it's actually moving at about 2/3 the speed of light in a vacuum so that's just a little overview of what IO are is so because in this place light is passing from an area of a lower io r to an area of higher IO R that means that the light is slowing down and the light will actually Bend for example think about having a cup of water or you know pop or whatever and you have a straw sitting in it if you have that straw sitting in it and you look at the glass from the side odds are that that straw will look completely detached from the straw that's sitting above the water or whatever liquid you have in the cup and this is because water or whatever liquid you have in the cup has a different IOR than the surrounding air so in this case because the io r is increasing the angle at which the light is traveling will increase greatly so before we have a pretty you know standard angle here but by allowing it to pass through it becomes significantly steeper now at the other boundary right we've hashed through one boundary here and then it comes across this other boundary and here light refracts once more this time though it's going from a high io r to a lower I ou R meaning it'll have the opposite effect that it has here so instead of the angle becoming steeper the angle will become slightly less about a Bing badda boom that's a curvy line doesn't matter so basically this is how refracting works here's an interesting tidbit though not all light refracts the same way when it enters a medium and what I mean by that is different IOR s correspond to different wavelengths or colors of light I once again drawn our cube appear that we'll be using to refract light through so let's say we have an incident ray of white light right and for those of you who are familiar with light you'll know that white light is actually the combination of all the different colors of light right its main components though are red green and blue because these are what are called the additive primary colors if you have light and you have a red a green and a blue light all signing on the same exact point that light will become white so anyway this is white light and it comes to our boundary here and as I mentioned not all colors of light will refract at the same rate so N equals one point four or five zero is just for one specific color of light in this case we'll assume that light is green so I'm going to drill head and grab my green marker here and I'll draw in our green line that has been refracted so there we have green light and you know green light school and all but what about the other two colors I just mentioned red and blue well red light usually doesn't refract quite as much as the others so we'll draw this with a little bit less steep of an angle that kind of maintains the existing angle and blue light having a very high frequency or a very small wavelength will refract a lot so this light will move down just like that so our light rays now encounter our second boundary well obviously they need to regain this same exact shape right because they're entering a different IOR so we'll let that light go on its happy way through just like so finally our red light which is actually pink marker but I couldn't find a good red marker so basically what we have here is white light entering the light splitting due to an effect called dispersion and then getting spit out in three separate areas and if we look at this and it is some sort of like you know a real world counterpart it might look something like this it begins to form a rainbow and of course these colors would blend a little bit differently it would be you know red orange yellow green blue indigo violet all the way you know down to those but there you have it so we can kind of determine that red light when it hits a boundary has a lower IOR than the rest green light kind of has the middle ground IR and that blue always has the higher IOR so let's hop back to my desk over here and we'll start actually creating the dispersion shader because I know that's kind of the point of this video and stuff hopefully that little lesson on physics wasn't too unbearable I know that sometimes physics and math can get a little bit boring but I think it's interesting and it helps us a lot when we're building shaders so I opened a blender here and because this is going to be a cycles dispersion shader we do need to switch over from blender under two cycles rendering engine because that's marginally important and what we'll do to get started is I'm actually going to leave everything in the scene like it is I'm just going to split my window and - and get rid of these panels by pressing T I'm gonna switch this left panel over to be the node editor and we'll get rid of that panel by pressing N and we'll start working on a material for our cube period alright so with my cube selected you can see I have the default material loaded up just a diffuse shader going into the surface input so let's think about this in terms of physics right as we talked about before each different color of light has a different index of refraction so we need to create three different glass shaders each corresponding to one different wavelength of light in this case we use red green and blue because those are the additive primary colors to create our shader so we'll start off by creating three glass shaders so I delete my diffuse shader here actually we'll make this a little bit bigger because we don't need that much space over there we'll add a glass shader and then we'll duplicate it by pressing shift D not shift tab shifty there you go and then all we'll do is add all three of these glass shaders together actually note before we do that let's assign them to their colors that way we don't lose track of them the top one is going to be purely red so in this RGB section here I'm just going to drop the green and the blue all the way down to zero this one is going to be green so we'll drop red and blue down to zero and then this last one is going to be pure blue so we'll drop it like that and make sure that whatever value that your color is in this case for example green make sure that's set to one I think by default it's sometimes set to 0.8 make sure that it's set all the way to one because we want pure green and we want the full value of the color if we switch in the rendered mode over here for one I have an HDR I loaded up on this but that doesn't really matter whether or not you have an HDR I loaded up your cube should be black because we have nothing connected to our material output what we're going to do is we're going to use add shader nodes to combine all three of these shaders together so at first we'll add the red and the green shader together using this add shader node and then we'll duplicate this add shader node plug the old add shader node into the top input of the new add shader node and then plug the blue into the bottom input of new add shader node then we'll take that shader output and plug it into the surface and just like that we have created probably the worst glass shader in the history of glass shaders because it is so incredibly horribly optimized I mean look at how noisy this cube is that is just atrocious but the trick is changing the IOR s right we talked about when we were over at the whiteboard the fact that each color has a different index of refraction when it's interacting with some sort of new medium but in this case all the three colors I ORS are set the same they're all set to 1.45 which means that when these light particles refract they're just refracting in the same path that the others do and we want to break that apart so that they can disperse so in order to do that we're going to set the red IOR slightly lower than the green IOR and the blue irr slightly higher than the green IOR so basically the green IOR is going to be our base IOR then the red is going to be slightly lower and the blue is going to be slightly higher let's try this out we'll change these we'll use an IO our offset of 0.05 so this will become 1.4 because it is 0.05 less than our based io r and we'll set this to 1.5 because it's point 5 higher than our base Iowa and by doing this you can begin to see really cool effects of dispersion occurring at the edges of our cube and this is so cool this is when I first figured out how to do this I was so excited because I'm a total physics nerd and seeing this and seeing dispersion replicated in an engine that doesn't support wavelengths is just it's cool it's cool anyway we need to make this a little bit easier to work with because right now we'd have to manually set each individual Iowa so in order to create a more encompassing I don't know what to call it a node group a more encompassing node group we're going to actually add in some like automation basically so that we can set one base IOR and the red and the blue io RS will be you know calculated on their own so in order to do this I'm first going to create a node group I'll press B to open up my box select tool and select all the nodes except for the material output node then I'll press ctrl G ctrl G makes a node group and this gives our group output node over here and our group input node over here so there's a few things that we want to stay constant between all three of our glass shaders and that's roughness and normal right so all we'll do is we'll connect those first so we'll click and drag from this blank group input up to roughness will connect roughness to all three of our glass shaders because we want all three of our glass shaders to have the same roughness there's no reason they should differ and the same goes for normal so collect connect the normal to all three of these so you can see we just have one roughness input and one normal input that connects to all three of these shaders and if we press tab to edit out of the group node you can see here that we had that roughness of normal input now in the shader output if whatever reason this shade or output didn't get hooked up automatically all you need to do is just click and drag this onto this blank dot of course that blank dot will be one higher but it should automatically do that if you follow it exactly what I did there all right cool so now we have our colors and our i/o our inputs left and we're actually not going to change the colors of our glass because we have to have these red green and blue values in order to accurately you know represent our dispersion so all we need to do is compensate for this i/o our offset so in order to do that we'll use math nodes all right so I'm gonna move this group input back quite a bit so we have some extra room I'm going to press shift a come down to converter math and then we'll drop that in and right now it's set to add which means that we can offset something by a certain amount so let's start by getting our IR input I'll click and drag from this blank input over here again and hook it up to the green IO our input this will add our base Iowa and we can work with this IOR to either create a higher IOR for one shader or a lower IR for another and we can use these math nodes to choose how much we want to affect that so if I plug in this IO R into the top input of our add shader up here right and I select subtract from the mode and change it to 0.05 and hook up this output into the input ILR shader of our glass basically that's taking the input value of 1.45 zero bringing it over here subtracting didn't mean to check that subtracting zero point or point zero five from it right which means it's now one point four and hooking that into this shader so before remember we had a IOR of one point four here and right now we're still getting an eye aware of one point four we can do the same thing except you know in the opposite direction by duplicating this node and moving it down here changing it to add hooking up this i/o our input into the top and hooking up the value into our io our output or our i/o our input into our blue glass shade over here and now we're just adding 0.05 so the input value of one point four five comes in point zero five is added to it so it's now one point five and it comes out into our glass shader so now we can change the base IOR to whatever number we desire for example eight but who would that's like not okay to for example or one point one all sorts of different things and we can get accurate dispersion from any i/o R that we throw at it but let's add one more layer of automation into it and let's add a way that we can control how much we want things to be dispersed this is actually pretty simple all we need to do is add another input down here at the bottom so I'll click and drag from this and hook it up to the bottom input of our subtract snowed up here and the bottom input of our add node down here this will allow us to control dispersion let me set this back to one point four five here so right now our dispersion value is set to zero point zero five but if I wanted this value to be higher say 0.1 and I wanted to disperse more we can change this value up more and more to create more and more dispersion in our glass shader just like this of course the name value is a little bit confusing so let's actually give this a name I'll press tab to go back into edit mode I'll press n to open up the side panel and we'll make it a little bit bigger here you can see all the different inputs and outputs from our node group right now I want this value to be changed I want to change its name to dispersion right and this is going to change the strength of this this version I'm also going to drop the normal input down to the very bottom because that's where it is on most nodes as you can see here so basically we have a roughness input and IO are input a dispersion input and a normal input and then a shader output and you can see this on our node right here so now we can basically change this to whatever value we want so if I wanted this to be have an IO R of 1.8 and a dispersion factor of 0.5 you can see that we get these really strong dispersion effects throughout our entire model and if we wanted to we could increase the roughness to say 0.1 and we get this really cool looking cube um there's all sorts of things you can do with this so this is a really awesome shader so hopefully you guys found this trick interesting I know it's one of my favorite shaders to use I use it like all the time and hopefully you guys can adapt this into your own workflows if you guys like these tips and you want more blender content don't be afraid to head over to see Chico be calm and hook yourself off with a citizen subscription there's tons and tons of blender kata like tons of blender content available to you guys to learn from
Info
Channel: CG Cookie
Views: 75,779
Rating: undefined out of 5
Keywords: light, physics, dispersion, rainbow, glass, real, realistic, lens, distortion, aberration, chromatic, chroma, abberation, shader, group, node, nodes, editor, refraction, transmit, transmittance, refract, diffract, diffraction, prism, tutorial, blender, training
Id: RiWn3i-TWQQ
Channel Id: undefined
Length: 17min 56sec (1076 seconds)
Published: Thu Jul 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.