summaryrefslogtreecommitdiffstats
path: root/Shaders/displacement/pbr.cginc
blob: 690f5471ec65e8e9f163796fb42694470670161f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef __PBR_INC
#define __PBR_INC

#include "UnityPBSLighting.cginc"

// xdd
UNITY_DECLARE_TEXCUBE(_Cubemap);

struct v2f
{
  float4 clipPos : SV_POSITION;
  float2 uv : TEXCOORD0;
  float3 normal : TEXCOORD1;
  float3 worldPos : TEXCOORD2;

  float3 objPos : TEXCOORD3;

  #if defined(VERTEXLIGHT_ON)
  float3 vertexLightColor : TEXCOORD3;
  #endif
};

UnityLight GetLight(v2f i, float3 worldPos, float3 normal)
{
  UNITY_LIGHT_ATTENUATION(attenuation, 0, worldPos);
  float3 light_color = _LightColor0.rgb * attenuation;

  UnityLight light;
  light.color = light_color;
  #if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT)
  light.dir = normalize(_WorldSpaceLightPos0.xyz - worldPos);
  #else
  light.dir = _WorldSpaceLightPos0.xyz;
  #endif
  light.ndotl = DotClamped(normal, light.dir);

  return light;
}

UnityIndirect GetIndirect(v2f i, float3 view_dir, float3 normal,
    float smoothness, bool custom_cubemap) {
  UnityIndirect indirect;
  indirect.diffuse = 0;
  indirect.specular = 0;

  #if defined(VERTEXLIGHT_ON)
  indirect.diffuse = i.vertexLightColor;
  #endif

  #if defined(FORWARD_BASE_PASS)
  indirect.diffuse += max(0, ShadeSH9(float4(normal, 1)));
  float3 reflect_dir = reflect(-view_dir, normal);
  // There's a nonlinear relationship between mipmap level and roughness.
  float roughness = 1 - smoothness;
  roughness *= 1.7 - .7 * roughness;
  if (custom_cubemap) {
    float3 env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
        _Cubemap,
        reflect_dir,
        roughness * UNITY_SPECCUBE_LOD_STEPS);
    indirect.specular = env_sample;
  } else {
    float3 env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
        unity_SpecCube0,
        reflect_dir,
        roughness * UNITY_SPECCUBE_LOD_STEPS);
    indirect.specular = env_sample;
  }
  #endif

  return indirect;
}

float4 getLitColor(v2f i, float4 albedo, float3 worldPos, float3 normal,
    float metallic, float smoothness,
    bool custom_cubemap)
{
  float3 specular_tint;
  float one_minus_reflectivity;
  albedo.rgb = DiffuseAndSpecularFromMetallic(
    albedo, metallic, specular_tint, one_minus_reflectivity);

  float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos);

  float3 pbr = UNITY_BRDF_PBS(albedo, specular_tint,
      one_minus_reflectivity, smoothness,
      view_dir, normal,
      GetLight(i, worldPos, normal),
      GetIndirect(i, view_dir, normal, smoothness, custom_cubemap)).rgb;

  return float4(saturate(pbr), albedo.a);
}

#endif  // __PBR_INC