Merge branch 'master' into blender2.8
[blender.git] / source / blender / nodes / shader / nodes / node_shader_bsdf_hair_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) 2018 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Lukas Stockner, L. E. Segovia
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include "../node_shader_util.h"
29
30 /* **************** OUTPUT ******************** */
31
32 /* Color, melanin and absorption coefficient default to approximately same brownish hair. */
33 static bNodeSocketTemplate sh_node_bsdf_hair_principled_in[] = {
34         {       SOCK_RGBA,              1, N_("Color"),                                         0.017513f, 0.005763f, 0.002059f, 1.0f, 0.0f, 1.0f},
35         {       SOCK_FLOAT,             1, N_("Melanin"),                                       0.8f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
36         {       SOCK_FLOAT,             1, N_("Melanin Redness"),                       1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
37         {       SOCK_RGBA,              1, N_("Tint"),                                          1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f},
38         {       SOCK_VECTOR,    1, N_("Absorption Coefficient"),        0.245531f, 0.52f, 1.365f, 0.0f, 0.0f, 1000.0f},
39         {       SOCK_FLOAT,             1, N_("Roughness"),                                     0.3f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
40         {       SOCK_FLOAT,             1, N_("Radial Roughness"),                      0.3f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
41         {       SOCK_FLOAT,             1, N_("Coat"),                                          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
42         {       SOCK_FLOAT,             1, N_("IOR"),                                           1.55f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
43         {       SOCK_FLOAT,             1, N_("Offset"),                                        2.f*((float)M_PI)/180.f, 0.0f, 0.0f, 0.0f, -M_PI_2, M_PI_2, PROP_ANGLE},
44         {       SOCK_FLOAT,     1, N_("Random Color"),                          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
45         {       SOCK_FLOAT,     1, N_("Random Roughness"),                      0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
46         {       SOCK_FLOAT,             1, N_("Random"),                                        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
47         {       -1, 0, ""       },
48 };
49
50 static bNodeSocketTemplate sh_node_bsdf_hair_principled_out[] = {
51         {       SOCK_SHADER, 0, N_("BSDF")},
52         {       -1, 0, ""       }
53 };
54
55 /* Initialize the custom Parametrization property to Color. */
56 static void node_shader_init_hair_principled(bNodeTree *UNUSED(ntree), bNode *node)
57 {
58         node->custom1 = SHD_PRINCIPLED_HAIR_REFLECTANCE;
59 }
60
61 /* Triggers (in)visibility of some sockets when changing Parametrization. */
62 static void node_shader_update_hair_principled(bNodeTree *UNUSED(ntree), bNode *node)
63 {
64         bNodeSocket *sock;
65         int parametrization = node->custom1;
66
67         for (sock = node->inputs.first; sock; sock = sock->next) {
68                 if (STREQ(sock->name, "Color")) {
69                         if (parametrization == SHD_PRINCIPLED_HAIR_REFLECTANCE) {
70                                 sock->flag &= ~SOCK_UNAVAIL;
71                         }
72                         else {
73                                 sock->flag |= SOCK_UNAVAIL;
74                         }
75                 }
76                 else if (STREQ(sock->name, "Melanin")) {
77                         if (parametrization == SHD_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION) {
78                                 sock->flag &= ~SOCK_UNAVAIL;
79                         }
80                         else {
81                                 sock->flag |= SOCK_UNAVAIL;
82                         }
83                 }
84                 else if (STREQ(sock->name, "Melanin Redness")) {
85                         if (parametrization == SHD_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION) {
86                                 sock->flag &= ~SOCK_UNAVAIL;
87                         }
88                         else {
89                                 sock->flag |= SOCK_UNAVAIL;
90                         }
91                 }
92                 else if (STREQ(sock->name, "Tint")) {
93                         if (parametrization == SHD_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION) {
94                                 sock->flag &= ~SOCK_UNAVAIL;
95                         }
96                         else {
97                                 sock->flag |= SOCK_UNAVAIL;
98                         }
99                 }
100                 else if (STREQ(sock->name, "Absorption Coefficient")) {
101                         if (parametrization == SHD_PRINCIPLED_HAIR_DIRECT_ABSORPTION) {
102                                 sock->flag &= ~SOCK_UNAVAIL;
103                         }
104                         else {
105                                 sock->flag |= SOCK_UNAVAIL;
106                         }
107                 }
108                 else if (STREQ(sock->name, "Random Color")) {
109                         if (parametrization == SHD_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION) {
110                                 sock->flag &= ~SOCK_UNAVAIL;
111                         }
112                         else {
113                                 sock->flag |= SOCK_UNAVAIL;
114                         }
115                 }
116         }
117 }
118
119 /* node type definition */
120 void register_node_type_sh_bsdf_hair_principled(void)
121 {
122         static bNodeType ntype;
123
124         sh_node_type_base(&ntype, SH_NODE_BSDF_HAIR_PRINCIPLED, "Principled Hair BSDF", NODE_CLASS_SHADER, 0);
125         node_type_socket_templates(&ntype, sh_node_bsdf_hair_principled_in, sh_node_bsdf_hair_principled_out);
126         node_type_size_preset(&ntype, NODE_SIZE_LARGE);
127         node_type_init(&ntype, node_shader_init_hair_principled);
128         node_type_storage(&ntype, "", NULL, NULL);
129         node_type_update(&ntype, node_shader_update_hair_principled, NULL);
130
131         nodeRegisterType(&ntype);
132 }