Advertisement
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;
- }
- struct VSIn
- {
- float4 Pos : POSITION;
- float2 TexCoord : TEXCOORD0;
- #ifdef BUMPMAP
- float4 Tangent : TANGENT;
- #endif
- #ifdef INSTANCED
- float4x3 ModelInstance : TEXCOORD2;
- #endif
- };
- struct PSIn
- {
- #ifndef BUMPMAP
- float2 TexCoord : TEXCOORD0;
- #else
- float4 TexCoord : TEXCOORD0;
- float4 Tangent : TEXCOORD3;
- #ifdef MODELNORMAL
- float2 ModelTexCoord : TEXCOORD8;
- #endif
- #endif
- float3 Normal : TEXCOORD1;
- float4 WorldPos : TEXCOORD2;
- float3 DetailTexCoord : TEXCOORD10;
- #ifdef PERPIXEL
- #ifdef SHADOW
- float4 ShadowPos[NUMCASCADES] : TEXCOORD4;
- #endif
- #ifdef SPOTLIGHT
- float4 SpotPos : TEXCOORD5;
- #endif
- #ifdef POINTLIGHT
- float3 CubeMaskVec : TEXCOORD5;
- #endif
- #else
- float3 VertexLight : TEXCOORD4;
- float4 ScreenPos : TEXCOORD5;
- #endif
- #if defined(D3D11) && defined(CLIPPLANE)
- float Clip : SV_CLIPDISTANCE0;
- #endif
- float4 Pos : SV_POSITION;
- };
- struct PSOut
- {
- float4 oColor : SV_TARGET0;
- float4 oAlbedo : SV_TARGET1;
- float4 oNormal : SV_TARGET2;
- float oDepth : SV_TARGET3;
- };
- #ifdef COMPILEVS
- PSIn VS(VSIn vsin)
- {
- PSIn vsout;
- #ifdef INSTANCED
- float4x3 modelMatrix = in.ModelInstance;
- #else
- float4x3 modelMatrix = cModel;
- #endif
- float3 worldPos = mul(vsin.Pos, modelMatrix);
- out.Pos = GetClipPos(worldPos);
- out.Normal = normalize(mul(in.Normal, (float3x3)modelMatrix));
- out.WorldPos = float4(worldPos, GetDepth(out.Pos));
- #ifdef BUMPMAP
- float3 tangent = normalize(mul(vsin.Tangent.xyz, (float3x3)modelMatrix));
- float3 bitangent = cross(tangent, vsout.Normal) * vsin.Tangent.w;
- vsout.TexCoord = float4(worldPos.xz*float2(1.0/cTilingFactors.x, 1.0/cTilingFactors.y), bitangent.xy);
- vsout.Tangent = float4(tangent, bitangent.z);
- #ifdef MODELNORMAL
- vsout.ModelTexCoord = vsin.TexCoord;
- #endif
- #else
- vsout.TexCoord = GetTexCoord(worldPos.xz*float2(1.0/cTilingFactors.x, 1.0/cTilingFactors.y));
- #endif
- vsout.DetailTexCoord=worldPos.xyz*0.8;
- #if defined(D3D11) && defined(CLIPPLANE)
- vsout.Clip = dot(vsout.Pos, 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
- vsout.SpotPos = mul(projWorldPos, cLightMatrices[0]);
- #endif
- #ifdef POINTLIGHT
- vsout.CubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
- #endif
- #else
- // Ambient & per-vertex lighting
- vsout.VertexLight = GetAmbient(GetZonePos(worldPos));
- #ifdef NUMVERTEXLIGHTS
- for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
- vsout.VertexLight += GetVertexLight(i, worldPos, out.Normal) * cVertexLights[i * 3].rgb;
- #endif
- vsout.ScreenPos = GetScreenPos(out.Pos);
- #endif
- return out;
- };
- #endif
- #ifdef COMPILEPS
- PSOut PS(PSIn psin)
- {
- PSOut psout;
- // Get material diffuse albedo
- float4 weights0=tWeightMap0.Sample(sWeightMap0, psin.TexCoord.xy);
- float3 nrm = normalize(psin.Normal);
- 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(psin.DetailTexCoord.zy, 4))*blending.x +
- tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xy, 4))*blending.z +
- tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xz, 0))*blending.y;
- float4 tex2=tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.zy, 5))*blending.x +
- tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xy, 5))*blending.z +
- tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xz, 1))*blending.y;
- float4 tex3=tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.zy, 6))*blending.x +
- tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xy, 6))*blending.z +
- tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xz, 2))*blending.y;
- float4 tex4=tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.zy, 7))*blending.x +
- tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xy, 7))*blending.z +
- tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.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(psin.WorldPos.y*0.125,0,1);
- diffColor*=grad;
- // Get normal
- #ifdef BUMPMAP
- float3 bump1=DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.zy, 4)))*blending.x +
- DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xy, 4)))*blending.z +
- DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xz, 0)))*blending.y;
- float3 bump2=DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.zy, 5)))*blending.x +
- DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xy, 5)))*blending.z +
- DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xz, 1)))*blending.y;
- float3 bump3=DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.zy, 6)))*blending.x +
- DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xy, 6)))*blending.z +
- DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xz, 2)))*blending.y;
- float3 bump4=DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.zy, 7)))*blending.x +
- DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xy, 7)))*blending.z +
- DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xz, 3)))*blending.y;
- float3x3 tbn = float3x3(psin.Tangent.xyz, float3(psin.TexCoord.zw, psin.Tangent.w), psin.Normal);
- #ifdef MODELNORMAL
- float3 texnormal=normalize((bump1*b1+bump2*b2+bump3*b3+bump4*b4)/bsum)*0.5+0.5;
- float3 modelnormal=tModelNormal.Sample(sModelNormal, psin.ModelTexCoord.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(psin.WorldPos.w, psin.WorldPos.y);
- #else
- float fogFactor = GetFogFactor(psin.WorldPos.w);
- #endif
- //diffColor=float4(fogFactor,fogFactor,fogFactor,1);
- //diffColor=float4(1,1,1,1);
- #if defined(PERPIXEL)
- // Per-pixel forward lighting
- float3 lightDir;
- float3 lightColor;
- float3 finalColor;
- float diff = GetDiffuse(normal, psin.WorldPos.xyz, lightDir);
- #ifdef SHADOW
- diff *= GetShadow(psin.ShadowPos, psin.WorldPos.w);
- #endif
- #if defined(SPOTLIGHT)
- lightColor = psin.SpotPos.w > 0.0 ? Sample2DProj(LightSpotMap, psin.SpotPos).rgb * cLightColor.rgb : 0.0;
- #elif defined(CUBEMASK)
- lightColor = SampleCube(LightCubeMap, psin.CubeMaskVec).rgb * cLightColor.rgb;
- #else
- lightColor = cLightColor.rgb;
- #endif
- #ifdef SPECULAR
- float spec = GetSpecular(normal, cCameraPosPS - psin.WorldPos.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;
- psout.oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
- #else
- psout.oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
- #endif
- #elif defined(PREPASS)
- // Fill light pre-pass G-Buffer
- float specPower = cMatSpecColor.a / 255.0;
- psout.oColor = float4(normal * 0.5 + 0.5, specPower);
- psout.oDepth = psin.WorldPos.w;
- #elif defined(DEFERRED)
- // Fill deferred G-buffer
- float specIntensity = specColor.g;
- float specPower = cMatSpecColor.a / 255.0;
- float3 finalColor = psin.VertexLight * diffColor.rgb;
- psout.oColor = float4(GetFog(finalColor, fogFactor), 1.0);
- psout.oAlbedo = fogFactor * float4(diffColor.rgb, specIntensity);
- psout.oNormal = float4(normal * 0.5 + 0.5, specPower);
- psout.oDepth = iWorldPos.w;
- #else
- // Ambient & per-vertex lighting
- float3 finalColor = psin.VertexLight * 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, psin.ScreenPos);
- float3 lightSpecColor = lightInput.a * (lightInput.rgb / GetIntensity(lightInput.rgb));
- finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
- #endif
- psout.oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
- #endif
- return psout;
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement