Fur.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /******************************************************************************/
  2. #include "!Header.h"
  3. #include "Fur.h"
  4. #define FACTOR (-0.7f) // prevents complete darkness at the bottom layers, gives ambient=0.3, it will match the 'size' version
  5. /******************************************************************************/
  6. inline VecH GetBoneFurVel(VecI bone, VecH weight) {return weight.x*FurVel[bone.x] + weight.y*FurVel[bone.y] + weight.z*FurVel[bone.z];}
  7. /******************************************************************************/
  8. #define BASE_PARAMS \
  9. uniform Bool skin ,\
  10. uniform Bool size ,\
  11. uniform Bool diffuse
  12. #define SOFT_PARAMS \
  13. uniform Bool skin ,\
  14. uniform Bool size ,\
  15. uniform Bool diffuse
  16. /******************************************************************************/
  17. void Base_VS
  18. (
  19. VtxInput vtx,
  20. out Vec2 outTex:TEXCOORD0,
  21. out VecH outNrm:TEXCOORD1, // !! not Normalized !!
  22. out Vec outPos:TEXCOORD2,
  23. out Vec outVel:TEXCOORD3,
  24. out Half outLen:TEXCOORD4,
  25. out Vec4 outVtx:POSITION ,
  26. IF_IS_CLIP
  27. BASE_PARAMS
  28. )
  29. {
  30. outTex=vtx.tex();
  31. if(!skin)
  32. {
  33. #if MODEL>=SM_4 || MODEL==SM_GL
  34. if(true) // instance
  35. {
  36. outVel=ObjVel[vtx.instance()]; // #PER_INSTANCE_VEL
  37. outPos=TransformPos(vtx.pos(), vtx.instance());
  38. outNrm=TransformDir(vtx.nrm(), vtx.instance());
  39. UpdateVelocities_VS(outVel, vtx.pos(), outPos, vtx.instance());
  40. }else
  41. #endif
  42. {
  43. outVel=ObjVel[0];
  44. outPos=TransformPos(vtx.pos());
  45. outNrm=TransformDir(vtx.nrm());
  46. UpdateVelocities_VS(outVel, vtx.pos(), outPos);
  47. }
  48. }else
  49. {
  50. VecI bone=vtx.bone();
  51. outVel=GetBoneVel ( bone, vtx.weight());
  52. outPos=TransformPos(vtx.pos(), bone, vtx.weight());
  53. outNrm=TransformDir(vtx.nrm(), bone, vtx.weight());
  54. UpdateVelocities_VS(outVel, vtx.pos(), outPos);
  55. }
  56. if(size)outLen=vtx.size();
  57. CLIP(outPos); outVtx=Project(outPos);
  58. }
  59. /******************************************************************************/
  60. void Base_PS
  61. (
  62. Vec2 inTex:TEXCOORD0,
  63. VecH inNrm:TEXCOORD1,
  64. Vec inPos:TEXCOORD2,
  65. Vec inVel:TEXCOORD3,
  66. Half inLen:TEXCOORD4,
  67. out DeferredSolidOutput output,
  68. BASE_PARAMS
  69. )
  70. {
  71. Half fur=Tex(FurCol, inTex*MaterialDetScale()).r;
  72. VecH col=Sat(size ? inLen*-fur+1 : fur*FACTOR+1); // inLen*-fur+step+1 : fur*FACTOR+step+1, here step=0
  73. if(diffuse)col*=Tex(Col, inTex).rgb;
  74. col=col*MaterialColor3()+Highlight.rgb;
  75. inNrm=Normalize(inNrm);
  76. //UpdateColorBySss(col, inNrm, MaterialSss());
  77. output.color (col);
  78. output.glow (0);
  79. output.normal (inNrm);
  80. output.specular(MaterialSpecular());
  81. output.velocity(inVel, inPos);
  82. }
  83. /******************************************************************************/
  84. void Soft_VS
  85. (
  86. VtxInput vtx,
  87. out Vec2 outTex :TEXCOORD0,
  88. out Vec4 outPos4:TEXCOORD1,
  89. out Half outLen :TEXCOORD2,
  90. out Vec4 outVtx :POSITION ,
  91. SOFT_PARAMS
  92. )
  93. {
  94. Vec pos=vtx.pos();
  95. VecH nrm=vtx.nrm();
  96. outTex=vtx.tex();
  97. if(!skin)
  98. {
  99. pos=TransformPos(pos); nrm+=FurVel[0]; nrm=Normalize(nrm);
  100. nrm=TransformDir(nrm);
  101. }else
  102. {
  103. VecI bone=vtx.bone();
  104. pos =TransformPos (pos, bone, vtx.weight());
  105. nrm+=GetBoneFurVel( bone, vtx.weight()); nrm=Normalize(nrm);
  106. nrm =TransformDir (nrm, bone, vtx.weight());
  107. }
  108. outPos4=Project(pos); // set in 'outPos4' the original position without expansion
  109. if(size)outLen=vtx.size();
  110. pos+=nrm*(size ? vtx.size()*MaterialDetPower()*FurStep.x : MaterialDetPower()*FurStep.x);
  111. outVtx=Project(pos);
  112. }
  113. /******************************************************************************/
  114. Vec4 Soft_PS
  115. (
  116. Vec2 inTex :TEXCOORD0,
  117. Vec4 inPos4:TEXCOORD1,
  118. Half inLen :TEXCOORD2,
  119. SOFT_PARAMS
  120. ):COLOR
  121. {
  122. Half fur=Tex(FurCol, inTex*MaterialDetScale()).r;
  123. VecH4 color;
  124. color.rgb=Sat(size ? inLen*-fur+FurStep.y : fur*FACTOR+FurStep.y); // inLen*-fur+step+1 : fur*FACTOR+step+1
  125. color.a =Sat(size ? inLen*(1-FurStep.x/fur) : 1-FurStep.x/fur ); // alternative: Sat(1-FurStep.x/(fur*inLen))
  126. if(diffuse)color.rgb*=Tex(Col, inTex).rgb;
  127. color.rgb =(color.rgb*MaterialColor3()+Highlight.rgb)*TexPoint(FurLight, PosToScreen(inPos4)).rgb; // we need to access the un-expanded pixel and not current pixel
  128. return color;
  129. }
  130. /******************************************************************************/
  131. // TECHNIQUES
  132. /******************************************************************************/
  133. CUSTOM_TECHNIQUE
  134. /******************************************************************************/