Ok, I have a geosphere “planet” illuminated by a point light. The “player” also has a point light attached. Once the player gets too close, the player light becomes the now focus of the shader. How do I combine the two lights?
CGPROGRAM
#pragma target 3.0
#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"
uniform float _WaterFloorStart;
uniform sampler2D _WaterFloorTex;
uniform float _WaterFoamStart;
uniform sampler2D _WaterFoamTex;
uniform float _ShoreLineStart;
uniform sampler2D _ShoreLineTex;
uniform float _DirtStart;
uniform sampler2D _DirtTex;
uniform float _GrassStart;
uniform sampler2D _GrassTex;
uniform float _MountainBaseStart;
uniform sampler2D _MountainBaseTex;
uniform float _MountainSideStart;
uniform sampler2D _MountainSideTex;
uniform float _MountainTopEnd;
uniform sampler2D _MountainTopTex;
struct v2f {
V2F_POS_FOG;
LIGHTING_COORDS
float2 uv;
float3 normal;
float3 lightDir;
float4 vertex;
};
v2f vert (appdata_base v) {
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.vertex = v.vertex;
o.normal = v.normal;
o.uv = v.texcoord * 2;
//if (o.uv.x > 1) o.uv.x = 1-o.uv.x;
//if (o.uv.y > 1) o.uv.y = 1-o.uv.y;
o.uv *= 50;
o.lightDir = ObjSpaceLightDir( v.vertex );
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
float4 frag (v2f i) : COLOR
{
// The eternal tradeoff: do we normalize the normal?
float3 normal = normalize(i.normal);
//float3 normal = i.normal;
float dist = sqrt(i.vertex.x*i.vertex.x + i.vertex.y*i.vertex.y + i.vertex.z*i.vertex.z);
// ... based on distance from the center of the object, find the two textures to blend ...
float mixPos1, mixPos2;
float4 texcol1, texcol2;
if (dist < _WaterFoamStart)
{
mixPos1 = _WaterFloorStart;
mixPos2 = _WaterFoamStart;
texcol1 = tex2D(_WaterFloorTex, i.uv);
texcol2 = (1,1,1,1); //tex2D(_WaterFoamTex, i.uv);
}
else if (dist < _ShoreLineStart)
{
mixPos1 = _WaterFoamStart;
mixPos2 = _ShoreLineStart;
texcol1 = (1,1,1,1); //tex2D(_WaterFoamTex, i.uv);
texcol2 = tex2D(_ShoreLineTex, i.uv);
}
else if (dist < _DirtStart)
{
mixPos1 = _ShoreLineStart;
mixPos2 = _DirtStart;
texcol1 = tex2D(_ShoreLineTex, i.uv);
texcol2 = tex2D(_DirtTex, i.uv);
}
else if (dist < _GrassStart)
{
mixPos1 = _DirtStart;
mixPos2 = _GrassStart;
texcol1 = tex2D(_DirtTex, i.uv);
texcol2 = tex2D(_GrassTex, i.uv);
}
else if (dist < _MountainBaseStart)
{
mixPos1 = _GrassStart;
mixPos2 = _MountainBaseStart;
texcol1 = tex2D(_GrassTex, i.uv);
texcol2 = tex2D(_MountainBaseTex, i.uv);
}
else if (dist < _MountainSideStart)
{
mixPos1 = _MountainBaseStart;
mixPos2 = _MountainSideStart;
texcol1 = tex2D(_MountainBaseTex, i.uv);
texcol2 = tex2D(_MountainSideTex, i.uv);
}
else
{
mixPos1 = _MountainSideStart;
mixPos2 = _MountainTopEnd;
texcol1 = tex2D(_MountainSideTex, i.uv);
texcol2 = tex2D(_MountainTopTex, i.uv);
}
// ... calculate the "mix" amount between the textures ...
float mixAmount = dist - mixPos1;
float mixRange = mixPos2 - mixPos1;
float percentMix2 = 0;
if (mixRange != 0) percentMix2 = mixAmount / mixRange;
if (percentMix2 < 0) percentMix2 = 0;
if (percentMix2 > 1) percentMix2 = 1;
float percentMix1 = 1 - percentMix2;
// ... interpolate the color ...
float4 texcolA = (texcol1 * percentMix1);
float4 texcolB = (texcol2 * percentMix2);
float4 texcol = texcolA + texcolB;
float4 difcol = DiffuseLight(i.lightDir, normal, texcol, LIGHT_ATTENUATION(i) );
return (difcol*0.5 + texcol) / 2; // (add a bit of ambience)
}
ENDCG
[/code]