Advertisement
Krythic

Untitled

Sep 1st, 2020
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.20 KB | None | 0 0
  1. Texture2D Texture1 : register( t0 );
  2. Texture2D Texture2 : register( t1 );
  3. Texture2D Texture3 : register( t2 );
  4. Texture2D Texture4 : register( t3 );
  5. Texture2D Texture5 : register( t4 );
  6. Texture2D Texture6 : register( t5 );
  7. Texture2D Texture7 : register( t6 );
  8. Texture2D Texture8 : register( t7 );
  9.  
  10. TextureCube CubeTexture1 : register( t8 );
  11. TextureCube CubeTexture2 : register( t9 );
  12. TextureCube CubeTexture3 : register( t10 );
  13. TextureCube CubeTexture4 : register( t11 );
  14. TextureCube CubeTexture5 : register( t12 );
  15. TextureCube CubeTexture6 : register( t13 );
  16. TextureCube CubeTexture7 : register( t14 );
  17. TextureCube CubeTexture8 : register( t15 );
  18.  
  19. SamplerState TexSampler : register( s0 );
  20.  
  21. cbuffer MaterialVars : register (b0)
  22. {
  23.     float4 MaterialAmbient;
  24.     float4 MaterialDiffuse;
  25.     float4 MaterialSpecular;
  26.     float4 MaterialEmissive;
  27.     float MaterialSpecularPower;
  28. };
  29.  
  30. cbuffer LightVars : register (b1)
  31. {
  32.     float4 AmbientLight;
  33.     float4 LightColor[4];
  34.     float4 LightAttenuation[4];
  35.     float3 LightDirection[4];
  36.     float LightSpecularIntensity[4];
  37.     uint IsPointLight[4];
  38.     uint ActiveLights;
  39. }
  40.  
  41. cbuffer ObjectVars : register(b2)
  42. {
  43.     float4x4 LocalToWorld4x4;
  44.     float4x4 LocalToProjected4x4;
  45.     float4x4 WorldToLocal4x4;
  46.     float4x4 WorldToView4x4;
  47.     float4x4 UVTransform4x4;
  48.     float3 EyePosition;
  49. };
  50.  
  51. cbuffer MiscVars : register(b3)
  52. {
  53.     float ViewportWidth;
  54.     float ViewportHeight;
  55.     float Time;
  56. };
  57.  
  58. struct A2V
  59. {
  60.     float4 pos : POSITION0;
  61.     float3 normal : NORMAL0;
  62.     float4 tangent : TANGENT0;
  63.     float4 color : COLOR0;
  64.     float2 uv : TEXCOORD0;
  65. };
  66.  
  67. struct V2P
  68. {
  69.     float4 pos : SV_POSITION;
  70.     float4 diffuse : COLOR;
  71.     float2 uv : TEXCOORD0;
  72.     float3 worldNorm : TEXCOORD1;
  73.     float3 worldPos : TEXCOORD2;
  74.     float3 toEye : TEXCOORD3;
  75.     float4 tangent : TEXCOORD4;
  76.     float3 normal : TEXCOORD5;
  77. };
  78.  
  79. struct P2F
  80. {
  81.     float4 fragment : SV_Target;
  82. };
  83.  
  84. //
  85. // desaturate
  86. //
  87. float3 Desaturate(
  88.     float3 color,
  89.     float3 luminance,
  90.     float percent
  91.     )
  92. {
  93.     float3 desatColor = dot(color, luminance);
  94.     return lerp(color, desatColor, percent);
  95. }
  96.  
  97. //
  98. // fresnel falloff
  99. //
  100. float Fresnel(
  101.     float3 surfaceNormal,
  102.     float3 toEye,
  103.     float exp
  104.     )
  105. {
  106.     float x = 1 - saturate(dot(surfaceNormal, toEye));
  107.     return pow(x, exp);
  108. }
  109.  
  110. //
  111. // panning offset
  112. //
  113. float2 PanningOffset(
  114.     float2 sourceUV,
  115.     float time,
  116.     float speedX,
  117.     float speedY
  118.     )
  119. {
  120.     float2 uvOffset = float2(time, time) * float2(speedX, speedY);
  121.  
  122.     return sourceUV + uvOffset;
  123. }
  124.  
  125. //
  126. // parallax offset
  127. //
  128. float2 ParallaxOffset(
  129.     float2 sourceUV,
  130.     float height,
  131.     float depthScale,
  132.     float depthPlane,
  133.     float3 tangentCameraVector
  134.     )
  135. {
  136.     float bias = -(depthScale * depthPlane);
  137.     float heightAdj = (depthScale * height) + bias;
  138.  
  139.     return sourceUV + (tangentCameraVector.xy * heightAdj);
  140. }
  141.  
  142. //
  143. // rotate offset
  144. //
  145. float2 RotateOffset(
  146.     float2 sourceUV,
  147.     float time,
  148.     float centerX,
  149.     float centerY,
  150.     float speed
  151.     )
  152. {
  153.     float2 ray = sourceUV - float2(centerX, centerY);
  154.     float theta = time * speed;
  155.  
  156.     float cosTheta = cos(theta);
  157.     float sinTheta = sin(theta);
  158.  
  159.     float2x2 rotMatrix = float2x2(float2(cosTheta, -sinTheta), float2(sinTheta, cosTheta));
  160.  
  161.     return mul(rotMatrix, ray) + float2(centerX, centerY);
  162. }
  163.  
  164. //
  165. // lambert lighting function
  166. //
  167. float3 LambertLighting(
  168.     float3 lightNormal,
  169.     float3 surfaceNormal,
  170.     float3 materialAmbient,
  171.     float3 lightAmbient,
  172.     float3 lightColor,
  173.     float3 pixelColor
  174.     )
  175. {
  176.     // compute amount of contribution per light
  177.     float diffuseAmount = saturate(dot(lightNormal, surfaceNormal));
  178.     float3 diffuse = diffuseAmount * lightColor * pixelColor;
  179.  
  180.     // combine ambient with diffuse
  181.     return saturate((materialAmbient * lightAmbient) + diffuse);
  182. }
  183.  
  184. //
  185. // specular contribution function
  186. //
  187. float3 SpecularContribution(
  188.     float3 toEye,
  189.     float3 lightNormal,
  190.     float3 surfaceNormal,
  191.     float3 materialSpecularColor,
  192.     float materialSpecularPower,
  193.     float lightSpecularIntensity,
  194.     float3 lightColor
  195.     )
  196. {
  197.     // compute specular contribution
  198.     float3 vHalf = normalize(lightNormal + toEye);
  199.     float specularAmount = saturate(dot(surfaceNormal, vHalf));
  200.     specularAmount = pow(specularAmount, max(materialSpecularPower,0.0001f)) * lightSpecularIntensity;
  201.     float3 specular = materialSpecularColor * lightColor * specularAmount;
  202.    
  203.     return specular;
  204. }
  205.  
  206. //
  207. // combines a float3 RGB value with an alpha value into a float4
  208. //
  209. float4 CombineRGBWithAlpha(float3 rgb, float a)
  210. {
  211.     return float4(rgb.r, rgb.g, rgb.b, a);
  212. }
  213.  
  214. //
  215. // returns texture dimensions of the specified texture as a float2
  216. //
  217. float2 GetTextureDimensions(Texture2D tex)
  218. {
  219.     float x;
  220.     float y;
  221.  
  222.     tex.GetDimensions(x,y);
  223.  
  224.     return float2(x, y);
  225. }
  226.  
  227. //
  228. // returns texel delta of the specified texture as a float2
  229. //
  230. float2 GetTexelDelta(Texture2D tex)
  231. {
  232.     float x;
  233.     float y;
  234.  
  235.     tex.GetDimensions(x,y);
  236.  
  237.     return float2(1.0f/x, 1.0f/y);
  238. }
  239.  
  240. //
  241. // runs an edge detection filter on the input
  242. //
  243. float4 EdgeDetectionFilter(Texture2D tex, float2 uv)
  244. {
  245.     float dx;
  246.     float dy;
  247.     tex.GetDimensions(dx,dy);
  248.     dx = 1.0f/dx;
  249.     dy = 1.0f/dy;
  250.  
  251.     float4 color0 = -2.0f * tex.Sample(TexSampler, uv + float2(-dx, 0));
  252.     float4 color1 = -tex.Sample(TexSampler, uv + float2(-dx, dy));
  253.     float4 color2 = -tex.Sample(TexSampler, uv + float2(-dx, -dy));
  254.     float4 color3 = 2.0f * tex.Sample(TexSampler, uv + float2(dx, 0));
  255.     float4 color4 = tex.Sample(TexSampler, uv + float2(dx, dy));
  256.     float4 color5 = tex.Sample(TexSampler, uv + float2(dx, -dy));
  257.     float4 sumX = color0 + color1 + color2 + color3 + color4 + color5;
  258.  
  259.     float4 color6 = -2.0f * tex.Sample(TexSampler, uv + float2(0, -dy));
  260.     float4 color7 = -tex.Sample(TexSampler, uv + float2(dx, -dy));
  261.     float4 color8 = color2;
  262.     float4 color9 = 2.0f * tex.Sample(TexSampler, uv + float2(0, dy));
  263.     float4 color10 = color4;
  264.     float4 color11 = tex.Sample(TexSampler, uv + float2(-dx, dy));
  265.     float4 sumY= color6 + color7 + color8 + color9 + color10 + color11;
  266.  
  267.     return sqrt(sumX * sumX + sumY * sumY);
  268. }
  269.  
  270.  
  271. //
  272. // runs a Gaussian blur filter on the input
  273. //
  274. float4 Blur(Texture2D tex, float2 uv)
  275. {
  276.     float dx;
  277.     float dy;
  278.     tex.GetDimensions(dx,dy);
  279.     dx = 1.0f/dx;
  280.     dy = 1.0f/dy;
  281.  
  282.     float4 colorSum = float4(0,0,0,0);
  283.  
  284.     colorSum += tex.Sample(TexSampler, uv + float2(-2 * dx, -2 * dy)) * 2;
  285.     colorSum += tex.Sample(TexSampler, uv + float2(-1 * dx, -2 * dy)) * 4;
  286.     colorSum += tex.Sample(TexSampler, uv + float2(      0, -2 * dy)) * 5;
  287.     colorSum += tex.Sample(TexSampler, uv + float2( 1 * dx, -2 * dy)) * 4;
  288.     colorSum += tex.Sample(TexSampler, uv + float2( 2 * dx, -2 * dy)) * 2;
  289.  
  290.     colorSum += tex.Sample(TexSampler, uv + float2(-2 * dx, -1 * dy)) * 4;
  291.     colorSum += tex.Sample(TexSampler, uv + float2(-1 * dx, -1 * dy)) * 9;
  292.     colorSum += tex.Sample(TexSampler, uv + float2(      0, -1 * dy)) * 12;
  293.     colorSum += tex.Sample(TexSampler, uv + float2( 1 * dx, -1 * dy)) * 9;
  294.     colorSum += tex.Sample(TexSampler, uv + float2( 2 * dx, -1 * dy)) * 4;
  295.  
  296.     colorSum += tex.Sample(TexSampler, uv + float2(-2 * dx,       0)) * 5;
  297.     colorSum += tex.Sample(TexSampler, uv + float2(-1 * dx,       0)) * 12;
  298.     colorSum += tex.Sample(TexSampler, uv + float2(      0,       0)) * 15;
  299.     colorSum += tex.Sample(TexSampler, uv + float2( 1 * dx,       0)) * 12;
  300.     colorSum += tex.Sample(TexSampler, uv + float2( 2 * dx,       0)) * 5;
  301.  
  302.     colorSum += tex.Sample(TexSampler, uv + float2(-2 * dx,  1 * dy)) * 4;
  303.     colorSum += tex.Sample(TexSampler, uv + float2(-1 * dx,  1 * dy)) * 9;
  304.     colorSum += tex.Sample(TexSampler, uv + float2(      0,  1 * dy)) * 12;
  305.     colorSum += tex.Sample(TexSampler, uv + float2( 1 * dx,  1 * dy)) * 9;
  306.     colorSum += tex.Sample(TexSampler, uv + float2( 2 * dx,  1 * dy)) * 4;
  307.  
  308.     colorSum += tex.Sample(TexSampler, uv + float2(-2 * dx,  2 * dy)) * 2;
  309.     colorSum += tex.Sample(TexSampler, uv + float2(-1 * dx,  2 * dy)) * 4;
  310.     colorSum += tex.Sample(TexSampler, uv + float2(      0,  2 * dy)) * 5;
  311.     colorSum += tex.Sample(TexSampler, uv + float2( 1 * dx,  2 * dy)) * 4;
  312.     colorSum += tex.Sample(TexSampler, uv + float2( 2 * dx,  2 * dy)) * 2;
  313.  
  314.     return colorSum/159;
  315. }
  316.  
  317.  
  318. //
  319. // Sharpen filter
  320. //
  321. float4 Sharpen(Texture2D tex, float2 uv)
  322. {
  323.     float dx;
  324.     float dy;
  325.     tex.GetDimensions(dx,dy);
  326.     dx = 1.0f/dx;
  327.     dy = 1.0f/dy;
  328.  
  329.     float4 colorSum = float4(0,0,0,0);
  330.  
  331.     colorSum += tex.Sample(TexSampler, uv + float2(-1 * dx, -1 * dy)) * -1;
  332.     colorSum += tex.Sample(TexSampler, uv + float2(      0, -1 * dy)) * -1;
  333.     colorSum += tex.Sample(TexSampler, uv + float2( 1 * dx, -1 * dy)) * -1;
  334.  
  335.     colorSum += tex.Sample(TexSampler, uv + float2(-1 * dx,       0)) * -1;
  336.     colorSum += tex.Sample(TexSampler, uv + float2(      0,       0)) * 17;
  337.     colorSum += tex.Sample(TexSampler, uv + float2( 1 * dx,       0)) * -1;
  338.  
  339.     colorSum += tex.Sample(TexSampler, uv + float2(-1 * dx,  1 * dy)) * -1;
  340.     colorSum += tex.Sample(TexSampler, uv + float2(      0,  1 * dy)) * -1;
  341.     colorSum += tex.Sample(TexSampler, uv + float2( 1 * dx,  1 * dy)) * -1;
  342.  
  343.     return colorSum/9;
  344. }
  345. P2F main(V2P pixel)
  346. {
  347.     P2F result;
  348.  
  349.     // we need to normalize incoming vectors
  350.     float3 surfaceNormal = normalize(pixel.normal);
  351.     float3 surfaceTangent = normalize(pixel.tangent.xyz);
  352.     float3 worldNormal = normalize(pixel.worldNorm);
  353.     float3 toEyeVector = normalize(pixel.toEye);
  354.  
  355.     // construct tangent matrix
  356.     float3x3 localToTangent = transpose(float3x3(surfaceTangent, cross(surfaceNormal, surfaceTangent) * pixel.tangent.w, surfaceNormal));
  357.     float3x3 worldToTangent = mul((float3x3)WorldToLocal4x4, localToTangent);
  358.  
  359.     // transform some vectors into tangent space
  360.     float3 tangentLightDir = normalize(mul(LightDirection[0], worldToTangent));
  361.     float3 tangentToEyeVec = normalize(mul(toEyeVector, worldToTangent));
  362.  
  363.     // BEGIN GENERATED CODE
  364.     float4 local0 = Blur(Texture1, pixel.uv);
  365.     result.fragment = CombineRGBWithAlpha(local0.rgb, local0.a);
  366.     // END GENERATED CODE
  367.  
  368.     if (result.fragment.a == 0.0f) discard;
  369.  
  370.     return result;
  371. }
  372.  
  373.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement