Advertisement
Guest User

shader

a guest
Feb 2nd, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.05 KB | None | 0 0
  1. //=============================================================================
  2. // Basic.fx by Frank Luna (C) 2011 All Rights Reserved.
  3. //
  4. // Basic effect that currently supports transformations, lighting, and texturing.
  5. //=============================================================================
  6.  
  7. #include "LightHelper.fx"
  8.  
  9. cbuffer cbPerFrame
  10. {
  11.     DirectionalLight gDirLights[3];
  12.     float3 gEyePosW;
  13.  
  14.     float  gFogStart;
  15.     float  gFogRange;
  16.     float4 gFogColor;
  17. };
  18.  
  19. cbuffer cbPerObject
  20. {
  21.     float4x4 gWorld;
  22.     float4x4 gWorldInvTranspose;
  23.     float4x4 gWorldViewProj;
  24.     float4x4 gTexTransform;
  25.     Material gMaterial;
  26. };
  27.  
  28. // Nonnumeric values cannot be added to a cbuffer.
  29. Texture2D gDiffuseMap;
  30. TextureCube gCubeMap;
  31.  
  32. SamplerState samAnisotropic
  33. {
  34.     Filter = ANISOTROPIC;
  35.     MaxAnisotropy = 4;
  36.  
  37.     AddressU = WRAP;
  38.     AddressV = WRAP;
  39. };
  40.  
  41. struct VertexIn
  42. {
  43.     float3 PosL    : POSITION;
  44.     float3 NormalL : NORMAL;
  45.     float2 Tex     : TEXCOORD;
  46. };
  47.  
  48. struct VertexOut
  49. {
  50.     float4 PosH    : SV_POSITION;
  51.     float3 PosW    : POSITION;
  52.     float3 NormalW : NORMAL;
  53.     float2 Tex     : TEXCOORD;
  54. };
  55.  
  56. VertexOut VS(VertexIn vin)
  57. {
  58.     VertexOut vout;
  59.    
  60.     // Transform to world space space.
  61.     vout.PosW    = mul(float4(vin.PosL, 1.0f), gWorld).xyz;
  62.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorldInvTranspose);
  63.        
  64.     // Transform to homogeneous clip space.
  65.     vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);
  66.    
  67.     // Output vertex attributes for interpolation across triangle.
  68.     vout.Tex = mul(float4(vin.Tex, 0.0f, 1.0f), gTexTransform).xy;
  69.  
  70.     return vout;
  71. }
  72.  
  73. float4 PS(VertexOut pin,
  74.           uniform int gLightCount,
  75.           uniform bool gUseTexure,
  76.           uniform bool gAlphaClip,
  77.           uniform bool gFogEnabled,
  78.           uniform bool gReflectionEnabled) : SV_Target
  79. {
  80.     // Interpolating normal can unnormalize it, so normalize it.
  81.     pin.NormalW = normalize(pin.NormalW);
  82.  
  83.     // The toEye vector is used in lighting.
  84.     float3 toEye = gEyePosW - pin.PosW;
  85.  
  86.     // Cache the distance to the eye from this surface point.
  87.     float distToEye = length(toEye);
  88.  
  89.     // Normalize.
  90.     toEye /= distToEye;
  91.    
  92.     // Default to multiplicative identity.
  93.     float4 texColor = float4(1, 1, 1, 1);
  94.     if(gUseTexure)
  95.     {
  96.         // Sample texture.
  97.         texColor = gDiffuseMap.Sample( samAnisotropic, pin.Tex );
  98.  
  99.         if(gAlphaClip)
  100.         {
  101.             // Discard pixel if texture alpha < 0.1.  Note that we do this
  102.             // test as soon as possible so that we can potentially exit the shader
  103.             // early, thereby skipping the rest of the shader code.
  104.             clip(texColor.a - 0.1f);
  105.         }
  106.     }
  107.      
  108.     //
  109.     // Lighting.
  110.     //
  111.  
  112.     float4 litColor = texColor;
  113.     if( gLightCount > 0  )
  114.     {  
  115.         // Start with a sum of zero.
  116.         float4 ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
  117.         float4 diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
  118.         float4 spec    = float4(0.0f, 0.0f, 0.0f, 0.0f);
  119.  
  120.         // Sum the light contribution from each light source.  
  121.         [unroll]
  122.         for(int i = 0; i < gLightCount; ++i)
  123.         {
  124.             float4 A, D, S;
  125.             ComputeDirectionalLight(gMaterial, gDirLights[i], pin.NormalW, toEye,
  126.                 A, D, S);
  127.  
  128.             ambient += A;
  129.             diffuse += D;
  130.             spec    += S;
  131.         }
  132.  
  133.         litColor = texColor*(ambient + diffuse) + spec;
  134.  
  135.         if( gReflectionEnabled )
  136.         {
  137.             float3 incident = -toEye;
  138.             float3 reflectionVector = reflect(incident, pin.NormalW);
  139.             float4 reflectionColor  = gCubeMap.Sample(samAnisotropic, reflectionVector);
  140.  
  141.             litColor += gMaterial.Reflect*reflectionColor;
  142.         }
  143.     }
  144.  
  145.     //
  146.     // Fogging
  147.     //
  148.  
  149.     if( gFogEnabled )
  150.     {
  151.         float fogLerp = saturate( (distToEye - gFogStart) / gFogRange );
  152.  
  153.         // Blend the fog color and the lit color.
  154.         litColor = lerp(litColor, gFogColor, fogLerp);
  155.     }
  156.  
  157.     // Common to take alpha from diffuse material and texture.
  158.     litColor.a = gMaterial.Diffuse.a * texColor.a;
  159.  
  160.     return litColor;
  161. }
  162.  
  163. technique11 Light1
  164. {
  165.     pass P0
  166.     {
  167.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  168.         SetGeometryShader( NULL );
  169.         SetPixelShader( CompileShader( ps_4_0, PS(1, false, false, false, false) ) );
  170.     }
  171. }
  172.  
  173. technique11 Light2
  174. {
  175.     pass P0
  176.     {
  177.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  178.         SetGeometryShader( NULL );
  179.         SetPixelShader( CompileShader( ps_4_0, PS(2, false, false, false, false) ) );
  180.     }
  181. }
  182.  
  183. technique11 Light3
  184. {
  185.     pass P0
  186.     {
  187.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  188.         SetGeometryShader( NULL );
  189.         SetPixelShader( CompileShader( ps_4_0, PS(3, false, false, false, false) ) );
  190.     }
  191. }
  192.  
  193. technique11 Light0Tex
  194. {
  195.     pass P0
  196.     {
  197.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  198.         SetGeometryShader( NULL );
  199.         SetPixelShader( CompileShader( ps_4_0, PS(0, true, false, false, false) ) );
  200.     }
  201. }
  202.  
  203. technique11 Light1Tex
  204. {
  205.     pass P0
  206.     {
  207.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  208.         SetGeometryShader( NULL );
  209.         SetPixelShader( CompileShader( ps_4_0, PS(1, true, false, false, false) ) );
  210.     }
  211. }
  212.  
  213. technique11 Light2Tex
  214. {
  215.     pass P0
  216.     {
  217.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  218.         SetGeometryShader( NULL );
  219.         SetPixelShader( CompileShader( ps_4_0, PS(2, true, false, false, false) ) );
  220.     }
  221. }
  222.  
  223. technique11 Light3Tex
  224. {
  225.     pass P0
  226.     {
  227.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  228.         SetGeometryShader( NULL );
  229.         SetPixelShader( CompileShader( ps_4_0, PS(3, true, false, false, false) ) );
  230.     }
  231. }
  232.  
  233. technique11 Light0TexAlphaClip
  234. {
  235.     pass P0
  236.     {
  237.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  238.         SetGeometryShader( NULL );
  239.         SetPixelShader( CompileShader( ps_4_0, PS(0, true, true, false, false) ) );
  240.     }
  241. }
  242.  
  243. technique11 Light1TexAlphaClip
  244. {
  245.     pass P0
  246.     {
  247.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  248.         SetGeometryShader( NULL );
  249.         SetPixelShader( CompileShader( ps_4_0, PS(1, true, true, false, false) ) );
  250.     }
  251. }
  252.  
  253. technique11 Light2TexAlphaClip
  254. {
  255.     pass P0
  256.     {
  257.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  258.         SetGeometryShader( NULL );
  259.         SetPixelShader( CompileShader( ps_4_0, PS(2, true, true, false, false) ) );
  260.     }
  261. }
  262.  
  263. technique11 Light3TexAlphaClip
  264. {
  265.     pass P0
  266.     {
  267.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  268.         SetGeometryShader( NULL );
  269.         SetPixelShader( CompileShader( ps_4_0, PS(3, true, true, false, false) ) );
  270.     }
  271. }
  272.  
  273. technique11 Light1Fog
  274. {
  275.     pass P0
  276.     {
  277.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  278.         SetGeometryShader( NULL );
  279.         SetPixelShader( CompileShader( ps_4_0, PS(1, false, false, true, false) ) );
  280.     }
  281. }
  282.  
  283. technique11 Light2Fog
  284. {
  285.     pass P0
  286.     {
  287.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  288.         SetGeometryShader( NULL );
  289.         SetPixelShader( CompileShader( ps_4_0, PS(2, false, false, true, false) ) );
  290.     }
  291. }
  292.  
  293. technique11 Light3Fog
  294. {
  295.     pass P0
  296.     {
  297.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  298.         SetGeometryShader( NULL );
  299.         SetPixelShader( CompileShader( ps_4_0, PS(3, false, false, true, false) ) );
  300.     }
  301. }
  302.  
  303. technique11 Light0TexFog
  304. {
  305.     pass P0
  306.     {
  307.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  308.         SetGeometryShader( NULL );
  309.         SetPixelShader( CompileShader( ps_4_0, PS(0, true, false, true, false) ) );
  310.     }
  311. }
  312.  
  313. technique11 Light1TexFog
  314. {
  315.     pass P0
  316.     {
  317.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  318.         SetGeometryShader( NULL );
  319.         SetPixelShader( CompileShader( ps_4_0, PS(1, true, false, true, false) ) );
  320.     }
  321. }
  322.  
  323. technique11 Light2TexFog
  324. {
  325.     pass P0
  326.     {
  327.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  328.         SetGeometryShader( NULL );
  329.         SetPixelShader( CompileShader( ps_4_0, PS(2, true, false, true, false) ) );
  330.     }
  331. }
  332.  
  333. technique11 Light3TexFog
  334. {
  335.     pass P0
  336.     {
  337.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  338.         SetGeometryShader( NULL );
  339.         SetPixelShader( CompileShader( ps_4_0, PS(3, true, false, true, false) ) );
  340.     }
  341. }
  342.  
  343. technique11 Light0TexAlphaClipFog
  344. {
  345.     pass P0
  346.     {
  347.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  348.         SetGeometryShader( NULL );
  349.         SetPixelShader( CompileShader( ps_4_0, PS(0, true, true, true, false) ) );
  350.     }
  351. }
  352.  
  353. technique11 Light1TexAlphaClipFog
  354. {
  355.     pass P0
  356.     {
  357.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  358.         SetGeometryShader( NULL );
  359.         SetPixelShader( CompileShader( ps_4_0, PS(1, true, true, true, false) ) );
  360.     }
  361. }
  362.  
  363. technique11 Light2TexAlphaClipFog
  364. {
  365.     pass P0
  366.     {
  367.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  368.         SetGeometryShader( NULL );
  369.         SetPixelShader( CompileShader( ps_4_0, PS(2, true, true, true, false) ) );
  370.     }
  371. }
  372.  
  373. technique11 Light3TexAlphaClipFog
  374. {
  375.     pass P0
  376.     {
  377.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  378.         SetGeometryShader( NULL );
  379.         SetPixelShader( CompileShader( ps_4_0, PS(3, true, true, true, false) ) );
  380.     }
  381. }
  382.  
  383. technique11 Light1Reflect
  384. {
  385.     pass P0
  386.     {
  387.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  388.         SetGeometryShader( NULL );
  389.         SetPixelShader( CompileShader( ps_4_0, PS(1, false, false, false, true) ) );
  390.     }
  391. }
  392.  
  393. technique11 Light2Reflect
  394. {
  395.     pass P0
  396.     {
  397.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  398.         SetGeometryShader( NULL );
  399.         SetPixelShader( CompileShader( ps_4_0, PS(2, false, false, false, true) ) );
  400.     }
  401. }
  402.  
  403. technique11 Light3Reflect
  404. {
  405.     pass P0
  406.     {
  407.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  408.         SetGeometryShader( NULL );
  409.         SetPixelShader( CompileShader( ps_4_0, PS(3, false, false, false, true) ) );
  410.     }
  411. }
  412.  
  413. technique11 Light0TexReflect
  414. {
  415.     pass P0
  416.     {
  417.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  418.         SetGeometryShader( NULL );
  419.         SetPixelShader( CompileShader( ps_4_0, PS(0, true, false, false, true) ) );
  420.     }
  421. }
  422.  
  423. technique11 Light1TexReflect
  424. {
  425.     pass P0
  426.     {
  427.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  428.         SetGeometryShader( NULL );
  429.         SetPixelShader( CompileShader( ps_4_0, PS(1, true, false, false, true) ) );
  430.     }
  431. }
  432.  
  433. technique11 Light2TexReflect
  434. {
  435.     pass P0
  436.     {
  437.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  438.         SetGeometryShader( NULL );
  439.         SetPixelShader( CompileShader( ps_4_0, PS(2, true, false, false, true) ) );
  440.     }
  441. }
  442.  
  443. technique11 Light3TexReflect
  444. {
  445.     pass P0
  446.     {
  447.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  448.         SetGeometryShader( NULL );
  449.         SetPixelShader( CompileShader( ps_4_0, PS(3, true, false, false, true) ) );
  450.     }
  451. }
  452.  
  453. technique11 Light0TexAlphaClipReflect
  454. {
  455.     pass P0
  456.     {
  457.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  458.         SetGeometryShader( NULL );
  459.         SetPixelShader( CompileShader( ps_4_0, PS(0, true, true, false, true) ) );
  460.     }
  461. }
  462.  
  463. technique11 Light1TexAlphaClipReflect
  464. {
  465.     pass P0
  466.     {
  467.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  468.         SetGeometryShader( NULL );
  469.         SetPixelShader( CompileShader( ps_4_0, PS(1, true, true, false, true) ) );
  470.     }
  471. }
  472.  
  473. technique11 Light2TexAlphaClipReflect
  474. {
  475.     pass P0
  476.     {
  477.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  478.         SetGeometryShader( NULL );
  479.         SetPixelShader( CompileShader( ps_4_0, PS(2, true, true, false, true) ) );
  480.     }
  481. }
  482.  
  483. technique11 Light3TexAlphaClipReflect
  484. {
  485.     pass P0
  486.     {
  487.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  488.         SetGeometryShader( NULL );
  489.         SetPixelShader( CompileShader( ps_4_0, PS(3, true, true, false, true) ) );
  490.     }
  491. }
  492.  
  493. technique11 Light1FogReflect
  494. {
  495.     pass P0
  496.     {
  497.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  498.         SetGeometryShader( NULL );
  499.         SetPixelShader( CompileShader( ps_4_0, PS(1, false, false, true, true) ) );
  500.     }
  501. }
  502.  
  503. technique11 Light2FogReflect
  504. {
  505.     pass P0
  506.     {
  507.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  508.         SetGeometryShader( NULL );
  509.         SetPixelShader( CompileShader( ps_4_0, PS(2, false, false, true, true) ) );
  510.     }
  511. }
  512.  
  513. technique11 Light3FogReflect
  514. {
  515.     pass P0
  516.     {
  517.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  518.         SetGeometryShader( NULL );
  519.         SetPixelShader( CompileShader( ps_4_0, PS(3, false, false, true, true) ) );
  520.     }
  521. }
  522.  
  523. technique11 Light0TexFogReflect
  524. {
  525.     pass P0
  526.     {
  527.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  528.         SetGeometryShader( NULL );
  529.         SetPixelShader( CompileShader( ps_4_0, PS(0, true, false, true, true) ) );
  530.     }
  531. }
  532.  
  533. technique11 Light1TexFogReflect
  534. {
  535.     pass P0
  536.     {
  537.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  538.         SetGeometryShader( NULL );
  539.         SetPixelShader( CompileShader( ps_4_0, PS(1, true, false, true, true) ) );
  540.     }
  541. }
  542.  
  543. technique11 Light2TexFogReflect
  544. {
  545.     pass P0
  546.     {
  547.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  548.         SetGeometryShader( NULL );
  549.         SetPixelShader( CompileShader( ps_4_0, PS(2, true, false, true, true) ) );
  550.     }
  551. }
  552.  
  553. technique11 Light3TexFogReflect
  554. {
  555.     pass P0
  556.     {
  557.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  558.         SetGeometryShader( NULL );
  559.         SetPixelShader( CompileShader( ps_4_0, PS(3, true, false, true, true) ) );
  560.     }
  561. }
  562.  
  563. technique11 Light0TexAlphaClipFogReflect
  564. {
  565.     pass P0
  566.     {
  567.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  568.         SetGeometryShader( NULL );
  569.         SetPixelShader( CompileShader( ps_4_0, PS(0, true, true, true, true) ) );
  570.     }
  571. }
  572.  
  573. technique11 Light1TexAlphaClipFogReflect
  574. {
  575.     pass P0
  576.     {
  577.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  578.         SetGeometryShader( NULL );
  579.         SetPixelShader( CompileShader( ps_4_0, PS(1, true, true, true, true) ) );
  580.     }
  581. }
  582.  
  583. technique11 Light2TexAlphaClipFogReflect
  584. {
  585.     pass P0
  586.     {
  587.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  588.         SetGeometryShader( NULL );
  589.         SetPixelShader( CompileShader( ps_4_0, PS(2, true, true, true, true) ) );
  590.     }
  591. }
  592.  
  593. technique11 Light3TexAlphaClipFogReflect
  594. {
  595.     pass P0
  596.     {
  597.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  598.         SetGeometryShader( NULL );
  599.         SetPixelShader( CompileShader( ps_4_0, PS(3, true, true, true, true) ) );
  600.     }
  601. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement