Advertisement
noradninja

VertexLightmapCommon.cginc

Feb 26th, 2025 (edited)
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.92 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 "UnityPBSLighting.cginc"
  7. #include "UnityStandardUtils.cginc"
  8.  
  9. #define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
  10. // ES2.0 can not do loops with non-constant-expression iteration counts :(
  11. #if defined(SHADER_API_GLES)
  12.     #define LIGHT_LOOP_LIMIT 8.0h
  13. #else
  14.     #define LIGHT_LOOP_LIMIT unity_VertexLightParams.x
  15. #endif
  16.  
  17. // Compute attenuation & illumination from one light
  18. half3 computeOneLight(int idx, half3 eyePosition, half3 eyeNormal) {
  19.  
  20.     half3 dirToLight = unity_LightPosition[idx].xyz;
  21.     half att = 1.0h;
  22.  
  23.     #if defined(POINT) || defined(SPOT)
  24.         dirToLight -= eyePosition * unity_LightPosition[idx].w;
  25.        
  26.         // distance attenuation
  27.         half distSqr = dot(dirToLight, dirToLight);
  28.         att /= (1.0h + unity_LightAtten[idx].z * distSqr);
  29.  
  30.         if (unity_LightPosition[idx].w != 0.0h &&
  31.             distSqr > unity_LightAtten[idx].w)
  32.             att = 0.0h; // set to 0 if outside of range
  33.  
  34.         dirToLight *= rsqrt(distSqr);
  35.  
  36.         #if defined(SPOT)
  37.             // spot angle attenuation
  38.             half rho = max(dot(dirToLight, unity_SpotDirection[idx].xyz), 0.0h);
  39.             half spotAtt = (rho - unity_LightAtten[idx].x) * unity_LightAtten[idx].y;
  40.             att *= saturate(spotAtt);
  41.         #endif
  42.  
  43.     #endif
  44.         att *= 0.5h; // passed v light colors are 2x brighter than what used to be v FFP
  45.  
  46.     const half NdotL = max(dot(eyeNormal, dirToLight), 0.0h);
  47.        
  48.     // diffuse
  49.     const half3 color = att * NdotL * unity_LightColor[idx].rgb;
  50.  
  51.     return min(color, 1.0);
  52. }
  53.  
  54. // uniforms
  55. int4 unity_VertexLightParams; // x: light count, y: zero, z: one (y/z needed by d3d9 vs loop instruction)
  56. sampler2D _MainTex;
  57. sampler2D _MOAR;
  58. half4 _MainTex_ST;
  59. half _Cutoff;
  60. half4 _wind_dir;
  61. half _wind_size;
  62. half _leaves_wiggle_disp;
  63. half _leaves_wiggle_speed;
  64. half _influence;
  65. half _LeavesOn;
  66. half _AlphaOn;
  67.  
  68.  
  69. // pos shader input data
  70. struct appdata {
  71.     half3 pos : POSITION;
  72.     half3 normal : NORMAL;
  73.     half4 color : COLOR0;
  74.     half3 uv0 : TEXCOORD0;
  75.     half3 uv1 : TEXCOORD1;
  76. };
  77.  
  78. // pos-to-fragment interpolators
  79. struct v2f {
  80.     half4 pos : SV_POSITION;
  81.     half2 uv0 : TEXCOORD0;
  82.     half2 uv1 : TEXCOORD1;
  83.     half4 screenPosition : TEXCOORD2;
  84.     half4 color : COLOR;
  85.     half3 normal : NORMAL;
  86.     SHADOW_COORDS(3)
  87.    
  88.     #if USING_FOG
  89.         UNITY_FOG_COORDS(4)
  90.     #endif
  91. };
  92.  
  93. // pos shader
  94. v2f vert(appdata v) {
  95.  
  96.     v2f o;
  97.     UNITY_INITIALIZE_OUTPUT(v2f, o);
  98.  
  99.     half3 worldPos = mul(unity_ObjectToWorld, half4(v.pos, 1.0h)).xyz;
  100.     const half3 eyePos = mul(UNITY_MATRIX_MV, half4(v.pos, 1.0h)).xyz;
  101.     const half3 eyeNormal = normalize(mul((half3x3)UNITY_MATRIX_IT_MV, v.normal).xyz);
  102.     const half dotProduct = 1 - saturate(dot(v.normal, eyeNormal));
  103.     half3 vertNormal = v.normal;
  104.  
  105.     if(_LeavesOn)
  106.     {
  107.         // Leaf Movement and Wiggle
  108.         ( (v.pos.x += sin(_Time.y * v.pos.x * _leaves_wiggle_speed + (worldPos.x/_wind_size)) * _leaves_wiggle_disp * _wind_dir.x * (_influence * v.color)),
  109.           (v.pos.y += sin(_Time.y * v.pos.y * _leaves_wiggle_speed + (worldPos.y/_wind_size)) * _leaves_wiggle_disp * _wind_dir.y * (_influence * v.color)),
  110.           (v.pos.z += sin(_Time.y * v.pos.z * _leaves_wiggle_speed + (worldPos.z/_wind_size)) * _leaves_wiggle_disp * _wind_dir.z * (_influence * v.color)) );
  111.     }              
  112.     // pos lighting
  113.     half4 color = half4(0, 0, 0, 1);
  114.  
  115.     #if defined(AMBIENT_ON) || !defined(CUSTOM_LIGHTMAPPED)
  116.         color.rgb = glstate_lightmodel_ambient.rgb;
  117.     #endif
  118.  
  119.     for (int il = 0; il < LIGHT_LOOP_LIMIT; ++il) {
  120.         color.rgb += computeOneLight(il, eyePos, eyeNormal);
  121.     }
  122.     color.rgb += smoothstep(0.0h, 1.0h, dotProduct) * 0.15h;
  123.     o.color = saturate(color);
  124.        
  125.     // compute texture coordinates
  126.     o.uv0 = v.uv0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
  127.     #if defined(CUSTOM_LIGHTMAPPED)
  128.         o.uv1 = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  129.     #endif
  130.     o.pos = UnityObjectToClipPos(v.pos);
  131.     o.screenPosition = ComputeScreenPos(o.pos);
  132.     o.normal = v.normal;
  133.     TRANSFER_SHADOW(o);
  134.    
  135.     UNITY_TRANSFER_FOG(o, o.pos);
  136.  
  137.     return o;
  138. }
  139.  
  140. // fragment shader
  141. fixed4 frag(v2f i) : SV_Target {
  142.     const half4 posLighting = i.color;
  143.     UNITY_EXTRACT_FOG(i);
  144.     #if defined(CUSTOM_LIGHTMAPPED)
  145.         const half4 lightmap = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1.xy);
  146.         #if CUSTOM_LIGHTMAPPED == 1
  147.             const half4 lighting = half4(lightmap.rgb * 0.25h, 1) + posLighting;
  148.         #endif
  149.     #else
  150.         const half4 lighting = posLighting;
  151.     #endif
  152.    
  153.     const half4 diffuse = tex2D(_MainTex, i.uv0.xy);
  154.     const half4 moar = tex2D(_MOAR, i.uv0.xy); //r- metal g-occlusion b-alpha a-roughness
  155.     // Reflections
  156.     half3 indirectLightSpecular = 0;
  157.     half3 reflectionDir = reflect(-i.pos, i.normal); // both in world space
  158.     Unity_GlossyEnvironmentData envData;
  159.     envData.roughness = 1 - moar.a;
  160.     envData.reflUVW = BoxProjectedCubemapDirection(reflectionDir, i.pos, unity_SpecCube0_ProbePosition, unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax);
  161.     #ifdef _GLOSSYREFLECTIONS_OFF
  162.         indirectLightSpecular = unity_IndirectSpecColor.rgb;
  163.     #else
  164.         half3 probe0 = Unity_GlossyEnvironment(UNITY_PASS_TEXCUBE(unity_SpecCube0), unity_SpecCube0_HDR, envData);
  165.         envData.reflUVW = BoxProjectedCubemapDirection(reflectionDir, i.pos, unity_SpecCube1_ProbePosition, unity_SpecCube1_BoxMin, unity_SpecCube1_BoxMax);
  166.         #if UNITY_SPECCUBE_BLENDING
  167.             float interpolator = unity_SpecCube0_BoxMin.w;
  168.             if (interpolator < 0.99999)
  169.             {
  170.                 half3 probe1 = Unity_GlossyEnvironment(UNITY_PASS_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0), unity_SpecCube0_HDR, envData);
  171.                 indirectLightSpecular = lerp(probe1, probe0, interpolator);
  172.             }
  173.             else
  174.             {
  175.                 indirectLightSpecular = probe0;
  176.             }
  177.         #else
  178.             indirectLightSpecular = probe0;
  179.         #endif
  180.     #endif
  181.     //End reflections
  182.    
  183.     // Compute final color (lerping diffuse to metal by metalness)
  184.     half4 col = half4( lerp(diffuse.rgb, diffuse + indirectLightSpecular, 1 - moar.r) * lighting.rgb * moar.g, moar.b);
  185.    
  186.     // Sample shadow attenuation using Unity’s built-in method- why doesn't this work?
  187.     float shadow = SHADOW_ATTENUATION(i);
  188.     col.rgb *= shadow;
  189.    
  190.     if(!_AlphaOn)
  191.     {
  192.         fixed4 texcol = tex2D(_MainTex, i.uv0.xy);
  193.         clip(texcol.a - _Cutoff);
  194.     }
  195.     else
  196.     {
  197.         fixed4 texcol = tex2D(_MOAR, i.uv0.xy);
  198.         clip(texcol.b - _Cutoff);
  199.     }
  200.  
  201.     #if USING_FOG
  202.         UNITY_APPLY_FOG(i.fogCoord, col);
  203.     #endif
  204.  
  205.     return col;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement