Spine-SkeletonLit-ForwardPass-URP.hlsl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef SKELETONLIT_FORWARD_PASS_URP_INCLUDED
  2. #define SKELETONLIT_FORWARD_PASS_URP_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  6. struct appdata {
  7. float3 pos : POSITION;
  8. float3 normal : NORMAL;
  9. half4 color : COLOR;
  10. float2 uv0 : TEXCOORD0;
  11. UNITY_VERTEX_INPUT_INSTANCE_ID
  12. };
  13. struct VertexOutput {
  14. half4 color : COLOR0;
  15. float2 uv0 : TEXCOORD0;
  16. float4 pos : SV_POSITION;
  17. UNITY_VERTEX_OUTPUT_STEREO
  18. };
  19. half3 LightweightLightVertexSimplified(float3 positionWS, half3 normalWS) {
  20. Light mainLight = GetMainLight();
  21. half3 attenuatedLightColor = mainLight.color * (mainLight.distanceAttenuation * mainLight.shadowAttenuation);
  22. half3 diffuseLightColor = LightingLambert(attenuatedLightColor, mainLight.direction, normalWS);
  23. // Note: we don't add any lighting in the fragment shader, thus we include both variants below
  24. #if defined(_ADDITIONAL_LIGHTS) || defined(_ADDITIONAL_LIGHTS_VERTEX)
  25. for (int i = 0; i < GetAdditionalLightsCount(); ++i)
  26. {
  27. Light light = GetAdditionalLight(i, positionWS);
  28. half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
  29. diffuseLightColor += LightingLambert(attenuatedLightColor, light.direction, normalWS);
  30. }
  31. #endif
  32. return diffuseLightColor;
  33. }
  34. VertexOutput vert(appdata v) {
  35. VertexOutput o;
  36. UNITY_SETUP_INSTANCE_ID(v);
  37. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  38. half4 color = v.color;
  39. float3 positionWS = TransformObjectToWorld(v.pos);
  40. half3 fixedNormal = half3(0, 0, -1);
  41. half3 normalWS = normalize(mul((float3x3)unity_ObjectToWorld, fixedNormal));
  42. #ifdef _DOUBLE_SIDED_LIGHTING
  43. // unfortunately we have to compute the sign here in the vertex shader
  44. // instead of using VFACE in fragment shader stage.
  45. half3 viewDirWS = UNITY_MATRIX_V[2].xyz;
  46. half faceSign = sign(dot(viewDirWS, normalWS));
  47. normalWS *= faceSign;
  48. #endif
  49. color.rgb = LightweightLightVertexSimplified(positionWS, normalWS);
  50. // Note: ambient light is also handled via SH.
  51. half3 vertexSH;
  52. OUTPUT_SH(normalWS.xyz, vertexSH);
  53. color.rgb += SAMPLE_GI(input.lightmapUV, vertexSH, normalWS);
  54. o.color = color;
  55. o.uv0 = v.uv0;
  56. o.pos = TransformWorldToHClip(positionWS);
  57. return o;
  58. }
  59. half4 frag(VertexOutput i) : SV_Target{
  60. half4 tex = tex2D(_MainTex, i.uv0);
  61. half4 col;
  62. #if defined(_STRAIGHT_ALPHA_INPUT)
  63. col.rgb = tex.rgb * i.color.rgb * tex.a;
  64. #else
  65. col.rgb = tex.rgb * i.color.rgb;
  66. #endif
  67. col.a = tex.a * i.color.a;
  68. return col;
  69. }
  70. #endif