this missed merging from trunk somehow (mesh make local edits)
[blender-staging.git] / intern / cycles / blender / blender_shader.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
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
19 #include "background.h"
20 #include "graph.h"
21 #include "light.h"
22 #include "nodes.h"
23 #include "scene.h"
24 #include "shader.h"
25
26 #include "blender_sync.h"
27 #include "blender_util.h"
28
29 #include "util_debug.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 typedef map<void*, ShaderNode*> PtrNodeMap;
34 typedef pair<ShaderNode*, std::string> SocketPair;
35 typedef map<void*, SocketPair> PtrSockMap;
36
37 /* Find */
38
39 void BlenderSync::find_shader(BL::ID id, vector<uint>& used_shaders, int default_shader)
40 {
41         Shader *shader = (id)? shader_map.find(id): scene->shaders[default_shader];
42
43         for(size_t i = 0; i < scene->shaders.size(); i++) {
44                 if(scene->shaders[i] == shader) {
45                         used_shaders.push_back(i);
46                         break;
47                 }
48         }
49 }
50
51 /* Graph */
52
53 static BL::NodeSocket get_node_output(BL::Node b_node, const string& name)
54 {
55         BL::Node::outputs_iterator b_out;
56
57         for(b_node.outputs.begin(b_out); b_out != b_node.outputs.end(); ++b_out)
58                 if(b_out->name() == name)
59                         return *b_out;
60
61         assert(0);
62
63         return *b_out;
64 }
65
66 static float3 get_node_output_rgba(BL::Node b_node, const string& name)
67 {
68         BL::NodeSocketRGBA sock(get_node_output(b_node, name));
69         return get_float3(sock.default_value());
70 }
71
72 static float get_node_output_value(BL::Node b_node, const string& name)
73 {
74         BL::NodeSocketFloatNone sock(get_node_output(b_node, name));
75         return sock.default_value();
76 }
77
78 static void get_tex_mapping(TextureMapping *mapping, BL::TexMapping b_mapping)
79 {
80         if(!b_mapping)
81                 return;
82
83         mapping->translation = get_float3(b_mapping.location());
84         mapping->rotation = get_float3(b_mapping.rotation());
85         mapping->scale = get_float3(b_mapping.scale());
86
87         mapping->x_mapping = (TextureMapping::Mapping)b_mapping.mapping_x();
88         mapping->y_mapping = (TextureMapping::Mapping)b_mapping.mapping_y();
89         mapping->z_mapping = (TextureMapping::Mapping)b_mapping.mapping_z();
90 }
91
92 static void get_tex_mapping(TextureMapping *mapping, BL::ShaderNodeMapping b_mapping)
93 {
94         if(!b_mapping)
95                 return;
96
97         mapping->translation = get_float3(b_mapping.location());
98         mapping->rotation = get_float3(b_mapping.rotation());
99         mapping->scale = get_float3(b_mapping.scale());
100 }
101
102 static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::ShaderNode b_node)
103 {
104         ShaderNode *node = NULL;
105
106         switch(b_node.type()) {
107                 /* not supported */
108                 case BL::ShaderNode::type_CURVE_RGB: break;
109                 case BL::ShaderNode::type_CURVE_VEC: break;
110                 case BL::ShaderNode::type_GEOMETRY: break;
111                 case BL::ShaderNode::type_MATERIAL: break;
112                 case BL::ShaderNode::type_MATERIAL_EXT: break;
113                 case BL::ShaderNode::type_OUTPUT: break;
114                 case BL::ShaderNode::type_SCRIPT: break;
115                 case BL::ShaderNode::type_SQUEEZE: break;
116                 case BL::ShaderNode::type_TEXTURE: break;
117                 case BL::ShaderNode::type_VALTORGB: break;
118                 /* handled outside this function */
119                 case BL::ShaderNode::type_GROUP: break;
120                 /* existing blender nodes */
121                 case BL::ShaderNode::type_RGB: {
122                         ColorNode *color = new ColorNode();
123                         color->value = get_node_output_rgba(b_node, "Color");
124                         node = color;
125                         break;
126                 }
127                 case BL::ShaderNode::type_VALUE: {
128                         ValueNode *value = new ValueNode();
129                         value->value = get_node_output_value(b_node, "Value");
130                         node = value;
131                         break;
132                 }
133                 case BL::ShaderNode::type_CAMERA: {
134                         node = new CameraNode();
135                         break;
136                 }
137                 case BL::ShaderNode::type_INVERT: {
138                         node = new InvertNode();
139                         break;
140                 }
141                 case BL::ShaderNode::type_GAMMA: {
142                         node = new GammaNode();
143                         break;
144                 }
145                 case BL::ShaderNode::type_MIX_RGB: {
146                         BL::ShaderNodeMixRGB b_mix_node(b_node);
147                         MixNode *mix = new MixNode();
148                         mix->type = MixNode::type_enum[b_mix_node.blend_type()];
149                         node = mix;
150                         break;
151                 }
152                 case BL::ShaderNode::type_SEPRGB: {
153                         node = new SeparateRGBNode();
154                         break;
155                 }
156                 case BL::ShaderNode::type_COMBRGB: {
157                         node = new CombineRGBNode();
158                         break;
159                 }
160                 case BL::ShaderNode::type_HUE_SAT: {
161                         node = new HSVNode();
162                         break;
163                 }
164                 case BL::ShaderNode::type_RGBTOBW: {
165                         node = new ConvertNode(SHADER_SOCKET_COLOR, SHADER_SOCKET_FLOAT);
166                         break;
167                 }
168                 case BL::ShaderNode::type_MATH: {
169                         BL::ShaderNodeMath b_math_node(b_node);
170                         MathNode *math = new MathNode();
171                         math->type = MathNode::type_enum[b_math_node.operation()];
172                         node = math;
173                         break;
174                 }
175                 case BL::ShaderNode::type_VECT_MATH: {
176                         BL::ShaderNodeVectorMath b_vector_math_node(b_node);
177                         VectorMathNode *vmath = new VectorMathNode();
178                         vmath->type = VectorMathNode::type_enum[b_vector_math_node.operation()];
179                         node = vmath;
180                         break;
181                 }
182                 case BL::ShaderNode::type_NORMAL: {
183                         BL::Node::outputs_iterator out_it;
184                         b_node.outputs.begin(out_it);
185                         BL::NodeSocketVectorNone vec_sock(*out_it);
186
187                         NormalNode *norm = new NormalNode();
188                         norm->direction = get_float3(vec_sock.default_value());
189
190                         node = norm;
191                         break;
192                 }
193                 case BL::ShaderNode::type_MAPPING: {
194                         BL::ShaderNodeMapping b_mapping_node(b_node);
195                         MappingNode *mapping = new MappingNode();
196
197                         get_tex_mapping(&mapping->tex_mapping, b_mapping_node);
198
199                         node = mapping;
200                         break;
201                 }
202
203                 /* new nodes */
204                 case BL::ShaderNode::type_OUTPUT_MATERIAL:
205                 case BL::ShaderNode::type_OUTPUT_WORLD:
206                 case BL::ShaderNode::type_OUTPUT_LAMP: {
207                         node = graph->output();
208                         break;
209                 }
210                 case BL::ShaderNode::type_FRESNEL: {
211                         node = new FresnelNode();
212                         break;
213                 }
214                 case BL::ShaderNode::type_LAYER_WEIGHT: {
215                         node = new LayerWeightNode();
216                         break;
217                 }
218                 case BL::ShaderNode::type_ADD_SHADER: {
219                         node = new AddClosureNode();
220                         break;
221                 }
222                 case BL::ShaderNode::type_MIX_SHADER: {
223                         node = new MixClosureNode();
224                         break;
225                 }
226                 case BL::ShaderNode::type_ATTRIBUTE: {
227                         BL::ShaderNodeAttribute b_attr_node(b_node);
228                         AttributeNode *attr = new AttributeNode();
229                         attr->attribute = b_attr_node.attribute_name();
230                         node = attr;
231                         break;
232                 }
233                 case BL::ShaderNode::type_BACKGROUND: {
234                         node = new BackgroundNode();
235                         break;
236                 }
237                 case BL::ShaderNode::type_HOLDOUT: {
238                         node = new HoldoutNode();
239                         break;
240                 }
241                 case BL::ShaderNode::type_BSDF_DIFFUSE: {
242                         node = new DiffuseBsdfNode();
243                         break;
244                 }
245                 case BL::ShaderNode::type_BSDF_GLOSSY: {
246                         BL::ShaderNodeBsdfGlossy b_glossy_node(b_node);
247                         GlossyBsdfNode *glossy = new GlossyBsdfNode();
248
249                         switch(b_glossy_node.distribution()) {
250                                 case BL::ShaderNodeBsdfGlossy::distribution_SHARP:
251                                         glossy->distribution = ustring("Sharp");
252                                         break;
253                                 case BL::ShaderNodeBsdfGlossy::distribution_BECKMANN:
254                                         glossy->distribution = ustring("Beckmann");
255                                         break;
256                                 case BL::ShaderNodeBsdfGlossy::distribution_GGX:
257                                         glossy->distribution = ustring("GGX");
258                                         break;
259                         }
260                         node = glossy;
261                         break;
262                 }
263                 case BL::ShaderNode::type_BSDF_GLASS: {
264                         BL::ShaderNodeBsdfGlass b_glass_node(b_node);
265                         GlassBsdfNode *glass = new GlassBsdfNode();
266                         switch(b_glass_node.distribution()) {
267                                 case BL::ShaderNodeBsdfGlass::distribution_SHARP:
268                                         glass->distribution = ustring("Sharp");
269                                         break;
270                                 case BL::ShaderNodeBsdfGlass::distribution_BECKMANN:
271                                         glass->distribution = ustring("Beckmann");
272                                         break;
273                                 case BL::ShaderNodeBsdfGlass::distribution_GGX:
274                                         glass->distribution = ustring("GGX");
275                                         break;
276                         }
277                         node = glass;
278                         break;
279                 }
280                 case BL::ShaderNode::type_BSDF_TRANSLUCENT: {
281                         node = new TranslucentBsdfNode();
282                         break;
283                 }
284                 case BL::ShaderNode::type_BSDF_TRANSPARENT: {
285                         node = new TransparentBsdfNode();
286                         break;
287                 }
288                 case BL::ShaderNode::type_BSDF_VELVET: {
289                         node = new VelvetBsdfNode();
290                         break;
291                 }
292                 case BL::ShaderNode::type_EMISSION: {
293                         node = new EmissionNode();
294                         break;
295                 }
296                 case BL::ShaderNode::type_VOLUME_ISOTROPIC: {
297                         node = new IsotropicVolumeNode();
298                         break;
299                 }
300                 case BL::ShaderNode::type_VOLUME_TRANSPARENT: {
301                         node = new TransparentVolumeNode();
302                         break;
303                 }
304                 case BL::ShaderNode::type_NEW_GEOMETRY: {
305                         node = new GeometryNode();
306                         break;
307                 }
308                 case BL::ShaderNode::type_LIGHT_PATH: {
309                         node = new LightPathNode();
310                         break;
311                 }
312                 case BL::ShaderNode::type_TEX_IMAGE: {
313                         BL::ShaderNodeTexImage b_image_node(b_node);
314                         BL::Image b_image(b_image_node.image());
315                         ImageTextureNode *image = new ImageTextureNode();
316                         /* todo: handle generated/builtin images */
317                         if(b_image)
318                                 image->filename = blender_absolute_path(b_data, b_image, b_image.filepath());
319                         image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()];
320                         get_tex_mapping(&image->tex_mapping, b_image_node.texture_mapping());
321                         node = image;
322                         break;
323                 }
324                 case BL::ShaderNode::type_TEX_ENVIRONMENT: {
325                         BL::ShaderNodeTexEnvironment b_env_node(b_node);
326                         BL::Image b_image(b_env_node.image());
327                         EnvironmentTextureNode *env = new EnvironmentTextureNode();
328                         if(b_image)
329                                 env->filename = blender_absolute_path(b_data, b_image, b_image.filepath());
330                         env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
331                         get_tex_mapping(&env->tex_mapping, b_env_node.texture_mapping());
332                         node = env;
333                         break;
334                 }
335                 case BL::ShaderNode::type_TEX_GRADIENT: {
336                         BL::ShaderNodeTexGradient b_gradient_node(b_node);
337                         GradientTextureNode *gradient = new GradientTextureNode();
338                         gradient->type = GradientTextureNode::type_enum[(int)b_gradient_node.gradient_type()];
339                         get_tex_mapping(&gradient->tex_mapping, b_gradient_node.texture_mapping());
340                         node = gradient;
341                         break;
342                 }
343                 case BL::ShaderNode::type_TEX_VORONOI: {
344                         BL::ShaderNodeTexVoronoi b_voronoi_node(b_node);
345                         VoronoiTextureNode *voronoi = new VoronoiTextureNode();
346                         voronoi->coloring = VoronoiTextureNode::coloring_enum[(int)b_voronoi_node.coloring()];
347                         get_tex_mapping(&voronoi->tex_mapping, b_voronoi_node.texture_mapping());
348                         node = voronoi;
349                         break;
350                 }
351                 case BL::ShaderNode::type_TEX_MAGIC: {
352                         BL::ShaderNodeTexMagic b_magic_node(b_node);
353                         MagicTextureNode *magic = new MagicTextureNode();
354                         magic->depth = b_magic_node.turbulence_depth();
355                         get_tex_mapping(&magic->tex_mapping, b_magic_node.texture_mapping());
356                         node = magic;
357                         break;
358                 }
359                 case BL::ShaderNode::type_TEX_WAVE: {
360                         BL::ShaderNodeTexWave b_wave_node(b_node);
361                         WaveTextureNode *wave = new WaveTextureNode();
362                         wave->type = WaveTextureNode::type_enum[(int)b_wave_node.wave_type()];
363                         get_tex_mapping(&wave->tex_mapping, b_wave_node.texture_mapping());
364                         node = wave;
365                         break;
366                 }
367                 case BL::ShaderNode::type_TEX_NOISE: {
368                         BL::ShaderNodeTexNoise b_noise_node(b_node);
369                         NoiseTextureNode *noise = new NoiseTextureNode();
370                         get_tex_mapping(&noise->tex_mapping, b_noise_node.texture_mapping());
371                         node = noise;
372                         break;
373                 }
374                 case BL::ShaderNode::type_TEX_MUSGRAVE: {
375                         BL::ShaderNodeTexMusgrave b_musgrave_node(b_node);
376                         MusgraveTextureNode *musgrave = new MusgraveTextureNode();
377                         musgrave->type = MusgraveTextureNode::type_enum[(int)b_musgrave_node.musgrave_type()];
378                         get_tex_mapping(&musgrave->tex_mapping, b_musgrave_node.texture_mapping());
379                         node = musgrave;
380                         break;
381                 }
382                 case BL::ShaderNode::type_TEX_COORD: {
383                         node = new TextureCoordinateNode();;
384                         break;
385                 }
386                 case BL::ShaderNode::type_TEX_SKY: {
387                         BL::ShaderNodeTexSky b_sky_node(b_node);
388                         SkyTextureNode *sky = new SkyTextureNode();
389                         sky->sun_direction = get_float3(b_sky_node.sun_direction());
390                         sky->turbidity = b_sky_node.turbidity();
391                         get_tex_mapping(&sky->tex_mapping, b_sky_node.texture_mapping());
392                         node = sky;
393                         break;
394                 }
395         }
396
397         if(node && node != graph->output())
398                 graph->add(node);
399
400         return node;
401 }
402
403 static SocketPair node_socket_map_pair(PtrNodeMap& node_map, BL::Node b_node, BL::NodeSocket b_socket)
404 {
405         BL::Node::inputs_iterator b_input;
406         BL::Node::outputs_iterator b_output;
407         string name = b_socket.name();
408         bool found = false;
409         int counter = 0, total = 0;
410
411         /* find in inputs */
412         for(b_node.inputs.begin(b_input); b_input != b_node.inputs.end(); ++b_input) {
413                 if(b_input->name() == name) {
414                         if(!found)
415                                 counter++;
416                         total++;
417                 }
418
419                 if(b_input->ptr.data == b_socket.ptr.data)
420                         found = true;
421         }
422
423         if(!found) {
424                 /* find in outputs */
425                 found = false;
426                 counter = 0;
427                 total = 0;
428
429                 for(b_node.outputs.begin(b_output); b_output != b_node.outputs.end(); ++b_output) {
430                         if(b_output->name() == name) {
431                                 if(!found)
432                                         counter++;
433                                 total++;
434                         }
435
436                         if(b_output->ptr.data == b_socket.ptr.data)
437                                 found = true;
438                 }
439         }
440
441         /* rename if needed */
442         if(name == "Shader")
443                 name = "Closure";
444
445         if(total > 1)
446                 name = string_printf("%s%d", name.c_str(), counter);
447
448         return SocketPair(node_map[b_node.ptr.data], name);
449 }
450
451 static ShaderSocketType convert_socket_type(BL::NodeSocket::type_enum b_type)
452 {
453         switch (b_type) {
454         case BL::NodeSocket::type_VALUE:
455                 return SHADER_SOCKET_FLOAT;
456         case BL::NodeSocket::type_VECTOR:
457                 return SHADER_SOCKET_VECTOR;
458         case BL::NodeSocket::type_RGBA:
459                 return SHADER_SOCKET_COLOR;
460         case BL::NodeSocket::type_SHADER:
461                 return SHADER_SOCKET_CLOSURE;
462         
463         case BL::NodeSocket::type_BOOLEAN:
464         case BL::NodeSocket::type_MESH:
465         case BL::NodeSocket::type_INT:
466         default:
467                 return SHADER_SOCKET_FLOAT;
468         }
469 }
470
471 static void set_default_value(ShaderInput *input, BL::NodeSocket sock)
472 {
473         /* copy values for non linked inputs */
474         switch(input->type) {
475         case SHADER_SOCKET_FLOAT: {
476                 BL::NodeSocketFloatNone value_sock(sock);
477                 input->set(value_sock.default_value());
478                 break;
479         }
480         case SHADER_SOCKET_COLOR: {
481                 BL::NodeSocketRGBA rgba_sock(sock);
482                 input->set(get_float3(rgba_sock.default_value()));
483                 break;
484         }
485         case SHADER_SOCKET_NORMAL:
486         case SHADER_SOCKET_POINT:
487         case SHADER_SOCKET_VECTOR: {
488                 BL::NodeSocketVectorNone vec_sock(sock);
489                 input->set(get_float3(vec_sock.default_value()));
490                 break;
491         }
492         case SHADER_SOCKET_CLOSURE:
493                 break;
494         }
495 }
496
497 static void add_nodes(BL::BlendData b_data, ShaderGraph *graph, BL::ShaderNodeTree b_ntree, PtrSockMap& sockets_map)
498 {
499         /* add nodes */
500         BL::ShaderNodeTree::nodes_iterator b_node;
501         PtrNodeMap node_map;
502         PtrSockMap proxy_map;
503
504         for(b_ntree.nodes.begin(b_node); b_node != b_ntree.nodes.end(); ++b_node) {
505                 if(b_node->is_a(&RNA_NodeGroup)) {
506                         /* add proxy converter nodes for inputs and outputs */
507                         BL::NodeGroup b_gnode(*b_node);
508                         BL::ShaderNodeTree b_group_ntree(b_gnode.node_tree());
509                         BL::Node::inputs_iterator b_input;
510                         BL::Node::outputs_iterator b_output;
511                         
512                         PtrSockMap group_sockmap;
513                         
514                         for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
515                                 ShaderSocketType extern_type = convert_socket_type(b_input->type());
516                                 ShaderSocketType intern_type = convert_socket_type(b_input->group_socket().type());
517                                 ShaderNode *proxy = graph->add(new ProxyNode(extern_type, intern_type));
518                                 
519                                 /* map the external node socket to the proxy node socket */
520                                 proxy_map[b_input->ptr.data] = SocketPair(proxy, proxy->inputs[0]->name);
521                                 /* map the internal group socket to the proxy node socket */
522                                 group_sockmap[b_input->group_socket().ptr.data] = SocketPair(proxy, proxy->outputs[0]->name);
523                                 
524                                 /* default input values of the group node */
525                                 set_default_value(proxy->inputs[0], *b_input);
526                         }
527                         
528                         for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
529                                 ShaderSocketType extern_type = convert_socket_type(b_output->type());
530                                 ShaderSocketType intern_type = convert_socket_type(b_output->group_socket().type());
531                                 ShaderNode *proxy = graph->add(new ProxyNode(intern_type, extern_type));
532                                 
533                                 /* map the external node socket to the proxy node socket */
534                                 proxy_map[b_output->ptr.data] = SocketPair(proxy, proxy->outputs[0]->name);
535                                 /* map the internal group socket to the proxy node socket */
536                                 group_sockmap[b_output->group_socket().ptr.data] = SocketPair(proxy, proxy->inputs[0]->name);
537                                 
538                                 /* default input values of internal, unlinked group outputs */
539                                 set_default_value(proxy->inputs[0], b_output->group_socket());
540                         }
541                         
542                         add_nodes(b_data, graph, b_group_ntree, group_sockmap);
543                 }
544                 else {
545                         ShaderNode *node = add_node(b_data, graph, BL::ShaderNode(*b_node));
546                         
547                         if(node) {
548                                 BL::Node::inputs_iterator b_input;
549                                 
550                                 node_map[b_node->ptr.data] = node;
551                                 
552                                 for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
553                                         SocketPair pair = node_socket_map_pair(node_map, *b_node, *b_input);
554                                         ShaderInput *input = pair.first->input(pair.second.c_str());
555                                         
556                                         assert(input);
557                                         
558                                         /* copy values for non linked inputs */
559                                         set_default_value(input, *b_input);
560                                 }
561                         }
562                 }
563         }
564
565         /* connect nodes */
566         BL::NodeTree::links_iterator b_link;
567
568         for(b_ntree.links.begin(b_link); b_link != b_ntree.links.end(); ++b_link) {
569                 /* get blender link data */
570                 BL::Node b_from_node = b_link->from_node();
571                 BL::Node b_to_node = b_link->to_node();
572
573                 BL::NodeSocket b_from_sock = b_link->from_socket();
574                 BL::NodeSocket b_to_sock = b_link->to_socket();
575
576                 SocketPair from_pair, to_pair;
577
578                 /* links without a node pointer are connections to group inputs/outputs */
579
580                 /* from sock */
581                 if(b_from_node) {
582                         if (b_from_node.is_a(&RNA_NodeGroup))
583                                 from_pair = proxy_map[b_from_sock.ptr.data];
584                         else
585                                 from_pair = node_socket_map_pair(node_map, b_from_node, b_from_sock);
586                 }
587                 else
588                         from_pair = sockets_map[b_from_sock.ptr.data];
589
590                 /* to sock */
591                 if(b_to_node) {
592                         if (b_to_node.is_a(&RNA_NodeGroup))
593                                 to_pair = proxy_map[b_to_sock.ptr.data];
594                         else
595                                 to_pair = node_socket_map_pair(node_map, b_to_node, b_to_sock);
596                 }
597                 else
598                         to_pair = sockets_map[b_to_sock.ptr.data];
599
600                 ShaderOutput *output = from_pair.first->output(from_pair.second.c_str());
601                 ShaderInput *input = to_pair.first->input(to_pair.second.c_str());
602
603                 graph->connect(output, input);
604         }
605 }
606
607 /* Sync Materials */
608
609 void BlenderSync::sync_materials()
610 {
611         shader_map.set_default(scene->shaders[scene->default_surface]);
612
613         /* material loop */
614         BL::BlendData::materials_iterator b_mat;
615
616         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat) {
617                 Shader *shader;
618                 
619                 /* test if we need to sync */
620                 if(shader_map.sync(&shader, *b_mat)) {
621                         ShaderGraph *graph = new ShaderGraph();
622
623                         shader->name = b_mat->name().c_str();
624
625                         /* create nodes */
626                         if(b_mat->use_nodes() && b_mat->node_tree()) {
627                                 PtrSockMap sock_to_node;
628                                 BL::ShaderNodeTree b_ntree(b_mat->node_tree());
629
630                                 add_nodes(b_data, graph, b_ntree, sock_to_node);
631                         }
632                         else {
633                                 ShaderNode *closure, *out;
634
635                                 closure = graph->add(new DiffuseBsdfNode());
636                                 closure->input("Color")->value = get_float3(b_mat->diffuse_color());
637                                 out = graph->output();
638
639                                 graph->connect(closure->output("BSDF"), out->input("Surface"));
640                         }
641
642                         /* settings */
643                         PointerRNA cmat = RNA_pointer_get(&b_mat->ptr, "cycles");
644                         shader->sample_as_light = get_boolean(cmat, "sample_as_light");
645                         shader->homogeneous_volume = get_boolean(cmat, "homogeneous_volume");
646
647                         shader->set_graph(graph);
648                         shader->tag_update(scene);
649                 }
650         }
651 }
652
653 /* Sync World */
654
655 void BlenderSync::sync_world()
656 {
657         Background *background = scene->background;
658         Background prevbackground = *background;
659
660         BL::World b_world = b_scene.world();
661
662         if(world_recalc || b_world.ptr.data != world_map) {
663                 Shader *shader = scene->shaders[scene->default_background];
664                 ShaderGraph *graph = new ShaderGraph();
665
666                 /* create nodes */
667                 if(b_world && b_world.use_nodes() && b_world.node_tree()) {
668                         PtrSockMap sock_to_node;
669                         BL::ShaderNodeTree b_ntree(b_world.node_tree());
670
671                         add_nodes(b_data, graph, b_ntree, sock_to_node);
672                 }
673                 else if(b_world) {
674                         ShaderNode *closure, *out;
675
676                         closure = graph->add(new BackgroundNode());
677                         closure->input("Color")->value = get_float3(b_world.horizon_color());
678                         out = graph->output();
679
680                         graph->connect(closure->output("Background"), out->input("Surface"));
681                 }
682
683                 shader->set_graph(graph);
684                 shader->tag_update(scene);
685         }
686
687         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
688         background->transparent = get_boolean(cscene, "film_transparent");
689
690         if(background->modified(prevbackground))
691                 background->tag_update(scene);
692
693         world_map = b_world.ptr.data;
694         world_recalc = false;
695 }
696
697 /* Sync Lamps */
698
699 void BlenderSync::sync_lamps()
700 {
701         shader_map.set_default(scene->shaders[scene->default_light]);
702
703         /* lamp loop */
704         BL::BlendData::lamps_iterator b_lamp;
705
706         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp) {
707                 Shader *shader;
708                 
709                 /* test if we need to sync */
710                 if(shader_map.sync(&shader, *b_lamp)) {
711                         ShaderGraph *graph = new ShaderGraph();
712
713                         /* create nodes */
714                         if(b_lamp->use_nodes() && b_lamp->node_tree()) {
715                                 shader->name = b_lamp->name().c_str();
716
717                                 PtrSockMap sock_to_node;
718                                 BL::ShaderNodeTree b_ntree(b_lamp->node_tree());
719
720                                 add_nodes(b_data, graph, b_ntree, sock_to_node);
721                         }
722                         else {
723                                 ShaderNode *closure, *out;
724                                 float strength = 1.0f;
725
726                                 if(b_lamp->type() == BL::Lamp::type_POINT ||
727                                    b_lamp->type() == BL::Lamp::type_SPOT ||
728                                    b_lamp->type() == BL::Lamp::type_AREA)
729                                         strength = 100.0f;
730
731                                 closure = graph->add(new EmissionNode());
732                                 closure->input("Color")->value = get_float3(b_lamp->color());
733                                 closure->input("Strength")->value.x = strength;
734                                 out = graph->output();
735
736                                 graph->connect(closure->output("Emission"), out->input("Surface"));
737                         }
738
739                         shader->set_graph(graph);
740                         shader->tag_update(scene);
741                 }
742         }
743 }
744
745 void BlenderSync::sync_shaders()
746 {
747         shader_map.pre_sync();
748
749         sync_world();
750         sync_lamps();
751         sync_materials();
752
753         /* false = don't delete unused shaders, not supported */
754         shader_map.post_sync(false);
755 }
756
757 CCL_NAMESPACE_END
758