Spine-Skeleton-Lit-Common.cginc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. UNITY_VERTEX_INPUT_INSTANCE_ID
  56. };
  57. struct VertexOutput {
  58. fixed4 color : COLOR0;
  59. float2 uv0 : TEXCOORD0;
  60. float4 pos : SV_POSITION;
  61. UNITY_VERTEX_OUTPUT_STEREO
  62. };
  63. VertexOutput vert (appdata v) {
  64. VertexOutput o;
  65. UNITY_SETUP_INSTANCE_ID(v);
  66. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  67. half4 color = PMAGammaToTargetSpace(v.color);
  68. float3 eyePos = UnityObjectToViewPos(float4(v.pos, 1)).xyz; //mul(UNITY_MATRIX_MV, float4(v.pos,1)).xyz;
  69. half3 fixedNormal = half3(0,0,-1);
  70. half3 eyeNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, fixedNormal));
  71. o.uv0 = v.uv0;
  72. o.pos = UnityObjectToClipPos(v.pos);
  73. #ifdef _DOUBLE_SIDED_LIGHTING
  74. // unfortunately we have to compute the sign here in the vertex shader
  75. // instead of using VFACE in fragment shader stage.
  76. half faceSign = sign(eyeNormal.z);
  77. eyeNormal *= faceSign;
  78. #endif
  79. half3 shadowedColor;
  80. #if !defined(_LIGHT_AFFECTS_ADDITIVE)
  81. if (color.a == 0) {
  82. o.color = color;
  83. return o;
  84. }
  85. #endif // !defined(_LIGHT_AFFECTS_ADDITIVE)
  86. // Lights
  87. half3 lcolor = half4(0,0,0,1).rgb + color.rgb * glstate_lightmodel_ambient.rgb;
  88. for (int il = 0; il < LIGHT_LOOP_LIMIT; ++il) {
  89. lcolor += computeOneLight(il, eyePos, eyeNormal, color);
  90. }
  91. color.rgb = lcolor.rgb;
  92. o.color = saturate(color);
  93. return o;
  94. }
  95. sampler2D _MainTex;
  96. fixed4 frag (VertexOutput i) : SV_Target {
  97. fixed4 tex = tex2D(_MainTex, i.uv0);
  98. ALPHA_CLIP(tex, i.color);
  99. #if defined(_STRAIGHT_ALPHA_INPUT)
  100. tex.rgb *= tex.a;
  101. #endif
  102. fixed4 col = tex * i.color;
  103. col.rgb *= 2;
  104. return col;
  105. }
  106. #endif