I wrote a vert-frag shader using unitycookie’s tutorials but I have problems with it. It won’t receive shadows. Read that I should add AutoLight.cginc or LIGHT_ATTENUATION(i) but I’ve set atten to 1/distance^2 in my code. I get declaration of “x” conflicts with previous declaration at (53) at line 47 for multiple properties when I try and include AutoLight.cginc. I tried to see how shadows are cast in AutoLight but I can’t figure out what part of the code or how to port it into my shader.
Also I would like to convert this to half Lambert lighting but I don’t know what I need to do to SNdotL.
here’s my shader code ( not targeted for mobile )
Shader"custom/Skin"
{
properties
{
//_Ramp("BlendingEffects", 2D) = "white" {}
//_Range("Gloss",range(0,20))= 5
//_AmbGlo("AmbiantLighting",range(0,50))= 10
//_Occl("Oclusion",range(0,50))= 10
_MainTex("Texture", 2D) = "white" {}
_Smap("SpecMap", 2D) = "white" {}
_Shininess ("Specularity",range(0.005,5))= 2
_SpecColor ("specTint",color)= (1.0,1.0,1.0,1.0)
_RimColor ("Rim Color", Color) = (1.0,1.0,1.0,1.0)
_RimPower ("Rim Power", Range(0.1,10.0)) = 3.0
_Normals ("Normal", 2D)="white" {}
_NorRange ("Dephs",range(1,5))= 3
// above is good
}
SubShader
{
Pass{
Tags{"LightMode" = "ForwardBase"}
CGINCLUDE
// #include "AutoLight.cginc"
CGPROGRAM
#pragma vertex vertsA
#pragma fragment fragsA
#pragma target 3.0
#pragma only_renderers d3d9 debug
// no simcs on the pragmas
//user variables and unity variables
uniform half4 _Color;
uniform half4 _SpecColor;
uniform half _Shininess;
uniform half4 _LightColor0;
uniform sampler2D _MainTex;
uniform half4 _MainTex_ST;
uniform sampler2D _Normals;
uniform half4 _Normals_ST;
uniform half _NorRange;
uniform sampler2D _Smap;
uniform half4 _Smap_ST;
uniform fixed4 _RimColor;
uniform half _RimPower;
half4x4 _Object2World;
half4x4 _World2Object;
half4 _WorldSpaceLightPos0;
half3 _WorldSpaceCameraPos;
//input structs
struct VertInput1 { // vertex imput info
half4 vertex : POSITION;
half3 normal : NORMAL;
half4 texcoord : TEXCOORD0;
half4 tangent : TANGENT;
};
//output structs
struct VertOutput1 { // vertex output info
half4 pos : SV_POSITION;
half4 colur: COLOR;
half4 sktex : TEXCOORD0;
half4 lightDir : TEXCOORD1;
half3 posNormal : TEXCOORD2;
half3 tans : TEXCOORD3;
half3 binoms : TEXCOORD4;
half3 cameraView :TEXCOORD5;
half4 spTex : TEXCOORD6;
};
// vertex
VertOutput1 vertsA(VertInput1 g)
{
VertOutput1 h;
// direction variables
h.posNormal = normalize(mul(half4(g.normal,0.0),_World2Object).xyz);
h.tans = normalize( mul(_Object2World, g.tangent).xyz);
h.binoms = normalize(cross(h.posNormal,h.tans)* g.tangent.w);
half4 posMain = mul(_Object2World, g.vertex);
h.pos = mul(UNITY_MATRIX_MVP, g.vertex);
h.sktex = g.texcoord;
h.cameraView = normalize (_WorldSpaceCameraPos.xyz -posMain.xyz); //tweakable
// light physics
half3 facetolite= _WorldSpaceLightPos0.xyz -posMain.xyz;
half distance = length(facetolite);
h.lightDir = half4(normalize(lerp(_WorldSpaceLightPos0.xyz,facetolite,_WorldSpaceLightPos0.w)),
lerp(1.0,(1.0/(distance*distance)),_WorldSpaceLightPos0.w));
return h;
}
// fragment
half4 fragsA(VertOutput1 i) : COLOR
{
//maps
half4 sktex = tex2D(_MainTex, i.sktex.xy + (_MainTex_ST.xy * _MainTex_ST.zw));
half4 tans = tex2D(_Normals, i.sktex.xy + (_Normals_ST.xy * _Normals_ST.zw));
half4 spTex = tex2D(_Smap, i.sktex.xy + (_Smap_ST.xy *_Smap_ST.zw));
//normals
half3 localCoords = half3(2.0 * tans.ag - half2(1.0,1.0),_NorRange);
half3x3 local2WorldTranspose = float3x3( i.tans, i.binoms, i.posNormal);
half3 normalDirection = normalize(mul(localCoords, local2WorldTranspose));
//Lambert lighting calc
half3 SNdotL= saturate( dot( normalDirection,i.lightDir.xyz));
half3 thelighting = i.lightDir.w * _LightColor0.xyz *2* SNdotL;
//Rim Lighting
fixed rim = 1 - SNdotL;
fixed3 rimLighting = SNdotL * _RimColor.xyz * _LightColor0.xyz * pow( rim, _RimPower );
//Specularity calc (0.7*normalDirection,0.7*mainlightDir for wetness, specularity and shine,
half3 theshine = i.lightDir.w * spTex.rgb* max( 0.0,dot(0.7*normalDirection,0.7* i.lightDir.xyz))
* pow(max(0.0, dot(reflect(-i.lightDir.xyz, normalDirection),i.cameraView )),_Shininess);
half3 matcaplight = thelighting + (UNITY_LIGHTMODEL_AMBIENT.xyz)+ rimLighting;
// messy overlay highlights
half3 anotherT = lerp((1-(1-sktex.rgb)),((1-2*sktex.rgb)*sktex.rgb*matcaplight),step(8.9*matcaplight, 1));
half3 overlayfun = lerp((1-2*(1-sktex.rgb)),(2*sktex.rgb* matcaplight),step(matcaplight, 0.2 ));
half3 supertest=(overlayfun)/(1-anotherT*0.3)*(theshine*0.2) + (0.2*theshine* (_SpecColor.rgb * _LightColor0.xyz ));
//final lighting
return half4 (sktex.rgb*matcaplight+supertest,1.0);
}
ENDCG
}