Spine-Skeleton-Lit-Common.cginc 3.4 KB

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