I have a sphere I want to have a glow / radial gradient look and it seems like Rim Lighting is the way to go, but almost all examples take viewDir into account. I'm trying to get the same lighting at any angle: So what's the best way to achieve this? Thanks!
I needed to achieve a similar effect and I did a lot of scouring on the internet for examples since I'm still very new to shaders, but I couldn't find any solution. I ended up using the depth map to find the edges of the models and interpolated the colour using that, but that only worked because of the special use case this was for (the camera never moved and the models were the only thing in the scene). I doubt that your use case is similar, so I hope someone more knowledgeable can help you out - I'm very interested to hear a proper solution for this myself.
did you try using the camera vector ? something like o.viewDir = UNITY_MATRIX_IT_MV[2].xyz; if you want a rim effect, that should work, but if you want an inner glow , not sure that will do what you want. i'm NOT a shader specialist but maybe you should explain more clearly what you want to achieve.
Are you looking for a "baked in Fresnel" look? If so you can used a basic Fresnel calc but with any direction passed in(not view direction). Or are you just going for the Fresnel/rim lighting look so it always looks like your above example? If that's the case then you need the view direction. Taken from Unity docs: Code (CSharp): half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal)); o.Emission = _RimColor.rgb * pow (rim, _RimPower); And for constant direction: Code (CSharp): half rim = 1.0 - saturate(dot (normalize(_MyDirVector.xyz), o.Normal)); o.Emission = _RimColor.rgb * pow (rim, _RimPower);
echo4papa, i get a question: what is frensel/rim? o.Emission= _RimColor.rgb* pow (rim, _RimPower); why use pow (rim, _RimPower);??
Ah. More info here: http://en.wikipedia.org/wiki/Fresnel_equations It's what makes glancing angles look more reflective than straight on angles for some materials. It can can also be used for rim lighting. The reason you use a pow is because it let's you use a shorter range for _RimPower to control the strength of the effect. You don't need it if you don't want it.
That's a custom variable you would need to make. Declare it like this in your shader: Code (CSharp): uniform float4 _MyDirVector; and then in C# set it like this: Code (CSharp): SomeMaterial.SetVector("_MyDirVector",SomeVector4); SomeMaterial is the reference to the material of your mesh and SomeVector4 is a vector4 containing the direction you want.
@echo4papa thanks for the code. What I'm trying to do is something like this: So, I think if the IN.viewDir vector can be changed to be looking a little bit downward, it will create this effect, but can't figure out how to do it. So please tell me how can I do this.
If you want it to always look like that, you can either modify the ViewDir vector in the shader or the camera vector in C#. Either way you'll probably want to make a rotation matrix, probably in C#. Either pass it into the shader and multiply it against ViewDir or you can take the camera vector and multiply it against the rotation matrix in C# and pass in the result as _MyDirVector. Hope that helps.
@echo4papa thanks for the info, can you please tell me how can I modify the ViewDir in the shader? I don't know much about shader coding. How can I define a vector which will be a little downward looking and multiply it with the ViewDir?