summaryrefslogtreecommitdiffstats
path: root/MochieStandardBRDF.cginc
blob: 0351dd83088a60c75a44a96844717d562f89142a (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifndef MOCHIE_STANDARD_BRDF_INCLUDED
#define MOCHIE_STANDARD_BRDF_INCLUDED

/*
 * MIT License
 *
 * Copyright (c) 2020 MochiesCode
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE
 * SOFTWARE.
 */

#include "UnityCG.cginc"
#include "UnityStandardConfig.cginc"
#include "UnityLightingCommon.cginc"
#include "MochieStandardSSR.cginc"
#include "MochieStandardSSS.cginc"

float3 get_camera_pos() {
  float3 worldCam;
  worldCam.x = unity_CameraToWorld[0][3];
  worldCam.y = unity_CameraToWorld[1][3];
  worldCam.z = unity_CameraToWorld[2][3];
  return worldCam;
}

float GSAARoughness(float3 normal, float roughness){
  float3 normalDDX = ddx(normal);
  float3 normalDDY = ddy(normal); 
  float dotX = dot(normalDDX, normalDDX);
  float dotY = dot(normalDDY, normalDDY);
  float base = saturate(max(dotX, dotY));
  return max(roughness, pow(base, 0.333)*_GSAAStrength);
}

float3 Desaturate(float3 col){
	return dot(col, float3(0.3, 0.59, 0.11));
}

float3 GetContrast(float3 col, float contrast){
    return lerp(float3(0.5,0.5,0.5), col, contrast);
}

float oetf_sRGB_scalar(float L) {
	float V = 1.055 * (pow(L, 1.0 / 2.4)) - 0.055;
	if (L <= 0.0031308)
		V = L * 12.92;
	return V;
}

float3 oetf_sRGB(float3 L) {
	return float3(oetf_sRGB_scalar(L.r), oetf_sRGB_scalar(L.g), oetf_sRGB_scalar(L.b));
}

float eotf_sRGB_scalar(float V) {
	float L = pow((V + 0.055) / 1.055, 2.4);
	if (V <= oetf_sRGB_scalar(0.0031308))
		L = V / 12.92;
	return L;
}

float3 GetHDR(float3 rgb) {
	return float3(eotf_sRGB_scalar(rgb.r), eotf_sRGB_scalar(rgb.g), eotf_sRGB_scalar(rgb.b));
}

half4 BRDF1_Mochie_PBS (
    half3 diffColor, half3 specColor, half oneMinusReflectivity, half smoothness,
    half3 normal, half3 viewDir, half3 worldPos, half2 screenUVs, half4 screenPos,
    half metallic, half thickness, half3 ssColor, half atten, float2 lightmapUV, float3 vertexColor,
    UnityLight light, UnityIndirect gi, float reflection_strength, float ssr_mask)
{

  half perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness);
  if (_GSAA == 1){
    perceptualRoughness = GSAARoughness(normal, perceptualRoughness);
  }
  half3 halfDir = Unity_SafeNormalize (half3(light.dir) + viewDir);
  half nv = abs(dot(normal, viewDir));
  half nl = saturate(dot(normal, light.dir));
  half nh = saturate(dot(normal, halfDir));
  half lv = saturate(dot(light.dir, viewDir));
  half lh = saturate(dot(light.dir, halfDir));

  // Diffuse term
  half diffuseTerm = DisneyDiffuse(nv, nl, lh, perceptualRoughness) * nl;
  float wrappedDiffuse = saturate((diffuseTerm + _WrappingFactor) /
      (1.0f + _WrappingFactor)) * 2 / (2 * (1 + _WrappingFactor));

  // Specular term
  half roughness = PerceptualRoughnessToRoughness(perceptualRoughness);
#if UNITY_BRDF_GGX
  roughness = max(roughness, 0.002);
  half V = SmithJointGGXVisibilityTerm (nl, nv, roughness);
  half D = GGXTerm(nh, roughness);
#else
  half V = SmithBeckmannVisibilityTerm (nl, nv, roughness);
  half D = NDFBlinnPhongNormalizedTerm (nh, PerceptualRoughnessToSpecPower(perceptualRoughness));
#endif

#if defined(_SPECULARHIGHLIGHTS_OFF)
  half specularTerm = 0.0;
#else
  half specularTerm = V*D * UNITY_PI;
#ifdef UNITY_COLORSPACE_GAMMA
  specularTerm = sqrt(max(1e-4h, specularTerm));
#endif
  specularTerm = max(0, specularTerm * nl);
#endif
  half surfaceReduction;
#ifdef UNITY_COLORSPACE_GAMMA
  surfaceReduction = 1.0-0.28*roughness*perceptualRoughness;
#else
  surfaceReduction = 1.0 / (roughness*roughness + 1.0);
#endif

  half grazingTerm = saturate(smoothness + (1-oneMinusReflectivity));

  half3 diffCol = 0;
  diffCol = diffColor * (gi.diffuse + light.color * lerp(diffuseTerm, wrappedDiffuse, thickness));

  // TODO this should probably use its own version of _WrappingFactor
  specularTerm = saturate((specularTerm + _WrappingFactor) /
      (1.0f + _WrappingFactor)) * 2 / (2 * (1 + _WrappingFactor));

  half3 specCol = specularTerm * light.color * FresnelTerm (specColor, lh) * _SpecularStrength;

  half3 reflCol = surfaceReduction * gi.specular * FresnelLerp (specColor, grazingTerm, lerp(1, nv, _FresnelStrength*_UseFresnel)) * reflection_strength;
#if SSR_ENABLED
  half4 ssrCol = GetSSR(worldPos, viewDir, reflect(-viewDir, normal), normal, smoothness, diffColor, metallic, screenUVs, screenPos);
  ssrCol.rgb *= _SSRStrength;
  if (_EdgeFade == 0)
    ssrCol.a = ssrCol.a > 0 ? 1 : 0;
  reflCol = lerp(reflCol, ssrCol.rgb, ssrCol.a * saturate(_SSRStrength * ssr_mask));
  specCol *= 1 - ssrCol.a * ssr_mask;
#endif

  half3 subsurfaceCol = 0;
  if (_Subsurface == 1){
    subsurfaceCol = GetSubsurfaceLight(
        light.color, 
        light.dir, 
        normal, 
        viewDir, 
        atten, 
        thickness, 
        gi.diffuse, 
        ssColor
        );
  }

#ifdef LTCGI
  if (_LTCGIStrength > 0){
    half3 diffLight = 0;
    LTCGI_Contribution(
        worldPos, 
        normal, 
        viewDir, 
        perceptualRoughness,
        (lightmapUV - unity_LightmapST.zw) / unity_LightmapST.xy,
        diffLight
#ifndef _GLOSSYREFLECTIONS_OFF
        , reflCol
#endif
        );
    diffCol += (diffColor * diffLight) * _LTCGIStrength;
  }
#endif

#if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
  if (_ReflShadows == 1){
    float3 lightmap = Desaturate(DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, lightmapUV)));
    lightmap = GetContrast(lightmap, _ContrastReflShad);
    lightmap = lerp(lightmap, GetHDR(lightmap), _HDRReflShad);
    lightmap *= _BrightnessReflShad;
    lightmap *= _TintReflShad;
    shadowedReflections = saturate(lerp(1, lightmap, _ReflShadowStrength));
    reflCol *= shadowedReflections;
    specCol *= shadowedReflections;
  }
#else
  shadowedReflections = lerp(1, lerp(1, atten, 0.9), _ReflShadows*_ReflShadowStrength);
  reflCol *= shadowedReflections;
#endif

  // #ifdef FULL_VERSION
  // 	reflCol *= lerp(1, vertexColor, _ReflVertexColor*_ReflVertexColorStrength);
  // #endif

  return half4(diffCol + specCol + reflCol + subsurfaceCol, 1);
}

#endif // MOCHIE_STANDARD_BRDF_INCLUDED