Advertisement
noradninja

VertexLightMapCommon.cginc

Dec 4th, 2024 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. #include "UnityCG.cginc"
  2. #include "HLSLSupport.cginc"
  3. #include "UnityShaderVariables.cginc"
  4. #include "UnityShaderUtilities.cginc"
  5. #include "AutoLight.cginc"
  6. #include "Lighting.cginc"
  7. #include "UnityShadowLibrary.cginc"
  8.  
  9. #include "UnityStandardConfig.cginc"
  10. #include "UnityPBSLighting.cginc"
  11. #include "UnityStandardUtils.cginc"
  12. #include "UnityGBuffer.cginc"
  13. #include "UnityStandardBRDF.cginc"
  14.  
  15. #define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
  16. // ES2.0 can not do loops with non-constant-expression iteration counts :(
  17. #if defined(SHADER_API_GLES)
  18. #define LIGHT_LOOP_LIMIT 8.0h
  19. #else
  20. #define LIGHT_LOOP_LIMIT unity_VertexLightParams.x
  21. #endif
  22.  
  23. // Compute attenuation & illumination from one light
  24. half3 computeOneLight(int idx, half3 eyePosition, half3 eyeNormal) {
  25.  
  26.     half3 dirToLight = unity_LightPosition[idx].xyz;
  27.     half att = 1.0h;
  28.  
  29.     #if defined(POINT) || defined(SPOT)
  30.         dirToLight -= eyePosition * unity_LightPosition[idx].w;
  31.        
  32.         // distance attenuation
  33.         half distSqr = dot(dirToLight, dirToLight);
  34.         att /= (1.0h + unity_LightAtten[idx].z * distSqr);
  35.  
  36.         if (unity_LightPosition[idx].w != 0.0h &&
  37.             distSqr > unity_LightAtten[idx].w)
  38.             att = 0.0h; // set to 0 if outside of range
  39.  
  40.         dirToLight *= rsqrt(distSqr);
  41.  
  42.     #if defined(SPOT)
  43.         // spot angle attenuation
  44.         half rho = max(dot(dirToLight, unity_SpotDirection[idx].xyz), 0.0h);
  45.         half spotAtt = (rho - unity_LightAtten[idx].x) * unity_LightAtten[idx].y;
  46.         att *= saturate(spotAtt);
  47.     #endif
  48.  
  49.     #endif
  50.         att *= 0.5h; // passed v light colors are 2x brighter than what used to be v FFP
  51.  
  52.     const half NdotL = max(dot(eyeNormal, dirToLight), 0.0h);
  53.        
  54.         // diffuse
  55.     const half3 color = att * NdotL * unity_LightColor[idx].rgb;
  56.  
  57.         return min(color, 1.0);
  58. }
  59.  
  60. // uniforms
  61. int4 unity_VertexLightParams; // x: light count, y: zero, z: one (y/z needed by d3d9 vs loop instruction)
  62. sampler2D _MainTex;
  63. half4 _MainTex_ST;
  64.  
  65. // pos shader input data
  66. struct appdata {
  67.     half3 pos : POSITION;
  68.     half3 normal : NORMAL;
  69.     half4 color : COLOR0;
  70.     half3 uv0 : TEXCOORD0;
  71.     half3 uv1 : TEXCOORD1;
  72. };
  73.  
  74. // pos-to-fragment interpolators
  75. struct v2f {
  76.     half4 pos : SV_POSITION;
  77.     half2 uv0 : TEXCOORD0;
  78.     half2 uv1 : TEXCOORD1;
  79.     half4 screenPosition : TEXCOORD2;
  80.     half4 color : TEXCOORD3;
  81.     #if USING_FOG
  82.             UNITY_FOG_COORDS(4)
  83.     #endif
  84.  
  85.  
  86. };
  87.  
  88. // pos shader
  89. v2f vert(appdata v) {
  90.  
  91.     v2f o;
  92.     UNITY_INITIALIZE_OUTPUT(v2f, o);
  93.  
  94.     half3 worldPos = mul (unity_ObjectToWorld, half4(v.pos, 1.0h) ).xyz;
  95.     const half3 eyePos = mul(UNITY_MATRIX_MV, half4(v.pos, 1.0h) ).xyz;
  96.     const half3 eyeNormal = normalize(mul( (half3x3)UNITY_MATRIX_IT_MV, v.normal).xyz);
  97.     const half dotProduct = 1 - saturate ( dot(v.normal, eyeNormal) );
  98.    
  99.     // pos lighting
  100.     half4 color = half4(0, 0, 0, 1);
  101.  
  102.     #if defined(AMBIENT_ON) || !defined(CUSTOM_LIGHTMAPPED)
  103.         color.rgb = glstate_lightmodel_ambient.rgb;
  104.     #endif
  105.  
  106.     for (int il = 0; il < LIGHT_LOOP_LIMIT; ++il) {
  107.         color.rgb += computeOneLight(il, eyePos, eyeNormal);
  108.     }
  109.     color.rgb += smoothstep(0.0h, 1.0h, dotProduct) * 0.15h;
  110.     saturate(color);
  111.        
  112.     // compute texture coordinates
  113.     o.uv0 = v.uv0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
  114.  
  115.     #if defined(CUSTOM_LIGHTMAPPED)
  116.         o.uv1 = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  117.     #endif
  118.     o.pos = UnityObjectToClipPos(v.pos);
  119.     o.screenPosition = ComputeScreenPos(o.pos);
  120.    
  121.     // Decode baked HDR vertex color (RGBM)
  122.     o.color =  half4 (v.color.rgb, 1);
  123.     o.color *= 0.5h;
  124.     UNITY_TRANSFER_FOG(o,o.pos);
  125.     return o;
  126.  
  127. }
  128.  
  129. // fragment shader
  130. fixed4 frag(v2f i) : SV_Target {
  131.     const half4 posLighting = i.color;
  132.     UNITY_EXTRACT_FOG(i);
  133.     #if defined(CUSTOM_LIGHTMAPPED)
  134.     const half4 lightmap = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy);
  135.  
  136.     #if CUSTOM_LIGHTMAPPED == 1
  137.     const half4 lighting = (lightmap * 0.25h) + posLighting;
  138.     #endif
  139.  
  140.     #else
  141.     const half4 lighting = posLighting;
  142.     #endif
  143.  
  144.     const half4 diffuse = tex2D(_MainTex, i.uv0.xy);
  145.     half4 col = (diffuse * lighting);
  146.     // Apply vertex lightmap
  147.     col.rgb *= i.color;
  148.     return col;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement