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