Advertisement
Guest User

Untitled

a guest
Aug 7th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.81 KB | None | 0 0
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "ScreenPos.hlsl"
  5. #include "Lighting.hlsl"
  6. #include "Fog.hlsl"
  7.  
  8.  
  9. void VS(float4 iPos : POSITION,
  10.     #ifndef BILLBOARD
  11.         float3 iNormal : NORMAL,
  12.     #endif
  13.     #ifndef NOUV
  14.         float2 iTexCoord : TEXCOORD0,
  15.     #endif
  16.     #ifdef VERTEXCOLOR
  17.         float4 iColor : COLOR0,
  18.     #endif
  19.     #if defined(LIGHTMAP) || defined(AO)
  20.         float2 iTexCoord2 : TEXCOORD1,
  21.     #endif
  22.     #ifdef NORMALMAP
  23.         float4 iTangent : TANGENT,
  24.     #endif
  25.     #ifdef SKINNED
  26.         float4 iBlendWeights : BLENDWEIGHT,
  27.         int4 iBlendIndices : BLENDINDICES,
  28.     #endif
  29.     #ifdef INSTANCED
  30.         float4x3 iModelInstance : TEXCOORD2,
  31.     #endif
  32.     #ifdef BILLBOARD
  33.         float2 iSize : TEXCOORD1,
  34.     #endif
  35.     #ifndef NORMALMAP
  36.         out float2 oTexCoord : TEXCOORD0,
  37.     #else
  38.         out float4 oTexCoord : TEXCOORD0,
  39.         out float4 oTangent : TEXCOORD3,
  40.     #endif
  41.     out float3 oNormal : TEXCOORD1,
  42.     out float4 oWorldPos : TEXCOORD2,
  43.     #ifdef PERPIXEL
  44.         #ifdef SHADOW
  45.             out float4 oShadowPos[NUMCASCADES] : TEXCOORD4,
  46.         #endif
  47.         #ifdef SPOTLIGHT
  48.             out float4 oSpotPos : TEXCOORD5,
  49.         #endif
  50.         #ifdef POINTLIGHT
  51.             out float3 oCubeMaskVec : TEXCOORD5,
  52.         #endif
  53.     #else
  54.         out float3 oVertexLight : TEXCOORD4,
  55.         out float4 oScreenPos : TEXCOORD5,
  56.         #ifdef ENVCUBEMAP
  57.             out float3 oReflectionVec : TEXCOORD6,
  58.         #endif
  59.         #if defined(LIGHTMAP) || defined(AO)
  60.             out float2 oTexCoord2 : TEXCOORD7,
  61.         #endif
  62.     #endif
  63.     #ifdef VERTEXCOLOR
  64.         out float4 oColor : COLOR0,
  65.     #endif
  66.     #if defined(D3D11) && defined(CLIPPLANE)
  67.         out float oClip : SV_CLIPDISTANCE0,
  68.     #endif
  69.     out float4 oPos : OUTPOSITION)
  70. {
  71.     // Define a 0,0 UV coord if not expected from the vertex data
  72.     #ifdef NOUV
  73.     float2 iTexCoord = float2(0.0, 0.0);
  74.     #endif
  75.  
  76.     // VERTEX FETCH
  77.     float3 displace = Sample2DLod0(DisplaceMap, iTexCoord.xy);    
  78.     float d = (displace.x * cChannelFactor.x + displace.y * cChannelFactor.y + displace.z * cChannelFactor.z);
  79.  
  80.     float4x3 modelMatrix = iModelMatrix;
  81.     float3 worldPos = GetWorldPos(modelMatrix) + (iNormal * d * cDisplacement);
  82.     oPos = GetClipPos(worldPos);
  83.     oNormal = GetWorldNormal(modelMatrix);
  84.     oWorldPos = float4(worldPos, GetDepth(oPos));
  85.  
  86.     #if defined(D3D11) && defined(CLIPPLANE)
  87.         oClip = dot(oPos, cClipPlane);
  88.     #endif
  89.  
  90.     #ifdef VERTEXCOLOR
  91.         oColor = iColor;
  92.     #endif
  93.  
  94.     #ifdef NORMALMAP
  95.         float3 tangent = GetWorldTangent(modelMatrix);
  96.         float3 bitangent = cross(tangent, oNormal) * iTangent.w;
  97.         oTexCoord = float4(GetTexCoord(iTexCoord), bitangent.xy);
  98.         oTangent = float4(tangent, bitangent.z);
  99.     #else
  100.         oTexCoord = GetTexCoord(iTexCoord);
  101.     #endif
  102.  
  103.     #ifdef PERPIXEL
  104.         // Per-pixel forward lighting
  105.         float4 projWorldPos = float4(worldPos.xyz, 1.0);
  106.  
  107.         #ifdef SHADOW
  108.             // Shadow projection: transform from world space to shadow space
  109.             GetShadowPos(projWorldPos, oShadowPos);
  110.         #endif
  111.  
  112.         #ifdef SPOTLIGHT
  113.             // Spotlight projection: transform from world space to projector texture coordinates
  114.             oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  115.         #endif
  116.  
  117.         #ifdef POINTLIGHT
  118.             oCubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
  119.         #endif
  120.     #else
  121.         // Ambient & per-vertex lighting
  122.         #if defined(LIGHTMAP) || defined(AO)
  123.             // If using lightmap, disregard zone ambient light
  124.             // If using AO, calculate ambient in the PS
  125.             oVertexLight = float3(0.0, 0.0, 0.0);
  126.             oTexCoord2 = iTexCoord2;
  127.         #else
  128.             oVertexLight = GetAmbient(GetZonePos(worldPos));
  129.         #endif
  130.  
  131.         #ifdef NUMVERTEXLIGHTS
  132.             for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  133.                 oVertexLight += GetVertexLight(i, worldPos, oNormal) * cVertexLights[i * 3].rgb;
  134.         #endif
  135.        
  136.         oScreenPos = GetScreenPos(oPos);
  137.  
  138.         #ifdef ENVCUBEMAP
  139.             oReflectionVec = worldPos - cCameraPos;
  140.         #endif
  141.     #endif
  142. }
  143.  
  144. void PS(
  145.     #ifndef NORMALMAP
  146.         float2 iTexCoord : TEXCOORD0,
  147.     #else
  148.         float4 iTexCoord : TEXCOORD0,
  149.         float4 iTangent : TEXCOORD3,
  150.     #endif
  151.     float3 iNormal : TEXCOORD1,
  152.     float4 iWorldPos : TEXCOORD2,
  153.     #ifdef PERPIXEL
  154.         #ifdef SHADOW
  155.             float4 iShadowPos[NUMCASCADES] : TEXCOORD4,
  156.         #endif
  157.         #ifdef SPOTLIGHT
  158.             float4 iSpotPos : TEXCOORD5,
  159.         #endif
  160.         #ifdef CUBEMASK
  161.             float3 iCubeMaskVec : TEXCOORD5,
  162.         #endif
  163.     #else
  164.         float3 iVertexLight : TEXCOORD4,
  165.         float4 iScreenPos : TEXCOORD5,
  166.         #ifdef ENVCUBEMAP
  167.             float3 iReflectionVec : TEXCOORD6,
  168.         #endif
  169.         #if defined(LIGHTMAP) || defined(AO)
  170.             float2 iTexCoord2 : TEXCOORD7,
  171.         #endif
  172.     #endif
  173.     #ifdef VERTEXCOLOR
  174.         float4 iColor : COLOR0,
  175.     #endif
  176.     #if defined(D3D11) && defined(CLIPPLANE)
  177.         float iClip : SV_CLIPDISTANCE0,
  178.     #endif
  179.     #ifdef PREPASS
  180.         out float4 oDepth : OUTCOLOR1,
  181.     #endif
  182.     #ifdef DEFERRED
  183.         out float4 oAlbedo : OUTCOLOR1,
  184.         out float4 oNormal : OUTCOLOR2,
  185.         out float4 oDepth : OUTCOLOR3,
  186.     #endif
  187.     out float4 oColor : OUTCOLOR0)
  188. {
  189.     // Get material diffuse albedo
  190.    
  191.     float4 displace = Sample2DLod0(NormalMap, iTexCoord.xy);
  192.     float d = (displace.x * cChannelFactor.x + displace.y * cChannelFactor.y + displace.z * cChannelFactor.z);
  193.    
  194.     if ((cClipRange - d) < 0.0 ) discard;
  195.  
  196.     // Get material diffuse albedo
  197.     #ifdef DIFFMAP
  198.         float4 diffInput = Sample2DLod0(DiffMap, float2(d, 0.5));
  199.         #ifdef ALPHAMASK
  200.             if (diffInput.a < 0.5)
  201.                 discard;
  202.         #endif
  203.         float4 diffColor = cMatDiffColor * diffInput;
  204.     #else
  205.         float4 diffColor = cMatDiffColor;
  206.     #endif
  207.  
  208.     #ifdef VERTEXCOLOR
  209.         diffColor *= iColor;
  210.     #endif
  211.  
  212.     // Get material specular albedo
  213.     #ifdef SPECMAP
  214.         float3 specColor = cMatSpecColor.rgb * Sample2D(SpecMap, iTexCoord.xy).rgb;
  215.     #else
  216.         float3 specColor = cMatSpecColor.rgb;
  217.     #endif
  218.  
  219.     // Get normal
  220.     #ifdef NORMALMAP
  221.         float3x3 tbn = float3x3(iTangent.xyz, float3(iTexCoord.zw, iTangent.w), iNormal);
  222.         float3 normal = normalize(mul(DecodeNormal(Sample2D(NormalMap, iTexCoord.xy)), tbn));
  223.     #else
  224.         float3 normal = normalize(iNormal);
  225.     #endif
  226.  
  227.     // Get fog factor
  228.     #ifdef HEIGHTFOG
  229.         float fogFactor = GetHeightFogFactor(iWorldPos.w, iWorldPos.y);
  230.     #else
  231.         float fogFactor = GetFogFactor(iWorldPos.w);
  232.     #endif
  233.  
  234.     #if defined(PERPIXEL)
  235.         // Per-pixel forward lighting
  236.         float3 lightDir;
  237.         float3 lightColor;
  238.         float3 finalColor;
  239.  
  240.         float diff = GetDiffuse(normal, iWorldPos.xyz, lightDir);
  241.  
  242.         #ifdef SHADOW
  243.             diff *= GetShadow(iShadowPos, iWorldPos.w);
  244.         #endif
  245.  
  246.         #if defined(SPOTLIGHT)
  247.             lightColor = iSpotPos.w > 0.0 ? Sample2DProj(LightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  248.         #elif defined(CUBEMASK)
  249.             lightColor = SampleCube(LightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  250.         #else
  251.             lightColor = cLightColor.rgb;
  252.         #endif
  253.    
  254.         #ifdef SPECULAR
  255.             float spec = GetSpecular(normal, cCameraPosPS - iWorldPos.xyz, lightDir, cMatSpecColor.a);
  256.             finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  257.         #else
  258.             finalColor = diff * lightColor * diffColor.rgb;
  259.         #endif
  260.  
  261.         #ifdef AMBIENT
  262.             finalColor += cAmbientColor * diffColor.rgb;
  263.             finalColor += cMatEmissiveColor;
  264.             oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  265.         #else
  266.             oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  267.         #endif
  268.     #elif defined(PREPASS)
  269.         // Fill light pre-pass G-Buffer
  270.         float specPower = cMatSpecColor.a / 255.0;
  271.  
  272.         oColor = float4(normal * 0.5 + 0.5, specPower);
  273.         oDepth = iWorldPos.w;
  274.     #elif defined(DEFERRED)
  275.         // Fill deferred G-buffer
  276.         float specIntensity = specColor.g;
  277.         float specPower = cMatSpecColor.a / 255.0;
  278.  
  279.         float3 finalColor = iVertexLight * diffColor.rgb;
  280.         #ifdef AO
  281.             // If using AO, the vertex light ambient is black, calculate occluded ambient here
  282.             finalColor += Sample2D(EmissiveMap, iTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  283.         #endif
  284.         #ifdef ENVCUBEMAP
  285.             finalColor += cMatEnvMapColor * SampleCube(EnvCubeMap, reflect(iReflectionVec, normal)).rgb;
  286.         #endif
  287.         #ifdef LIGHTMAP
  288.             finalColor += Sample2D(EmissiveMap, iTexCoord2).rgb * diffColor.rgb;
  289.         #endif
  290.         #ifdef EMISSIVEMAP
  291.             finalColor += cMatEmissiveColor * Sample2D(EmissiveMap, iTexCoord.xy).rgb;
  292.         #else
  293.             finalColor += cMatEmissiveColor;
  294.         #endif
  295.  
  296.         oColor = float4(GetFog(finalColor, fogFactor), 1.0);
  297.         oAlbedo = fogFactor * float4(diffColor.rgb, specIntensity);
  298.         oNormal = float4(normal * 0.5 + 0.5, specPower);
  299.         oDepth = iWorldPos.w;
  300.     #else
  301.         // Ambient & per-vertex lighting
  302.         float3 finalColor = iVertexLight * diffColor.rgb;
  303.         #ifdef AO
  304.             // If using AO, the vertex light ambient is black, calculate occluded ambient here
  305.             finalColor += Sample2D(EmissiveMap, iTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  306.         #endif
  307.  
  308.         #ifdef MATERIAL
  309.             // Add light pre-pass accumulation result
  310.             // Lights are accumulated at half intensity. Bring back to full intensity now
  311.             float4 lightInput = 2.0 * Sample2DProj(LightBuffer, iScreenPos);
  312.             float3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  313.  
  314.             finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  315.         #endif
  316.  
  317.         #ifdef ENVCUBEMAP
  318.             finalColor += cMatEnvMapColor * SampleCube(EnvCubeMap, reflect(iReflectionVec, normal)).rgb;
  319.         #endif
  320.         #ifdef LIGHTMAP
  321.             finalColor += Sample2D(EmissiveMap, iTexCoord2).rgb * diffColor.rgb;
  322.         #endif
  323.         #ifdef EMISSIVEMAP
  324.             finalColor += cMatEmissiveColor * Sample2D(EmissiveMap, iTexCoord.xy).rgb;
  325.         #else
  326.             finalColor += cMatEmissiveColor;
  327.         #endif
  328.  
  329.         oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  330.     #endif
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement