Advertisement
Guest User

Untitled

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