yum-archive/SoggyShaders

Old Unity shaders.

git clone https://git.yummers.dev/yum-archive/SoggyShaders

yumAdd avatar cloning shader273a3ab

master
2.4 KiB94 linesraw
1#ifndef __PBR_INC
2#define __PBR_INC
3
4#include "UnityPBSLighting.cginc"
5
6// xdd
7UNITY_DECLARE_TEXCUBE(_Cubemap);
8
9struct v2f
10{
11  float4 clipPos : SV_POSITION;
12  float2 uv : TEXCOORD0;
13  float3 normal : TEXCOORD1;
14  float3 worldPos : TEXCOORD2;
15
16  float3 objPos : TEXCOORD3;
17
18  #if defined(VERTEXLIGHT_ON)
19  float3 vertexLightColor : TEXCOORD3;
20  #endif
21};
22
23UnityLight GetLight(v2f i, float3 worldPos, float3 normal)
24{
25  UNITY_LIGHT_ATTENUATION(attenuation, 0, worldPos);
26  float3 light_color = _LightColor0.rgb * attenuation;
27
28  UnityLight light;
29  light.color = light_color;
30  #if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT)
31  light.dir = normalize(_WorldSpaceLightPos0.xyz - worldPos);
32  #else
33  light.dir = _WorldSpaceLightPos0.xyz;
34  #endif
35  light.ndotl = DotClamped(normal, light.dir);
36
37  return light;
38}
39
40UnityIndirect GetIndirect(v2f i, float3 view_dir, float3 normal,
41    float smoothness, bool custom_cubemap) {
42  UnityIndirect indirect;
43  indirect.diffuse = 0;
44  indirect.specular = 0;
45
46  #if defined(VERTEXLIGHT_ON)
47  indirect.diffuse = i.vertexLightColor;
48  #endif
49
50  #if defined(FORWARD_BASE_PASS)
51  indirect.diffuse += max(0, ShadeSH9(float4(normal, 1)));
52  float3 reflect_dir = reflect(-view_dir, normal);
53  // There's a nonlinear relationship between mipmap level and roughness.
54  float roughness = 1 - smoothness;
55  roughness *= 1.7 - .7 * roughness;
56  if (custom_cubemap) {
57    float3 env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
58        _Cubemap,
59        reflect_dir,
60        roughness * UNITY_SPECCUBE_LOD_STEPS);
61    indirect.specular = env_sample;
62  } else {
63    float3 env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
64        unity_SpecCube0,
65        reflect_dir,
66        roughness * UNITY_SPECCUBE_LOD_STEPS);
67    indirect.specular = env_sample;
68  }
69  #endif
70
71  return indirect;
72}
73
74float4 getLitColor(v2f i, float4 albedo, float3 worldPos, float3 normal,
75    float metallic, float smoothness,
76    bool custom_cubemap)
77{
78  float3 specular_tint;
79  float one_minus_reflectivity;
80  albedo.rgb = DiffuseAndSpecularFromMetallic(
81    albedo, metallic, specular_tint, one_minus_reflectivity);
82
83  float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos);
84
85  float3 pbr = UNITY_BRDF_PBS(albedo, specular_tint,
86      one_minus_reflectivity, smoothness,
87      view_dir, normal,
88      GetLight(i, worldPos, normal),
89      GetIndirect(i, view_dir, normal, smoothness, custom_cubemap)).rgb;
90
91  return float4(saturate(pbr), albedo.a);
92}
93
94#endif  // __PBR_INC