Guest User

Untitled

a guest
Apr 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. //--------------------------------------------------------------------------------------
  2. // Particle.fx
  3. // Direct3D 10 Shader Model 4.0 Line Drawing Demo
  4. // Copyright (c) Stefan Petersson, 2008
  5. //--------------------------------------------------------------------------------------
  6.  
  7. //-----------------------------------------------------------------------------------------
  8. // Input and Output Structures
  9. //-----------------------------------------------------------------------------------------
  10.  
  11. //World matrices
  12. Matrix World;
  13. Matrix View;
  14. Matrix Projection;
  15.  
  16.  
  17.  
  18. struct VSSceneIn
  19. {
  20. float3 Pos : POS;
  21. float3 WorldPos : POS;
  22. float4 Color : COL;
  23. float3 Normal : NORMAL;
  24. float2 texCo : TC;
  25. };
  26.  
  27. struct PSSceneIn
  28. {
  29. float4 Pos : SV_Position; // SV_Position is a (S)ystem (V)ariable that denotes transformed position
  30. float3 WorldPos : POS;
  31. float4 Color : COLOR;
  32. float3 Normal : NORMAL;
  33. float2 texCo : TC;
  34. };
  35.  
  36. struct DirectionalLight
  37. {
  38. float4 color;
  39. float3 dir;
  40. };
  41.  
  42. struct Material
  43. {
  44. float Ka, Kd, Ks, A;
  45. };
  46.  
  47. // Nonnumeric values cannot be added to a cbuffer.
  48. Texture2D gDiffuseMap;
  49.  
  50. //lighting vars
  51. DirectionalLight light;
  52. Material material;
  53. float4 ambientLight;
  54. float3 CameraPos;
  55.  
  56. //-----------------------------------------------------------------------------------------
  57. // Constant Buffers (where we store variables by frequency of update)
  58. //-----------------------------------------------------------------------------------------
  59. cbuffer cbEveryFrame
  60. {
  61. matrix g_mWorldViewProjection;
  62. };
  63.  
  64. //Simple samplerstate
  65. SamplerState linearSampler
  66. {
  67. Filter = MIN_MAG_MIP_LINEAR;
  68. AddressU = Wrap;
  69. AddressV = Wrap;
  70. MaxAnisotropy = 16;
  71. };
  72.  
  73. //RASTERIZER STATES
  74. //--------------------------------------------------------------------------------------
  75. RasterizerState rsSolid
  76. {
  77. FillMode = Solid;
  78. CullMode = NONE;
  79. FrontCounterClockwise = false;
  80. };
  81.  
  82. //-----------------------------------------------------------------------------------------
  83. // State Structures
  84. //-----------------------------------------------------------------------------------------
  85. DepthStencilState DisableDepth
  86. {
  87. DepthEnable = FALSE;
  88. DepthWriteMask = ZERO;
  89. };
  90.  
  91. DepthStencilState EnableDepthTestOnly
  92. {
  93. DepthEnable = TRUE;
  94. DepthWriteMask = ZERO;
  95. };
  96.  
  97. DepthStencilState EnableDepth
  98. {
  99. DepthEnable = TRUE;
  100. DepthWriteMask = ALL;
  101. };
  102.  
  103. RasterizerState NoCulling
  104. {
  105. CullMode = NONE;
  106. };
  107.  
  108.  
  109. //-----------------------------------------------------------------------------------------
  110. // VertexShader: VSScene
  111. //-----------------------------------------------------------------------------------------
  112.  
  113. PSSceneIn VSScene(VSSceneIn input)
  114. {
  115. PSSceneIn output = (PSSceneIn)0;
  116.  
  117. // transform the point into view space
  118. output.Pos = mul( float4(input.Pos,1.0), g_mWorldViewProjection );
  119. output.Color = input.Color;
  120. output.texCo = input.texCo;
  121.  
  122. return output;
  123. }
  124.  
  125. //--------------------------------------------------------------------------------------
  126. // Phong Lighting Reflection Model
  127. //--------------------------------------------------------------------------------------
  128. float4 calcPhongLighting( Material M, float4 LColor, float3 N, float3 L, float3 V, float3 R )
  129. {
  130. float4 Ia = M.Ka * ambientLight;
  131. float4 Id = M.Kd * saturate( dot(N,L) );
  132. float4 Is = M.Ks * pow( saturate(dot(R,V)), M.A );
  133.  
  134. return Ia + (Id + Is) * LColor;
  135. }
  136.  
  137. //--------------------------------------------------------------------------------------
  138. // Blinn-Phong Lighting Reflection Model
  139. //--------------------------------------------------------------------------------------
  140. float4 calcBlinnPhongLighting( Material M, float4 LColor, float3 N, float3 L, float3 H )
  141. {
  142. float4 Ia = M.Ka * ambientLight;
  143. float4 Id = M.Kd * saturate( dot(N,L) );
  144. float4 Is = M.Ks * pow( saturate(dot(N,H)), M.A );
  145.  
  146. return Ia + (Id + Is) * LColor;
  147. }
  148.  
  149. //--------------------------------------------------------------------------------------
  150. // PER VERTEX LIGHTING - PHONG
  151. //--------------------------------------------------------------------------------------
  152. PSSceneIn VS_VERTEX_LIGHTING_PHONG( VSSceneIn input )
  153. {
  154. PSSceneIn output;
  155.  
  156. // transform the point into clip space
  157. output.Pos = mul( float4(input.Pos,1.0), g_mWorldViewProjection );
  158.  
  159. //set texture coords
  160. output.texCo = input.texCo;
  161.  
  162. //calculate lighting vectors
  163. float3 N = normalize( mul( input.Normal, (float3x3) World) );
  164. float3 V = normalize( CameraPos - (float3) input.Pos );
  165. //DONOT USE -light.dir since the reflection returns a ray from the surface
  166. float3 R = reflect( light.dir, N);
  167.  
  168. //calculate per vertex lighting intensity and interpolate it like a color
  169. output.Color = calcPhongLighting( material, light.color, N, -light.dir, V, R);
  170.  
  171. return output;
  172. }
  173.  
  174. float4 PS_VERTEX_LIGHTING_PHONG( PSSceneIn input ) : SV_Target
  175. {
  176. return input.Color;
  177. }
  178.  
  179. //--------------------------------------------------------------------------------------
  180. // PER PIXEL LIGHTING
  181. //--------------------------------------------------------------------------------------
  182. PSSceneIn VS_PIXEL_LIGHTING_PHONG( VSSceneIn input )
  183. {
  184. PSSceneIn output;
  185.  
  186. //transform position to clip space - keep worldspace position
  187. output.WorldPos = mul( float4(input.Pos,1.0f), World );
  188. output.Pos = mul( float4(output.WorldPos,1.0f), View );
  189. output.Pos = mul( output.Pos, Projection);
  190.  
  191. //set texture coords
  192. output.texCo = input.texCo;
  193.  
  194. //set required lighting vectors for interpolation
  195. output.Normal = normalize( mul(input.Normal, (float3x3)World) );
  196.  
  197. return output;
  198. }
  199.  
  200. float4 PS_PIXEL_LIGHTING_PHONG( PSSceneIn input ) : SV_Target
  201. {
  202. //calculate lighting vectors - renormalize vectors
  203. input.Normal = normalize( input.Normal );
  204. float3 V = normalize( CameraPos - (float3) input.WorldPos );
  205. //DONOT USE -light.dir since the reflection returns a ray from the surface
  206. float3 R = reflect( light.dir, input.Normal);
  207.  
  208. //calculate lighting
  209. float4 I = calcPhongLighting( material, light.color, input.Normal, -light.dir, V, R );
  210. return I * gDiffuseMap.Sample(linearSampler, input.texCo);
  211. }
  212.  
  213. //-----------------------------------------------------------------------------------------
  214. // PixelShader: PSSceneMain
  215. //-----------------------------------------------------------------------------------------
  216. float4 PSScene(PSSceneIn input) : SV_Target
  217. {
  218. return gDiffuseMap.Sample(linearSampler, input.texCo);
  219.  
  220. //return input.Color;
  221. //return float4(1,1,1,1);
  222. }
  223.  
  224. //-----------------------------------------------------------------------------------------
  225. // Technique: RenderTextured
  226. //-----------------------------------------------------------------------------------------
  227. technique10 DrawLine
  228. {
  229. pass p0
  230. {
  231. // Set VS, GS, and PS
  232. SetVertexShader( CompileShader( vs_4_0, VS_PIXEL_LIGHTING_PHONG() ) );
  233. SetGeometryShader( NULL );
  234. SetPixelShader( CompileShader( ps_4_0, PS_PIXEL_LIGHTING_PHONG() ) );
  235.  
  236. SetRasterizerState( NoCulling );
  237.  
  238. SetDepthStencilState( EnableDepth, 0 );
  239. //SetDepthStencilState( DisableDepth, 0 );
  240. //SetDepthStencilState( EnableDepthTestOnly, 0 );
  241. }
  242. }
  243.  
  244.  
  245. struct SurfaceInfo
  246. {
  247. float3 pos;
  248. float3 normal;
  249. float4 diffuse;
  250. float4 spec;
  251. };
  252.  
  253. float3 PointLight(SurfaceInfo v, Light L, float3 eyePos)
  254. {
  255. float3 litColor = float3(0.0f, 0.0f, 0.0f);
  256.  
  257. // The vector from the surface to the light.
  258. float3 lightVec = L.pos - v.pos;
  259.  
  260. // The distance from surface to light.
  261. float d = length(lightVec);
  262.  
  263. if( d > L.range )
  264. return float3(0.0f, 0.0f, 0.0f);
  265.  
  266. // Normalize the light vector.
  267. lightVec /= d;
  268.  
  269. // Add the ambient light term.
  270. litColor += v.diffuse * L.ambient;
  271.  
  272. // Add diffuse and specular term, provided the surface is in
  273. // the line of sight of the light.
  274.  
  275. float diffuseFactor = dot(lightVec, v.normal);
  276. [branch]
  277. if( diffuseFactor > 0.0f )
  278. {
  279. float specPower = max(v.spec.a, 1.0f);
  280. float3 toEye = normalize(eyePos - v.pos);
  281. float3 R = reflect(-lightVec, v.normal);
  282. float specFactor = pow(max(dot(R, toEye), 0.0f), specPower);
  283.  
  284. // diffuse and specular terms
  285. litColor += diffuseFactor * v.diffuse * L.diffuse;
  286. litColor += specFactor * v.spec * L.spec;
  287. }
  288.  
  289. // attenuate
  290. return litColor / dot(L.att, float3(1.0f, d, d*d));
  291. }
Add Comment
Please, Sign In to add comment