The environments the shader will be used on are pretty high poly so keeping the draw count down might help in the long run.
Comparing my own vertex lighting with the lighting the multipass shader is making me scratch my head though. What’s the proper way to get the position of the light in terms of model space?
Based on the tree shader this is how I’m attempting to get the light position in model space:
L.xyz = mul ( glstate.light[i].position.xyz, (float3x3)glstate.matrix.invtrans.modelview[0]);
float diffuseLight;
diffuseLight = dot(L.xyz, v.normal);
diffuseLight = max(diffuseLight, 0);
diffuse += glstate.light[i].diffuse * diffuseLight; // * att;
But it doesn’t really seem to work. Which space is glstate.light*.position.xyz in? World, view or model?*
It might help if I posted the whole shader:
```
/
This shader blends two diffuse maps based on the vertex colour and a mixmap. Lights using vertex lighting.
*/
Shader “Vertex Blended/Vertex Lit Diffuse” {
Properties {
_SpecColor (“Spec Color”, Color) = (1,1,1,1)
_DiffuseColor (“Diffuse Color”, Color) = (1,1,1,1)
_Shininess (“Shininess”, Range (0.01, 1)) = 0.7
_Diffuse1 ("Diffuse 1 (RGB) Spec (A)", 2D) = "white" {}
_Diffuse2 ("Diffuse 2 (RGB) Spec (A)", 2D) = "white" {}
_UseMix ("Use Mix Control", Range(0,1)) = 0
_MixControl ("Mix Control (A)", 2D) = "gray" {}
}
Category {
Blend AppSrcAdd AppDstAdd
Fog { Color [_AddFog] }
// ------------------------------------------------------------------
// ARB fragment program
SubShader {
Pass {
Tags { "LightMode" = "Vertex" }
//SeparateSpecular on
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_builtin
#include “UnityCG.cginc”
#include “AutoLight.cginc”
struct appdata {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD;
float4 color : COLOR;
};
struct v2f_vertex_blended {
float4 position : POSITION;
float fog : FOGC;
float2 uv[3] : TEXCOORD2 ;
float4 diff : TEXCOORD0;
//float4 spec : TEXCOORD1;
float4 color : COLOR ;
};
float4 _Diffuse1_ST, _Diffuse2_ST, _MixControl_ST;
uniform float _Shininess;
uniform float4 _DiffuseColor;
uniform float4 _SpecColor;
float attenuation (float3 len, int lightNum)
{
float d = length(len); //distance(position, glstate.light[lightNum].position);
float3 lightAtt = glstate.light[lightNum].attenuation;
return 1 / (lightAtt.x + lightAtt.y * d + lightAtt.z * d * d);
}
v2f_vertex_blended vert (appdata v)
{
v2f_vertex_blended o;
o.position = mul(glstate.matrix.mvp, v.vertex);
o.fog = o.position.z;
float4 lightColor = glstate.lightmodel.ambient;
float3 eyeView = normalize(ObjSpaceViewDir(v.vertex)); // Eye position
float4 diffuse = lightColor;
float4 specular = lightColor;
float4 L;
L.w = 1;
for (int i = 0; i < 4; i++) {
// Calculate the diffuse
L.xyz = mul ( glstate.light[i].position.xyz, (float3x3)glstate.matrix.invtrans.modelview[0]);
float att = attenuation(L.xyz, i);
float diffuseLight;
diffuseLight = dot(L.xyz, v.normal);
diffuseLight = max(diffuseLight, 0);
diffuse += glstate.light[i].diffuse * diffuseLight * att;
// Calculate the spec
float3 vTerm = normalize(eyeView - v.vertex.xyz);
float3 hTerm = normalize(L.xyz + vTerm);
float specLight = dot(v.normal, hTerm);
specLight = max(specLight, 0);
specLight = pow(specLight, _Shininess);
specular += glstate.light[i].diffuse * specLight;
}
o.diff = _DiffuseColor * diffuse;
//o.spec = _SpecColor * specular;// * att;
o.color = v.color;
o.uv[0] = TRANSFORM_TEX(v.texcoord, _Diffuse1);
o.uv[1] = TRANSFORM_TEX(v.texcoord, _Diffuse2);
o.uv[2] = TRANSFORM_TEX(v.texcoord, _MixControl);
return o;
}
uniform sampler2D _Diffuse1;
uniform sampler2D _Diffuse2;
uniform sampler2D _MixControl;
uniform float _UseMix;
float4 frag (v2f_vertex_blended i) : COLOR
{
//return i.diff;
float4 col1 = tex2D(_Diffuse1, i.uv[0]);
float4 col2 = tex2D(_Diffuse2, i.uv[1]);
float4 texcol;
if (_UseMix < 1) {
texcol = lerp(col1, col2, i.color.r);
texcol = (texcol * i.diff);// + i.spec;
return texcol;
}
float4 mix = tex2D(_MixControl, i.uv[2]);
if (mix.r < i.color.r)
{
texcol = col2;
}
else
{
texcol = col1;
}
return texcol;
}
ENDCG
}
}
}
FallBack “VertexLit”
}*
```