- Home /
Normal map mask
I am using glass distortion shader from standard assets. I would like to apply mask to normal map, but i don't know how. It's easy to make a mask for texture, but i don't know how to make it with normal maps. I could just apply a mask in external app like Gimp, but i want it to be applied at runtime, because i want to change normal map offset (animate it) without losing mask. I don't want transparency but just applying black&white/transparent texture to normal map to make it flat in certain places.
Example:
1) I have this normal map as input image: Click
2) I use this image as mask: Click
3) Generated normal map: Click
And then it just uses new normal map in output.
Here is a default shader: Click
Can anyone help mi with that?
Answer by Bunny83 · Mar 03, 2016 at 03:32 PM
First of all your "normal map" texture isn't a normal map. It's a bumpmap. However Unity doesn't really use bumpmapping but normalmapping. The Unity editor usually converts imported bumpmaps into normalmaps. While a bumpmap is just a grayscale heightmap, a normal map contains actual normal vectors where each component of the vector is encoded in one of the color channels.
When a shader uses a normal map it will sample the normal map and use the unpacked vector as normal vector for lighting calculation. To "mask" a normal map at shader level you just need to do this in the fragment / surface shader:
grab the usual interpolated vertex normal (let's call it "N")
grab the unpacked sampled normal from the normal map (let's call it "M")
sample your "masking texture" to get a value between 0 and 1 (let's call it "i")
Lerp between "N" and "M" using "i"
normalize the result and use it as normal vector.
Answer by salamdaher0 · Feb 10, 2018 at 06:03 AM
@miszelfox , did it work? I am looking to make the same thing but I am not sure how to write it. do you have a sample code you can share? Thank you
Your answer
Follow this Question
Related Questions
a shader or decal that only renders the normal map 0 Answers
Shader issue: Normal map from tangent to world space - Textures flipping? 0 Answers
Mobile Bumped Diffuse problem 1 Answer
Layering multiple Normal maps into one without blending in a Surface shader script 0 Answers
Two bump map shader 0 Answers