Spine-Skeleton-Lit-Common.cginc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef SKELETON_LIT_COMMON_INCLUDED
  2. #define SKELETON_LIT_COMMON_INCLUDED
  3. #include "UnityCG.cginc"
  4. #include "CGIncludes/Spine-Common.cginc"
  5. // ES2.0/WebGL/3DS can not do loops with non-constant-expression iteration counts :(
  6. #if defined(SHADER_API_GLES)
  7. #define LIGHT_LOOP_LIMIT 8
  8. #elif defined(SHADER_API_N3DS)
  9. #define LIGHT_LOOP_LIMIT 4
  10. #else
  11. #define LIGHT_LOOP_LIMIT unity_VertexLightParams.x
  12. #endif
  13. ////////////////////////////////////////
  14. // Alpha Clipping
  15. //
  16. #if defined(_ALPHA_CLIP)
  17. uniform fixed _Cutoff;
  18. #define ALPHA_CLIP(pixel, color) clip((pixel.a * color.a) - _Cutoff);
  19. #else
  20. #define ALPHA_CLIP(pixel, color)
  21. #endif
  22. half3 computeLighting (int idx, half3 dirToLight, half3 eyeNormal, half4 diffuseColor, half atten) {
  23. half NdotL = max(dot(eyeNormal, dirToLight), 0.0);
  24. // diffuse
  25. half3 color = NdotL * diffuseColor.rgb * unity_LightColor[idx].rgb;
  26. return color * atten;
  27. }
  28. half3 computeOneLight (int idx, float3 eyePosition, half3 eyeNormal, half4 diffuseColor) {
  29. float3 dirToLight = unity_LightPosition[idx].xyz;
  30. half att = 1.0;
  31. #if defined(POINT) || defined(SPOT)
  32. dirToLight -= eyePosition * unity_LightPosition[idx].w;
  33. // distance attenuation
  34. float distSqr = dot(dirToLight, dirToLight);
  35. att /= (1.0 + unity_LightAtten[idx].z * distSqr);
  36. if (unity_LightPosition[idx].w != 0 && distSqr > unity_LightAtten[idx].w) att = 0.0; // set to 0 if outside of range
  37. distSqr = max(distSqr, 0.000001); // don't produce NaNs if some vertex position overlaps with the light
  38. dirToLight *= rsqrt(distSqr);
  39. #if defined(SPOT)
  40. // spot angle attenuation
  41. half rho = max(dot(dirToLight, unity_SpotDirection[idx].xyz), 0.0);
  42. half spotAtt = (rho - unity_LightAtten[idx].x) * unity_LightAtten[idx].y;
  43. att *= saturate(spotAtt);
  44. #endif
  45. #endif
  46. att *= 0.5; // passed in light colors are 2x brighter than what used to be in FFP
  47. return min (computeLighting (idx, dirToLight, eyeNormal, diffuseColor, att), 1.0);
  48. }
  49. int4 unity_VertexLightParams; // x: light count, y: zero, z: one (y/z needed by d3d9 vs loop instruction)
  50. struct appdata {
  51. float3 pos : POSITION;
  52. float3 normal : NORMAL;
  53. half4 color : COLOR;
  54. float2 uv0 : TEXCOORD0;
  55. #if defined(_TINT_BLACK_ON)
  56. float2 tintBlackRG : TEXCOORD1;
  57. float2 tintBlackB : TEXCOORD2;
  58. #endif
  59. UNITY_VERTEX_INPUT_INSTANCE_ID
  60. };
  61. struct VertexOutput {
  62. fixed4 color : COLOR0;
  63. float2 uv0 : TEXCOORD0;
  64. float4 pos : SV_POSITION;
  65. UNITY_VERTEX_OUTPUT_STEREO
  66. };
  67. VertexOutput vert (appdata v) {
  68. VertexOutput o;
  69. UNITY_SETUP_INSTANCE_ID(v);
  70. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  71. half4 color = PMAGammaToTargetSpace(v.color);
  72. float3 eyePos = UnityObjectToViewPos(float4(v.pos, 1)).xyz; //mul(UNITY_MATRIX_MV, float4(v.pos,1)).xyz;
  73. half3 fixedNormal = half3(0,0,-1);
  74. half3 eyeNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, fixedNormal));
  75. o.uv0 = v.uv0;
  76. o.pos = UnityObjectToClipPos(v.pos);
  77. #ifdef _DOUBLE_SIDED_LIGHTING
  78. // unfortunately we have to compute the sign here in the vertex shader
  79. // instead of using VFACE in fragment shader stage.
  80. half faceSign = sign(eyeNormal.z);
  81. eyeNormal *= faceSign;
  82. #endif
  83. half3 shadowedColor;
  84. #if !defined(_LIGHT_AFFECTS_ADDITIVE)
  85. if (color.a == 0) {
  86. o.color = color;
  87. return o;
  88. }
  89. #endif // !defined(_LIGHT_AFFECTS_ADDITIVE)
  90. // Lights
  91. half3 lcolor = half4(0,0,0,1).rgb + color.rgb * glstate_lightmodel_ambient.rgb;
  92. for (int il = 0; il < LIGHT_LOOP_LIMIT; ++il) {
  93. lcolor += computeOneLight(il, eyePos, eyeNormal, color);
  94. }
  95. color.rgb = lcolor.rgb;
  96. o.color = saturate(color);
  97. return o;
  98. }
  99. sampler2D _MainTex;
  100. fixed4 frag (VertexOutput i) : SV_Target {
  101. fixed4 tex = tex2D(_MainTex, i.uv0);
  102. ALPHA_CLIP(tex, i.color);
  103. #if defined(_STRAIGHT_ALPHA_INPUT)
  104. tex.rgb *= tex.a;
  105. #endif
  106. fixed4 col = tex * i.color;
  107. col.rgb *= 2;
  108. return col;
  109. }
  110. #endif