Last active
September 12, 2018 04:36
-
-
Save Kink3d/7d7a8da1a8a67d808f74a3f824d8a294 to your computer and use it in GitHub Desktop.
A port of the "Bumped Lightmapped Diffuse" shader in Source Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------- | |
// Source Engine Bumped Lightmapped Diffuse | |
// Replace all values in full captials (not vertex semantics) with constants from the renderer | |
// Replace all functions in full captials with equivalent samplers | |
VertexInput | |
{ | |
float3 position : POSITION; | |
float3 normal : NORMAL; | |
float3 tangent : TANGENT; // Every other engines ive used takes vec4 tangents from the mesh | |
float4 texcoord : TEXCOORD0; | |
float2 lightmapUV0 : TEXCOORD1; | |
float2 lightmapUV1 : TEXCOORD2; | |
}; | |
VertexOutput | |
{ | |
float4 positionCS; | |
float2 uv; | |
float3 normalWS; | |
float3 positionWS; | |
float3 tangentWS; | |
float3 bitangentWS; | |
float4 lightmap01UV; | |
float2 lightmap2UV; | |
}; | |
VertexOutput vs (VertexInput i) | |
{ | |
VertexOutput o; | |
o.positionWS = mul(i.position, MODEL_MATRIX); | |
o.positionCS = mul(o.positionWS, VIEW_PROJECTION_MATRIX); | |
o.uv = i.texcoord; | |
o.normalWS = normalize(mul(i.normal, MODEL_MATRIX)); | |
o.tangentWS = normalize(mul(i.tangent.xyz, MODEL_MATRIX)); | |
o.bitangentWS = cross(o.tangentWS, o.normalWS); // Every other engine I've used divides this result by i.tangent.w | |
float2 lm1 = = i.lightmapUV0 + i.lightmapUV1; | |
float2 lm2 = lm1 + i.lightmapUV1; | |
o.lightmap01UV = float4(lm1.xy, lm2.xy); | |
o.lightmap2UV = lm2 + i.lightmapUV1; | |
} | |
float4 ps (VertexOutput IN) | |
{ | |
// Sample normal | |
float3 nrm = SAMPLE_TEXTURE(NORMAL_TEXTURE, IN.uv); | |
nrm = nrm * 2 - 1; | |
// Bump basis | |
float3 basisX = dot(nrm, BUMP_BASIS.x); | |
float3 basisY = dot(nrm, BUMP_BASIS.y); | |
float3 basisZ = dot(nrm, BUMP_BASIS.z); | |
float3 basis = saturate(float3(basisX, basisY, basisZ)); | |
basis = basis * basis; | |
// Sample lightmaps | |
float3 lm0 = SAMPLE_TEXTURE(LIGHTMAP_TEXTURE, IN.lightmap01UV.xy) * basis.x; | |
float3 lm1 = SAMPLE_TEXTURE(LIGHTMAP_TEXTURE, IN.lightmap01UV.zw) * basis.y; | |
float3 lm2 = SAMPLE_TEXTURE(LIGHTMAP_TEXTURE, IN.lightmap01UV.xy) * basis.z; | |
float lmScale = LIGHTMAPSCALE / (dot(basis, 1); | |
float3 lmFinal = (lm0 + lm1 + lm2) * lmScale; | |
// Sample Color | |
float3 albedo = SAMPLE_TEXTURE(ALBEDO_TEXTURE, IN.uv); | |
// Fianl | |
float fogFactor = saturate(max(-(IN.positionCS.z), 0) * (-1/FARCLIP) + (FARCLIP/FARCLIP)); | |
float3 color = lerp(FOG_COLOR, color, fogFactor) * lmFinal; | |
return color; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment