SHARE
TWEET

phong shader

a guest Dec 19th, 2018 9 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // some badass macro magic for both dx9+ and gl3+ support
  2. #ifdef SM4
  3. #define TECH(n,v,p) \
  4.     technique n { pass { VertexShader = compile vs_4_0_level_9_3 v (); PixelShader = compile ps_4_0_level_9_3 p (); }}
  5. #else
  6. #define TECH(n,v,p) \
  7.     technique n { pass { VertexShader = compile vs_2_0 v (); PixelShader = compile ps_3_0 p (); }}
  8. #endif
  9.  
  10.  
  11. float3 LightPosition = float3(-100, 100, -100);
  12. float3 CameraPosition;
  13. float4x4 View;
  14. float4x4 Projection;
  15. float4x4 World;
  16. float3 BaseColor;
  17.  
  18. struct VertexShaderInput
  19. {
  20.    float4 Position : POSITION;
  21.    float2 Texcoord : TEXCOORD0;
  22.    float3 Normal :   NORMAL0;
  23.    
  24. };
  25.  
  26. struct VertexShaderOutput
  27. {
  28.    float4 Position :        POSITION;
  29.    float2 Texcoord :        TEXCOORD0;
  30.    float3 ViewDirection :   TEXCOORD1;
  31.    float3 LightDirection :  TEXCOORD2;
  32.    float3 Normal :          TEXCOORD3;
  33.    
  34. };
  35.  
  36. VertexShaderOutput VertexShaderFunction( VertexShaderInput Input )
  37. {
  38.    VertexShaderOutput Output;
  39.    
  40.    float4x4 vp = mul(View, Projection);
  41.  
  42.    Output.Position         = mul( Input.Position, mul(World, vp) );
  43.    Output.Texcoord         = Input.Texcoord;
  44.    
  45.    float3 ObjectPosition = mul( Input.Position, View ).xyz;
  46.      
  47.    Output.ViewDirection    = CameraPosition - ObjectPosition;
  48.    Output.LightDirection   = LightPosition - ObjectPosition;
  49.    Output.Normal           = (mul( Input.Normal, View )).xyz;
  50.      
  51.    return( Output );
  52.    
  53. }
  54.  
  55. float4 fvAmbient = float4(.3686, .3686, .3686, 1.0);
  56. float4 fvSpecular = float4(.4902, .4902, .4902, 1.0);
  57. float4 fvDiffuse = float4(.8863, .8863, .8863, 1.0);
  58. float fSpecularPower = 25;
  59.  
  60. struct PS_INPUT
  61. {
  62.    float2 Texcoord :        TEXCOORD0;
  63.    float3 ViewDirection :   TEXCOORD1;
  64.    float3 LightDirection:   TEXCOORD2;
  65.    float3 Normal :          TEXCOORD3;
  66.    
  67. };
  68.  
  69. float u_time;
  70. float2 u_resolution;
  71.  
  72. float2 random2(float2 st){
  73.     st = float2( dot(st,float2(127.1,311.7)),
  74.               dot(st,float2(269.5,183.3)) );
  75.     return -1.0 + 2.0*frac(sin(st)*43758.5453123);
  76. }
  77.  
  78. float noise(float2 st) {
  79.     float2 i = floor(st);
  80.     float2 f = frac(st);
  81.  
  82.     float2 u = f*f*(3.0-2.0*f);
  83.  
  84.     return lerp(lerp( dot( random2(i + float2(0.0,0.0) ), f - float2(0.0,0.0) ),
  85.                       dot( random2(i + float2(1.0,0.0) ), f - float2(1.0,0.0) ), u.x),
  86.                 lerp( dot( random2(i + float2(0.0,1.0) ), f - float2(0.0,1.0) ),
  87.                       dot( random2(i + float2(1.0,1.0) ), f - float2(1.0,1.0) ), u.x), u.y);
  88. }
  89.  
  90. float2 toPolar(float2 src){
  91.     return float2( atan(src.x / src.y), length(src) );
  92. }
  93.  
  94. float4 mc(float2 tex) {
  95.     float2 st = toPolar( tex.xy );
  96.     st.x *= u_resolution.x/u_resolution.y;
  97.     float3 color = float3(0,0,0);
  98.  
  99.     float t = 1.0;
  100.     // Uncomment to animate
  101.     t = abs(1-sin(u_time*.125))*5;
  102.     // Comment and uncomment the following lines:
  103.     st += noise(st*2.)*t; // Animate the coordinate space
  104.     color = float3(1,1,1) * smoothstep(.18,.2,noise(st)); // Big black drops
  105.     color += smoothstep(.15,.2,noise(st*10.)); // Black splatter
  106.     color -= smoothstep(.35,.4,noise(st*10.)); // Holes on splatter
  107.  
  108.     color = BaseColor - color;
  109.  
  110.     return float4(color.x, color.y, color.z,1.0);
  111. }
  112.  
  113. float4 PixelShaderFunction( PS_INPUT Input ) : COLOR0
  114. {      
  115.    float3 fvLightDirection = normalize( Input.LightDirection );
  116.    float3 fvNormal         = normalize( Input.Normal );
  117.    float  fNDotL           = dot( fvNormal, fvLightDirection );
  118.    
  119.    float3 fvReflection     = normalize( ( ( 2.0f * fvNormal ) * ( fNDotL ) ) - fvLightDirection );
  120.    float3 fvViewDirection  = normalize( Input.ViewDirection );
  121.    float  fRDotV           = max( 0.0f, dot( fvReflection, fvViewDirection ) );
  122.    
  123.    float4 fvBaseColor      = mc( Input.Texcoord );
  124.    
  125.    float4 fvTotalAmbient   = fvAmbient * fvBaseColor;
  126.    float4 fvTotalDiffuse   = fvDiffuse * fNDotL * fvBaseColor;
  127.    float4 fvTotalSpecular  = fvSpecular * pow( fRDotV, fSpecularPower );
  128.    
  129.    return( saturate( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular ) );
  130.      
  131. }
  132.  
  133. TECH(texturer, VertexShaderFunction, PixelShaderFunction)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top