Invert Color Cycles Node
[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         if(!b_mapping)
103                 return;
104
105         mapping->translation = get_float3(b_mapping.location());
106         mapping->rotation = get_float3(b_mapping.rotation());
107         mapping->scale = get_float3(b_mapping.scale());
108
109         mapping->x_mapping = (TextureMapping::Mapping)b_mapping.mapping_x();
110         mapping->y_mapping = (TextureMapping::Mapping)b_mapping.mapping_y();
111         mapping->z_mapping = (TextureMapping::Mapping)b_mapping.mapping_z();
112 }
113
114 static void get_tex_mapping(TextureMapping *mapping, BL::ShaderNodeMapping b_mapping)
115 {
116         if(!b_mapping)
117                 return;
118
119         mapping->translation = get_float3(b_mapping.location());
120         mapping->rotation = get_float3(b_mapping.rotation());
121         mapping->scale = get_float3(b_mapping.scale());
122 }
123
124 static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *b_group_node, BL::ShaderNode b_node)
125 {
126         ShaderNode *node = NULL;
127
128         switch(b_node.type()) {
129                 /* not supported */
130                 case BL::ShaderNode::type_CURVE_RGB: break;
131                 case BL::ShaderNode::type_CURVE_VEC: break;
132                 case BL::ShaderNode::type_GEOMETRY: break;
133                 case BL::ShaderNode::type_MATERIAL: break;
134                 case BL::ShaderNode::type_MATERIAL_EXT: break;
135                 case BL::ShaderNode::type_NORMAL: break;
136                 case BL::ShaderNode::type_OUTPUT: break;
137                 case BL::ShaderNode::type_SCRIPT: break;
138                 case BL::ShaderNode::type_SQUEEZE: break;
139                 case BL::ShaderNode::type_TEXTURE: break;
140                 case BL::ShaderNode::type_VALTORGB: break;
141                 /* handled outside this function */
142                 case BL::ShaderNode::type_GROUP: break;
143                 /* existing blender nodes */
144                 case BL::ShaderNode::type_RGB: {
145                         ColorNode *color = new ColorNode();
146                         color->value = get_node_output_rgba(b_node, "Color");
147                         node = color;
148                         break;
149                 }
150                 case BL::ShaderNode::type_VALUE: {
151                         ValueNode *value = new ValueNode();
152                         value->value = get_node_output_value(b_node, "Value");
153                         node = value;
154                         break;
155                 }
156                 case BL::ShaderNode::type_CAMERA: {
157                         node = new CameraNode();
158                         break;
159                 }
160                 case BL::ShaderNode::type_INVERT: {
161                         node = new InvertNode();
162                         break;
163                 }
164                 case BL::ShaderNode::type_MIX_RGB: {
165                         BL::ShaderNodeMixRGB b_mix_node(b_node);
166                         MixNode *mix = new MixNode();
167                         mix->type = MixNode::type_enum[b_mix_node.blend_type()];
168                         node = mix;
169                         break;
170                 }
171                 case BL::ShaderNode::type_SEPRGB: {
172                         node = new SeparateRGBNode();
173                         break;
174                 }
175                 case BL::ShaderNode::type_COMBRGB: {
176                         node = new CombineRGBNode();
177                         break;
178                 }
179                 case BL::ShaderNode::type_HUE_SAT: {
180                         node = new HSVNode();
181                         break;
182                 }
183                 case BL::ShaderNode::type_RGBTOBW: {
184                         node = new ConvertNode(SHADER_SOCKET_COLOR, SHADER_SOCKET_FLOAT);
185                         break;
186                 }
187                 case BL::ShaderNode::type_MATH: {
188                         BL::ShaderNodeMath b_math_node(b_node);
189                         MathNode *math = new MathNode();
190                         math->type = MathNode::type_enum[b_math_node.operation()];
191                         node = math;
192                         break;
193                 }
194                 case BL::ShaderNode::type_VECT_MATH: {
195                         BL::ShaderNodeVectorMath b_vector_math_node(b_node);
196                         VectorMathNode *vmath = new VectorMathNode();
197                         vmath->type = VectorMathNode::type_enum[b_vector_math_node.operation()];
198                         node = vmath;
199                         break;
200                 }
201                 case BL::ShaderNode::type_MAPPING: {
202                         BL::ShaderNodeMapping b_mapping_node(b_node);
203                         MappingNode *mapping = new MappingNode();
204
205                         get_tex_mapping(&mapping->tex_mapping, b_mapping_node);
206
207                         node = mapping;
208                         break;
209                 }
210
211                 /* new nodes */
212                 case BL::ShaderNode::type_OUTPUT_MATERIAL:
213                 case BL::ShaderNode::type_OUTPUT_WORLD:
214                 case BL::ShaderNode::type_OUTPUT_LAMP: {
215                         node = graph->output();
216                         break;
217                 }
218                 case BL::ShaderNode::type_FRESNEL: {
219                         node = new FresnelNode();
220                         break;
221                 }
222                 case BL::ShaderNode::type_LAYER_WEIGHT: {
223                         node = new LayerWeightNode();
224                         break;
225                 }
226                 case BL::ShaderNode::type_ADD_SHADER: {
227                         node = new AddClosureNode();
228                         break;
229                 }
230                 case BL::ShaderNode::type_MIX_SHADER: {
231                         node = new MixClosureNode();
232                         break;
233                 }
234                 case BL::ShaderNode::type_ATTRIBUTE: {
235                         BL::ShaderNodeAttribute b_attr_node(b_node);
236                         AttributeNode *attr = new AttributeNode();
237                         attr->attribute = b_attr_node.attribute_name();
238                         node = attr;
239                         break;
240                 }
241                 case BL::ShaderNode::type_BACKGROUND: {
242                         node = new BackgroundNode();
243                         break;
244                 }
245                 case BL::ShaderNode::type_HOLDOUT: {
246                         node = new HoldoutNode();
247                         break;
248                 }
249                 case BL::ShaderNode::type_BSDF_DIFFUSE: {
250                         node = new DiffuseBsdfNode();
251                         break;
252                 }
253                 case BL::ShaderNode::type_BSDF_GLOSSY: {
254                         BL::ShaderNodeBsdfGlossy b_glossy_node(b_node);
255                         GlossyBsdfNode *glossy = new GlossyBsdfNode();
256
257                         switch(b_glossy_node.distribution()) {
258                                 case BL::ShaderNodeBsdfGlossy::distribution_SHARP:
259                                         glossy->distribution = ustring("Sharp");
260                                         break;
261                                 case BL::ShaderNodeBsdfGlossy::distribution_BECKMANN:
262                                         glossy->distribution = ustring("Beckmann");
263                                         break;
264                                 case BL::ShaderNodeBsdfGlossy::distribution_GGX:
265                                         glossy->distribution = ustring("GGX");
266                                         break;
267                         }
268                         node = glossy;
269                         break;
270                 }
271                 case BL::ShaderNode::type_BSDF_GLASS: {
272                         BL::ShaderNodeBsdfGlass b_glass_node(b_node);
273                         GlassBsdfNode *glass = new GlassBsdfNode();
274                         switch(b_glass_node.distribution()) {
275                                 case BL::ShaderNodeBsdfGlass::distribution_SHARP:
276                                         glass->distribution = ustring("Sharp");
277                                         break;
278                                 case BL::ShaderNodeBsdfGlass::distribution_BECKMANN:
279                                         glass->distribution = ustring("Beckmann");
280                                         break;
281                                 case BL::ShaderNodeBsdfGlass::distribution_GGX:
282                                         glass->distribution = ustring("GGX");
283                                         break;
284                         }
285                         node = glass;
286                         break;
287                 }
288                 case BL::ShaderNode::type_BSDF_TRANSLUCENT: {
289                         node = new TranslucentBsdfNode();
290                         break;
291                 }
292                 case BL::ShaderNode::type_BSDF_TRANSPARENT: {
293                         node = new TransparentBsdfNode();
294                         break;
295                 }
296                 case BL::ShaderNode::type_BSDF_VELVET: {
297                         node = new VelvetBsdfNode();
298                         break;
299                 }
300                 case BL::ShaderNode::type_EMISSION: {
301                         node = new EmissionNode();
302                         break;
303                 }
304                 case BL::ShaderNode::type_VOLUME_ISOTROPIC: {
305                         node = new IsotropicVolumeNode();
306                         break;
307                 }
308                 case BL::ShaderNode::type_VOLUME_TRANSPARENT: {
309                         node = new TransparentVolumeNode();
310                         break;
311                 }
312                 case BL::ShaderNode::type_NEW_GEOMETRY: {
313                         node = new GeometryNode();
314                         break;
315                 }
316                 case BL::ShaderNode::type_LIGHT_PATH: {
317                         node = new LightPathNode();
318                         break;
319                 }
320                 case BL::ShaderNode::type_TEX_IMAGE: {
321                         BL::ShaderNodeTexImage b_image_node(b_node);
322                         BL::Image b_image(b_image_node.image());
323                         ImageTextureNode *image = new ImageTextureNode();
324                         /* todo: handle generated/builtin images */
325                         if(b_image)
326                                 image->filename = blender_absolute_path(b_data, b_image, b_image.filepath());
327                         image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()];
328                         get_tex_mapping(&image->tex_mapping, b_image_node.texture_mapping());
329                         node = image;
330                         break;
331                 }
332                 case BL::ShaderNode::type_TEX_ENVIRONMENT: {
333                         BL::ShaderNodeTexEnvironment b_env_node(b_node);
334                         BL::Image b_image(b_env_node.image());
335                         EnvironmentTextureNode *env = new EnvironmentTextureNode();
336                         if(b_image)
337                                 env->filename = blender_absolute_path(b_data, b_image, b_image.filepath());
338                         env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
339                         get_tex_mapping(&env->tex_mapping, b_env_node.texture_mapping());
340                         node = env;
341                         break;
342                 }
343                 case BL::ShaderNode::type_TEX_GRADIENT: {
344                         BL::ShaderNodeTexGradient b_gradient_node(b_node);
345                         GradientTextureNode *gradient = new GradientTextureNode();
346                         gradient->type = GradientTextureNode::type_enum[(int)b_gradient_node.gradient_type()];
347                         get_tex_mapping(&gradient->tex_mapping, b_gradient_node.texture_mapping());
348                         node = gradient;
349                         break;
350                 }
351                 case BL::ShaderNode::type_TEX_VORONOI: {
352                         BL::ShaderNodeTexVoronoi b_voronoi_node(b_node);
353                         VoronoiTextureNode *voronoi = new VoronoiTextureNode();
354                         voronoi->coloring = VoronoiTextureNode::coloring_enum[(int)b_voronoi_node.coloring()];
355                         get_tex_mapping(&voronoi->tex_mapping, b_voronoi_node.texture_mapping());
356                         node = voronoi;
357                         break;
358                 }
359                 case BL::ShaderNode::type_TEX_MAGIC: {
360                         BL::ShaderNodeTexMagic b_magic_node(b_node);
361                         MagicTextureNode *magic = new MagicTextureNode();
362                         magic->depth = b_magic_node.turbulence_depth();
363                         get_tex_mapping(&magic->tex_mapping, b_magic_node.texture_mapping());
364                         node = magic;
365                         break;
366                 }
367                 case BL::ShaderNode::type_TEX_WAVE: {
368                         BL::ShaderNodeTexWave b_wave_node(b_node);
369                         WaveTextureNode *wave = new WaveTextureNode();
370                         wave->type = WaveTextureNode::type_enum[(int)b_wave_node.wave_type()];
371                         get_tex_mapping(&wave->tex_mapping, b_wave_node.texture_mapping());
372                         node = wave;
373                         break;
374                 }
375                 case BL::ShaderNode::type_TEX_NOISE: {
376                         BL::ShaderNodeTexNoise b_noise_node(b_node);
377                         NoiseTextureNode *noise = new NoiseTextureNode();
378                         get_tex_mapping(&noise->tex_mapping, b_noise_node.texture_mapping());
379                         node = noise;
380                         break;
381                 }
382                 case BL::ShaderNode::type_TEX_MUSGRAVE: {
383                         BL::ShaderNodeTexMusgrave b_musgrave_node(b_node);
384                         MusgraveTextureNode *musgrave = new MusgraveTextureNode();
385                         musgrave->type = MusgraveTextureNode::type_enum[(int)b_musgrave_node.musgrave_type()];
386                         get_tex_mapping(&musgrave->tex_mapping, b_musgrave_node.texture_mapping());
387                         node = musgrave;
388                         break;
389                 }
390                 case BL::ShaderNode::type_TEX_COORD: {
391                         node = new TextureCoordinateNode();;
392                         break;
393                 }
394                 case BL::ShaderNode::type_TEX_SKY: {
395                         BL::ShaderNodeTexSky b_sky_node(b_node);
396                         SkyTextureNode *sky = new SkyTextureNode();
397                         sky->sun_direction = get_float3(b_sky_node.sun_direction());
398                         sky->turbidity = b_sky_node.turbidity();
399                         get_tex_mapping(&sky->tex_mapping, b_sky_node.texture_mapping());
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                                 float strength = 1.0f;
697
698                                 if(b_lamp->type() == BL::Lamp::type_POINT ||
699                                    b_lamp->type() == BL::Lamp::type_SPOT ||
700                                    b_lamp->type() == BL::Lamp::type_AREA)
701                                         strength = 100.0f;
702
703                                 closure = graph->add(new EmissionNode());
704                                 closure->input("Color")->value = get_float3(b_lamp->color());
705                                 closure->input("Strength")->value.x = strength;
706                                 out = graph->output();
707
708                                 graph->connect(closure->output("Emission"), out->input("Surface"));
709                         }
710
711                         shader->set_graph(graph);
712                         shader->tag_update(scene);
713                 }
714         }
715 }
716
717 void BlenderSync::sync_shaders()
718 {
719         shader_map.pre_sync();
720
721         sync_world();
722         sync_lamps();
723         sync_materials();
724
725         /* false = don't delete unused shaders, not supported */
726         shader_map.post_sync(false);
727 }
728
729 CCL_NAMESPACE_END
730