Simple.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /******************************************************************************/
  2. #include "!Header.h"
  3. #include "Simple.h"
  4. #include "Ambient Occlusion.h"
  5. #include "Vertex Fog.h"
  6. /******************************************************************************/
  7. #define PARAMS \
  8. uniform Bool skin ,\
  9. uniform Int materials ,\
  10. uniform Int textures ,\
  11. uniform Int bump_mode ,\
  12. uniform Bool alpha_test,\
  13. uniform Bool light_map ,\
  14. uniform Bool rflct ,\
  15. uniform Bool color ,\
  16. uniform Bool mtrl_blend,\
  17. uniform Bool heightmap ,\
  18. uniform Int fx ,\
  19. uniform Bool per_pixel ,\
  20. uniform Bool tesselate
  21. /******************************************************************************/
  22. struct VS_PS
  23. {
  24. VecH nrm :TEXCOORD0; // !! may not be Normalized !!
  25. Vec2 tex :TEXCOORD1;
  26. Vec2 tex_l :TEXCOORD2;
  27. Vec pos :TEXCOORD3;
  28. Half fade_out:TEXCOORD4;
  29. VecH col_add :TEXCOORD5;
  30. Vec rfl :TEXCOORD6;
  31. VecH col :COLOR0 ;
  32. VecH4 material:COLOR1 ;
  33. };
  34. /******************************************************************************/
  35. void VS
  36. (
  37. VtxInput vtx,
  38. out VS_PS O,
  39. out Vec4 O_vtx:POSITION,
  40. IF_IS_CLIP
  41. PARAMS
  42. )
  43. {
  44. Vec pos=vtx.pos();
  45. VecH nrm; if(bump_mode>=SBUMP_FLAT)nrm=vtx.nrm();
  46. if(textures )O.tex =vtx.tex (heightmap);
  47. if(light_map )O.tex_l =vtx.tex1 ();
  48. if(materials>1)O.material=vtx.material();
  49. if(materials<=1)O.col.rgb=MaterialColor3();/*else
  50. if(!mtrl_blend )
  51. {
  52. O.col.rgb =O.material.x*MultiMaterial0Color3()
  53. +O.material.y*MultiMaterial1Color3();
  54. if(materials>=3)O.col.rgb+=O.material.z*MultiMaterial2Color3();
  55. if(materials>=4)O.col.rgb+=O.material.w*MultiMaterial3Color3();
  56. }*/
  57. if(color)
  58. {
  59. if(materials<=1/* || !mtrl_blend*/)O.col.rgb*=vtx.color3();
  60. else O.col.rgb =vtx.color3();
  61. }
  62. if(heightmap && textures && materials==1)O.tex*=MaterialTexScale();
  63. if(fx==FX_LEAF)
  64. {
  65. if(bump_mode==SBUMP_FLAT)BendLeaf(vtx.hlp(), pos, nrm);
  66. else BendLeaf(vtx.hlp(), pos);
  67. }
  68. if(fx==FX_LEAFS)
  69. {
  70. if(bump_mode==SBUMP_FLAT)BendLeafs(vtx.hlp(), vtx.size(), pos, nrm);
  71. else BendLeafs(vtx.hlp(), vtx.size(), pos);
  72. }
  73. if(!skin)
  74. {
  75. #if MODEL>=SM_4 || MODEL==SM_GL
  76. if(true) // instance
  77. {
  78. O.pos=TransformPos(pos, vtx.instance());
  79. if(bump_mode==SBUMP_FLAT)O.nrm=TransformDir(nrm, vtx.instance());
  80. if(fx==FX_GRASS)
  81. {
  82. BendGrass(pos, O.pos, vtx.instance());
  83. O.fade_out=GrassFadeOut(vtx.instance());
  84. }
  85. }else
  86. #endif
  87. {
  88. O.pos=TransformPos(pos);
  89. if(bump_mode==SBUMP_FLAT)O.nrm=TransformDir(nrm);
  90. if(fx==FX_GRASS)
  91. {
  92. BendGrass(pos, O.pos);
  93. O.fade_out=GrassFadeOut();
  94. }
  95. }
  96. }else
  97. {
  98. VecI bone=vtx.bone();
  99. O.pos=TransformPos(pos, bone, vtx.weight());
  100. if(bump_mode==SBUMP_FLAT)O.nrm=TransformDir(nrm, bone, vtx.weight());
  101. }
  102. // normalize
  103. if(bump_mode==SBUMP_FLAT && (tesselate || rflct || !per_pixel))O.nrm=Normalize(O.nrm);
  104. if(fx==FX_BONE)
  105. {
  106. Flt b; if(!skin)b=0;else
  107. {
  108. VecI bone=vtx._bone; // use real bone regardless if skinning is enabled
  109. b=vtx.weight().x*Sat(1-Abs(bone.x-BoneHighlight))
  110. +vtx.weight().y*Sat(1-Abs(bone.y-BoneHighlight))
  111. +vtx.weight().z*Sat(1-Abs(bone.z-BoneHighlight));
  112. }
  113. O.col=((b>EPS) ? Vec(b, 0, 1-b) : Vec(1, 1, 1));
  114. }
  115. if(rflct)O.rfl=Transform3(reflect(Normalize(O.pos), O.nrm), CamMatrix);
  116. if(!per_pixel)
  117. {
  118. if(!(materials<=1 /*|| !mtrl_blend*/ || color || fx==FX_BONE))O.col=1;
  119. // lighting
  120. if(bump_mode>=SBUMP_FLAT)
  121. {
  122. Half d =Sat(Dot(O.nrm, Light_dir.dir));
  123. VecH lum=Light_dir.color.rgb*d + AmbColor;
  124. if(materials<=1 && fx!=FX_BONE)lum+=MaterialAmbient();
  125. O.col.rgb*=lum;
  126. }
  127. // fog
  128. Half fog_rev=Sat(Length2(O.pos)*VertexFogMulAdd.x+VertexFogMulAdd.y); // fog_rev=1-fog
  129. O.col.rgb*= fog_rev ; // *=1-fog
  130. O.col_add =Lerp(VertexFogColor.rgb, Highlight.rgb, fog_rev); // 1-fog
  131. }
  132. O_vtx=Project(O.pos); CLIP(O.pos);
  133. }
  134. /******************************************************************************/
  135. Vec4 PS
  136. (
  137. VS_PS I,
  138. IF_IS_FRONT
  139. PARAMS
  140. ):COLOR
  141. {
  142. Half glow;
  143. if(fx==FX_BONE)
  144. {
  145. glow=0;
  146. }else
  147. {
  148. if(materials<=1)
  149. {
  150. if(textures==0)
  151. {
  152. glow=MaterialGlow();
  153. }else
  154. {
  155. VecH4 tex_nrm; // #MaterialTextureChannelOrder
  156. if(textures==1)
  157. {
  158. VecH4 tex_col=Tex(Col, I.tex); if(alpha_test)AlphaTest(tex_col.w, I.fade_out, fx);
  159. I.col.rgb*=tex_col.rgb;
  160. glow=MaterialGlow();
  161. }else
  162. if(textures==2)
  163. {
  164. /*if(per_pixel || alpha_test || rflct || is_glow)*/
  165. {
  166. tex_nrm=Tex(Nrm, I.tex); if( alpha_test)AlphaTest(tex_nrm.a, I.fade_out, fx);
  167. glow =MaterialGlow() ; if(!alpha_test)glow*=tex_nrm.a;
  168. }//else glow=MaterialGlow(); always read 2nd texture to set glow, but in the future add a separate shader path that doesn't do this if material has no glow
  169. I.col.rgb*=Tex(Col, I.tex).rgb;
  170. }
  171. if(light_map)I.col.rgb*=Tex(Lum, I.tex_l).rgb;
  172. // reflection
  173. if(rflct)I.col.rgb+=TexCube(Rfl, I.rfl).rgb*((textures==2) ? MaterialReflect()*tex_nrm.z : MaterialReflect());
  174. }
  175. }else // materials>1
  176. {
  177. glow=0;
  178. VecH tex;
  179. if(mtrl_blend)
  180. {
  181. VecH4 col0, col1, col2, col3;
  182. col0=Tex(Col , I.tex*MultiMaterial0TexScale()); col0.rgb*=MultiMaterial0Color3(); I.material.x=MultiMaterialWeight(I.material.x, col0.a);
  183. col1=Tex(Col1, I.tex*MultiMaterial1TexScale()); col1.rgb*=MultiMaterial1Color3(); I.material.y=MultiMaterialWeight(I.material.y, col1.a); if(materials==2)I.material.xy /=I.material.x+I.material.y;
  184. if(materials>=3){col2=Tex(Col2, I.tex*MultiMaterial2TexScale()); col2.rgb*=MultiMaterial2Color3(); I.material.z=MultiMaterialWeight(I.material.z, col2.a); if(materials==3)I.material.xyz /=I.material.x+I.material.y+I.material.z;}
  185. if(materials>=4){col3=Tex(Col3, I.tex*MultiMaterial3TexScale()); col3.rgb*=MultiMaterial3Color3(); I.material.w=MultiMaterialWeight(I.material.w, col3.a); if(materials==4)I.material.xyzw/=I.material.x+I.material.y+I.material.z+I.material.w;}
  186. tex =I.material.x*col0.rgb
  187. +I.material.y*col1.rgb;
  188. if(materials>=3)tex+=I.material.z*col2.rgb;
  189. if(materials>=4)tex+=I.material.w*col3.rgb;
  190. }else
  191. {
  192. tex =I.material.x*Tex(Col , I.tex*MultiMaterial0TexScale()).rgb*MultiMaterial0Color3()
  193. +I.material.y*Tex(Col1, I.tex*MultiMaterial1TexScale()).rgb*MultiMaterial1Color3();
  194. if(materials>=3)tex+=I.material.z*Tex(Col2, I.tex*MultiMaterial2TexScale()).rgb*MultiMaterial2Color3();
  195. if(materials>=4)tex+=I.material.w*Tex(Col3, I.tex*MultiMaterial3TexScale()).rgb*MultiMaterial3Color3();
  196. }
  197. if(materials<=1 /*|| !mtrl_blend*/ || color || fx==FX_BONE || !per_pixel)I.col.rgb*=tex;
  198. else I.col.rgb =tex;
  199. // reflection
  200. if(rflct)
  201. {
  202. I.col.rgb+=TexCube(Rfl , I.rfl).rgb*(MultiMaterial0Reflect()*I.material.x*((textures==2) ? Tex(Nrm , I.tex*MultiMaterial0TexScale()).z : 1));
  203. I.col.rgb+=TexCube(Rfl1, I.rfl).rgb*(MultiMaterial1Reflect()*I.material.y*((textures==2) ? Tex(Nrm1, I.tex*MultiMaterial1TexScale()).z : 1));
  204. if(materials>=3)I.col.rgb+=TexCube(Rfl2, I.rfl).rgb*(MultiMaterial2Reflect()*I.material.z*((textures==2) ? Tex(Nrm2, I.tex*MultiMaterial2TexScale()).z : 1));
  205. if(materials>=4)I.col.rgb+=TexCube(Rfl3, I.rfl).rgb*(MultiMaterial3Reflect()*I.material.w*((textures==2) ? Tex(Nrm3, I.tex*MultiMaterial3TexScale()).z : 1));
  206. }
  207. }
  208. }
  209. // perform lighting
  210. if(per_pixel)
  211. {
  212. I.col.rgb+=Highlight.rgb;
  213. if(bump_mode==SBUMP_FLAT)
  214. {
  215. VecH nrm=Normalize(I.nrm); if(fx!=FX_GRASS && fx!=FX_LEAF && fx!=FX_LEAFS)BackFlip(nrm, front);
  216. Half d =Sat(Dot(nrm, Light_dir.dir));
  217. VecH lum=Light_dir.color.rgb*d + AmbColor;
  218. if(materials<=1 && fx!=FX_BONE)lum+=MaterialAmbient();
  219. I.col.rgb*=lum;
  220. }
  221. }else
  222. {
  223. I.col.rgb+=I.col_add;
  224. }
  225. return Vec4(I.col.rgb, glow);
  226. }
  227. /******************************************************************************/
  228. // HULL / DOMAIN
  229. /******************************************************************************/
  230. #if MODEL>=SM_4
  231. HSData HSConstant(InputPatch<VS_PS,3> I) {return GetHSData(I[0].pos, I[1].pos, I[2].pos, I[0].nrm, I[1].nrm, I[2].nrm);}
  232. [maxtessfactor(5.0)]
  233. [domain("tri")]
  234. [partitioning("fractional_odd")] // use 'odd' because it supports range from 1.0 ('even' supports range from 2.0)
  235. [outputtopology("triangle_cw")]
  236. [patchconstantfunc("HSConstant")]
  237. [outputcontrolpoints(3)]
  238. VS_PS HS
  239. (
  240. InputPatch<VS_PS,3> I, UInt cp_id:SV_OutputControlPointID,
  241. PARAMS
  242. )
  243. {
  244. VS_PS O;
  245. O.pos =I[cp_id].pos;
  246. O.nrm =I[cp_id].nrm;
  247. if(materials<=1 /*|| !mtrl_blend*/ || color || fx==FX_BONE || !per_pixel)O.col =I[cp_id].col;
  248. if(textures )O.tex =I[cp_id].tex;
  249. if(light_map )O.tex_l =I[cp_id].tex_l;
  250. if(rflct )O.rfl =I[cp_id].rfl;
  251. if(materials>1 )O.material=I[cp_id].material;
  252. if(fx==FX_GRASS )O.fade_out=I[cp_id].fade_out;
  253. if(!per_pixel )O.col_add =I[cp_id].col_add;
  254. return O;
  255. }
  256. /******************************************************************************/
  257. [domain("tri")]
  258. void DS
  259. (
  260. HSData hs_data, const OutputPatch<VS_PS,3> I, Vec B:SV_DomainLocation,
  261. out VS_PS O,
  262. out Vec4 O_vtx:POSITION,
  263. PARAMS
  264. )
  265. {
  266. if(materials<=1 /*|| !mtrl_blend*/ || color || fx==FX_BONE || !per_pixel)O.col =I[0].col *B.z + I[1].col *B.x + I[2].col *B.y;
  267. if(textures )O.tex =I[0].tex *B.z + I[1].tex *B.x + I[2].tex *B.y;
  268. if(light_map )O.tex_l =I[0].tex_l *B.z + I[1].tex_l *B.x + I[2].tex_l *B.y;
  269. if(rflct )O.rfl =I[0].rfl *B.z + I[1].rfl *B.x + I[2].rfl *B.y;
  270. if(materials>1 )O.material=I[0].material*B.z + I[1].material*B.x + I[2].material*B.y;
  271. if(fx==FX_GRASS )O.fade_out=I[0].fade_out*B.z + I[1].fade_out*B.x + I[2].fade_out*B.y;
  272. if(!per_pixel )O.col_add =I[0].col_add *B.z + I[1].col_add *B.x + I[2].col_add *B.y;
  273. SetDSPosNrm(O.pos, O.nrm, I[0].pos, I[1].pos, I[2].pos, I[0].nrm, I[1].nrm, I[2].nrm, B, hs_data, false, 0);
  274. O_vtx=Project(O.pos);
  275. }
  276. #endif
  277. /******************************************************************************/
  278. // TECHNIQUES
  279. /******************************************************************************/
  280. CUSTOM_TECHNIQUE
  281. /******************************************************************************/
  282. #if 0
  283. @GROUP "Main"
  284. @SHARED
  285. #include "Glsl.h"
  286. #include "Glsl Material.h"
  287. #include "Glsl MultiMaterial.h"
  288. #include "Glsl Light.h"
  289. #define MULTI_TEXCOORD 1
  290. #if materials<=1 /*|| mtrl_blend==0*/ || COLOR!=0 || per_pixel==0
  291. VAR LP Vec IO_col;
  292. #endif
  293. #if per_pixel==0
  294. VAR LP Vec IO_col_add;
  295. #else
  296. VAR MP Vec IO_nrm;
  297. #endif
  298. #if fx==FX_GRASS
  299. VAR MP Flt IO_fade_out;
  300. #endif
  301. #if materials>1
  302. VAR LP Vec4 IO_material;
  303. #endif
  304. #if textures>=1
  305. VAR HP Vec2 IO_tex;
  306. #if MULTI_TEXCOORD!=0
  307. #if materials>=2
  308. VAR HP Vec2 IO_tex1;
  309. #endif
  310. #if materials>=3
  311. VAR HP Vec2 IO_tex2;
  312. #endif
  313. #if materials>=4
  314. VAR HP Vec2 IO_tex3;
  315. #endif
  316. #endif
  317. #endif
  318. #if light_map==1
  319. VAR HP Vec2 IO_tex_l;
  320. #endif
  321. #if rflct!=0
  322. VAR MP Vec IO_rfl;
  323. #endif
  324. @SHARED_END
  325. @VS
  326. #include "Glsl VS.h"
  327. #include "Glsl VS 3D.h"
  328. void main()
  329. {
  330. HP Vec pos=vtx_pos();
  331. MP Vec nrm=vtx_nrm();
  332. HP Vec O_pos;
  333. #if textures>=1
  334. IO_tex=((heightmap!=0) ? vtx_texHM() : vtx_tex());
  335. if(heightmap!=0 && textures>=1)
  336. {
  337. #if MULTI_TEXCOORD!=0
  338. #if materials>=2
  339. IO_tex1=IO_tex*MultiMaterial1TexScale();
  340. #endif
  341. #if materials>=3
  342. IO_tex2=IO_tex*MultiMaterial2TexScale();
  343. #endif
  344. #if materials>=4
  345. IO_tex3=IO_tex*MultiMaterial3TexScale();
  346. #endif
  347. if(materials>=2)IO_tex*=MultiMaterial0TexScale();else
  348. if(materials==1)IO_tex*= MaterialTexScale ();
  349. #else
  350. if(materials==1)IO_tex*=MaterialTexScale();
  351. #endif
  352. }
  353. #endif
  354. #if light_map==1
  355. IO_tex_l=vtx_tex1();
  356. #endif
  357. #if materials<=1
  358. IO_col.rgb=MaterialColor3();
  359. #else
  360. IO_material=vtx_material();
  361. /*#if mtrl_blend==0
  362. IO_col.rgb =IO_material.x*MultiMaterial0Color3()
  363. +IO_material.y*MultiMaterial1Color3();
  364. if(materials>=3)IO_col.rgb+=IO_material.z*MultiMaterial2Color3();
  365. if(materials>=4)IO_col.rgb+=IO_material.w*MultiMaterial3Color3();
  366. #endif*/
  367. #endif
  368. #if COLOR!=0
  369. if(materials<=1/* || mtrl_blend==0*/)IO_col.rgb*=vtx_color3();
  370. else IO_col.rgb =vtx_color3();
  371. #endif
  372. #if fx==FX_GRASS
  373. IO_fade_out=GrassFadeOut();
  374. #endif
  375. if(fx==FX_LEAF )pos=BendLeaf (vtx_hlp(), pos);
  376. if(fx==FX_LEAFS)pos=BendLeafs(vtx_hlp(), vtx_size(), pos);
  377. #if skin==0
  378. {
  379. O_pos= TransformPos(pos, gl_InstanceID) ; if(fx==FX_GRASS)O_pos+=BendGrass(pos);
  380. if(bump_mode==SBUMP_FLAT)nrm=Normalize(TransformDir(nrm, gl_InstanceID));
  381. }
  382. #else
  383. {
  384. MP VecI bone =vtx_bone ();
  385. MP Vec weight=vtx_weight();
  386. O_pos= TransformPos(pos, bone, weight) ;
  387. if(bump_mode==SBUMP_FLAT)nrm=Normalize(TransformDir(nrm, bone, weight));
  388. }
  389. #endif
  390. O_vtx=Project(O_pos);
  391. gl_ClipDistance[0]=Dot(Vec4(O_pos, 1), ClipPlane);
  392. #if rflct!=0
  393. IO_rfl=Normalize(O_pos); // convert to MP
  394. IO_rfl=Transform3(reflect(IO_rfl, nrm), CamMatrix);
  395. #endif
  396. #if per_pixel==0
  397. {
  398. if(!(materials<=1 /*|| mtrl_blend==0*/ || COLOR!=0))IO_col.rgb=Vec(1.0, 1.0, 1.0);
  399. #if bump_mode>=SBUMP_FLAT
  400. {
  401. LP Flt d =Max(Dot(nrm, Light_dir.dir), 0.0);
  402. LP Vec lum=Light_dir.color.rgb*d + AmbColor;
  403. if(materials<=1 && fx!=FX_BONE)lum+=MaterialAmbient();
  404. IO_col.rgb*=lum;
  405. }
  406. #endif
  407. LP Flt fog_rev=Sat(Length2(O_pos)*VertexFogMulAdd.x+VertexFogMulAdd.y);
  408. IO_col.rgb*= fog_rev ;
  409. IO_col_add =Lerp(VertexFogColor.rgb, Highlight.rgb, fog_rev);
  410. }
  411. #else
  412. IO_nrm=nrm;
  413. #endif
  414. }
  415. @VS_END
  416. @PS
  417. #include "Glsl PS.h"
  418. void main()
  419. {
  420. LP Vec col;
  421. #if materials<=1 /*|| mtrl_blend==0*/ || COLOR!=0 || per_pixel==0
  422. col=IO_col.rgb;
  423. #endif
  424. MP Flt glow;
  425. #if materials<=1
  426. #if textures==0
  427. glow=MaterialGlow();
  428. #else
  429. #if textures==1
  430. LP Vec4 tex_col=Tex(Col, IO_tex);
  431. #if alpha_test!=0
  432. LP Flt alpha=tex_col.a;
  433. #if fx==FX_GRASS
  434. alpha-=IO_fade_out;
  435. #endif
  436. AlphaTest(alpha);
  437. #endif
  438. glow=MaterialGlow();
  439. #elif textures==2
  440. LP Vec4 tex_nrm=Tex(Nrm, IO_tex); // #MaterialTextureChannelOrder
  441. #if alpha_test!=0
  442. LP Flt alpha=tex_nrm.a;
  443. #if fx==FX_GRASS
  444. alpha-=IO_fade_out;
  445. #endif
  446. AlphaTest(alpha);
  447. #endif
  448. glow=MaterialGlow();
  449. #if alpha_test==0
  450. glow*=tex_nrm.a;
  451. #endif
  452. #endif
  453. #if textures==1
  454. col*=tex_col.rgb;
  455. #elif textures==2
  456. col*=Tex(Col, IO_tex).rgb;
  457. #endif
  458. #if light_map==1
  459. col*=Tex(Lum, IO_tex_l).rgb;
  460. #endif
  461. #if rflct!=0
  462. #if textures==2
  463. col.rgb+=TexCube(Rfl, IO_rfl).rgb*(MaterialReflect()*tex_nrm.z);
  464. #else
  465. col.rgb+=TexCube(Rfl, IO_rfl).rgb*MaterialReflect();
  466. #endif
  467. #endif
  468. #endif
  469. #else // materials>1
  470. glow=0.0;
  471. LP Vec tex;
  472. #if mtrl_blend!=0
  473. LP Vec4 col0, col1, col2, col3;
  474. #if MULTI_TEXCOORD!=0
  475. col0=Tex(Col , IO_tex );
  476. col1=Tex(Col1, IO_tex1);
  477. #if materials>=3
  478. col2=Tex(Col2, IO_tex2);
  479. #endif
  480. #if materials>=4
  481. col3=Tex(Col3, IO_tex3);
  482. #endif
  483. #else
  484. col0=Tex(Col , IO_tex*MultiMaterial0TexScale());
  485. col1=Tex(Col1, IO_tex*MultiMaterial1TexScale());
  486. if(materials>=3)col2=Tex(Col2, IO_tex*MultiMaterial2TexScale());
  487. if(materials>=4)col3=Tex(Col3, IO_tex*MultiMaterial3TexScale());
  488. #endif
  489. col0.rgb*=MultiMaterial0Color3();
  490. col1.rgb*=MultiMaterial1Color3();
  491. if(materials>=3)col2.rgb*=MultiMaterial2Color3();
  492. if(materials>=4)col3.rgb*=MultiMaterial3Color3();
  493. MP Vec4 material;
  494. material.x=MultiMaterialWeight(IO_material.x, col0.a);
  495. material.y=MultiMaterialWeight(IO_material.y, col1.a); if(materials==2)material.xy /=material.x+material.y;
  496. if(materials>=3){material.z=MultiMaterialWeight(IO_material.z, col2.a); if(materials==3)material.xyz /=material.x+material.y+material.z;}
  497. if(materials>=4){material.w=MultiMaterialWeight(IO_material.w, col3.a); if(materials==4)material.xyzw/=material.x+material.y+material.z+material.w;}
  498. tex =material.x*col0.rgb
  499. +material.y*col1.rgb;
  500. if(materials>=3)tex+=material.z*col2.rgb;
  501. if(materials>=4)tex+=material.w*col3.rgb;
  502. #else
  503. #if MULTI_TEXCOORD!=0
  504. tex =IO_material.x*Tex(Col , IO_tex ).rgb*MultiMaterial0Color3()
  505. +IO_material.y*Tex(Col1, IO_tex1).rgb*MultiMaterial1Color3();
  506. #if materials>=3
  507. tex+=IO_material.z*Tex(Col2, IO_tex2).rgb*MultiMaterial2Color3();
  508. #endif
  509. #if materials>=4
  510. tex+=IO_material.w*Tex(Col3, IO_tex3).rgb*MultiMaterial3Color3();
  511. #endif
  512. #else
  513. tex =IO_material.x*Tex(Col , IO_tex*MultiMaterial0TexScale()).rgb
  514. +IO_material.y*Tex(Col1, IO_tex*MultiMaterial1TexScale()).rgb;
  515. if(materials>=3)tex+=IO_material.z*Tex(Col2, IO_tex*MultiMaterial2TexScale()).rgb;
  516. if(materials>=4)tex+=IO_material.w*Tex(Col3, IO_tex*MultiMaterial3TexScale()).rgb;
  517. #endif
  518. #endif
  519. #if materials<=1 /*|| mtrl_blend==0*/ || COLOR!=0 || per_pixel==0
  520. col*=tex;
  521. #else
  522. col=tex;
  523. #endif
  524. #endif
  525. // lighting
  526. #if per_pixel!=0
  527. {
  528. col+=Highlight.rgb;
  529. #if bump_mode==SBUMP_FLAT
  530. {
  531. MP Vec nrm=Normalize(IO_nrm); if(fx!=FX_GRASS && fx!=FX_LEAF && fx!=FX_LEAFS)BackFlip(nrm);
  532. LP Flt d =Max(Dot(nrm, Light_dir.dir), 0.0);
  533. LP Vec lum=Light_dir.color.rgb*d + AmbColor;
  534. if(materials<=1 && fx!=FX_BONE)lum+=MaterialAmbient();
  535. col*=lum;
  536. }
  537. #endif
  538. }
  539. #else
  540. col+=IO_col_add;
  541. #endif
  542. gl_FragColor.rgb=col; // set 'gl_FragColor' at end since it's MP
  543. gl_FragColor.a =glow;
  544. }
  545. @PS_END
  546. @GROUP_END
  547. #endif
  548. /******************************************************************************/