0c962e3c5f0aa14538d042a359790456d8006dce
[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::Node b_node, const string& name)
54 {
55         BL::Node::inputs_iterator b_in;
56
57         for(b_node.inputs.begin(b_in); b_in != b_node.inputs.end(); ++b_in) {
58                 if(b_in->name() == name) {
59                         if(b_group_node) {
60
61                                 BL::NodeTree b_ntree = BL::NodeGroup(*b_group_node).node_tree();
62                                 BL::NodeTree::links_iterator b_link;
63
64                                 for(b_ntree.links.begin(b_link); b_link != b_ntree.links.end(); ++b_link) {
65                                         if(b_link->to_socket().ptr.data == b_in->ptr.data) {
66                                                 BL::Node::inputs_iterator b_gin;
67
68                                                 for(b_group_node->inputs.begin(b_gin); b_gin != b_group_node->inputs.end(); ++b_gin)
69                                                         if(b_gin->group_socket().ptr.data == b_link->from_socket().ptr.data)
70                                                                 return *b_gin;
71
72                                         }
73                                 }
74                         }
75
76                         return *b_in;
77                 }
78         }
79
80         assert(0);
81
82         return *b_in;
83 }
84
85 static BL::NodeSocket get_node_output(BL::Node b_node, const string& name)
86 {
87         BL::Node::outputs_iterator b_out;
88
89         for(b_node.outputs.begin(b_out); b_out != b_node.outputs.end(); ++b_out)
90                 if(b_out->name() == name)
91                         return *b_out;
92
93         assert(0);
94
95         return *b_out;
96 }
97
98 static float3 get_node_output_rgba(BL::Node b_node, const string& name)
99 {
100         BL::NodeSocketRGBA sock(get_node_output(b_node, name));
101         return get_float3(sock.default_value());
102 }
103
104 static float get_node_output_value(BL::Node b_node, const string& name)
105 {
106         BL::NodeSocketFloatNone sock(get_node_output(b_node, name));
107         return sock.default_value();
108 }
109
110 static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *b_group_node, BL::ShaderNode b_node)
111 {
112         ShaderNode *node = NULL;
113
114         switch(b_node.type()) {
115                 /* not supported */
116                 case BL::ShaderNode::type_CAMERA: break;
117                 case BL::ShaderNode::type_COMBRGB: break;
118                 case BL::ShaderNode::type_CURVE_RGB: break;
119                 case BL::ShaderNode::type_CURVE_VEC: break;
120                 case BL::ShaderNode::type_GEOM: break;
121                 case BL::ShaderNode::type_HUE_SAT: break;
122                 case BL::ShaderNode::type_INVERT: break;
123                 case BL::ShaderNode::type_MATERIAL: break;
124                 case BL::ShaderNode::type_MATERIAL_EXT: break;
125                 case BL::ShaderNode::type_NORMAL: break;
126                 case BL::ShaderNode::type_OUTPUT: break;
127                 case BL::ShaderNode::type_SCRIPT: break;
128                 case BL::ShaderNode::type_SEPRGB: break;
129                 case BL::ShaderNode::type_SQUEEZE: break;
130                 case BL::ShaderNode::type_TEXTURE: break;
131                 case BL::ShaderNode::type_VALTORGB: break;
132                 /* handled outside this function */
133                 case BL::ShaderNode::type_GROUP: break;
134                 /* existing blender nodes */
135                 case BL::ShaderNode::type_RGB: {
136                         ColorNode *color = new ColorNode();
137                         color->value = get_node_output_rgba(b_node, "Color");
138                         node = color;
139                         break;
140                 }
141                 case BL::ShaderNode::type_VALUE: {
142                         ValueNode *value = new ValueNode();
143                         value->value = get_node_output_value(b_node, "Value");
144                         node = value;
145                         break;
146                 }
147                 case BL::ShaderNode::type_MIX_RGB: {
148                         BL::ShaderNodeMixRGB b_mix_node(b_node);
149                         MixNode *mix = new MixNode();
150                         mix->type = MixNode::type_enum[b_mix_node.blend_type()];
151                         node = mix;
152                         break;
153                 }
154                 case BL::ShaderNode::type_RGBTOBW: {
155                         node = new ConvertNode(SHADER_SOCKET_COLOR, SHADER_SOCKET_FLOAT);
156                         break;
157                 }
158                 case BL::ShaderNode::type_MATH: {
159                         BL::ShaderNodeMath b_math_node(b_node);
160                         MathNode *math = new MathNode();
161                         math->type = MathNode::type_enum[b_math_node.operation()];
162                         node = math;
163                         break;
164                 }
165                 case BL::ShaderNode::type_VECT_MATH: {
166                         BL::ShaderNodeVectorMath b_vector_math_node(b_node);
167                         VectorMathNode *vmath = new VectorMathNode();
168                         vmath->type = VectorMathNode::type_enum[b_vector_math_node.operation()];
169                         node = vmath;
170                         break;
171                 }
172                 case BL::ShaderNode::type_MAPPING: {
173                         BL::ShaderNodeMapping b_mapping_node(b_node);
174                         MappingNode *mapping = new MappingNode();
175                         mapping->translation = get_float3(b_mapping_node.location());
176                         mapping->rotation = get_float3(b_mapping_node.rotation());
177                         mapping->scale = get_float3(b_mapping_node.scale());
178                         node = mapping;
179                         break;
180                 }
181
182                 /* new nodes */
183                 case BL::ShaderNode::type_OUTPUT_MATERIAL:
184                 case BL::ShaderNode::type_OUTPUT_WORLD:
185                 case BL::ShaderNode::type_OUTPUT_LAMP: {
186                         node = graph->output();
187                         break;
188                 }
189                 case BL::ShaderNode::type_FRESNEL: {
190                         node = new FresnelNode();
191                         break;
192                 }
193                 case BL::ShaderNode::type_BLEND_WEIGHT: {
194                         node = new BlendWeightNode();
195                         break;
196                 }
197                 case BL::ShaderNode::type_ADD_SHADER: {
198                         node = new AddClosureNode();
199                         break;
200                 }
201                 case BL::ShaderNode::type_MIX_SHADER: {
202                         node = new MixClosureNode();
203                         break;
204                 }
205                 case BL::ShaderNode::type_ATTRIBUTE: {
206                         AttributeNode *attr = new AttributeNode();
207                         attr->attribute = "";
208                         node = attr;
209                         break;
210                 }
211                 case BL::ShaderNode::type_BACKGROUND: {
212                         node = new BackgroundNode();
213                         break;
214                 }
215                 case BL::ShaderNode::type_HOLDOUT: {
216                         node = new HoldoutNode();
217                         break;
218                 }
219                 case BL::ShaderNode::type_BSDF_ANISOTROPIC: {
220                         node = new WardBsdfNode();
221                         break;
222                 }
223                 case BL::ShaderNode::type_BSDF_DIFFUSE: {
224                         node = new DiffuseBsdfNode();
225                         break;
226                 }
227                 case BL::ShaderNode::type_BSDF_GLOSSY: {
228                         BL::ShaderNodeBsdfGlossy b_glossy_node(b_node);
229                         GlossyBsdfNode *glossy = new GlossyBsdfNode();
230
231                         switch(b_glossy_node.distribution()) {
232                                 case BL::ShaderNodeBsdfGlossy::distribution_SHARP:
233                                         glossy->distribution = ustring("Sharp");
234                                         break;
235                                 case BL::ShaderNodeBsdfGlossy::distribution_BECKMANN:
236                                         glossy->distribution = ustring("Beckmann");
237                                         break;
238                                 case BL::ShaderNodeBsdfGlossy::distribution_GGX:
239                                         glossy->distribution = ustring("GGX");
240                                         break;
241                         }
242                         node = glossy;
243                         break;
244                 }
245                 case BL::ShaderNode::type_BSDF_GLASS: {
246                         BL::ShaderNodeBsdfGlass b_glass_node(b_node);
247                         GlassBsdfNode *glass = new GlassBsdfNode();
248                         switch(b_glass_node.distribution()) {
249                                 case BL::ShaderNodeBsdfGlass::distribution_SHARP:
250                                         glass->distribution = ustring("Sharp");
251                                         break;
252                                 case BL::ShaderNodeBsdfGlass::distribution_BECKMANN:
253                                         glass->distribution = ustring("Beckmann");
254                                         break;
255                                 case BL::ShaderNodeBsdfGlass::distribution_GGX:
256                                         glass->distribution = ustring("GGX");
257                                         break;
258                         }
259                         node = glass;
260                         break;
261                 }
262                 case BL::ShaderNode::type_BSDF_TRANSLUCENT: {
263                         node = new TranslucentBsdfNode();
264                         break;
265                 }
266                 case BL::ShaderNode::type_BSDF_TRANSPARENT: {
267                         node = new TransparentBsdfNode();
268                         break;
269                 }
270                 case BL::ShaderNode::type_BSDF_VELVET: {
271                         node = new VelvetBsdfNode();
272                         break;
273                 }
274                 case BL::ShaderNode::type_EMISSION: {
275                         node = new EmissionNode();
276                         break;
277                 }
278                 case BL::ShaderNode::type_VOLUME_ISOTROPIC: {
279                         node = new IsotropicVolumeNode();
280                         break;
281                 }
282                 case BL::ShaderNode::type_VOLUME_TRANSPARENT: {
283                         node = new TransparentVolumeNode();
284                         break;
285                 }
286                 case BL::ShaderNode::type_GEOMETRY: {
287                         node = new GeometryNode();
288                         break;
289                 }
290                 case BL::ShaderNode::type_LIGHT_PATH: {
291                         node = new LightPathNode();
292                         break;
293                 }
294                 case BL::ShaderNode::type_TEX_IMAGE: {
295                         BL::ShaderNodeTexImage b_image_node(b_node);
296                         BL::Image b_image(b_image_node.image());
297                         ImageTextureNode *image = new ImageTextureNode();
298                         /* todo: handle generated/builtin images */
299                         if(b_image)
300                                 image->filename = blender_absolute_path(b_data, b_image, b_image.filepath());
301                         image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()];
302                         node = image;
303                         break;
304                 }
305                 case BL::ShaderNode::type_TEX_ENVIRONMENT: {
306                         BL::ShaderNodeTexEnvironment b_env_node(b_node);
307                         BL::Image b_image(b_env_node.image());
308                         EnvironmentTextureNode *env = new EnvironmentTextureNode();
309                         if(b_image)
310                                 env->filename = blender_absolute_path(b_data, b_image, b_image.filepath());
311                         env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
312                         node = env;
313                         break;
314                 }
315                 case BL::ShaderNode::type_TEX_NOISE: {
316                         node = new NoiseTextureNode();
317                         break;
318                 }
319                 case BL::ShaderNode::type_TEX_BLEND: {
320                         BL::ShaderNodeTexBlend b_blend_node(b_node);
321                         BlendTextureNode *blend = new BlendTextureNode();
322                         blend->progression = BlendTextureNode::progression_enum[(int)b_blend_node.progression()];
323                         blend->axis = BlendTextureNode::axis_enum[(int)b_blend_node.axis()];
324                         node = blend;
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->distance_metric = VoronoiTextureNode::distance_metric_enum[(int)b_voronoi_node.distance_metric()];
331                         voronoi->coloring = VoronoiTextureNode::coloring_enum[(int)b_voronoi_node.coloring()];
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                         node = magic;
340                         break;
341                 }
342                 case BL::ShaderNode::type_TEX_MARBLE: {
343                         BL::ShaderNodeTexMarble b_marble_node(b_node);
344                         MarbleTextureNode *marble = new MarbleTextureNode();
345                         marble->depth = b_marble_node.turbulence_depth();
346                         marble->basis = MarbleTextureNode::basis_enum[(int)b_marble_node.noise_basis()];
347                         marble->type = MarbleTextureNode::type_enum[(int)b_marble_node.marble_type()];
348                         marble->wave = MarbleTextureNode::wave_enum[(int)b_marble_node.wave_type()];
349                         marble->hard = b_marble_node.noise_type() == BL::ShaderNodeTexMarble::noise_type_HARD;
350                         node = marble;
351                         break;
352                 }
353                 case BL::ShaderNode::type_TEX_CLOUDS: {
354                         BL::ShaderNodeTexClouds b_clouds_node(b_node);
355                         CloudsTextureNode *clouds = new CloudsTextureNode();
356                         clouds->depth = b_clouds_node.turbulence_depth();
357                         clouds->basis = CloudsTextureNode::basis_enum[(int)b_clouds_node.noise_basis()];
358                         clouds->hard = b_clouds_node.noise_type() == BL::ShaderNodeTexClouds::noise_type_HARD;
359                         node = clouds;
360                         break;
361                 }
362                 case BL::ShaderNode::type_TEX_WOOD: {
363                         BL::ShaderNodeTexWood b_wood_node(b_node);
364                         WoodTextureNode *wood = new WoodTextureNode();
365                         wood->type = WoodTextureNode::type_enum[(int)b_wood_node.wood_type()];
366                         wood->basis = WoodTextureNode::basis_enum[(int)b_wood_node.noise_basis()];
367                         wood->hard = b_wood_node.noise_type() == BL::ShaderNodeTexWood::noise_type_HARD;
368                         wood->wave = WoodTextureNode::wave_enum[(int)b_wood_node.wave_type()];
369                         node = wood;
370                         break;
371                 }
372                 case BL::ShaderNode::type_TEX_MUSGRAVE: {
373                         BL::ShaderNodeTexMusgrave b_musgrave_node(b_node);
374                         MusgraveTextureNode *musgrave = new MusgraveTextureNode();
375                         musgrave->type = MusgraveTextureNode::type_enum[(int)b_musgrave_node.musgrave_type()];
376                         musgrave->basis = MusgraveTextureNode::basis_enum[(int)b_musgrave_node.noise_basis()];
377                         node = musgrave;
378                         break;
379                 }
380                 case BL::ShaderNode::type_TEX_STUCCI: {
381                         BL::ShaderNodeTexStucci b_stucci_node(b_node);
382                         StucciTextureNode *stucci = new StucciTextureNode();
383                         stucci->type = StucciTextureNode::type_enum[(int)b_stucci_node.stucci_type()];
384                         stucci->basis = StucciTextureNode::basis_enum[(int)b_stucci_node.noise_basis()];
385                         stucci->hard = b_stucci_node.noise_type() == BL::ShaderNodeTexStucci::noise_type_HARD;
386                         node = stucci;
387                         break;
388                 }
389                 case BL::ShaderNode::type_TEX_DISTORTED_NOISE: {
390                         BL::ShaderNodeTexDistortedNoise b_distnoise_node(b_node);
391                         DistortedNoiseTextureNode *distnoise = new DistortedNoiseTextureNode();
392                         distnoise->basis = DistortedNoiseTextureNode::basis_enum[(int)b_distnoise_node.noise_basis()];
393                         distnoise->distortion_basis = DistortedNoiseTextureNode::basis_enum[(int)b_distnoise_node.noise_distortion()];
394                         node = distnoise;
395                         break;
396                 }
397                 case BL::ShaderNode::type_TEX_COORD: {
398                         node = new TextureCoordinateNode();;
399                         break;
400                 }
401                 case BL::ShaderNode::type_TEX_SKY: {
402                         BL::ShaderNodeTexSky b_sky_node(b_node);
403                         SkyTextureNode *sky = new SkyTextureNode();
404                         sky->sun_direction = get_float3(b_sky_node.sun_direction());
405                         sky->turbidity = b_sky_node.turbidity();
406                         node = sky;
407                         break;
408                 }
409         }
410
411         if(node && node != graph->output())
412                 graph->add(node);
413
414         return node;
415 }
416
417 static const char *node_socket_map_name(const char *name)
418 {
419         if(strstr(name, "Shader")) {
420                 if(strcmp(name, "Shader") == 0)
421                         return "Closure";
422                 if(strcmp(name, "Shader1") == 0)
423                         return "Closure1";
424                 if(strcmp(name, "Shader2") == 0)
425                         return "Closure2";
426         }
427
428         return name;
429 }
430
431 static void add_nodes(BL::BlendData b_data, ShaderGraph *graph, BL::ShaderNodeTree b_ntree, BL::Node *b_group_node, PtrSockMap& sockets_map)
432 {
433         /* add nodes */
434         BL::ShaderNodeTree::nodes_iterator b_node;
435         PtrNodeMap node_map;
436         map<void*, PtrSockMap> node_groups;
437
438         for(b_ntree.nodes.begin(b_node); b_node != b_ntree.nodes.end(); ++b_node) {
439                 if(b_node->is_a(&RNA_NodeGroup)) {
440                         BL::NodeGroup b_gnode(*b_node);
441                         BL::ShaderNodeTree b_group_ntree(b_gnode.node_tree());
442
443                         node_groups[b_node->ptr.data] = PtrSockMap();
444                         add_nodes(b_data, graph, b_group_ntree, &b_gnode, node_groups[b_node->ptr.data]);
445                 }
446                 else {
447                         ShaderNode *node = add_node(b_data, graph, b_group_node, BL::ShaderNode(*b_node));
448
449                         if(node) {
450                                 BL::Node::inputs_iterator b_input;
451                                 BL::Node::outputs_iterator b_output;
452
453                                 node_map[b_node->ptr.data] = node;
454
455                                 for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
456                                         ShaderInput *input = node->input(node_socket_map_name(b_input->name().c_str()));
457                                         BL::NodeSocket sock(get_node_input(b_group_node, *b_node, b_input->name()));
458
459                                         assert(input);
460
461                                         /* copy values for non linked inputs */
462                                         switch(input->type) {
463                                                 case SHADER_SOCKET_FLOAT: {
464                                                         BL::NodeSocketFloatNone value_sock(sock);
465                                                         input->set(value_sock.default_value());
466                                                         break;
467                                                 }
468                                                 case SHADER_SOCKET_COLOR: {
469                                                         BL::NodeSocketRGBA rgba_sock(sock);
470                                                         input->set(get_float3(rgba_sock.default_value()));
471                                                         break;
472                                                 }
473                                                 case SHADER_SOCKET_NORMAL:
474                                                 case SHADER_SOCKET_POINT:
475                                                 case SHADER_SOCKET_VECTOR: {
476                                                         BL::NodeSocketVectorNone vec_sock(sock);
477                                                         input->set(get_float3(vec_sock.default_value()));
478                                                         break;
479                                                 }
480                                                 case SHADER_SOCKET_CLOSURE:
481                                                         break;
482                                         }
483                                 }
484                         }
485                 }
486         }
487
488         /* connect nodes */
489         BL::NodeTree::links_iterator b_link;
490
491         for(b_ntree.links.begin(b_link); b_link != b_ntree.links.end(); ++b_link) {
492                 /* get blender link data */
493                 BL::Node b_from_node = b_link->from_node();
494                 BL::Node b_to_node = b_link->to_node();
495
496                 BL::NodeSocket b_from_sock = b_link->from_socket();
497                 BL::NodeSocket b_to_sock = b_link->to_socket();
498
499                 /* if link with group socket, add to map so we can connect it later */
500                 if(b_group_node) {
501                         if(!b_from_node) {
502                                 sockets_map[b_from_sock.ptr.data] =
503                                         SocketPair(node_map[b_to_node.ptr.data], b_to_sock.name());
504
505                                 continue;
506                         }
507                         else if(!b_to_node) {
508                                 sockets_map[b_to_sock.ptr.data] =
509                                         SocketPair(node_map[b_from_node.ptr.data], b_from_sock.name());
510
511                                 continue;
512                         }
513                 }
514
515                 ShaderNode *from_node, *to_node;
516                 string from_name, to_name;
517
518                 /* from sock */
519                 if(b_from_node.is_a(&RNA_NodeGroup)) {
520                         /* group node */
521                         BL::NodeSocket group_sock = b_from_sock.group_socket();
522                         SocketPair& pair = node_groups[b_from_node.ptr.data][group_sock.ptr.data];
523
524                         from_node = pair.first;
525                         from_name = pair.second;
526                 }
527                 else {
528                         /* regular node */
529                         from_node = node_map[b_from_node.ptr.data];
530                         from_name = b_from_sock.name();
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                         SocketPair& pair = node_groups[b_to_node.ptr.data][group_sock.ptr.data];
538
539                         to_node = pair.first;
540                         to_name = pair.second;
541                 }
542                 else {
543                         /* regular node */
544                         to_node = node_map[b_to_node.ptr.data];
545                         to_name = b_to_sock.name();
546                 }
547
548                 /* in case of groups there may not actually be a node inside the group
549                    that the group socket connects to, so from_node or to_node may be NULL */
550                 if(from_node && to_node)
551                         graph->connect(from_node->output(node_socket_map_name(from_name.c_str())), to_node->input(node_socket_map_name(to_name.c_str())));
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();
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();
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