Fresnel/Edge shader problem
Greetings,
I am having a weird problem with my custom fresnel shading code.
It apparently works correctly as long as in-game object doesn't have any rotation applied to it.
Shader code: http://pastebin.com/ZSsdk0py
As you can see I'm picking the view vector (as per materials on the net) with this function:
float3 direction = normalize(WorldSpaceViewDir(v.vertex));
Material Preview and expected output (Yes I am aware that having majority of work in Vertex part isn't pixel perfect result, this is what I want):
But in-game, it only really works on objects that have not have any rotation applied to them (and even then it doesn't appear to be perfect): The right sphere was rotated by some 85° over Y. (To the left basically).
I have tried changing the direction code to this:
float3 direction = normalize(_WorldSpaceCameraPos - v.vertex);
intensity = 1 / dot(direction, v.normal);
But it still produces the same result somehow. How can I obtain the actual camera->vertex vector?
Thank you!
Answer by Eudaimonium · Mar 14, 2016 at 03:47 PM
I figured it out:
We need local space, not world space:
float3 direction = normalize(ObjSpaceViewDir (v.vertex));
intensity = 1 / dot(direction, v.normal);
If anybody needs it, here is the pixel-shader version of the shader, with pixel perfect results:
Thank you for posting the shader for us. Will this work for cubes too? And is it mobile friendly?
It does work with cube-y objects, depending on what you want to use it for. Best try it out for yourself and see if it works for you.
Not sure what you mean by "mobile friendly". It's not any larger performance hog than any other alpha/transparency effects such as various particle materials.
Your answer
Follow this Question
Related Questions
Normal mapping distance of view? 0 Answers
looking for a substitution for Shader "Debug/Vertex Color" 0 Answers
How to add light maps to a custom Rflection shader 0 Answers
"particle standard surface" shader build error, PC 4 Answers
Creating a 3D portal effect using stencil buffer with a deferred shading setup? 1 Answer