a4145a350d2bdd6dc0756bce55990f4e49680eb3
[blender.git] / source / blender / nodes / shader / nodes / node_shader_tex_voronoi.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 /* **************** VORONOI ******************** */
31
32 static bNodeSocketTemplate sh_node_tex_voronoi_in[] = {
33         {       SOCK_VECTOR, 1, N_("Vector"),           0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
34         {       SOCK_FLOAT, 1, N_("Scale"),                     5.0f, 0.0f, 0.0f, 0.0f, -1000.0f, 1000.0f},
35         {       SOCK_FLOAT, 1, N_("Exponent"),          0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 32.0f},
36         {       -1, 0, ""       }
37 };
38
39 static bNodeSocketTemplate sh_node_tex_voronoi_out[] = {
40         {       SOCK_RGBA, 0, N_("Color"),              0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_NO_INTERNAL_LINK},
41         {       SOCK_FLOAT, 0, N_("Fac"),               0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR, SOCK_NO_INTERNAL_LINK},
42         {       -1, 0, ""       }
43 };
44
45 static void node_shader_init_tex_voronoi(bNodeTree *UNUSED(ntree), bNode *node)
46 {
47         NodeTexVoronoi *tex = MEM_callocN(sizeof(NodeTexVoronoi), "NodeTexVoronoi");
48         BKE_texture_mapping_default(&tex->base.tex_mapping, TEXMAP_TYPE_POINT);
49         BKE_texture_colormapping_default(&tex->base.color_mapping);
50         tex->coloring = SHD_VORONOI_INTENSITY;
51         tex->distance = SHD_VORONOI_DISTANCE;
52         tex->feature = SHD_VORONOI_F1;
53
54         node->storage = tex;
55 }
56
57 static int node_shader_gpu_tex_voronoi(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
58 {
59         if (!in[0].link) {
60                 in[0].link = GPU_attribute(CD_ORCO, "");
61                 GPU_link(mat, "generated_from_orco", in[0].link, &in[0].link);
62         }
63
64         node_shader_gpu_tex_mapping(mat, node, in, out);
65
66         NodeTexVoronoi *tex = (NodeTexVoronoi *)node->storage;
67         float coloring = tex->coloring;
68         float metric = tex->distance;
69         float feature = tex->feature;
70
71         return GPU_stack_link(mat, node, "node_tex_voronoi", in, out, GPU_constant(&coloring), GPU_constant(&metric), GPU_constant(&feature));
72 }
73
74 static void node_shader_update_tex_voronoi(bNodeTree *UNUSED(ntree), bNode *node)
75 {
76         NodeTexVoronoi *tex = (NodeTexVoronoi *)node->storage;
77         bNodeSocket *sock;
78
79         for (sock = node->inputs.first; sock; sock = sock->next) {
80                 if (STREQ(sock->name, "Exponent")) {
81                         if (tex->distance == SHD_VORONOI_MINKOWSKI) {
82                                 sock->flag &= ~SOCK_UNAVAIL;
83                         }
84                         else {
85                                 sock->flag |= SOCK_UNAVAIL;
86                         }
87                 }
88         }
89 }
90
91 /* node type definition */
92 void register_node_type_sh_tex_voronoi(void)
93 {
94         static bNodeType ntype;
95
96         sh_node_type_base(&ntype, SH_NODE_TEX_VORONOI, "Voronoi Texture", NODE_CLASS_TEXTURE, 0);
97         node_type_socket_templates(&ntype, sh_node_tex_voronoi_in, sh_node_tex_voronoi_out);
98         node_type_init(&ntype, node_shader_init_tex_voronoi);
99         node_type_storage(&ntype, "NodeTexVoronoi", node_free_standard_storage, node_copy_standard_storage);
100         node_type_gpu(&ntype, node_shader_gpu_tex_voronoi);
101         node_type_update(&ntype, node_shader_update_tex_voronoi, NULL);
102
103         nodeRegisterType(&ntype);
104 }