Skip to content

Instantly share code, notes, and snippets.

@julhe
Last active January 19, 2018 15:35
Show Gist options
  • Save julhe/3ff3de154c09e956624e7a677b3c2d5e to your computer and use it in GitHub Desktop.
Save julhe/3ff3de154c09e956624e7a677b3c2d5e to your computer and use it in GitHub Desktop.
Dirty-Cheap VertexLightmaps for Unity
Shader "Unlit/VertexLightmap"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float3 vColor : COLOR;
};
sampler2D _MainTex;
float4 _MainTex_ST;
#define VERTEXLIGHTMAP_MIPMAP_LEVEL 0
SamplerState bilinear_clamp_sampler;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
o.vColor.rgb = DecodeLightmap(
unity_Lightmap.SampleLevel(bilinear_clamp_sampler, v.uv2 * unity_LightmapST.xy + unity_LightmapST.zw, VERTEXLIGHTMAP_MIPMAP_LEVEL));
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
col.rgb *= i.vColor; //apply lightmap to albedo
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment