Face's Normal direction (Without accounting normal map!)

Greetings, i’m trying to use wether a normal is facing in an upwards position or not to further define of the shaders functions, but so far i only managed to face the normal direction INCLUDING any used normal maps, i want to use the function without any of the normal map’s data taken into account.

Currently i’m using the following function in my surf shader:

float TestValue = 0.5+(dot(WorldNormalVector(IN, o.Normal), _TestDirection.xyz))/2;
TestValue = smoothstep(0.5,1,TestValue);

_Testdirection is a float4 with value (0.0,1.0,0.0,0.0)

So the only problem i have now is that the code uses the resulting normal including applied normal map, i want the value without the normal map.

Hope someone can help me out on this one! Thanks!

From the two lines I deduce you you are writing a surface shader. It’s in tangent space so just replace o.Normal with float(0,0,1).
Or, to make it more efficient add float3 worldNormal; INTERNAL_DATA in the Input struct and use that directly.

see Unity - Manual: Writing Surface Shaders.

aye, it’s a surface shader.

I tried using your example as follows:
float TestValue = 0.5+(dot(IN.worldNormal, _TestDirection.xyz))/2; but now the returned value somehow always is 0 :frowning:

Currently the preview of the shader looks like this;

I want the same result, only excluded the normal map. so basically what it’d look like on a sphere that wouldn’t be using the normalmap (however i need my normalmap, but for other purposes then this calculation)

This is the code to get the world normal of the flat-shaded model in a surface shader.

WorldNormalVector(IN, float3(0,0,1))

Or you could set up a custom vertex shader in your surface shader to work that out and then pass that value through to the fragment shader (probably a bit cheaper to calculate).
In vert:

float3 normalW = mul((float3x3)_Object2World, v.normal);

In surf:

normalW = normalize(normalW);

Thanks Farfarer! after a bit of rummaging i managed to get the vertex function working!

I’m putting the result of the test/debug version of the shader (purely for seeing the result value) online;

Shader "Custom/FaceDirectionTester" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_Bump ("Bump", 2D) = "bump" {}
		_BaseColor ("Base Color", Color) = (0.0,0.0,0.0,1.0)
		_TestColor ("Test Color", Color) = (1.0,1.0,1.0,1.0)
		_TestDirection ("Testing Direction", Vector) = (0,1,0)
		_TestFactor ("Test Angle Hardness", Range(0,975) ) = 0.95
		
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf BlinnPhong vertex:myvert
		#include "UnityCG.cginc"

		sampler2D _Bump;
		float4 _BaseColor;
		float4 _TestColor;
		float4 _TestDirection;
		float _TestFactor

		struct Input {
			float2 uv_Bump;
			float3 normalW;
			INTERNAL_DATA
		};
		
		void myvert (inout appdata_full v, out Input data){
            UNITY_INITIALIZE_OUTPUT(Input,data);
            data.normalW = mul((float3x3)_Object2World, v.normal);
        }

		void surf (Input IN, inout SurfaceOutput o) {
			o.Normal = UnpackNormal (tex2D (_Bump, IN.uv_Bump));
			float TestValue = 0.5+(dot(normalize(IN.normalW), _TestDirection.xyz))/2;
			half4 result = 0;
			o.Albedo = 0;
			result = smoothstep(_TestFactor,1,TestValue);
			o.Emission = result.rgb;
			o.Alpha = 0;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

Who knows, maybe someone else will have another use for it in the future :slight_smile:

1 Like