Hi,
I am working on Diffuse Shader that bends objects and I want to have it fully compatible with the normal light system in unity. The problem seems to be in the ambient pass because I don’t know what exactly is happing there. I cannot use the ShaderLab Ambient pass because I am not able to transform vertices before, right? So I tried to rebuild it. The problem is now that it is only considering one light in the scene. Any help would be very nice, thank you.
Shader "Diffuse-Bend" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Direction("Direction", Vector) = (0,0,0,0)
}
Category {
Tags { "RenderType"="Opaque" }
LOD 200
Blend AppSrcAdd AppDstAdd
Fog { Color [_AddFog] }
// ------------------------------------------------------------------
// ARB fragment program
SubShader {
// Pixel lights
Pass {
Name "PPL"
Tags { "LightMode" = "Pixel" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct v2f {
V2F_POS_FOG;
LIGHTING_COORDS
float2 uv;
float3 normal;
float3 lightDir;
};
uniform float4 _MainTex_ST;
uniform float4 _Color;
uniform float4 _Direction;
v2f vert (appdata_base v)
{
v2f o;
float4 f4Vertex = v.vertex;
// transform vertex in world space
float4 f4WorldPos = mul(_Object2World,f4Vertex );
f4WorldPos += _Direction * (pow(v.vertex.z,2)/10);
float4 f4NewPos = mul(_World2Object,f4WorldPos );
// put i back to object space
o.pos = mul( glstate.matrix.mvp, f4NewPos );
o.fog = o.pos.z;
o.normal = v.normal;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.lightDir = ObjSpaceLightDir( f4NewPos );
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
uniform sampler2D _MainTex;
float4 frag (v2f i) : COLOR
{
// The eternal tradeoff: do we normalize the normal?
//float3 normal = normalize(i.normal);
float3 normal = i.normal;
half4 texcol = tex2D( _MainTex, i.uv );
float4 f4Ambient = _PPLAmbient*2*texcol;
float4 f4Diff = saturate(DiffuseLight( i.lightDir, normal, texcol, LIGHT_ATTENUATION(i) ));
return f4Ambient+f4Diff;
}
ENDCG
}
}
[/code]