Advertisement
Guest User

FishEyeShader

a guest
Aug 8th, 2012
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.53 KB | None | 0 0
  1. // This is used by 3dsmax to load the correct parser
  2. string ParamID = "0x0";
  3.  
  4. // light directions (world space)
  5. uniform extern float3 lightPos1 : Position <  
  6.     string UIName = "Light Direction 1";
  7.     string Object = "omnilight";
  8.     int RefID = 0;
  9.     > = {-0.577, -0.577, 0.577};
  10.  
  11. float3 lightPos2 : Position <  
  12.     string UIName = "Light Direction 2";
  13.     string Object = "OmniLight";
  14.     int RefID = 1;
  15.     > = {-0.577, -0.577, 0.577};
  16.  
  17. float3 lightPos3 : Position <  
  18.     string UIName = "Light Direction 3";
  19.     string Object = "OmniLight";
  20.     int RefID = 2;
  21.     > = {-0.577, -0.577, 0.577};
  22.  
  23.  
  24. float warpWeight <
  25.     string UIName = "Warp Weight";
  26.     string UIType = "FloatSpinner";
  27.     float UIMin = 0.0f;
  28.     float UIMax = 1.0f;
  29.     > = 0.75f;
  30.  
  31. // material values
  32. float4 k_d  <
  33.     string UIName = "Diffuse";
  34.     > = float4( 0.47f, 0.47f, 0.47f, 1.0f );    // diffuse
  35.    
  36. float d<
  37.     string UIName = "   Diffuse Power";
  38.     string UIType = "FloatSpinner";
  39.     float UIMin = 0.0f;
  40.     float UIMax = 100.0f;  
  41.     >  = 1;
  42.  
  43. bool bDiffuse <
  44.     string UIName = "Use Diffuse Map";
  45.     string UIType = "Checkbox";
  46.     >  = false;
  47.  
  48. // diffuse texture passed in:
  49. texture diffuseMap : DIFFUSEMAP
  50. <
  51.     string name ="";
  52.     string UIName = "     Diffuse Map";
  53. >;
  54.  
  55. float4 k_s  <
  56.     string UIName = "Specular";
  57.     > = float4( 0.47f, 0.47f, 0.47f, 1.0f );    // specular
  58.    
  59. float specPower <
  60.     string UIName = "   Specular Power";
  61.     string UIType = "FloatSpinner";
  62.     float UIMin = 0.0f;
  63.     float UIMax = 100.0f;  
  64.     >  = 50;
  65.    
  66. bool bSpecular <
  67.     string UIName = "Use Specular Map";
  68.     string UIType = "Checkbox";
  69.     >  = false;
  70.  
  71. // diffuse texture passed in:
  72. texture specularMap : SPECULARMAP
  73. <
  74.     string name ="";
  75.     string UIName = "     Specular Map";   
  76. >;
  77.  
  78. bool bNormal <
  79.     string UIName = "Use Normal Map";
  80.     string UIType = "Checkbox";
  81.     >  = false;
  82.  
  83. // normal texture passed in:
  84. texture normalMap : NORMALMAP
  85. <
  86.     string name ="";
  87.     string UIName = "     Normal Map"; 
  88. >;
  89.  
  90. // samplers for our textures:
  91. sampler2D diffuseSampler = sampler_state
  92. {
  93.     Texture = <diffuseMap>;
  94.     MinFilter=LINEAR;
  95.     MagFilter=LINEAR;
  96.     MipFilter=LINEAR;
  97.     MipLODBias=-1;
  98.     AddressU = WRAP;
  99.     AddressV = WRAP;
  100. };
  101.  
  102. sampler2D specSampler = sampler_state
  103. {
  104.     Texture = <specularMap>;
  105.     MinFilter=LINEAR;
  106.     MagFilter=LINEAR;
  107.     MipFilter=LINEAR;
  108.     MipLODBias=-1;
  109.     AddressU = WRAP;
  110.     AddressV = WRAP;
  111. };
  112.  
  113. sampler2D normalSampler = sampler_state
  114. {
  115.     Texture = <normalMap>;
  116.     MinFilter=LINEAR;
  117.     MagFilter=LINEAR;
  118.     MipFilter=LINEAR;
  119.     MipLODBias=-1;
  120.     AddressU = WRAP;
  121.     AddressV = WRAP;
  122. };
  123.  
  124. // transformations
  125. float4x4 World          :   WORLD;
  126. float4x4 View           :   VIEW;
  127. float4x4 Projection     :   PROJECTION;
  128. float4x4 ViewInv        :   ViewInverse;
  129. float4x4 WorldViewProj  :   WORLDVIEWPROJ;
  130. float4x4 WorldView      :   WORLDVIEW;
  131.  
  132. struct VS_OUTPUT
  133. {
  134.     float4 Pos      : POSITION;
  135.     float3 normal   : TEXCOORD0;
  136.     float3 tangent  : TEXCOORD1;
  137.     float3 binormal : TEXCOORD2;
  138.     float3 light    : TEXCOORD3;
  139.     float3 eyeVec   : TEXCOORD4;
  140.     float3 half     : TEXCOORD5;
  141.     float2 Tex      : TEXCOORD6;
  142. };
  143.  
  144. // vertex shader
  145. VS_OUTPUT VS(
  146.     float3 Pos      : POSITION,
  147.     float3 col      : COLOR,
  148.     float3 Norm     : NORMAL,
  149.     float3 Tangent  : TANGENT,
  150.     float3 Binormal : BINORMAL,
  151.     float2 Tex      : TEXCOORD0,
  152.     uniform float3 lightPos )
  153. {
  154.     VS_OUTPUT Out = (VS_OUTPUT)0;
  155.  
  156.     float3 P = mul(float4(Pos, 1),(float4x4)World).rgb; // position (from object to world space)
  157.     float3 L = normalize(lightPos - P);
  158.    
  159.     Out.normal =    normalize(mul(Norm,     (float3x3)World)); // normal (from object to world space)
  160.     Out.tangent =   normalize(mul(Tangent,  (float3x3)World)); // normal (from object to world space)
  161.     Out.binormal =  normalize(mul(Binormal, (float3x3)World)); // normal (from object to world space)
  162.  
  163.     Out.light = normalize(L);
  164.     Out.eyeVec = normalize(ViewInv[3].xyz - P); //calculate eyevector
  165.     Out.half = (Out.light + Out.eyeVec);
  166.  
  167.     float distToCam = length(ViewInv[3].xyz - P);
  168.     Out.Pos  = mul(float4(Pos,1),WorldViewProj);        // position (projected)
  169.    
  170.     Out.Pos.xy *= lerp( 1, Out.Pos.z / max(distToCam, 1.f), min(warpWeight,0.99f));
  171.  
  172.     Out.Tex = Tex;
  173.    
  174.     return Out;
  175. }
  176.  
  177. // pixel shader
  178. float4 PS(
  179.     VS_OUTPUT input, uniform bool bAdditive ) : COLOR
  180. {
  181.     float3 normal = normalize( input.normal.rgb );
  182.     float3 light  = normalize( input.light.rgb );
  183.     float3 half   = normalize( input.half );
  184.    
  185.     float4 color = 0;
  186.    
  187.     // Sample the normal map and convert it to a proper normal vector:
  188.     float4 normalTex = float4( normal, 1 );
  189.     if (bNormal)
  190.     {
  191.         // Sample normals:
  192.         normalTex = tex2D(normalSampler, input.Tex);
  193.         normalTex = (2*normalTex)-1;
  194.         normalTex.xyz = normalTex.yxz;
  195.    
  196.         // Create a tangent space matrix:
  197.         const float3x3 basis = float3x3( normalize( input.tangent ), normalize( input.binormal ), normalize( input.normal ) );
  198.         normalTex.rgb = normalize( mul(normalTex.rgb, basis) );
  199.     }
  200.    
  201.     float4 diffuse = k_d;
  202.     if (bDiffuse)
  203.     {
  204.         diffuse *= tex2D(diffuseSampler, input.Tex);   
  205.     }
  206.    
  207.     float4 specular = k_s;
  208.     if (bSpecular)
  209.         specular *= tex2D(specSampler, input.Tex);
  210.  
  211.     float lambert = pow( saturate(dot(normalTex, light)), d ); // LAMBERT
  212.     float blinn = pow( saturate(dot(normalTex, half) ), specPower ); // BLINN
  213.    
  214.     color += saturate(lambert * diffuse) + (blinn * specular);
  215.        
  216.     color.a = 1;
  217.        
  218.     return  color;
  219. }
  220.  
  221. technique ThreeLight
  222. {
  223.     pass P0
  224.     {
  225.         // shaders
  226.         CullMode = None;
  227.  
  228.         VertexShader = compile vs_3_0 VS(lightPos1);
  229.         PixelShader  = compile ps_3_0 PS(false);
  230.     }  
  231.  
  232.     pass P1
  233.     {
  234.         // shaders
  235.         CullMode = None;
  236.  
  237.         AlphaBlendEnable = true;
  238.         srcblend = one;
  239.         destblend = one;
  240.  
  241.         VertexShader = compile vs_3_0 VS(lightPos2);
  242.         PixelShader  = compile ps_3_0 PS(true);
  243.     }  
  244.  
  245.     pass P2
  246.     {
  247.         // shaders
  248.         CullMode = None;
  249.  
  250.         AlphaBlendEnable = true;
  251.         SrcBlend = one;
  252.         DestBlend = one;
  253.  
  254.         VertexShader = compile vs_3_0 VS(lightPos3);
  255.         PixelShader  = compile ps_3_0 PS(true);
  256.     }  
  257.  
  258. }
  259.  
  260. technique TwoLight
  261. {
  262.     pass P0
  263.     {
  264.         // shaders
  265.         CullMode = None;
  266.  
  267.         VertexShader = compile vs_3_0 VS(lightPos1);
  268.         PixelShader  = compile ps_3_0 PS(false);
  269.     }  
  270.  
  271.     pass P1
  272.     {
  273.         // shaders
  274.         CullMode = None;
  275.  
  276.         AlphaBlendEnable = true;
  277.         srcblend = one;
  278.         destblend = one;
  279.  
  280.         VertexShader = compile vs_3_0 VS(lightPos2);
  281.         PixelShader  = compile ps_3_0 PS(true);
  282.     }  
  283. }
  284.  
  285.  
  286. technique OneLight
  287. {
  288.     pass P0
  289.     {
  290.         // shaders
  291.         CullMode = None;
  292.  
  293.         VertexShader = compile vs_3_0 VS(lightPos1);
  294.         PixelShader  = compile ps_3_0 PS(false);
  295.     }  
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement