Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Uniforms.hlsl"
- #include "Samplers.hlsl"
- #include "Transform.hlsl"
- #include "ScreenPos.hlsl"
- #include "Lighting.hlsl"
- #include "Fog.hlsl"
- // When rendering a shadowed point light, disable specular calculations on Shader Model 2 to avoid exceeding the instruction limit
- #if !defined(SM3) && defined(SHADOW) && defined(POINTLIGHT)
- #undef SPECULAR
- #endif
- #ifdef COMPILEPS
- Texture2D tWeightMap0 : register(t0);
- Texture2DArray tDetailMap : register(t2);
- SamplerState sWeightMap0 : register(s0);
- SamplerState sDetailMap : register(s2);
- #ifdef BUMPMAP
- #ifdef MODELNORMAL
- Texture2D tModelNormal : register(t1);
- SamplerState sModelNormal : register(s1);
- #endif
- Texture2DArray tNormal : register(t3);
- SamplerState sNormal : register(s3);
- #endif
- #endif
- #ifdef COMPILEVS
- cbuffer CustomVS : register(b6)
- {
- float2 cTilingFactors;
- };
- #endif
- float3 combineNormals(float3 n1, float3 n2)
- {
- float3 t=n1*float3(2,2,2)+float3(-1,-1,0);
- float3 u=n2*float3(-2,-2,2)+float3(1,1,-1);
- float3 r=t*dot(t,u)/t.z-u;
- return r;
- }
- void VS(float4 iPos : POSITION,
- float3 iNormal : NORMAL,
- float2 iTexCoord : TEXCOORD0,
- #ifdef BUMPMAP
- float4 iTangent : TANGENT,
- #endif
- #ifdef INSTANCED
- float4x3 iModelInstance : TEXCOORD2,
- #endif
- #ifndef BUMPMAP
- out float2 oTexCoord : TEXCOORD0,
- #else
- out float4 oTexCoord : TEXCOORD0,
- out float4 oTangent : TEXCOORD3,
- #ifdef MODELNORMAL
- out float2 oModelTexCoord : TEXCOORD11,
- #endif
- #endif
- out float3 oNormal : TEXCOORD1,
- out float4 oWorldPos : TEXCOORD2,
- out float3 oDetailTexCoord : TEXCOORD10,
- #ifdef PERPIXEL
- #ifdef SHADOW
- out float4 oShadowPos[NUMCASCADES] : TEXCOORD4,
- #endif
- #ifdef SPOTLIGHT
- out float4 oSpotPos : TEXCOORD5,
- #endif
- #ifdef POINTLIGHT
- out float3 oCubeMaskVec : TEXCOORD5,
- #endif
- #else
- out float3 oVertexLight : TEXCOORD4,
- out float4 oScreenPos : TEXCOORD5,
- #endif
- #if defined(D3D11) && defined(CLIPPLANE)
- out float oClip : SV_CLIPDISTANCE0,
- #endif
- out float4 oPos : OUTPOSITION)
- {
- float4x3 modelMatrix = iModelMatrix;
- float3 worldPos = GetWorldPos(modelMatrix);
- oPos = GetClipPos(worldPos);
- oNormal = GetWorldNormal(modelMatrix);
- oWorldPos = float4(worldPos, GetDepth(oPos));
- #ifdef BUMPMAP
- float3 tangent = GetWorldTangent(modelMatrix);
- float3 bitangent = cross(tangent, oNormal) * iTangent.w;
- oTexCoord = float4(worldPos.xz*float2(1.0/cTilingFactors.x, 1.0/cTilingFactors.y), bitangent.xy);
- oTangent = float4(tangent, bitangent.z);
- #ifdef MODELNORMAL
- oModelTexCoord = iTexCoord;
- #endif
- #else
- oTexCoord = GetTexCoord(worldPos.xz*float2(1.0/cTilingFactors.x, 1.0/cTilingFactors.y));
- #endif
- oDetailTexCoord=worldPos.xyz*0.8;
- #if defined(D3D11) && defined(CLIPPLANE)
- oClip = dot(oPos, cClipPlane);
- #endif
- #ifdef PERPIXEL
- // Per-pixel forward lighting
- float4 projWorldPos = float4(worldPos.xyz, 1.0);
- #ifdef SHADOW
- // Shadow projection: transform from world space to shadow space
- GetShadowPos(projWorldPos, oShadowPos);
- #endif
- #ifdef SPOTLIGHT
- // Spotlight projection: transform from world space to projector texture coordinates
- oSpotPos = mul(projWorldPos, cLightMatrices[0]);
- #endif
- #ifdef POINTLIGHT
- oCubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
- #endif
- #else
- // Ambient & per-vertex lighting
- oVertexLight = GetAmbient(GetZonePos(worldPos));
- #ifdef NUMVERTEXLIGHTS
- for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
- oVertexLight += GetVertexLight(i, worldPos, oNormal) * cVertexLights[i * 3].rgb;
- #endif
- oScreenPos = GetScreenPos(oPos);
- #endif
- }
- void PS(
- #ifndef BUMPMAP
- float2 iTexCoord : TEXCOORD0,
- #else
- float4 iTexCoord : TEXCOORD0,
- float4 iTangent : TEXCOORD3,
- #ifdef MODELNORMAL
- float2 iModelTexCoord : TEXCOORD11,
- #endif
- #endif
- float3 iNormal : TEXCOORD1,
- float4 iWorldPos : TEXCOORD2,
- float3 iDetailTexCoord : TEXCOORD10,
- #if defined(D3D11) && defined(CLIPPLANE)
- float iClip : SV_CLIPDISTANCE0,
- #endif
- #ifdef PERPIXEL
- #ifdef SHADOW
- float4 iShadowPos[NUMCASCADES] : TEXCOORD4,
- #endif
- #ifdef SPOTLIGHT
- float4 iSpotPos : TEXCOORD5,
- #endif
- #ifdef CUBEMASK
- float3 iCubeMaskVec : TEXCOORD5,
- #endif
- #else
- float3 iVertexLight : TEXCOORD4,
- float4 iScreenPos : TEXCOORD5,
- #endif
- #ifdef PREPASS
- out float4 oDepth : OUTCOLOR1,
- #endif
- #ifdef DEFERRED
- out float4 oAlbedo : OUTCOLOR1,
- out float4 oNormal : OUTCOLOR2,
- out float4 oDepth : OUTCOLOR3,
- #endif
- out float4 oColor : OUTCOLOR0)
- {
- // Get material diffuse albedo
- float4 weights0=tWeightMap0.Sample(sWeightMap0, iTexCoord.xy);
- float3 nrm = normalize(iNormal);
- float3 blending=abs(nrm);
- blending = normalize(max(blending, 0.00001));
- float b=blending.x+blending.y+blending.z;
- blending=blending/b;
- float4 tex1=tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.zy, 4))*blending.x +
- tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.xy, 4))*blending.z +
- tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.xz, 0))*blending.y;
- float4 tex2=tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.zy, 5))*blending.x +
- tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.xy, 5))*blending.z +
- tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.xz, 1))*blending.y;
- float4 tex3=tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.zy, 6))*blending.x +
- tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.xy, 6))*blending.z +
- tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.xz, 2))*blending.y;
- float4 tex4=tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.zy, 7))*blending.x +
- tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.xy, 7))*blending.z +
- tDetailMap.Sample(sDetailMap, float3(iDetailTexCoord.xz, 3))*blending.y;
- float ma=max(tex1.a+weights0.r, max(tex2.a+weights0.g, max(tex3.a+weights0.b, tex4.a+weights0.a)))-0.2;
- float b1=max(0, tex1.a+weights0.r-ma);
- float b2=max(0, tex2.a+weights0.g-ma);
- float b3=max(0, tex3.a+weights0.b-ma);
- float b4=max(0, tex4.a+weights0.a-ma);
- float bsum=b1+b2+b3+b4;
- float4 diffColor=((tex1*b1+tex2*b2+tex3*b3+tex4*b4)/bsum);
- float grad=clamp(iWorldPos.y*0.125,0,1);
- diffColor*=grad;
- // Get normal
- #ifdef BUMPMAP
- float3 bump1=DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.zy, 4)))*blending.x +
- DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.xy, 4)))*blending.z +
- DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.xz, 0)))*blending.y;
- float3 bump2=DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.zy, 5)))*blending.x +
- DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.xy, 5)))*blending.z +
- DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.xz, 1)))*blending.y;
- float3 bump3=DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.zy, 6)))*blending.x +
- DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.xy, 6)))*blending.z +
- DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.xz, 2)))*blending.y;
- float3 bump4=DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.zy, 7)))*blending.x +
- DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.xy, 7)))*blending.z +
- DecodeNormal(tNormal.Sample(sNormal, float3(iDetailTexCoord.xz, 3)))*blending.y;
- float3x3 tbn = float3x3(iTangent.xyz, float3(iTexCoord.zw, iTangent.w), iNormal);
- float3 texnormal=normalize((bump1*b1+bump2*b2+bump3*b3+bump4*b4)/bsum)*0.5+0.5;
- #ifdef MODELNORMAL
- float3 modelnormal=tModelNormal.Sample(sModelNormal, iModelTexCoord.xy).rgb;
- float3 normal=normalize(combineNormals(modelnormal, texnormal));
- #else
- float3 normal=normalize((bump1*b1+bump2*b2+bump3*b3+bump4*b4)/bsum);
- #endif
- normal=normalize(mul(normal, tbn));
- #else
- float3 normal = normalize(iNormal);
- #endif
- float3 specColor = cMatSpecColor.rgb;
- #ifdef HEIGHTFOG
- float fogFactor = GetHeightFogFactor(iWorldPos.w, iWorldPos.y);
- #else
- float fogFactor = GetFogFactor(iWorldPos.w);
- #endif
- #if defined(PERPIXEL)
- // Per-pixel forward lighting
- float3 lightDir;
- float3 lightColor;
- float3 finalColor;
- float diff = GetDiffuse(normal, iWorldPos.xyz, lightDir);
- #ifdef SHADOW
- diff *= GetShadow(iShadowPos, iWorldPos.w);
- #endif
- #if defined(SPOTLIGHT)
- lightColor = iSpotPos.w > 0.0 ? Sample2DProj(LightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
- #elif defined(CUBEMASK)
- lightColor = SampleCube(LightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
- #else
- lightColor = cLightColor.rgb;
- #endif
- #ifdef SPECULAR
- float spec = GetSpecular(normal, cCameraPosPS - iWorldPos.xyz, lightDir, cMatSpecColor.a);
- finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
- #else
- finalColor = diff * lightColor * diffColor.rgb;
- #endif
- #ifdef AMBIENT
- finalColor += cAmbientColor * diffColor.rgb;
- finalColor += cMatEmissiveColor;
- oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
- #else
- oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
- #endif
- #elif defined(PREPASS)
- // Fill light pre-pass G-Buffer
- float specPower = cMatSpecColor.a / 255.0;
- oColor = float4(normal * 0.5 + 0.5, specPower);
- oDepth = iWorldPos.w;
- #elif defined(DEFERRED)
- // Fill deferred G-buffer
- float specIntensity = specColor.g;
- float specPower = cMatSpecColor.a / 255.0;
- float3 finalColor = iVertexLight * diffColor.rgb;
- oColor = float4(GetFog(finalColor, fogFactor), 1.0);
- oAlbedo = fogFactor * float4(diffColor.rgb, specIntensity);
- oNormal = float4(normal * 0.5 + 0.5, specPower);
- oDepth = iWorldPos.w;
- #else
- // Ambient & per-vertex lighting
- float3 finalColor = iVertexLight * diffColor.rgb;
- #ifdef MATERIAL
- // Add light pre-pass accumulation result
- // Lights are accumulated at half intensity. Bring back to full intensity now
- float4 lightInput = 2.0 * Sample2DProj(LightBuffer, iScreenPos);
- float3 lightSpecColor = lightInput.a * (lightInput.rgb / GetIntensity(lightInput.rgb));
- finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
- #endif
- oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment