- Home /
Add a slider to bumped specular shader for normal strength?
I want to add a slider to the bumped specular shader that adjusts the strength of the normals. I created a slider for the shader with the following code:
File: Normal-BumpSpec.shader
_NormalStrength ("Normal Strength", Range (0.01, 1)) = 0.5 //line 8
half _NormalStrength; //line 22
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); // original of line 33 (now 35)
//o.Normal = _NormalStrength; //attempt to modify normal strength based on slider - replaced previous line of code
o.Normal = _NormalStrength;
does not properly affect the power of the normal. What is the correct way to set this?
Answer by ScroodgeM · Jul 14, 2012 at 09:16 AM
o.Normal is a vector, not a color, so you can't just multiply it by a number. you need to turn it over surface original normal and be sure it stays normalized. one of the way to turn vector away or close to original normal is to change it's z coordinate and normalize just after it.
Shader "Bumped Diffuse Customizible" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} //1 means do nothing. 0 means max power, 2 means low power _BumpPower ("Bump Power", Range (3, 0.01)) = 1 } SubShader { Tags { "RenderType"="Opaque" } LOD 350
CGPROGRAM #pragma surface surf Lambert
sampler2D _MainTex; sampler2D _BumpMap; fixed4 _Color; fixed _BumpPower;
struct Input { float2 uv_MainTex; float2 uv_BumpMap; };
void surf (Input IN, inout SurfaceOutput o) { fixed4 c = tex2D(_MainTex, IN.uv_MainTex) _Color; o.Albedo = c.rgb; o.Alpha = c.a; fixed3 normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); normal.z = normal.z _BumpPower; o.Normal = normalize(normal); }
ENDCG
} FallBack "Bumped Diffuse" }
i found there are some artifacts, i think that we should lerp between the bumpmap normal and the vertex normal?
hm... it seems to work with better quality normal map.
this didnt work for me, well kinda i found you need to change
normal.z = normal.z _BumpPower;
to
normal.z = normal.z /_BumpPower;
worked for me,
we also used:
normal.z = normal.z / _BumpPower;
Thanks! :)
Answer by Abram-Painter · Jan 29, 2017 at 02:10 AM
wrong, you do it like this... o.Normal = lerp(UnpackNormal(tex2D (_BumpMap, IN.uv_BumpMap), fixed3(0,0,1), -_BumpPower + 1); This doesn't just get close to the original vertex normals, it goes all the way.
Abram Answer is correct leaving no artefacts . $$anonymous$$inor bracket missing that's all.
o.Normal = lerp(UnpackNormal (tex2D (__Bump$$anonymous$$ap, IN.uv_Bump$$anonymous$$ap)), fixed3(0,0,1), - _BumpPower + 1.0);
Thanks Abram!
Answer by jaised · Jul 13, 2012 at 03:28 PM
I did something similar to this:
float3 normalTex = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
float3 normal = normalTex * _NormalStrength;
I found that by using this, it increases the brightness of the spec and does nothing with the contrast. AKA the lighter parts get lighter, but the darker spots stay the same. It's a weird issue. If you try this, its almost like a brightness/contrast slider rather than intensifying the normals which is weird. If anyone has something to add to this, as to why this isn't working the way intended, please say something. It should sharpen or fade the normals out based on the value (which it sort of does, while affecting the lighting)...
Old question, but the reason for this is that by multiplying the normal only it's length will increase. But what you really want is strengthen it, getting it away from the straight normal (0,0,1), where z is the 'height'. So in order to do this, you can decrease the z value and then normalize, which will result in an increase in the x and y values. This results in an increase of the normal vector's contrast (in relation to (0,0,1), not the normal color!) and features will be pronounced. Hope that clears it up:)
Your answer
![](https://koobas.hobune.stream/wayback/20220613073639im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
3D Texture and normals 0 Answers
CutOut diffuse shader visible from both sides on a plane? 1 Answer
colorize mesh based on dot product 1 Answer
Weird sphere normals??? 2 Answers