Cycles: Initial support for volume ray visibility.
[blender.git] / intern / cycles / blender / blender_shader.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16
17 #include "background.h"
18 #include "graph.h"
19 #include "light.h"
20 #include "nodes.h"
21 #include "osl.h"
22 #include "scene.h"
23 #include "shader.h"
24
25 #include "blender_sync.h"
26 #include "blender_util.h"
27
28 #include "util_debug.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 typedef map<void*, ShaderInput*> PtrInputMap;
33 typedef map<void*, ShaderOutput*> PtrOutputMap;
34 typedef map<std::string, ProxyNode*> ProxyMap;
35
36 /* Find */
37
38 void BlenderSync::find_shader(BL::ID id, vector<uint>& used_shaders, int default_shader)
39 {
40         Shader *shader = (id)? shader_map.find(id): scene->shaders[default_shader];
41
42         for(size_t i = 0; i < scene->shaders.size(); i++) {
43                 if(scene->shaders[i] == shader) {
44                         used_shaders.push_back(i);
45                         scene->shaders[i]->tag_used(scene);
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::NodeSocket b_sock = get_node_output(b_node, name);
69         float value[4];
70         RNA_float_get_array(&b_sock.ptr, "default_value", value);
71         return make_float3(value[0], value[1], value[2]);
72 }
73
74 static float get_node_output_value(BL::Node b_node, const string& name)
75 {
76         BL::NodeSocket b_sock = get_node_output(b_node, name);
77         return RNA_float_get(&b_sock.ptr, "default_value");
78 }
79
80 static float3 get_node_output_vector(BL::Node b_node, const string& name)
81 {
82         BL::NodeSocket b_sock = get_node_output(b_node, name);
83         float value[3];
84         RNA_float_get_array(&b_sock.ptr, "default_value", value);
85         return make_float3(value[0], value[1], value[2]);
86 }
87
88 static ShaderSocketType convert_socket_type(BL::NodeSocket b_socket)
89 {
90         switch (b_socket.type()) {
91                 case BL::NodeSocket::type_VALUE:
92                         return SHADER_SOCKET_FLOAT;
93                 case BL::NodeSocket::type_INT:
94                         return SHADER_SOCKET_INT;
95                 case BL::NodeSocket::type_VECTOR:
96                         return SHADER_SOCKET_VECTOR;
97                 case BL::NodeSocket::type_RGBA:
98                         return SHADER_SOCKET_COLOR;
99                 case BL::NodeSocket::type_STRING:
100                         return SHADER_SOCKET_STRING;
101                 case BL::NodeSocket::type_SHADER:
102                         return SHADER_SOCKET_CLOSURE;
103                 
104                 default:
105                         return SHADER_SOCKET_UNDEFINED;
106         }
107 }
108
109 static void set_default_value(ShaderInput *input, BL::Node b_node, BL::NodeSocket b_sock, BL::BlendData b_data, BL::ID b_id)
110 {
111         /* copy values for non linked inputs */
112         switch(input->type) {
113         case SHADER_SOCKET_FLOAT: {
114                 input->set(get_float(b_sock.ptr, "default_value"));
115                 break;
116         }
117         case SHADER_SOCKET_INT: {
118                 input->set((float)get_int(b_sock.ptr, "default_value"));
119                 break;
120         }
121         case SHADER_SOCKET_COLOR: {
122                 input->set(float4_to_float3(get_float4(b_sock.ptr, "default_value")));
123                 break;
124         }
125         case SHADER_SOCKET_NORMAL:
126         case SHADER_SOCKET_POINT:
127         case SHADER_SOCKET_VECTOR: {
128                 input->set(get_float3(b_sock.ptr, "default_value"));
129                 break;
130         }
131         case SHADER_SOCKET_STRING: {
132                 input->set((ustring)blender_absolute_path(b_data, b_id, get_string(b_sock.ptr, "default_value")));
133                 break;
134         }
135         
136         case SHADER_SOCKET_CLOSURE:
137         case SHADER_SOCKET_UNDEFINED:
138                 break;
139         }
140 }
141
142 static void get_tex_mapping(TextureMapping *mapping, BL::TexMapping b_mapping)
143 {
144         if(!b_mapping)
145                 return;
146
147         mapping->translation = get_float3(b_mapping.translation());
148         mapping->rotation = get_float3(b_mapping.rotation());
149         mapping->scale = get_float3(b_mapping.scale());
150         mapping->type = (TextureMapping::Type)b_mapping.vector_type();
151
152         mapping->x_mapping = (TextureMapping::Mapping)b_mapping.mapping_x();
153         mapping->y_mapping = (TextureMapping::Mapping)b_mapping.mapping_y();
154         mapping->z_mapping = (TextureMapping::Mapping)b_mapping.mapping_z();
155 }
156
157 static void get_tex_mapping(TextureMapping *mapping, BL::ShaderNodeMapping b_mapping)
158 {
159         if(!b_mapping)
160                 return;
161
162         mapping->translation = get_float3(b_mapping.translation());
163         mapping->rotation = get_float3(b_mapping.rotation());
164         mapping->scale = get_float3(b_mapping.scale());
165         mapping->type = (TextureMapping::Type)b_mapping.vector_type();
166
167         mapping->use_minmax = b_mapping.use_min() || b_mapping.use_max();
168
169         if(b_mapping.use_min())
170                 mapping->min = get_float3(b_mapping.min());
171         if(b_mapping.use_max())
172                 mapping->max = get_float3(b_mapping.max());
173 }
174
175 static bool is_output_node(BL::Node b_node)
176 {
177         return (b_node.is_a(&RNA_ShaderNodeOutputMaterial)
178                     || b_node.is_a(&RNA_ShaderNodeOutputWorld)
179                     || b_node.is_a(&RNA_ShaderNodeOutputLamp));
180 }
181
182 static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree, BL::ShaderNode b_node)
183 {
184         ShaderNode *node = NULL;
185
186         /* existing blender nodes */
187         if (b_node.is_a(&RNA_ShaderNodeRGBCurve)) {
188                 BL::ShaderNodeRGBCurve b_curve_node(b_node);
189                 RGBCurvesNode *curves = new RGBCurvesNode();
190                 curvemapping_color_to_array(b_curve_node.mapping(), curves->curves, RAMP_TABLE_SIZE, true);
191                 node = curves;
192         }
193         if (b_node.is_a(&RNA_ShaderNodeVectorCurve)) {
194                 BL::ShaderNodeVectorCurve b_curve_node(b_node);
195                 VectorCurvesNode *curves = new VectorCurvesNode();
196                 curvemapping_color_to_array(b_curve_node.mapping(), curves->curves, RAMP_TABLE_SIZE, false);
197                 node = curves;
198         }
199         else if (b_node.is_a(&RNA_ShaderNodeValToRGB)) {
200                 RGBRampNode *ramp = new RGBRampNode();
201                 BL::ShaderNodeValToRGB b_ramp_node(b_node);
202                 colorramp_to_array(b_ramp_node.color_ramp(), ramp->ramp, RAMP_TABLE_SIZE);
203                 ramp->interpolate = b_ramp_node.color_ramp().interpolation() != BL::ColorRamp::interpolation_CONSTANT;
204                 node = ramp;
205         }
206         else if (b_node.is_a(&RNA_ShaderNodeRGB)) {
207                 ColorNode *color = new ColorNode();
208                 color->value = get_node_output_rgba(b_node, "Color");
209                 node = color;
210         }
211         else if (b_node.is_a(&RNA_ShaderNodeValue)) {
212                 ValueNode *value = new ValueNode();
213                 value->value = get_node_output_value(b_node, "Value");
214                 node = value;
215         }
216         else if (b_node.is_a(&RNA_ShaderNodeCameraData)) {
217                 node = new CameraNode();
218         }
219         else if (b_node.is_a(&RNA_ShaderNodeInvert)) {
220                 node = new InvertNode();
221         }
222         else if (b_node.is_a(&RNA_ShaderNodeGamma)) {
223                 node = new GammaNode();
224         }
225         else if (b_node.is_a(&RNA_ShaderNodeBrightContrast)) {
226                 node = new BrightContrastNode();
227         }
228         else if (b_node.is_a(&RNA_ShaderNodeMixRGB)) {
229                 BL::ShaderNodeMixRGB b_mix_node(b_node);
230                 MixNode *mix = new MixNode();
231                 mix->type = MixNode::type_enum[b_mix_node.blend_type()];
232                         mix->use_clamp = b_mix_node.use_clamp();
233                 node = mix;
234         }
235         else if (b_node.is_a(&RNA_ShaderNodeSeparateRGB)) {
236                 node = new SeparateRGBNode();
237         }
238         else if (b_node.is_a(&RNA_ShaderNodeCombineRGB)) {
239                 node = new CombineRGBNode();
240         }
241         else if (b_node.is_a(&RNA_ShaderNodeSeparateHSV)) {
242                 node = new SeparateHSVNode();
243         }
244         else if (b_node.is_a(&RNA_ShaderNodeCombineHSV)) {
245                 node = new CombineHSVNode();
246         }
247         else if (b_node.is_a(&RNA_ShaderNodeSeparateXYZ)) {
248                 node = new SeparateXYZNode();
249         }
250         else if (b_node.is_a(&RNA_ShaderNodeCombineXYZ)) {
251                 node = new CombineXYZNode();
252         }
253         else if (b_node.is_a(&RNA_ShaderNodeHueSaturation)) {
254                 node = new HSVNode();
255         }
256         else if (b_node.is_a(&RNA_ShaderNodeRGBToBW)) {
257                 node = new ConvertNode(SHADER_SOCKET_COLOR, SHADER_SOCKET_FLOAT);
258         }
259         else if (b_node.is_a(&RNA_ShaderNodeMath)) {
260                 BL::ShaderNodeMath b_math_node(b_node);
261                 MathNode *math = new MathNode();
262                 math->type = MathNode::type_enum[b_math_node.operation()];
263                         math->use_clamp = b_math_node.use_clamp();
264                 node = math;
265         }
266         else if (b_node.is_a(&RNA_ShaderNodeVectorMath)) {
267                 BL::ShaderNodeVectorMath b_vector_math_node(b_node);
268                 VectorMathNode *vmath = new VectorMathNode();
269                 vmath->type = VectorMathNode::type_enum[b_vector_math_node.operation()];
270                 node = vmath;
271         }
272         else if (b_node.is_a(&RNA_ShaderNodeVectorTransform)) {
273                 BL::ShaderNodeVectorTransform b_vector_transform_node(b_node);
274                 VectorTransformNode *vtransform = new VectorTransformNode();
275                 vtransform->type = VectorTransformNode::type_enum[b_vector_transform_node.type()];
276                 vtransform->convert_from = VectorTransformNode::convert_space_enum[b_vector_transform_node.convert_from()];
277                 vtransform->convert_to = VectorTransformNode::convert_space_enum[b_vector_transform_node.convert_to()];
278                 node = vtransform;
279         }
280         else if (b_node.is_a(&RNA_ShaderNodeNormal)) {
281                 BL::Node::outputs_iterator out_it;
282                 b_node.outputs.begin(out_it);
283                 
284                 NormalNode *norm = new NormalNode();
285                 norm->direction = get_node_output_vector(b_node, "Normal");
286                 node = norm;
287         }
288         else if (b_node.is_a(&RNA_ShaderNodeMapping)) {
289                 BL::ShaderNodeMapping b_mapping_node(b_node);
290                 MappingNode *mapping = new MappingNode();
291                 
292                 get_tex_mapping(&mapping->tex_mapping, b_mapping_node);
293                 
294                 node = mapping;
295         }
296         else if (b_node.is_a(&RNA_ShaderNodeFresnel)) {
297                 node = new FresnelNode();
298         }
299         else if (b_node.is_a(&RNA_ShaderNodeLayerWeight)) {
300                 node = new LayerWeightNode();
301         }
302         else if (b_node.is_a(&RNA_ShaderNodeAddShader)) {
303                 node = new AddClosureNode();
304         }
305         else if (b_node.is_a(&RNA_ShaderNodeMixShader)) {
306                 node = new MixClosureNode();
307         }
308         else if (b_node.is_a(&RNA_ShaderNodeAttribute)) {
309                 BL::ShaderNodeAttribute b_attr_node(b_node);
310                 AttributeNode *attr = new AttributeNode();
311                 attr->attribute = b_attr_node.attribute_name();
312                 node = attr;
313         }
314         else if (b_node.is_a(&RNA_ShaderNodeBackground)) {
315                 node = new BackgroundNode();
316         }
317         else if (b_node.is_a(&RNA_ShaderNodeHoldout)) {
318                 node = new HoldoutNode();
319         }
320         else if (b_node.is_a(&RNA_ShaderNodeBsdfAnisotropic)) {
321                 BL::ShaderNodeBsdfAnisotropic b_aniso_node(b_node);
322                 AnisotropicBsdfNode *aniso = new AnisotropicBsdfNode();
323
324                 switch (b_aniso_node.distribution())
325                 {
326                 case BL::ShaderNodeBsdfAnisotropic::distribution_BECKMANN:
327                         aniso->distribution = ustring("Beckmann");
328                         break;
329                 case BL::ShaderNodeBsdfAnisotropic::distribution_GGX:
330                         aniso->distribution = ustring("GGX");
331                         break;
332                 case BL::ShaderNodeBsdfAnisotropic::distribution_ASHIKHMIN_SHIRLEY:
333                         aniso->distribution = ustring("Ashikhmin-Shirley");
334                         break;
335                 }
336
337                 node = aniso;
338         }
339         else if (b_node.is_a(&RNA_ShaderNodeBsdfDiffuse)) {
340                 node = new DiffuseBsdfNode();
341         }
342         else if (b_node.is_a(&RNA_ShaderNodeSubsurfaceScattering)) {
343                 BL::ShaderNodeSubsurfaceScattering b_subsurface_node(b_node);
344
345                 SubsurfaceScatteringNode *subsurface = new SubsurfaceScatteringNode();
346
347                 switch(b_subsurface_node.falloff()) {
348                 case BL::ShaderNodeSubsurfaceScattering::falloff_CUBIC:
349                         subsurface->closure = CLOSURE_BSSRDF_CUBIC_ID;
350                         break;
351                 case BL::ShaderNodeSubsurfaceScattering::falloff_GAUSSIAN:
352                         subsurface->closure = CLOSURE_BSSRDF_GAUSSIAN_ID;
353                         break;
354                 }
355
356                 node = subsurface;
357         }
358         else if (b_node.is_a(&RNA_ShaderNodeBsdfGlossy)) {
359                 BL::ShaderNodeBsdfGlossy b_glossy_node(b_node);
360                 GlossyBsdfNode *glossy = new GlossyBsdfNode();
361                 
362                 switch(b_glossy_node.distribution()) {
363                 case BL::ShaderNodeBsdfGlossy::distribution_SHARP:
364                         glossy->distribution = ustring("Sharp");
365                         break;
366                 case BL::ShaderNodeBsdfGlossy::distribution_BECKMANN:
367                         glossy->distribution = ustring("Beckmann");
368                         break;
369                 case BL::ShaderNodeBsdfGlossy::distribution_GGX:
370                         glossy->distribution = ustring("GGX");
371                         break;
372                 case BL::ShaderNodeBsdfGlossy::distribution_ASHIKHMIN_SHIRLEY:
373                         glossy->distribution = ustring("Ashikhmin-Shirley");
374                         break;
375                 }
376                 node = glossy;
377         }
378         else if (b_node.is_a(&RNA_ShaderNodeBsdfGlass)) {
379                 BL::ShaderNodeBsdfGlass b_glass_node(b_node);
380                 GlassBsdfNode *glass = new GlassBsdfNode();
381                 switch(b_glass_node.distribution()) {
382                 case BL::ShaderNodeBsdfGlass::distribution_SHARP:
383                         glass->distribution = ustring("Sharp");
384                         break;
385                 case BL::ShaderNodeBsdfGlass::distribution_BECKMANN:
386                         glass->distribution = ustring("Beckmann");
387                         break;
388                 case BL::ShaderNodeBsdfGlass::distribution_GGX:
389                         glass->distribution = ustring("GGX");
390                         break;
391                 }
392                 node = glass;
393         }
394         else if (b_node.is_a(&RNA_ShaderNodeBsdfRefraction)) {
395                 BL::ShaderNodeBsdfRefraction b_refraction_node(b_node);
396                 RefractionBsdfNode *refraction = new RefractionBsdfNode();
397                 switch(b_refraction_node.distribution()) {
398                         case BL::ShaderNodeBsdfRefraction::distribution_SHARP:
399                                 refraction->distribution = ustring("Sharp");
400                                 break;
401                         case BL::ShaderNodeBsdfRefraction::distribution_BECKMANN:
402                                 refraction->distribution = ustring("Beckmann");
403                                 break;
404                         case BL::ShaderNodeBsdfRefraction::distribution_GGX:
405                                 refraction->distribution = ustring("GGX");
406                                 break;
407                 }
408                 node = refraction;
409         }
410         else if (b_node.is_a(&RNA_ShaderNodeBsdfToon)) {
411                 BL::ShaderNodeBsdfToon b_toon_node(b_node);
412                 ToonBsdfNode *toon = new ToonBsdfNode();
413                 switch(b_toon_node.component()) {
414                         case BL::ShaderNodeBsdfToon::component_DIFFUSE:
415                                 toon->component = ustring("Diffuse");
416                                 break;
417                         case BL::ShaderNodeBsdfToon::component_GLOSSY:
418                                 toon->component = ustring("Glossy");
419                                 break;
420                 }
421                 node = toon;
422         }
423         else if (b_node.is_a(&RNA_ShaderNodeBsdfHair)) {
424                 BL::ShaderNodeBsdfHair b_hair_node(b_node);
425                 HairBsdfNode *hair = new HairBsdfNode();
426                 switch(b_hair_node.component()) {
427                         case BL::ShaderNodeBsdfHair::component_Reflection:
428                                 hair->component = ustring("Reflection");
429                                 break;
430                         case BL::ShaderNodeBsdfHair::component_Transmission:
431                                 hair->component = ustring("Transmission");
432                                 break;
433                 }
434                 node = hair;
435         }
436         else if (b_node.is_a(&RNA_ShaderNodeBsdfTranslucent)) {
437                 node = new TranslucentBsdfNode();
438         }
439         else if (b_node.is_a(&RNA_ShaderNodeBsdfTransparent)) {
440                 node = new TransparentBsdfNode();
441         }
442         else if (b_node.is_a(&RNA_ShaderNodeBsdfVelvet)) {
443                 node = new VelvetBsdfNode();
444         }
445         else if (b_node.is_a(&RNA_ShaderNodeEmission)) {
446                 node = new EmissionNode();
447         }
448         else if (b_node.is_a(&RNA_ShaderNodeAmbientOcclusion)) {
449                 node = new AmbientOcclusionNode();
450         }
451         else if (b_node.is_a(&RNA_ShaderNodeVolumeScatter)) {
452                 node = new ScatterVolumeNode();
453         }
454         else if (b_node.is_a(&RNA_ShaderNodeVolumeAbsorption)) {
455                 node = new AbsorptionVolumeNode();
456         }
457         else if (b_node.is_a(&RNA_ShaderNodeNewGeometry)) {
458                 node = new GeometryNode();
459         }
460         else if (b_node.is_a(&RNA_ShaderNodeWireframe)) {
461                 BL::ShaderNodeWireframe b_wireframe_node(b_node);
462                 WireframeNode *wire = new WireframeNode();
463                 wire->use_pixel_size = b_wireframe_node.use_pixel_size();
464                 node = wire;
465         }
466         else if (b_node.is_a(&RNA_ShaderNodeWavelength)) {
467                 node = new WavelengthNode();
468         }
469         else if (b_node.is_a(&RNA_ShaderNodeBlackbody)) {
470                 node = new BlackbodyNode();
471         }
472         else if (b_node.is_a(&RNA_ShaderNodeLightPath)) {
473                 node = new LightPathNode();
474         }
475         else if (b_node.is_a(&RNA_ShaderNodeLightFalloff)) {
476                 node = new LightFalloffNode();
477         }
478         else if (b_node.is_a(&RNA_ShaderNodeObjectInfo)) {
479                 node = new ObjectInfoNode();
480         }
481         else if (b_node.is_a(&RNA_ShaderNodeParticleInfo)) {
482                 node = new ParticleInfoNode();
483         }
484         else if (b_node.is_a(&RNA_ShaderNodeHairInfo)) {
485                 node = new HairInfoNode();
486         }
487         else if (b_node.is_a(&RNA_ShaderNodeBump)) {
488                 BL::ShaderNodeBump b_bump_node(b_node);
489                 BumpNode *bump = new BumpNode();
490                 bump->invert = b_bump_node.invert();
491                 node = bump;
492         }
493         else if (b_node.is_a(&RNA_ShaderNodeScript)) {
494 #ifdef WITH_OSL
495                 if(scene->shader_manager->use_osl()) {
496                         /* create script node */
497                         BL::ShaderNodeScript b_script_node(b_node);
498                         OSLScriptNode *script_node = new OSLScriptNode();
499                         
500                         /* Generate inputs/outputs from node sockets
501                          *
502                          * Note: the node sockets are generated from OSL parameters,
503                          * so the names match those of the corresponding parameters exactly.
504                          *
505                          * Note 2: ShaderInput/ShaderOutput store shallow string copies only!
506                          * Socket names must be stored in the extra lists instead. */
507                         BL::Node::inputs_iterator b_input;
508                         
509                         for (b_script_node.inputs.begin(b_input); b_input != b_script_node.inputs.end(); ++b_input) {
510                                 script_node->input_names.push_back(ustring(b_input->name()));
511                                 ShaderInput *input = script_node->add_input(script_node->input_names.back().c_str(),
512                                                                             convert_socket_type(*b_input));
513                                 set_default_value(input, b_node, *b_input, b_data, b_ntree);
514                         }
515                         
516                         BL::Node::outputs_iterator b_output;
517                         
518                         for (b_script_node.outputs.begin(b_output); b_output != b_script_node.outputs.end(); ++b_output) {
519                                 script_node->output_names.push_back(ustring(b_output->name()));
520                                 script_node->add_output(script_node->output_names.back().c_str(),
521                                                         convert_socket_type(*b_output));
522                         }
523                         
524                         /* load bytecode or filepath */
525                         OSLShaderManager *manager = (OSLShaderManager*)scene->shader_manager;
526                         string bytecode_hash = b_script_node.bytecode_hash();
527                         
528                         if(!bytecode_hash.empty()) {
529                                 /* loaded bytecode if not already done */
530                                 if(!manager->shader_test_loaded(bytecode_hash))
531                                         manager->shader_load_bytecode(bytecode_hash, b_script_node.bytecode());
532                                 
533                                 script_node->bytecode_hash = bytecode_hash;
534                         }
535                         else {
536                                 /* set filepath */
537                                 script_node->filepath = blender_absolute_path(b_data, b_ntree, b_script_node.filepath());
538                         }
539                         
540                         node = script_node;
541                 }
542 #endif
543         }
544         else if (b_node.is_a(&RNA_ShaderNodeTexImage)) {
545                 BL::ShaderNodeTexImage b_image_node(b_node);
546                 BL::Image b_image(b_image_node.image());
547                 ImageTextureNode *image = new ImageTextureNode();
548                 if(b_image) {
549                         /* builtin images will use callback-based reading because
550                          * they could only be loaded correct from blender side
551                          */
552                         bool is_builtin = b_image.packed_file() ||
553                                           b_image.source() == BL::Image::source_GENERATED ||
554                                           b_image.source() == BL::Image::source_MOVIE;
555
556                         if(is_builtin) {
557                                 /* for builtin images we're using image datablock name to find an image to
558                                  * read pixels from later
559                                  *
560                                  * also store frame number as well, so there's no differences in handling
561                                  * builtin names for packed images and movies
562                                  */
563                                 int scene_frame = b_scene.frame_current();
564                                 int image_frame = image_user_frame_number(b_image_node.image_user(), scene_frame);
565                                 image->filename = b_image.name() + "@" + string_printf("%d", image_frame);
566                                 image->builtin_data = b_image.ptr.data;
567                         }
568                         else {
569                                 image->filename = image_user_file_path(b_image_node.image_user(), b_image, b_scene.frame_current());
570                                 image->builtin_data = NULL;
571                         }
572
573                         image->animated = b_image_node.image_user().use_auto_refresh();
574                         image->use_alpha = b_image.use_alpha();
575
576                         /* TODO(sergey): Does not work properly when we change builtin type. */
577                         if (b_image.is_updated()) {
578                                 scene->image_manager->tag_reload_image(image->filename,
579                                                                        image->builtin_data,
580                                                                        (InterpolationType)b_image_node.interpolation());
581                         }
582                 }
583                 image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()];
584                 image->projection = ImageTextureNode::projection_enum[(int)b_image_node.projection()];
585                 image->interpolation = (InterpolationType)b_image_node.interpolation();
586                 image->projection_blend = b_image_node.projection_blend();
587                 get_tex_mapping(&image->tex_mapping, b_image_node.texture_mapping());
588                 node = image;
589         }
590         else if (b_node.is_a(&RNA_ShaderNodeTexEnvironment)) {
591                 BL::ShaderNodeTexEnvironment b_env_node(b_node);
592                 BL::Image b_image(b_env_node.image());
593                 EnvironmentTextureNode *env = new EnvironmentTextureNode();
594                 if(b_image) {
595                         bool is_builtin = b_image.packed_file() ||
596                                           b_image.source() == BL::Image::source_GENERATED ||
597                                           b_image.source() == BL::Image::source_MOVIE;
598
599                         if(is_builtin) {
600                                 int scene_frame = b_scene.frame_current();
601                                 int image_frame = image_user_frame_number(b_env_node.image_user(), scene_frame);
602                                 env->filename = b_image.name() + "@" + string_printf("%d", image_frame);
603                                 env->builtin_data = b_image.ptr.data;
604                         }
605                         else {
606                                 env->filename = image_user_file_path(b_env_node.image_user(), b_image, b_scene.frame_current());
607                                 env->animated = b_env_node.image_user().use_auto_refresh();
608                                 env->builtin_data = NULL;
609                         }
610
611                         env->use_alpha = b_image.use_alpha();
612
613                         /* TODO(sergey): Does not work properly when we change builtin type. */
614                         if (b_image.is_updated()) {
615                                 scene->image_manager->tag_reload_image(env->filename,
616                                                                        env->builtin_data,
617                                                                        INTERPOLATION_LINEAR);
618                         }
619                 }
620                 env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
621                 env->projection = EnvironmentTextureNode::projection_enum[(int)b_env_node.projection()];
622                 get_tex_mapping(&env->tex_mapping, b_env_node.texture_mapping());
623                 node = env;
624         }
625         else if (b_node.is_a(&RNA_ShaderNodeTexGradient)) {
626                 BL::ShaderNodeTexGradient b_gradient_node(b_node);
627                 GradientTextureNode *gradient = new GradientTextureNode();
628                 gradient->type = GradientTextureNode::type_enum[(int)b_gradient_node.gradient_type()];
629                 get_tex_mapping(&gradient->tex_mapping, b_gradient_node.texture_mapping());
630                 node = gradient;
631         }
632         else if (b_node.is_a(&RNA_ShaderNodeTexVoronoi)) {
633                 BL::ShaderNodeTexVoronoi b_voronoi_node(b_node);
634                 VoronoiTextureNode *voronoi = new VoronoiTextureNode();
635                 voronoi->coloring = VoronoiTextureNode::coloring_enum[(int)b_voronoi_node.coloring()];
636                 get_tex_mapping(&voronoi->tex_mapping, b_voronoi_node.texture_mapping());
637                 node = voronoi;
638         }
639         else if (b_node.is_a(&RNA_ShaderNodeTexMagic)) {
640                 BL::ShaderNodeTexMagic b_magic_node(b_node);
641                 MagicTextureNode *magic = new MagicTextureNode();
642                 magic->depth = b_magic_node.turbulence_depth();
643                 get_tex_mapping(&magic->tex_mapping, b_magic_node.texture_mapping());
644                 node = magic;
645         }
646         else if (b_node.is_a(&RNA_ShaderNodeTexWave)) {
647                 BL::ShaderNodeTexWave b_wave_node(b_node);
648                 WaveTextureNode *wave = new WaveTextureNode();
649                 wave->type = WaveTextureNode::type_enum[(int)b_wave_node.wave_type()];
650                 get_tex_mapping(&wave->tex_mapping, b_wave_node.texture_mapping());
651                 node = wave;
652         }
653         else if (b_node.is_a(&RNA_ShaderNodeTexChecker)) {
654                 BL::ShaderNodeTexChecker b_checker_node(b_node);
655                 CheckerTextureNode *checker = new CheckerTextureNode();
656                 get_tex_mapping(&checker->tex_mapping, b_checker_node.texture_mapping());
657                 node = checker;
658         }
659         else if (b_node.is_a(&RNA_ShaderNodeTexBrick)) {
660                 BL::ShaderNodeTexBrick b_brick_node(b_node);
661                 BrickTextureNode *brick = new BrickTextureNode();
662                 brick->offset = b_brick_node.offset();
663                 brick->offset_frequency = b_brick_node.offset_frequency();
664                 brick->squash = b_brick_node.squash();
665                 brick->squash_frequency = b_brick_node.squash_frequency();
666                 get_tex_mapping(&brick->tex_mapping, b_brick_node.texture_mapping());
667                 node = brick;
668         }
669         else if (b_node.is_a(&RNA_ShaderNodeTexNoise)) {
670                 BL::ShaderNodeTexNoise b_noise_node(b_node);
671                 NoiseTextureNode *noise = new NoiseTextureNode();
672                 get_tex_mapping(&noise->tex_mapping, b_noise_node.texture_mapping());
673                 node = noise;
674         }
675         else if (b_node.is_a(&RNA_ShaderNodeTexMusgrave)) {
676                 BL::ShaderNodeTexMusgrave b_musgrave_node(b_node);
677                 MusgraveTextureNode *musgrave = new MusgraveTextureNode();
678                 musgrave->type = MusgraveTextureNode::type_enum[(int)b_musgrave_node.musgrave_type()];
679                 get_tex_mapping(&musgrave->tex_mapping, b_musgrave_node.texture_mapping());
680                 node = musgrave;
681         }
682         else if (b_node.is_a(&RNA_ShaderNodeTexCoord)) {
683                 BL::ShaderNodeTexCoord b_tex_coord_node(b_node);
684                 TextureCoordinateNode *tex_coord = new TextureCoordinateNode();
685                 tex_coord->from_dupli = b_tex_coord_node.from_dupli();
686                 node = tex_coord;
687         }
688         else if (b_node.is_a(&RNA_ShaderNodeTexSky)) {
689                 BL::ShaderNodeTexSky b_sky_node(b_node);
690                 SkyTextureNode *sky = new SkyTextureNode();
691                 sky->type = SkyTextureNode::type_enum[(int)b_sky_node.sky_type()];
692                 sky->sun_direction = normalize(get_float3(b_sky_node.sun_direction()));
693                 sky->turbidity = b_sky_node.turbidity();
694                 sky->ground_albedo = b_sky_node.ground_albedo();
695                 get_tex_mapping(&sky->tex_mapping, b_sky_node.texture_mapping());
696                 node = sky;
697         }
698         else if (b_node.is_a(&RNA_ShaderNodeNormalMap)) {
699                 BL::ShaderNodeNormalMap b_normal_map_node(b_node);
700                 NormalMapNode *nmap = new NormalMapNode();
701                 nmap->space = NormalMapNode::space_enum[(int)b_normal_map_node.space()];
702                 nmap->attribute = b_normal_map_node.uv_map();
703                 node = nmap;
704         }
705         else if (b_node.is_a(&RNA_ShaderNodeTangent)) {
706                 BL::ShaderNodeTangent b_tangent_node(b_node);
707                 TangentNode *tangent = new TangentNode();
708                 tangent->direction_type = TangentNode::direction_type_enum[(int)b_tangent_node.direction_type()];
709                 tangent->axis = TangentNode::axis_enum[(int)b_tangent_node.axis()];
710                 tangent->attribute = b_tangent_node.uv_map();
711                 node = tangent;
712         }
713         else if (b_node.is_a(&RNA_ShaderNodeUVMap)) {
714                 BL::ShaderNodeUVMap b_uvmap_node(b_node);
715                 UVMapNode *uvm = new UVMapNode();
716                 uvm->attribute = b_uvmap_node.uv_map();
717                 uvm->from_dupli = b_uvmap_node.from_dupli();
718                 node = uvm;
719         }
720
721         if(node)
722                 graph->add(node);
723
724         return node;
725 }
726
727 static bool node_use_modified_socket_name(ShaderNode *node)
728 {
729         if (node->special_type == SHADER_SPECIAL_TYPE_SCRIPT)
730                 return false;
731         
732         return true;
733 }
734
735 static ShaderInput *node_find_input_by_name(ShaderNode *node, BL::Node b_node, BL::NodeSocket b_socket)
736 {
737         string name = b_socket.name();
738         
739         if (node_use_modified_socket_name(node)) {
740                 BL::Node::inputs_iterator b_input;
741                 bool found = false;
742                 int counter = 0, total = 0;
743                 
744                 for (b_node.inputs.begin(b_input); b_input != b_node.inputs.end(); ++b_input) {
745                         if (b_input->name() == name) {
746                                 if (!found)
747                                         counter++;
748                                 total++;
749                         }
750                         
751                         if(b_input->ptr.data == b_socket.ptr.data)
752                                 found = true;
753                 }
754                 
755                 /* rename if needed */
756                 if (name == "Shader")
757                         name = "Closure";
758                 
759                 if (total > 1)
760                         name = string_printf("%s%d", name.c_str(), counter);
761         }
762         
763         return node->input(name.c_str());
764 }
765
766 static ShaderOutput *node_find_output_by_name(ShaderNode *node, BL::Node b_node, BL::NodeSocket b_socket)
767 {
768         string name = b_socket.name();
769         
770         if (node_use_modified_socket_name(node)) {
771                 BL::Node::outputs_iterator b_output;
772                 bool found = false;
773                 int counter = 0, total = 0;
774                 
775                 for (b_node.outputs.begin(b_output); b_output != b_node.outputs.end(); ++b_output) {
776                         if (b_output->name() == name) {
777                                 if (!found)
778                                         counter++;
779                                 total++;
780                         }
781                         
782                         if(b_output->ptr.data == b_socket.ptr.data)
783                                 found = true;
784                 }
785                 
786                 /* rename if needed */
787                 if (name == "Shader")
788                         name = "Closure";
789                 
790                 if (total > 1)
791                         name = string_printf("%s%d", name.c_str(), counter);
792         }
793         
794         return node->output(name.c_str());
795 }
796
797 static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree,
798                       const ProxyMap &proxy_input_map, const ProxyMap &proxy_output_map)
799 {
800         /* add nodes */
801         BL::ShaderNodeTree::nodes_iterator b_node;
802         PtrInputMap input_map;
803         PtrOutputMap output_map;
804         
805         BL::Node::inputs_iterator b_input;
806         BL::Node::outputs_iterator b_output;
807
808         /* find the node to use for output if there are multiple */
809         bool found_active_output = false;
810         BL::ShaderNode output_node(PointerRNA_NULL);
811
812         for(b_ntree.nodes.begin(b_node); b_node != b_ntree.nodes.end(); ++b_node) {
813                 if (is_output_node(*b_node)) {
814                         BL::ShaderNodeOutputMaterial b_output_node(*b_node);
815
816                         if(b_output_node.is_active_output()) {
817                                 output_node = b_output_node;
818                                 found_active_output = true;
819                                 break;
820                         }
821                         else if(!output_node.ptr.data && !found_active_output) {
822                                 output_node = b_output_node;
823                         }
824                 }
825         }
826
827         /* add nodes */
828         for(b_ntree.nodes.begin(b_node); b_node != b_ntree.nodes.end(); ++b_node) {
829                 if (b_node->mute() || b_node->is_a(&RNA_NodeReroute)) {
830                         /* replace muted node with internal links */
831                         BL::Node::internal_links_iterator b_link;
832                         for (b_node->internal_links.begin(b_link); b_link != b_node->internal_links.end(); ++b_link) {
833                                 ProxyNode *proxy = new ProxyNode(convert_socket_type(b_link->to_socket()));
834                                 
835                                 input_map[b_link->from_socket().ptr.data] = proxy->inputs[0];
836                                 output_map[b_link->to_socket().ptr.data] = proxy->outputs[0];
837                                 
838                                 graph->add(proxy);
839                         }
840                 }
841                 else if (b_node->is_a(&RNA_ShaderNodeGroup) || b_node->is_a(&RNA_NodeCustomGroup)) {
842                         
843                         BL::ShaderNodeTree b_group_ntree(PointerRNA_NULL);
844                         if (b_node->is_a(&RNA_ShaderNodeGroup))
845                                 b_group_ntree = BL::ShaderNodeTree(((BL::NodeGroup)(*b_node)).node_tree());
846                         else
847                                 b_group_ntree = BL::ShaderNodeTree(((BL::NodeCustomGroup)(*b_node)).node_tree());
848                         ProxyMap group_proxy_input_map, group_proxy_output_map;
849                         
850                         /* Add a proxy node for each socket
851                          * Do this even if the node group has no internal tree,
852                          * so that links have something to connect to and assert won't fail.
853                          */
854                         for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
855                                 ProxyNode *proxy = new ProxyNode(convert_socket_type(*b_input));
856                                 graph->add(proxy);
857                                 
858                                 /* register the proxy node for internal binding */
859                                 group_proxy_input_map[b_input->identifier()] = proxy;
860                                 
861                                 input_map[b_input->ptr.data] = proxy->inputs[0];
862                                 
863                                 set_default_value(proxy->inputs[0], *b_node, *b_input, b_data, b_ntree);
864                         }
865                         for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
866                                 ProxyNode *proxy = new ProxyNode(convert_socket_type(*b_output));
867                                 graph->add(proxy);
868                                 
869                                 /* register the proxy node for internal binding */
870                                 group_proxy_output_map[b_output->identifier()] = proxy;
871                                 
872                                 output_map[b_output->ptr.data] = proxy->outputs[0];
873                         }
874                         
875                         if (b_group_ntree)
876                                 add_nodes(scene, b_data, b_scene, graph, b_group_ntree, group_proxy_input_map, group_proxy_output_map);
877                 }
878                 else if (b_node->is_a(&RNA_NodeGroupInput)) {
879                         /* map each socket to a proxy node */
880                         for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
881                                 ProxyMap::const_iterator proxy_it = proxy_input_map.find(b_output->identifier());
882                                 if (proxy_it != proxy_input_map.end()) {
883                                         ProxyNode *proxy = proxy_it->second;
884                                         
885                                         output_map[b_output->ptr.data] = proxy->outputs[0];
886                                 }
887                         }
888                 }
889                 else if (b_node->is_a(&RNA_NodeGroupOutput)) {
890                         BL::NodeGroupOutput b_output_node(*b_node);
891                         /* only the active group output is used */
892                         if (b_output_node.is_active_output()) {
893                                 /* map each socket to a proxy node */
894                                 for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
895                                         ProxyMap::const_iterator proxy_it = proxy_output_map.find(b_input->identifier());
896                                         if (proxy_it != proxy_output_map.end()) {
897                                                 ProxyNode *proxy = proxy_it->second;
898                                                 
899                                                 input_map[b_input->ptr.data] = proxy->inputs[0];
900                                                 
901                                                 set_default_value(proxy->inputs[0], *b_node, *b_input, b_data, b_ntree);
902                                         }
903                                 }
904                         }
905                 }
906                 else {
907                         ShaderNode *node = NULL;
908
909                         if (is_output_node(*b_node)) {
910                                 if (b_node->ptr.data == output_node.ptr.data) {
911                                         node = graph->output();
912                                 }
913                         }
914                         else {
915                                 node = add_node(scene, b_data, b_scene, graph, b_ntree, BL::ShaderNode(*b_node));
916                         }
917                         
918                         if(node) {
919                                 /* map node sockets for linking */
920                                 for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
921                                         ShaderInput *input = node_find_input_by_name(node, *b_node, *b_input);
922                                         if (!input) {
923                                                 /* XXX should not happen, report error? */
924                                                 continue;
925                                         }
926                                         input_map[b_input->ptr.data] = input;
927                                         
928                                         set_default_value(input, *b_node, *b_input, b_data, b_ntree);
929                                 }
930                                 for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
931                                         ShaderOutput *output = node_find_output_by_name(node, *b_node, *b_output);
932                                         if (!output) {
933                                                 /* XXX should not happen, report error? */
934                                                 continue;
935                                         }
936                                         output_map[b_output->ptr.data] = output;
937                                 }
938                         }
939                 }
940         }
941
942         /* connect nodes */
943         BL::NodeTree::links_iterator b_link;
944
945         for(b_ntree.links.begin(b_link); b_link != b_ntree.links.end(); ++b_link) {
946                 /* get blender link data */
947                 BL::NodeSocket b_from_sock = b_link->from_socket();
948                 BL::NodeSocket b_to_sock = b_link->to_socket();
949
950                 ShaderOutput *output = 0;
951                 ShaderInput *input = 0;
952                 
953                 PtrOutputMap::iterator output_it = output_map.find(b_from_sock.ptr.data);
954                 if (output_it != output_map.end())
955                         output = output_it->second;
956                 PtrInputMap::iterator input_it = input_map.find(b_to_sock.ptr.data);
957                 if (input_it != input_map.end())
958                         input = input_it->second;
959
960                 /* either node may be NULL when the node was not exported, typically
961                  * because the node type is not supported */
962                 if(output && input)
963                         graph->connect(output, input);
964         }
965 }
966
967 static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree)
968 {
969         static const ProxyMap empty_proxy_map;
970         add_nodes(scene, b_data, b_scene, graph, b_ntree, empty_proxy_map, empty_proxy_map);
971 }
972
973 /* Sync Materials */
974
975 void BlenderSync::sync_materials(bool update_all)
976 {
977         shader_map.set_default(scene->shaders[scene->default_surface]);
978
979         /* material loop */
980         BL::BlendData::materials_iterator b_mat;
981
982         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat) {
983                 Shader *shader;
984                 
985                 /* test if we need to sync */
986                 if(shader_map.sync(&shader, *b_mat) || update_all) {
987                         ShaderGraph *graph = new ShaderGraph();
988
989                         shader->name = b_mat->name().c_str();
990                         shader->pass_id = b_mat->pass_index();
991
992                         /* create nodes */
993                         if(b_mat->use_nodes() && b_mat->node_tree()) {
994                                 BL::ShaderNodeTree b_ntree(b_mat->node_tree());
995
996                                 add_nodes(scene, b_data, b_scene, graph, b_ntree);
997                         }
998                         else {
999                                 ShaderNode *closure, *out;
1000
1001                                 closure = graph->add(new DiffuseBsdfNode());
1002                                 closure->input("Color")->value = get_float3(b_mat->diffuse_color());
1003                                 out = graph->output();
1004
1005                                 graph->connect(closure->output("BSDF"), out->input("Surface"));
1006                         }
1007
1008                         /* settings */
1009                         PointerRNA cmat = RNA_pointer_get(&b_mat->ptr, "cycles");
1010                         shader->use_mis = get_boolean(cmat, "sample_as_light");
1011                         shader->use_transparent_shadow = get_boolean(cmat, "use_transparent_shadow");
1012                         shader->heterogeneous_volume = !get_boolean(cmat, "homogeneous_volume");
1013                         shader->volume_sampling_method = RNA_enum_get(&cmat, "volume_sampling");
1014
1015                         shader->set_graph(graph);
1016                         shader->tag_update(scene);
1017                 }
1018         }
1019 }
1020
1021 /* Sync World */
1022
1023 void BlenderSync::sync_world(bool update_all)
1024 {
1025         Background *background = scene->background;
1026         Background prevbackground = *background;
1027
1028         BL::World b_world = b_scene.world();
1029
1030         if(world_recalc || update_all || b_world.ptr.data != world_map) {
1031                 Shader *shader = scene->shaders[scene->default_background];
1032                 ShaderGraph *graph = new ShaderGraph();
1033
1034                 /* create nodes */
1035                 if(b_world && b_world.use_nodes() && b_world.node_tree()) {
1036                         BL::ShaderNodeTree b_ntree(b_world.node_tree());
1037
1038                         add_nodes(scene, b_data, b_scene, graph, b_ntree);
1039                         
1040                         /* volume */
1041                         PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
1042                         shader->heterogeneous_volume = !get_boolean(cworld, "homogeneous_volume");
1043                         shader->volume_sampling_method = RNA_enum_get(&cworld, "volume_sampling");
1044                 }
1045                 else if(b_world) {
1046                         ShaderNode *closure, *out;
1047
1048                         closure = graph->add(new BackgroundNode());
1049                         closure->input("Color")->value = get_float3(b_world.horizon_color());
1050                         out = graph->output();
1051
1052                         graph->connect(closure->output("Background"), out->input("Surface"));
1053                 }
1054
1055                 if(b_world) {
1056                         /* AO */
1057                         BL::WorldLighting b_light = b_world.light_settings();
1058
1059                         if(b_light.use_ambient_occlusion())
1060                                 background->ao_factor = b_light.ao_factor();
1061                         else
1062                                 background->ao_factor = 0.0f;
1063
1064                         background->ao_distance = b_light.distance();
1065
1066                         /* visibility */
1067                         PointerRNA cvisibility = RNA_pointer_get(&b_world.ptr, "cycles_visibility");
1068                         uint visibility = 0;
1069
1070                         visibility |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
1071                         visibility |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
1072                         visibility |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
1073                         visibility |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
1074                         visibility |= get_boolean(cvisibility, "scatter")? PATH_RAY_VOLUME_SCATTER: 0;
1075
1076                         background->visibility = visibility;
1077                 }
1078
1079                 shader->set_graph(graph);
1080                 shader->tag_update(scene);
1081                 background->tag_update(scene);
1082         }
1083
1084         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
1085
1086         /* when doing preview render check for BI's transparency settings,
1087          * this is so because bledner's preview render routines are not able
1088          * to tweak all cycles's settings depending on different circumstances
1089          */
1090         if(b_engine.is_preview() == false)
1091                 background->transparent = get_boolean(cscene, "film_transparent");
1092         else
1093                 background->transparent = b_scene.render().alpha_mode() == BL::RenderSettings::alpha_mode_TRANSPARENT;
1094
1095         background->use = render_layer.use_background;
1096
1097         if(background->modified(prevbackground))
1098                 background->tag_update(scene);
1099 }
1100
1101 /* Sync Lamps */
1102
1103 void BlenderSync::sync_lamps(bool update_all)
1104 {
1105         shader_map.set_default(scene->shaders[scene->default_light]);
1106
1107         /* lamp loop */
1108         BL::BlendData::lamps_iterator b_lamp;
1109
1110         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp) {
1111                 Shader *shader;
1112                 
1113                 /* test if we need to sync */
1114                 if(shader_map.sync(&shader, *b_lamp) || update_all) {
1115                         ShaderGraph *graph = new ShaderGraph();
1116
1117                         /* create nodes */
1118                         if(b_lamp->use_nodes() && b_lamp->node_tree()) {
1119                                 shader->name = b_lamp->name().c_str();
1120
1121                                 BL::ShaderNodeTree b_ntree(b_lamp->node_tree());
1122
1123                                 add_nodes(scene, b_data, b_scene, graph, b_ntree);
1124                         }
1125                         else {
1126                                 ShaderNode *closure, *out;
1127                                 float strength = 1.0f;
1128
1129                                 if(b_lamp->type() == BL::Lamp::type_POINT ||
1130                                    b_lamp->type() == BL::Lamp::type_SPOT ||
1131                                    b_lamp->type() == BL::Lamp::type_AREA)
1132                                 {
1133                                         strength = 100.0f;
1134                                 }
1135
1136                                 closure = graph->add(new EmissionNode());
1137                                 closure->input("Color")->value = get_float3(b_lamp->color());
1138                                 closure->input("Strength")->value.x = strength;
1139                                 out = graph->output();
1140
1141                                 graph->connect(closure->output("Emission"), out->input("Surface"));
1142                         }
1143
1144                         shader->set_graph(graph);
1145                         shader->tag_update(scene);
1146                 }
1147         }
1148 }
1149
1150 void BlenderSync::sync_shaders()
1151 {
1152         /* for auto refresh images */
1153         bool auto_refresh_update = false;
1154
1155         if(preview) {
1156                 ImageManager *image_manager = scene->image_manager;
1157                 int frame = b_scene.frame_current();
1158                 auto_refresh_update = image_manager->set_animation_frame_update(frame);
1159         }
1160
1161         shader_map.pre_sync();
1162
1163         sync_world(auto_refresh_update);
1164         sync_lamps(auto_refresh_update);
1165         sync_materials(auto_refresh_update);
1166
1167         /* false = don't delete unused shaders, not supported */
1168         shader_map.post_sync(false);
1169 }
1170
1171 CCL_NAMESPACE_END
1172