Cycles: add random walk subsurface scattering to Principled BSDF.
[blender.git] / source / blender / nodes / shader / nodes / node_shader_bsdf_principled.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include "../node_shader_util.h"
29
30 /* **************** OUTPUT ******************** */
31
32 static bNodeSocketTemplate sh_node_bsdf_principled_in[] = {
33         {       SOCK_RGBA, 1, N_("Base Color"),                         0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
34         {       SOCK_FLOAT, 1, N_("Subsurface"),                        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
35         {       SOCK_VECTOR, 1, N_("Subsurface Radius"),        1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f},
36         {       SOCK_RGBA, 1, N_("Subsurface Color"),           0.7f, 0.1f, 0.1f, 1.0f, 0.0f, 1.0f},
37         {       SOCK_FLOAT, 1, N_("Metallic"),                          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
38         {       SOCK_FLOAT, 1, N_("Specular"),                          0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
39         {       SOCK_FLOAT, 1, N_("Specular Tint"),                     0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
40         {       SOCK_FLOAT, 1, N_("Roughness"),                         0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
41         {       SOCK_FLOAT, 1, N_("Anisotropic"),                       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
42         {       SOCK_FLOAT, 1, N_("Anisotropic Rotation"),      0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
43         {       SOCK_FLOAT, 1, N_("Sheen"),                                     0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
44         {       SOCK_FLOAT, 1, N_("Sheen Tint"),                        0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
45         {       SOCK_FLOAT, 1, N_("Clearcoat"),                         0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
46         {       SOCK_FLOAT, 1, N_("Clearcoat Roughness"),       0.03f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
47         {       SOCK_FLOAT, 1, N_("IOR"),                                       1.45f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
48         {       SOCK_FLOAT, 1, N_("Transmission"),                      0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
49         {       SOCK_FLOAT, 1, N_("Transmission Roughness"),0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
50         {       SOCK_VECTOR, 1, N_("Normal"),                           0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
51         {       SOCK_VECTOR, 1, N_("Clearcoat Normal"),         0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
52         {       SOCK_VECTOR, 1, N_("Tangent"),                          0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
53         {       -1, 0, ""       }
54 };
55
56 static bNodeSocketTemplate sh_node_bsdf_principled_out[] = {
57         {       SOCK_SHADER, 0, N_("BSDF")},
58         {       -1, 0, ""       }
59 };
60
61 static void node_shader_init_principled(bNodeTree *UNUSED(ntree), bNode *node)
62 {
63         node->custom1 = SHD_GLOSSY_MULTI_GGX;
64         node->custom2 = SHD_SUBSURFACE_BURLEY;
65 }
66
67 static int node_shader_gpu_bsdf_principled(GPUMaterial *mat, bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
68 {
69         // normal
70         if (!in[17].link)
71                 in[17].link = GPU_builtin(GPU_VIEW_NORMAL);
72         else
73                 GPU_link(mat, "direction_transform_m4v3", in[17].link, GPU_builtin(GPU_VIEW_MATRIX), &in[17].link);
74
75         // clearcoat normal
76         if (!in[18].link)
77                 in[18].link = GPU_builtin(GPU_VIEW_NORMAL);
78         else
79                 GPU_link(mat, "direction_transform_m4v3", in[18].link, GPU_builtin(GPU_VIEW_MATRIX), &in[18].link);
80
81         return GPU_stack_link(mat, "node_bsdf_principled", in, out, GPU_builtin(GPU_VIEW_POSITION));
82 }
83
84 static void node_shader_update_principled(bNodeTree *UNUSED(ntree), bNode *node)
85 {
86         bNodeSocket *sock;
87         int distribution = node->custom1;
88
89         for (sock = node->inputs.first; sock; sock = sock->next) {
90                 if (STREQ(sock->name, "Transmission Roughness")) {
91                         if (distribution == SHD_GLOSSY_GGX)
92                                 sock->flag &= ~SOCK_UNAVAIL;
93                         else
94                                 sock->flag |= SOCK_UNAVAIL;
95
96                 }
97         }
98 }
99
100 /* node type definition */
101 void register_node_type_sh_bsdf_principled(void)
102 {
103         static bNodeType ntype;
104
105         sh_node_type_base(&ntype, SH_NODE_BSDF_PRINCIPLED, "Principled BSDF", NODE_CLASS_SHADER, 0);
106         node_type_compatibility(&ntype, NODE_NEW_SHADING);
107         node_type_socket_templates(&ntype, sh_node_bsdf_principled_in, sh_node_bsdf_principled_out);
108         node_type_size_preset(&ntype, NODE_SIZE_MIDDLE);
109         node_type_init(&ntype, node_shader_init_principled);
110         node_type_storage(&ntype, "", NULL, NULL);
111         node_type_gpu(&ntype, node_shader_gpu_bsdf_principled);
112         node_type_update(&ntype, node_shader_update_principled, NULL);
113
114         nodeRegisterType(&ntype);
115 }