here is normal map what is baked from blender, type changed “Normal map” in unity
in surface shader, it works so… i think that maybe has problem with my script.
and here is code…
Shader "Unlit/NewUnlitShader"
{
Properties
{
_NormalMap ("Texture", 2D) = "Normal" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct appdata
{
float4 vertex:POSITION;
float2 uv:TEXCOORD0;
float3 tangent:TANGENT;
float3 normal:NORMAL;
};
struct v2f
{
float4 vertex:SV_POSITION;
float2 uv:TEXCOORD0;
float3 tangent:TEXCOORD1;
float3 normal:TEXCOORD2;
float3 bitangent:TEXCOORD3;
};
TEXTURE2D(_NormalMap);
SAMPLER(sampler_NormalMap);
v2f vert (appdata v)
{
v2f o;
o.vertex=TransformObjectToHClip(v.vertex.xyz);
o.uv=v.uv;
o.tangent=v.tangent;
o.normal=v.normal;
return o;
}
real4 frag (v2f i) : SV_Target
{
//calculate bitangent
i.bitangent=cross(i.normal,i.tangent);
//tbn matrix
float4x4 tbn=float4x4(i.tangent,0,i.bitangent,0,i.normal,0,0,0,0,1);
//compose matrix
tbn=mul(GetObjectToWorldMatrix(),transpose(tbn));
//get tangent space normal
float3 normalmap=SAMPLE_TEXTURE2D(_NormalMap,sampler_NormalMap,i.uv).wyxz*2-1;
//world transform
normalmap=mul(tbn,normalmap);
//calculate diffuse
float ndl=saturate(dot(normalmap,_MainLightPosition.xyz));
//output
real4 col=real4(ndl,ndl,ndl,1);
return col;
}
ENDHLSL
}
}
}
(i do my best…)
according to the result (ratio of shadow), it roughly works. or my delusion…
the mesh is triangulated, and why it has wave??
i changed green channel negative and got some progress…