yum-archive/SoggyShaders
Old Unity shaders.
git clone https://git.yummers.dev/yum-archive/SoggyShaders
8427cb4
master
1#ifndef __PBR_INC__ 2#define __PBR_INC__ 3 4#include "AutoLight.cginc" 5#include "eyes_data.cginc" 6#include "UnityPBSLighting.cginc" 7 8float _Enable_Custom_Cubemap; 9UNITY_DECLARE_TEXCUBE(_Custom_Cubemap); 10 11sampler2D _Matcap; 12float _Matcap_Str; 13float _Enable_Matcap; 14float _Matcap_Mode; 15 16UnityIndirect GetIndirect(v2f i, float3 view_dir, float smoothness) { 17 UnityIndirect indirect; 18 indirect.diffuse = 0; 19 indirect.specular = 0; 20 21 #if defined(VERTEXLIGHT_ON) 22 indirect.diffuse = i.vertexLightColor; 23 #endif 24 25 #if defined(FORWARD_BASE_PASS) 26 indirect.diffuse += max(0, ShadeSH9(float4(i.normal, 1))); 27 float3 reflect_dir = reflect(-view_dir, i.normal); 28 // There's a nonlinear relationship between mipmap level and roughness. 29 float roughness = 1 - smoothness; 30 roughness *= 1.7 - .7 * roughness; 31 float3 env_sample; 32 if (_Enable_Custom_Cubemap) { 33 env_sample = UNITY_SAMPLE_TEXCUBE_LOD( 34 _Custom_Cubemap, 35 reflect_dir, 36 roughness * UNITY_SPECCUBE_LOD_STEPS); 37 } else { 38 env_sample = UNITY_SAMPLE_TEXCUBE_LOD( 39 unity_SpecCube0, 40 reflect_dir, 41 roughness * UNITY_SPECCUBE_LOD_STEPS); 42 } 43 if (_Enable_Matcap) { 44 // identity: (a, b, c) and (c, c, -(a +b)) are perpendicular to each other 45 float3 ortho_1 = normalize(float3(view_dir.z, view_dir.z, -(view_dir.y + view_dir.x))); 46 float3 ortho_2 = cross(view_dir, ortho_1); 47 float2 matcap_uv = (float2(dot(i.normal, ortho_1), dot(i.normal, ortho_2)) + 1) * .43; 48 float iddx = ddx(i.uv.x); 49 float iddy = ddy(i.uv.y); 50 float3 matcap = tex2Dgrad(_Matcap, matcap_uv, iddx, iddy) * _Matcap_Str; 51 52 int mode = round(_Matcap_Mode); 53 switch (mode) { 54 case 1: 55 indirect.specular = clamp(env_sample + matcap, 0, 1); 56 break; 57 case 2: 58 indirect.specular = clamp(env_sample * matcap, 0, 1); 59 break; 60 case 3: 61 indirect.specular = clamp(matcap, 0, 1); 62 break; 63 case 4: 64 indirect.specular = clamp(env_sample - matcap, 0, 1); 65 break; 66 } 67 } else { 68 indirect.specular = env_sample; 69 } 70 #endif 71 72 return indirect; 73} 74 75UnityLight GetLight(v2f i) 76{ 77 UNITY_LIGHT_ATTENUATION(attenuation, 0, i.worldPos.xyz); 78 float3 light_color = _LightColor0.rgb * attenuation; 79 80 UnityLight light; 81 light.color = light_color; 82 #if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT) 83 light.dir = normalize(_WorldSpaceLightPos0.xyz - i.worldPos); 84 #else 85 light.dir = _WorldSpaceLightPos0.xyz; 86 #endif 87 light.ndotl = DotClamped(i.normal, light.dir); 88 89 return light; 90} 91 92void initNormal(inout v2f i) 93{ 94 i.normal = normalize(i.normal); 95} 96 97float4 light(inout v2f i, 98 float4 albedo, 99 float metallic, 100 float smoothness) 101{ 102 initNormal(i); 103 104 float3 specular_tint; 105 float one_minus_reflectivity; 106 albedo.rgb = DiffuseAndSpecularFromMetallic( 107 albedo, metallic, specular_tint, one_minus_reflectivity); 108 109 float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos); 110 float3 pbr = UNITY_BRDF_PBS(albedo, 111 specular_tint, 112 one_minus_reflectivity, 113 smoothness, 114 i.normal, 115 view_dir, 116 GetLight(i), 117 GetIndirect(i, view_dir, smoothness)).rgb; 118 119 return float4(saturate(pbr), albedo.a); 120} 121 122float getWorldSpaceDepth(in float4 world_pos) 123{ 124 float4 clip_pos = mul(UNITY_MATRIX_VP, world_pos); 125 return LinearEyeDepth(clip_pos.z / clip_pos.w); 126} 127 128sampler2D _CameraDepthTexture; 129 130// Return depth buffer at coordinate, on [0, 1]. 131float getDepthBufferAt(in float4 world_pos) 132{ 133 float4 clip_pos = mul(UNITY_MATRIX_VP, world_pos); 134 float4 uv = ComputeGrabScreenPos(clip_pos); 135 return LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv.xy / uv.w)); 136} 137 138#endif // __PBR_INC__ 139