how to add tangent and binormal to surface shader

for add add tangent and binormal to surface shader , but find something error .
it only says : too many texture interpolators would be used for forwardbase pass at line 9

how to add tangent and binormal to surface shader, what is the correct way to do this ,
is any sample code here in unity?

here is my code :
Shader “Custom/sur” {
Properties {
_MainTex (“Base (RGB)”, 2D) = “white” {}
}
SubShader {
Tags { “RenderType”=“Opaque” }
LOD 200

CGPROGRAM
#pragma surface surf LambertA vertex:vert

sampler2D _MainTex;

struct Input {
float2 uv_MainTex;
half3 normal_input;
half3 tangent_input;
half3 binormal_input;
};
struct SurfaceOutputEx{
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
half3 normal_input;
half3 tangent_input;
half3 binormal_input;
};

void vert(inout appdata_full i, out Input o){
half4 p_normal =mul(float4(i.normal,0.0f),_World2Object);
half4 p_tangent =mul(_Object2World,i.tangent);

half3 normal_input =normalize(p_normal.xyz);
half3 tangent_input=normalize(p_tangent.xyz);
half3 binormal_input =cross(p_normal.xyz,p_tangent.xyz);

o.normal_input =normal_input;
o.tangent_input =tangent_input ;
o.binormal_input =binormal_input ;
}

void surf (Input IN, inout SurfaceOutputEx o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb ;
o.Alpha = c.a;
o.normal_input =IN.normal_input;
o.tangent_input =IN.tangent_input ;
o.binormal_input =IN.binormal_input ;
}

inline half4 LightingLambertA (SurfaceOutputEx s , half3 lightDir, half3 viewDir, half atten)
{
half3 h =normalize(lightDir+viewDir);
half3 b =s.binormal_input;
half diff = max (0, dot (s.Normal, lightDir));

half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2) ;
c.a = s.Alpha;
return c ;
}

ENDCG
}
FallBack “Diffuse”
}

I don’t know surface shaders very well, but it’s not a binormal. It’s a bitangent.

The terms are pretty much interchangeable. Technically it is, indeed, a bitangent, but it’s generally referred to as a binormal.

You’ll need to add #pragma target 3.0 to your shader to get around the interpolator limit.

You’ll also want to multiply your binormal by the w component of the tangent (which stores handedness);

half3 binormal_input =cross(p_normal.xyz,p_tangent.xyz) * i.tangent.w;

1 Like

The way to compute the binormal can be found in the cginclude UnityCG.cginc (found in your Editor/Data/CGIncludes folder):

float3 binormal = cross( v.normal, v.tangent.xyz ) * v.tangent.w;

The error you got, “too many texture interpolators” is indicative of having too much data in your Input struct. Like James said, you can use #pragma target 3.0 to allow for more data, or you can get creative with how you pack things into your input struct.

Stop propagating the problem. The term hasn’t been misused long enough, and it’s not widely used enough, for it to be hard to fix the issue.

Doesn’t the UnityCG.cginc file refer to it as the binormal? If you think it should be called something different, you might want to take it up with Aras or Rej.

Haha, fair point.

I think it’s widely used enough that you’ll have a fight on your hands to change it now, but I’ll try :stuck_out_tongue:

thanks Farfarer , you are right
thanks for help .