Advertisement
Guest User

Untitled

a guest
Dec 29th, 2020
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.19 KB | None | 0 0
  1. float4x4 mProj;
  2. float4x4 mView;
  3. float4x4 mWorldMatrixArray;
  4.  
  5. int lightCount = 0;
  6. float4 lightAmbient[3] : LIGHTARRAYAMBIENT;
  7. float4 lightDiffuse[3] : LIGHTARRAYDIFFUSE;
  8. float4 lightDir[3] : LIGHTARRAYDIR;
  9. float4 CamPos = 0.0f;
  10.  
  11. float4 mtrAmbient : MATERIALAMBIENT;
  12. float4 mtrDiffuse : MATERIALDIFFUSE;
  13. float4 Ambient;
  14.  
  15. bool Skinning = 0;
  16. bool CanDiffuse = 0;
  17. bool CanFog = 0;
  18.  
  19. float4 Fog;
  20. float4 FogColor : FOGCOLOR;
  21.  
  22. float3x3 TexTrans;
  23. float3x3 TexTrans2;
  24.  
  25. texture t;
  26.  
  27. sampler samp =
  28. sampler_state
  29. {
  30. Texture = <t>;
  31. MinFilter = POINT;
  32. MagFilter = POINT;
  33. MipFilter = NONE;
  34. AddressU = CLAMP;
  35. AddressV = CLAMP;
  36. };
  37.  
  38.  
  39. sampler2D samp1 : register(s0);
  40. sampler2D samp2 : register(s1);
  41.  
  42. struct VS_INPUT
  43. {
  44. float3 vPosition : POSITION0;
  45. float3 BlendWeights : BLENDWEIGHT;
  46. float4 BlendIndices : BLENDINDICES;
  47. float3 vNormal : NORMAL0;
  48. float2 vUV : TEXCOORD0;
  49. };
  50.  
  51. struct VS_OUTPUT
  52. {
  53. float4 vPosition : POSITION0;
  54. float4 Diffuse : COLOR0;
  55. float2 vUV : TEXCOORD0;
  56. float2 vUV2 : TEXCOORD1;
  57. float3 vNormal : NORMAL0;
  58. float3 vCamDir : NORMAL1;
  59. float3 vLightDir : NORMAL2;
  60. float vFog : FOG;
  61. };
  62.  
  63. float4x4 getMatrix(int nIdx)
  64. {
  65. if (!Skinning)
  66. return mWorldMatrixArray;
  67.  
  68. float fIdx = ((float) nIdx / 256.0f);
  69.  
  70. float4x4 m = float4x4(
  71. tex2Dlod(samp, float4(float2(0.0, fIdx), 0.0f, 0.0f)),
  72. tex2Dlod(samp, float4(float2(1.0f / 3.0f, fIdx), 0.0f, 0.0f)),
  73. tex2Dlod(samp, float4(float2(2.0f / 3.0f, fIdx), 0.0f, 0.0f)),
  74. tex2Dlod(samp, float4(float2(1.0f, fIdx), 0.0f, 0.0f)));
  75.  
  76. return m;
  77. }
  78. float3 getMulSkinnedPos(float3 vPos, int nIdx, float fWeight,float4x4 mWord)
  79. {
  80. if (nIdx == 0)
  81. return (mul(float4(vPos, 1.0f), mWord) * fWeight).xyz;
  82.  
  83. return (mul(float4(vPos, 1.0f), getMatrix(nIdx)) * fWeight).xyz;
  84. }
  85. float4 getDiffuse(float3 pos,float3 normal)
  86. {
  87. float4 Diffuse = Ambient;
  88. for (int i = 0; i < lightCount; i++)
  89. {
  90. if (CanDiffuse)
  91. {
  92. if (lightDir[i].w < 0.0f)
  93. {
  94. // Point Light
  95. float fDist = distance(lightDir[i].xyz, pos);
  96. float lMul = fDist * lightAmbient[i].x;
  97.  
  98. if (lMul < 1.0f)
  99. {
  100. float3 fColor = lerp(0.f, lightDiffuse[i].xyz, lMul);
  101. float3 normlPos = normalize(lightDir[i].xyz);
  102. float fIntenzity = max(-dot(normal, normlPos), 0.0f);
  103. float3 mulVar = (fIntenzity * mtrDiffuse.xyz) * fColor;
  104.  
  105. Diffuse.xyz += mulVar;
  106. }
  107. }
  108. else
  109. {
  110. //Direction Light
  111.  
  112. float fIntenzity = saturate(-dot(normal, lightDir[i].xyz));
  113. float4 mulVar = fIntenzity * mtrDiffuse * lightDiffuse[i];
  114. Diffuse += max(mulVar, mtrAmbient * lightAmbient[i]);
  115.  
  116.  
  117. // float fIntenzity = saturate(-dot(normal, lightDir[i].xyz));
  118. // float4 mulVar = (fIntenzity * mtrDiffuse) * lightDiffuse[i];
  119. ////float4 madVar = mad(mtrAmbient, lightAmbient[i], mulVar);
  120.  
  121. // Diffuse += max(mulVar, mtrAmbient * lightAmbient[i]);
  122. }
  123. }
  124. else
  125. Diffuse += mtrAmbient * lightAmbient[i];
  126.  
  127. }
  128. return saturate(float4(Diffuse.xyz, mtrDiffuse.w));
  129. }
  130. VS_OUTPUT VSkin(VS_INPUT Input)
  131. {
  132. VS_OUTPUT Output;
  133.  
  134. float3 Position = float3(0.0f, 0.0f, 0.0f);
  135. float3 Normal = 0.0f;
  136.  
  137. float4x4 mWorld = getMatrix(0);
  138.  
  139. if (Skinning)
  140. {
  141. int4 aiIndices = D3DCOLORtoUBYTE4(Input.BlendIndices);
  142. float fWeight = 1.0f;
  143. for (int i = 0; i < 3; i++)
  144. {
  145. if(Input.BlendWeights[i] > 0.0f)
  146. {
  147. Position += getMulSkinnedPos(Input.vPosition, aiIndices[i], Input.BlendWeights[i],mWorld);
  148. fWeight -= Input.BlendWeights[i];
  149. }
  150. }
  151.  
  152. if(fWeight)
  153. {
  154. Position += getMulSkinnedPos(Input.vPosition, aiIndices[3], fWeight,mWorld);
  155. }
  156. }
  157. else
  158. {
  159. Position = Position = mul(float4(Input.vPosition, 1.0f), mWorld);
  160. }
  161.  
  162. Normal = mul(Input.vNormal, (float3x3) mWorld);
  163.  
  164. Output.vCamDir = Position - CamPos.xyz;
  165.  
  166. Output.vPosition = mul(float4(Position, 1.0f), mView);
  167. Output.vFog = CanFog ? Fog.w < 0.0f ? saturate((Fog.y - Output.vPosition.z) / (Fog.y - Fog.x)) : (length(Output.vPosition.xyz) * Fog.z) : 1;
  168.  
  169. Output.vPosition = mul(Output.vPosition, mProj);
  170.  
  171. Output.vNormal = normalize(Normal);
  172. Output.vLightDir = lightDir[0].xyz;
  173. Output.Diffuse = getDiffuse(Position,Output.vNormal);
  174. Output.vUV = mul(float3(Input.vUV, 1.0f), TexTrans).xy;
  175. Output.vUV2 = mul(float3(Input.vUV, 1.0f), TexTrans2).xy;
  176.  
  177. return Output;
  178. }
  179. /*
  180. struct VS_INPUT_NORMAL
  181. {
  182. float3 vPosition : POSITION0;
  183. float3 vNormal : NORMAL0;
  184. float2 vUV : TEXCOORD0;
  185. float2 vUV2 : TEXCOORD1;
  186. };
  187. VS_OUTPUT VNormal(VS_INPUT_NORMAL Input)
  188. {
  189. VS_OUTPUT Output;
  190.  
  191. float4x3 worldM = getMatrix(0);
  192. float3 Position = mul(float4(Input.vPosition, 1.0f), worldM);
  193. float3 Normal = mul(Input.vNormal, (float3x3)worldM);
  194.  
  195. Output.vCamDir = Position - CamPos.xyz;
  196.  
  197. float4 camPosCur = mul(float4(Position, 1.0f), mView);
  198.  
  199.  
  200. float fog_s = Fog.w < 0.0f ? saturate((Fog.y - camPosCur.z) / (Fog.y - Fog.x)) : (length(camPosCur.xyz) * Fog.z);
  201. Output.vFog = CanFog ? fog_s : 1;
  202.  
  203. Output.vPosition = mul(camPosCur, mProj);
  204.  
  205. Normal = normalize(Normal);
  206.  
  207. Output.vNormal = Normal;
  208. Output.vLightDir = lightDir[0].xyz;
  209. Output.Diffuse = getDiffuse(Position,Normal);
  210.  
  211. Output.vUV = mul(float3(Input.vUV, 1.0f), TexTrans).xy;
  212. Output.vUV2 = mul(float3(Input.vUV2, 1.0f), TexTrans2).xy;
  213.  
  214. return Output;
  215. }*/
  216. float4 TextureFactor;
  217. float4 PixelArg;
  218. uint nPsShader;
  219.  
  220. float4 comp(float Arg, float4 c , float4 t)
  221. {
  222. return Arg >= 0 ? c : t;
  223. }
  224.  
  225. float4 PixelShaderFunction(VS_OUTPUT Input): COLOR0
  226. {
  227. float4 colorTex1 = comp(PixelArg.x, tex2D(samp1, Input.vUV), TextureFactor);
  228. float4 colorTex2 = comp(PixelArg.y, tex2D(samp2, Input.vUV2), TextureFactor);
  229. float4 color = 0;
  230. switch (nPsShader)
  231. {
  232.  
  233. case 0:
  234. {
  235. color = colorTex1;
  236. }
  237. break;
  238. case 1:
  239. {
  240. color = (colorTex1 * colorTex2);
  241. }
  242. break;
  243. case 2:
  244. {
  245. color = (colorTex1 * 2.0f) * colorTex2;
  246. }
  247. break;
  248. case 3:
  249. {
  250. color = (colorTex1 * 4.0f) * colorTex2;
  251. }
  252. break;
  253. case 4:
  254. {
  255. color = (colorTex1 + colorTex2);
  256. }
  257. break;
  258. case 5:
  259. {
  260. color = colorTex1 + colorTex2 - 0.5f;
  261. }
  262. break;
  263. case 6:
  264. {
  265. color = float4((2 * (colorTex1 + colorTex2 - 0.5f)).xyz, colorTex1.w);
  266. }
  267. break;
  268. case 7:
  269. {
  270. color = (colorTex1 - colorTex2);
  271. }
  272. break;
  273. case 8:
  274. {
  275. color = (mad(-colorTex1, colorTex2, colorTex2) + colorTex2);
  276. }
  277. break;
  278. case 9:
  279. {
  280. float blend = 1 - Input.Diffuse.w;
  281. float4 colb = colorTex1 * Input.Diffuse.wwww;
  282. color = mad(colorTex2, blend, colb);
  283. }
  284. break;
  285. case 10:
  286. {
  287. float blend = 1 - colorTex2.w;
  288. float4 colb = colorTex1 * colorTex2.w;
  289. color = mad(colorTex2, blend, colb);
  290. }
  291. break;
  292. case 11:
  293. {
  294. float blend = 1 - TextureFactor.w;
  295. float4 colb = colorTex1 * TextureFactor.w;
  296. color = mad(colorTex2, blend, colb);
  297. }
  298. break;
  299. case 12:
  300. {
  301. float blend = 1 - colorTex2.w;
  302. color = mad(colorTex2, blend, colorTex1);
  303. }
  304. break;
  305. case 13:
  306. {
  307. float blend = 1 - colorTex1.w;
  308. float4 colb = colorTex1 * colorTex1.w;
  309. color = mad(colorTex2, blend, colb);
  310. }
  311. break;
  312. case 14:
  313. {
  314. float3 blend = mad(colorTex2, colorTex1.wwww, colorTex1);
  315. color = float4(blend, colorTex1.w);
  316. }
  317. break;
  318. case 15:
  319. {
  320. float4 blend = mad( colorTex1,colorTex2, colorTex1.wwww);
  321. color = float4(blend.xyz, colorTex1.w);
  322. }
  323. break;
  324. case 16:
  325. {
  326.  
  327. float blend = 1 - colorTex1.w;
  328. float3 col = mad(colorTex2, blend, colorTex1);
  329. color = float4(col, colorTex1.w);
  330. }
  331. break;
  332. case 17:
  333. {
  334.  
  335. float4 blend = 1 - colorTex1;
  336. float3 colr = mad(colorTex2, blend, colorTex1.wwww);
  337. color = float4(colr, colorTex1.w);
  338.  
  339. }
  340. break;
  341. case 18:
  342. {
  343. color = dot(colorTex1, colorTex2);
  344. }
  345. break;
  346. case 19:
  347. {
  348. color = mad(colorTex1, colorTex2, colorTex1);
  349. }
  350. break;
  351. case 20:
  352. {
  353. color = lerp(colorTex1, colorTex2, colorTex1);
  354. }
  355. break;
  356. case 21:
  357. {
  358. //Set alpha and specular color
  359.  
  360. float rcp = 1 / 257.0f;
  361.  
  362. float idk = mad(256.0f, colorTex2.y, colorTex2.z) * rcp;
  363. float alpha = mad(256.0f, colorTex2.w, colorTex2.x);
  364. colorTex1.w = alpha * rcp;
  365.  
  366. //Compute specular color
  367.  
  368. float3 View = normalize(Input.vCamDir) - lightDir[0].xyz;
  369. float3 Colr = normalize(View);
  370. float3 Normal = normalize(Input.vNormal);
  371.  
  372. float sColor = dot(Colr, Normal);
  373. sColor = max(sColor, 0.0f);
  374. sColor = pow(sColor, PixelArg.z);
  375.  
  376. color = mad(idk, sColor, colorTex1);
  377.  
  378. }
  379. break;
  380. }
  381.  
  382. float4 finalTex = float4(color.xyz * Input.Diffuse.xyz, min(color.w,Input.Diffuse.w));
  383.  
  384. if (!CanFog || Input.vFog == 1)
  385. return finalTex;
  386.  
  387.  
  388. return float4(lerp(FogColor.xyz, finalTex.xyz, Input.vFog), finalTex.w);
  389.  
  390. }
  391.  
  392. struct VS_INPUT_SFX
  393. {
  394. float3 vPosition : POSITION0;
  395. float3 BlendWeights : BLENDWEIGHT;
  396. float4 BlendIndices : BLENDINDICES;
  397. float4 vColor : COLOR0;
  398. float2 vUV : TEXCOORD0;
  399. };
  400. VS_OUTPUT VSfx(VS_INPUT_SFX Input)
  401. {
  402. VS_OUTPUT Output;
  403.  
  404. float4x4 worldM = getMatrix(0);
  405. float3 Position = 0;
  406.  
  407. int4 aiIndices = D3DCOLORtoUBYTE4(Input.BlendIndices);
  408. float fWeight = 1.0f;
  409. for (int i = 0; i < 3; i++)
  410. {
  411. if(Input.BlendWeights[i] > 0.0f)
  412. {
  413. Position += getMulSkinnedPos(Input.vPosition, aiIndices[i], Input.BlendWeights[i],worldM);
  414. fWeight -= Input.BlendWeights[i];
  415. }
  416. }
  417. if(fWeight > 0.0f)
  418. Position += getMulSkinnedPos(Input.vPosition, aiIndices[3], fWeight,worldM);
  419.  
  420. Output.vCamDir = 0;
  421.  
  422. float4 camPosCur = mul(float4(Position, 1.0f), mView);
  423.  
  424.  
  425. float fog_s = Fog.w < 0.0f ? saturate((Fog.y - camPosCur.z) / (Fog.y - Fog.x)) : (length(camPosCur.xyz) * Fog.z);
  426. Output.vFog = CanFog ? fog_s : 1;
  427.  
  428. Output.vPosition = mul(camPosCur, mProj);
  429. Output.vNormal = 0;
  430. Output.vLightDir = 0;
  431. Output.Diffuse = Input.vColor;
  432.  
  433. Output.vUV = mul(float3(Input.vUV, 1.0f), TexTrans).xy;
  434. Output.vUV2 = 0;
  435.  
  436. return Output;
  437. }
  438. struct VS_INPUT_MAP
  439. {
  440. float3 vPosition : POSITION0;
  441. float4 vColor : COLOR0;
  442. float2 vUV : TEXCOORD0;
  443. };
  444. VS_OUTPUT VMap(VS_INPUT_MAP Input)
  445. {
  446. VS_OUTPUT Output;
  447.  
  448. float4x3 worldM = getMatrix(0);
  449. float3 Position = mul(float4(Input.vPosition, 1.0f), worldM);
  450.  
  451. Output.vCamDir = 0;
  452.  
  453. float4 camPosCur = mul(float4(Position, 1.0f), mView);
  454.  
  455.  
  456. float fog_s = Fog.w < 0.0f ? saturate((Fog.y - camPosCur.z) / (Fog.y - Fog.x)) : (length(camPosCur.xyz) * Fog.z);
  457. Output.vFog = CanFog ? fog_s : 1;
  458.  
  459. Output.vPosition = mul(camPosCur, mProj);
  460. Output.vNormal = 0;
  461. Output.vLightDir = 0;
  462. Output.Diffuse = Input.vColor;
  463.  
  464. Output.vUV = mul(float3(Input.vUV, 1.0f), TexTrans).xy;
  465. Output.vUV2 = 0;
  466.  
  467. return Output;
  468. }
  469. technique TSkinning
  470. {
  471. pass p0
  472. {
  473. VertexShader = compile vs_3_0 VSkin();
  474. PixelShader = compile ps_3_0 PixelShaderFunction();
  475. }
  476. }
  477. //technique TNormal
  478. //{
  479. // pass p0
  480. // {
  481. // VertexShader = compile vs_3_0 VNormal();
  482. // PixelShader = compile ps_3_0 PixelShaderFunction();
  483. // }
  484. //}
  485. technique Sfx
  486. {
  487. pass p0
  488. {
  489. VertexShader = compile vs_3_0 VSfx();
  490. PixelShader = compile ps_3_0 PixelShaderFunction();
  491. }
  492. }
  493. technique Map
  494. {
  495. pass p0
  496. {
  497. VertexShader = compile vs_3_0 VMap();
  498. PixelShader = compile ps_3_0 PixelShaderFunction();
  499. }
  500. }
  501.  
  502.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement