Advertisement
Guest User

HLSL Normal Map Specular Shader - 3 Lights

a guest
Apr 9th, 2012
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.98 KB | None | 0 0
  1. /******************************************************************************
  2. HLSL Normal Map Specular Shader - 3 Lights
  3. by Ben Cloward
  4. http://www.monitorstudios.com/bcloward/
  5.  
  6. This shader is based on several shaders written by Kevin Bjorke of Nvidia and
  7. included with the Cg plugin for 3DS Max 5.1.
  8.  
  9. This shader uses a normal map for per-pixel lighting - to give the illusion that
  10. the model surface contains more detail than is really there. It also adds a specular
  11. component for making models look shiny.
  12.  
  13. It accepts the following inputs:
  14.  
  15. Ambient Color
  16. Diffuse Color
  17. Diffuse Texture
  18. Specular Texture (a colored specular mask)
  19. Specular Color
  20. Shininess
  21. Normal Map Texture
  22. DXT5 Compressed Normal Map
  23. Flip Normal Map Green Channel
  24. Point Light1 Position
  25. Point Light1 Color
  26. Point Light2 Position
  27. Point Light2 Color
  28. Point Light3 Position
  29. Point Light3 Color
  30.  
  31. It requires hardware support for DirectX 9.
  32.  
  33. Normal Map compression is an option that you can use with this shader.
  34. Copy the red channel of your normal map to the alpha channel. Then delete the
  35. red and blue channels and save your normal map in DXT5 DDS format. Put your
  36. compressed normal map in the DXT5 Normal Map slot for the shader. Then choose
  37. "Compressed" as the technique instead of "complete."
  38.  
  39. This shader is intended to be used by the DirectX 9 shader of 3DS Max
  40. but it could also be used in other applications that support the FX format.
  41.  
  42. ******************************************************************************/
  43.  
  44. /************* TWEAKABLES **************/
  45.  
  46. half4 ambient : Ambient
  47. <
  48. string UIName = "Ambient Color";
  49. > = {0.25f, 0.25f, 0.25f, 1.0f};
  50.  
  51. half4 surfColor : Diffuse
  52. <
  53. string UIName = "Diffuse Color";
  54. > = {1.0f, 1.0f, 1.0f, 1.0f};
  55.  
  56. texture colorTexture : DiffuseMap
  57. <
  58. string name = "default_color.dds";
  59. string UIName = "Diffuse Texture";
  60. string TextureType = "2D";
  61. >;
  62.  
  63. texture specTexture : SpecularMap
  64. <
  65. string name = "default_color.dds";
  66. string UIName = "Specular Texture";
  67. string TextureType = "2D";
  68. >;
  69.  
  70. half4 specularColor : Specular
  71. <
  72. string UIName = "Specular Color";
  73. > = { 0.2f, 0.2f, 0.2f, 1.0f };
  74.  
  75. half shininess <
  76. string UIWidget = "slider";
  77. float UIMin = 1;
  78. float UIMax = 128;
  79. string UIName = "Shininess";
  80. > = 40;
  81.  
  82. texture normalMap : NormalMap
  83. <
  84. string name = "default_bump_normal.dds";
  85. string UIName = "Normal Map";
  86. string TextureType = "2D";
  87. >;
  88.  
  89. texture CnormalMap : CNormalMap
  90. <
  91. string name = "default_bump_normal.dds";
  92. string UIName = "DXT5 Normal Map";
  93. string TextureType = "2D";
  94. >;
  95.  
  96. bool direction
  97. <
  98. string gui = "slider";
  99. string UIName = "Flip Normal Map Green Channel";
  100. > = false;
  101.  
  102. /************** light info **************/
  103.  
  104. half4 light1Pos : POSITION
  105. <
  106. string UIName = "Light 1 Position";
  107. string Object = "PointLight";
  108. string Space = "World";
  109. int refID = 0;
  110. > = {100.0f, 100.0f, 100.0f, 0.0f};
  111.  
  112. half4 light1Color : LIGHTCOLOR
  113. <
  114. int LightRef = 0;
  115. > = { 1.0f, 1.0f, 1.0f, 0.0f };
  116. //----------------------------------
  117. half4 light2Pos : POSITION
  118. <
  119. string UIName = "Light 2 Position";
  120. string Object = "PointLight";
  121. string Space = "World";
  122. int refID = 1;
  123. > = {-100.0f, 100.0f, 100.0f, 0.0f};
  124.  
  125. half4 light2Color : LIGHTCOLOR
  126. <
  127. int LightRef = 1;
  128. > = { 1.0f, 1.0f, 1.0f, 0.0f };
  129. //----------------------------------
  130. half4 light3Pos : POSITION
  131. <
  132. string UIName = "Light 3 Position";
  133. string Object = "PointLight";
  134. string Space = "World";
  135. int refID = 2;
  136. > = {100.0f, -100.0f, 100.0f, 0.0f};
  137.  
  138. half4 light3Color : LIGHTCOLOR
  139. <
  140. int LightRef = 2;
  141. > = { 1.0f, 1.0f, 1.0f, 0.0f };
  142.  
  143. /****************************************************/
  144. /********** SAMPLERS ********************************/
  145. /****************************************************/
  146.  
  147. sampler2D colorTextureSampler = sampler_state
  148. {
  149. Texture = <colorTexture>;
  150. MinFilter = Linear;
  151. MagFilter = Linear;
  152. MipFilter = Anisotropic;
  153. };
  154.  
  155. sampler2D specTextureSampler = sampler_state
  156. {
  157. Texture = <specTexture>;
  158. MinFilter = Linear;
  159. MagFilter = Linear;
  160. MipFilter = Anisotropic;
  161. };
  162.  
  163. sampler2D normalMapSampler = sampler_state
  164. {
  165. Texture = <normalMap>;
  166. MinFilter = Linear;
  167. MagFilter = Linear;
  168. MipFilter = Anisotropic;
  169. };
  170.  
  171. sampler2D CnormalMapSampler = sampler_state
  172. {
  173. Texture = <CnormalMap>;
  174. MinFilter = Linear;
  175. MagFilter = Linear;
  176. MipFilter = Anisotropic;
  177. };
  178.  
  179. /***********************************************/
  180. /*** automatically-tracked "tweakables" ********/
  181. /***********************************************/
  182.  
  183. half4x4 wvp : WorldViewProjection < string UIWidget = "None"; >;
  184. half4x4 worldIT : WorldInverseTranspose < string UIWidget = "None"; >;
  185. half4x4 viewInv : ViewInverse < string UIWidget = "None"; >;
  186. half4x4 world : World < string UIWidget = "None"; >;
  187.  
  188.  
  189. /****************************************************/
  190. /********** CG SHADER FUNCTIONS *********************/
  191. /****************************************************/
  192.  
  193. // input from application
  194. struct a2v {
  195. half4 position : POSITION;
  196. half2 texCoord : TEXCOORD0;
  197. half3 tangent : TANGENT;
  198. half3 binormal : BINORMAL;
  199. half3 normal : NORMAL;
  200. };
  201.  
  202. // output to fragment program
  203. struct v2f {
  204. half4 position : POSITION;
  205. half2 texCoord : TEXCOORD0;
  206. half3 eyeVec : TEXCOORD1;
  207. half3 lightVec : TEXCOORD2;
  208. half3 worldNormal : TEXCOORD3;
  209. half3 worldTangent : TEXCOORD4;
  210. half3 worldBinormal : TEXCOORD5;
  211. };
  212.  
  213. // blinn lighting with lit function
  214. half4 blinn2(half3 N,
  215. half3 L,
  216. half3 V,
  217. uniform half4 specularColor,
  218. uniform half shininess
  219. )
  220. {
  221. half3 H = normalize(V+L);
  222. half4 lighting = lit(dot(L,N), dot(H,N), shininess);
  223. return lighting.y + specularColor*lighting.z;
  224. }
  225.  
  226. /**************************************/
  227. /***** VERTEX SHADER ******************/
  228. /**************************************/
  229.  
  230. v2f v(a2v In, uniform half4 lightPosition)
  231. {
  232. v2f Out;
  233. Out.worldNormal = mul(In.normal, worldIT).xyz;
  234. Out.worldTangent = mul(In.tangent, worldIT).xyz;
  235. Out.worldBinormal = mul(In.binormal, worldIT).xyz;
  236. //can use either positive or negative y format normal maps
  237. //comment out this if statement to save 6 instructions!
  238. if (direction == true) Out.worldTangent = -Out.worldTangent;
  239. half3 worldSpacePos = mul(In.position, world);
  240. Out.lightVec = lightPosition - worldSpacePos;
  241. Out.texCoord.xy = In.texCoord;
  242. Out.eyeVec = viewInv[3].xyz - worldSpacePos;
  243. Out.position = mul(In.position, wvp);
  244. return Out;
  245. }
  246.  
  247. /**************************************/
  248. /***** FRAGMENT PROGRAM ***************/
  249. /**************************************/
  250.  
  251. float4 f(v2f In,uniform half4 lightColor) : COLOR
  252. {
  253. //fetch the diffuse and normal and spec maps
  254. half4 colorMap = tex2D(colorTextureSampler, In.texCoord.xy);
  255. half4 specMap = tex2D(specTextureSampler, In.texCoord.xy);
  256. half3 normal = tex2D(normalMapSampler, In.texCoord).xyz * 2.0 - 1.0;
  257.  
  258. //create tangent space vectors
  259. half3 Nn = In.worldNormal;
  260. half3 Tn = In.worldTangent;
  261. half3 Bn = In.worldBinormal;
  262.  
  263. //these vectors could be normalized, but it costs 4 more instructions
  264. //and makes almost no difference to image quality
  265. //half3 Nn = normalize(In.worldNormal);
  266. //half3 Tn = normalize(In.worldTangent);
  267. //half3 Bn = normalize(In.worldBinormal);
  268.  
  269. //offset world space normal with normal map values
  270. half3 N = (Nn * normal.z) + (normal.x * Bn + normal.y * -Tn);
  271. N = normalize(N);
  272.  
  273. //create lighting vectors - view vector and light vector
  274. half3 V = normalize(In.eyeVec);
  275. half3 L = normalize(In.lightVec.xyz);
  276.  
  277. //lighting
  278.  
  279. //lighting
  280.  
  281. //ambient light
  282. half4 C = ambient*colorMap * 0.33;
  283.  
  284. //specular color
  285. half4 specCol = specularColor*specMap;
  286.  
  287. //diffuse and specular
  288. C += lightColor * blinn2(N, L, V, specCol, shininess);
  289. C *= colorMap*surfColor;
  290.  
  291. return C;
  292. }
  293.  
  294. float4 f2(v2f In,uniform half4 lightColor) : COLOR
  295. {
  296. //fetch the diffuse, specular and normal maps
  297. half4 colorMap = tex2D(colorTextureSampler, In.texCoord.xy);
  298. half4 specMap = tex2D(specTextureSampler, In.texCoord.xy);
  299. //swizzle the compressed dxt5 format
  300. half3 normal = tex2D(CnormalMapSampler, In.texCoord).wyz * 2.0 - 1.0;
  301. //generate the z component of the vector
  302. normal.z = sqrt(1 - normal.x * normal.x - normal.y * normal.y);
  303.  
  304. //create tangent space vectors
  305. half3 Nn = In.worldNormal;
  306. half3 Tn = In.worldTangent;
  307. half3 Bn = In.worldBinormal;
  308.  
  309. //these vectors could be normalized, but it costs 4 more instructions
  310. //and makes almost no difference to image quality
  311. //half3 Nn = normalize(In.worldNormal);
  312. //half3 Tn = normalize(In.worldTangent);
  313. //half3 Bn = normalize(In.worldBinormal);
  314.  
  315. //offset world space normal with normal map values
  316. half3 N = (Nn * normal.z) + (normal.x * Bn + normal.y * -Tn);
  317. N = normalize(N);
  318.  
  319. //create lighting vectors - view vector and light vector
  320. half3 V = normalize(In.eyeVec);
  321. half3 L = normalize(In.lightVec.xyz);
  322.  
  323. //lighting
  324.  
  325. //ambient light
  326. half4 C = ambient*colorMap * 0.33;
  327.  
  328. //specular color
  329. half4 specCol = specularColor*specMap;
  330.  
  331. //diffuse and specular
  332. C += lightColor * blinn2(N, L, V, specCol, shininess);
  333. C *= colorMap*surfColor;
  334.  
  335. return C;
  336. }
  337.  
  338.  
  339. /****************************************************/
  340. /********** TECHNIQUES ******************************/
  341. /****************************************************/
  342.  
  343. technique Complete
  344. {
  345. pass light1
  346. {
  347. VertexShader = compile vs_1_1 v(light1Pos);
  348. ZEnable = true;
  349. ZWriteEnable = true;
  350. CullMode = cw;
  351. AlphaBlendEnable = false;
  352. PixelShader = compile ps_2_0 f(light1Color);
  353. }
  354.  
  355. pass light2
  356. {
  357. VertexShader = compile vs_1_1 v(light2Pos);
  358. ZEnable = true;
  359. ZWriteEnable = false;
  360. ZFunc = LessEqual;
  361. CullMode = None;
  362. AlphaBlendEnable = true;
  363. SrcBlend = One;
  364. DestBlend = One;
  365. PixelShader = compile ps_2_0 f(light2Color);
  366. }
  367.  
  368. pass light3
  369. {
  370. VertexShader = compile vs_1_1 v(light3Pos);
  371. ZEnable = true;
  372. ZWriteEnable = false;
  373. ZFunc = LessEqual;
  374. CullMode = None;
  375. AlphaBlendEnable = true;
  376. SrcBlend = One;
  377. DestBlend = One;
  378. PixelShader = compile ps_2_0 f(light3Color);
  379. }
  380. }
  381.  
  382. technique Compressed
  383. {
  384. pass light1
  385. {
  386. VertexShader = compile vs_1_1 v(light1Pos);
  387. ZEnable = true;
  388. ZWriteEnable = true;
  389. CullMode = cw;
  390. AlphaBlendEnable = false;
  391. PixelShader = compile ps_2_0 f2(light1Color);
  392. }
  393.  
  394. pass light2
  395. {
  396. VertexShader = compile vs_1_1 v(light2Pos);
  397. ZEnable = true;
  398. ZWriteEnable = false;
  399. ZFunc = LessEqual;
  400. CullMode = None;
  401. AlphaBlendEnable = true;
  402. SrcBlend = One;
  403. DestBlend = One;
  404. PixelShader = compile ps_2_0 f2(light2Color);
  405. }
  406.  
  407. pass light3
  408. {
  409. VertexShader = compile vs_1_1 v(light3Pos);
  410. ZEnable = true;
  411. ZWriteEnable = false;
  412. ZFunc = LessEqual;
  413. CullMode = None;
  414. AlphaBlendEnable = true;
  415. SrcBlend = One;
  416. DestBlend = One;
  417. PixelShader = compile ps_2_0 f2(light3Color);
  418. }
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement