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