Skip to content

Instantly share code, notes, and snippets.

@fanhexin
Created August 30, 2024 08:41
Show Gist options
  • Save fanhexin/bef0a288498900ec284561158ea2dc6e to your computer and use it in GitHub Desktop.
Save fanhexin/bef0a288498900ec284561158ea2dc6e to your computer and use it in GitHub Desktop.
Sample lightmap texture unity generated and multiply to the diffuse color. Must setup correct _LightmapTex_ST first.
Shader "Custom/DiffusePlusLightmap"
{
Properties
{
_MainTex ("Main Texture", 2D) = "white" {}
_LightmapTex ("Lightmap", 2D) = "white" {}
_LightIntensity ("Light Intensity", Range(0, 10)) = 1
_Emission ("Emission", Color) = (0, 0, 0, 1)
}
SubShader
{
LOD 200
Tags
{
"RenderType" = "Opaque"
"Queue" = "Geometry"
}
Pass
{
Cull Off
Fog { Mode Off }
AlphaTest Off
Blend Off
Lighting Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma glsl_no_auto_normalization
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _LightmapTex;
float4 _MainTex_ST;
float4 _LightmapTex_ST;
float _LightIntensity;
float4 _Emission;
struct Vertex
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
float4 uv1 : TEXCOORD1;
};
struct Fragment
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
float4 uv1 : TEXCOORD1;
};
Fragment vert(Vertex v)
{
Fragment o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv1.xy = v.uv1.xy * _LightmapTex_ST.xy + _LightmapTex_ST.zw;
return o;
}
fixed4 frag(Fragment IN) : COLOR
{
fixed4 output = fixed4(0, 0, 0, 1);
output.rgb = tex2D(_MainTex, IN.uv.xy).rgb;
output.rgb *= _LightIntensity * DecodeLightmap(tex2D(_LightmapTex, IN.uv1.xy));
output.rgb += _Emission.rgb;
return saturate(output);
}
ENDCG
}
}
Fallback Off
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment