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