diff options
Diffstat (limited to 'source/blender/collada/collada_utils.cpp')
-rw-r--r-- | source/blender/collada/collada_utils.cpp | 672 |
1 files changed, 515 insertions, 157 deletions
diff --git a/source/blender/collada/collada_utils.cpp b/source/blender/collada/collada_utils.cpp index b25a35a5452..41e661ffd40 100644 --- a/source/blender/collada/collada_utils.cpp +++ b/source/blender/collada/collada_utils.cpp @@ -33,39 +33,59 @@ #include "COLLADAFWMeshVertexData.h" #include <set> - +#include <string> extern "C" { #include "DNA_modifier_types.h" #include "DNA_customdata_types.h" +#include "DNA_key_types.h" #include "DNA_object_types.h" +#include "DNA_constraint_types.h" #include "DNA_mesh_types.h" #include "DNA_scene_types.h" #include "DNA_armature_types.h" #include "BLI_math.h" #include "BLI_linklist.h" +#include "BLI_listbase.h" +#include "BKE_action.h" #include "BKE_context.h" #include "BKE_customdata.h" -#include "BKE_depsgraph.h" +#include "BKE_constraint.h" +#include "BKE_key.h" +#include "BKE_material.h" +#include "BKE_node.h" #include "BKE_object.h" #include "BKE_global.h" +#include "BKE_layer.h" +#include "BKE_library.h" #include "BKE_mesh.h" +#include "BKE_mesh_runtime.h" +#include "BKE_object.h" #include "BKE_scene.h" -#include "BKE_DerivedMesh.h" -#include "BKE_main.h" #include "ED_armature.h" +#include "ED_screen.h" +#include "ED_node.h" + +#include "MEM_guardedalloc.h" #include "WM_api.h" // XXX hrm, see if we can do without this #include "WM_types.h" #include "bmesh.h" #include "bmesh_tools.h" + +#include "DEG_depsgraph.h" +#include "DEG_depsgraph_query.h" +#if 0 +#include "NOD_common.h" +#endif } #include "collada_utils.h" #include "ExportSettings.h" +#include "BlenderContext.h" float bc_get_float_value(const COLLADAFW::FloatOrDoubleArray& array, unsigned int index) { @@ -89,11 +109,53 @@ int bc_test_parent_loop(Object *par, Object *ob) return bc_test_parent_loop(par->parent, ob); } +void bc_get_children(std::vector<Object *> &child_set, Object *ob, ViewLayer *view_layer) +{ + Base *base; + for (base = (Base *)view_layer->object_bases.first; base; base = base->next) { + Object *cob = base->object; + if (cob->parent == ob) { + switch (ob->type) { + case OB_MESH: + case OB_CAMERA: + case OB_LAMP: + case OB_EMPTY: + case OB_ARMATURE: + child_set.push_back(cob); + default: break; + } + } + } +} + +bool bc_validateConstraints(bConstraint *con) +{ + const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con); + + /* these we can skip completely (invalid constraints...) */ + if (cti == NULL) + return false; + if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) + return false; + + /* these constraints can't be evaluated anyway */ + if (cti->evaluate_constraint == NULL) + return false; + + /* influence == 0 should be ignored */ + if (con->enforce == 0.0f) + return false; + + /* validation passed */ + return true; +} + // a shortened version of parent_set_exec() // if is_parent_space is true then ob->obmat will be multiplied by par->obmat before parenting int bc_set_parent(Object *ob, Object *par, bContext *C, bool is_parent_space) { Object workob; + Depsgraph *depsgraph = CTX_data_depsgraph(C); Scene *sce = CTX_data_scene(C); if (!par || bc_test_parent_loop(par, ob)) @@ -107,7 +169,7 @@ int bc_set_parent(Object *ob, Object *par, bContext *C, bool is_parent_space) if (is_parent_space) { float mat[4][4]; // calc par->obmat - BKE_object_where_is_calc(sce, par); + BKE_object_where_is_calc(depsgraph, sce, par); // move child obmat into world space mul_m4_m4m4(mat, par->obmat, ob->obmat); @@ -118,74 +180,121 @@ int bc_set_parent(Object *ob, Object *par, bContext *C, bool is_parent_space) BKE_object_apply_mat4(ob, ob->obmat, 0, 0); // compute parentinv - BKE_object_workob_calc_parent(sce, ob, &workob); + BKE_object_workob_calc_parent(depsgraph, sce, ob, &workob); invert_m4_m4(ob->parentinv, workob.obmat); - DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA); - DAG_id_tag_update(&par->id, OB_RECALC_OB); - - /** done once after import */ -#if 0 - DAG_relations_tag_update(bmain); - WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL); -#endif + DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA); + DEG_id_tag_update(&par->id, OB_RECALC_OB); return true; } -EvaluationContext *bc_get_evaluation_context(Main *bmain) +std::vector<bAction *> bc_getSceneActions(const bContext *C, Object *ob, bool all_actions) { - return bmain->eval_ctx; + std::vector<bAction *> actions; + if (all_actions) { + Main *bmain = CTX_data_main(C); + ID *id; + + for (id = (ID *)bmain->action.first; id; id = (ID *)(id->next)) { + bAction *act = (bAction *)id; + /* XXX This currently creates too many actions. + TODO Need to check if the action is compatible to the given object + */ + actions.push_back(act); + } + } + else + { + bAction *action = bc_getSceneObjectAction(ob); + actions.push_back(action); + } + + return actions; } -void bc_update_scene(Main *bmain, Scene *scene, float ctime) +std::string bc_get_action_id(std::string action_name, std::string ob_name, std::string channel_type, std::string axis_name, std::string axis_separator) { + std::string result = action_name + "_" + channel_type; + if (ob_name.length() > 0) + result = ob_name + "_" + result; + if (axis_name.length() > 0) + result += axis_separator + axis_name; + return translate_id(result); +} + +void bc_update_scene(BlenderContext &blender_context, float ctime) +{ + Main *bmain = blender_context.get_main(); + Scene *scene = blender_context.get_scene(); + Depsgraph *depsgraph = blender_context.get_depsgraph(); + + /* + * See remark in physics_fluid.c lines 395...) + * BKE_scene_update_for_newframe(ev_context, bmain, scene, scene->lay); + */ BKE_scene_frame_set(scene, ctime); - EvaluationContext *ev_context = bc_get_evaluation_context(bmain); - BKE_scene_update_for_newframe(ev_context, bmain, scene, scene->lay); + ED_update_for_newframe(bmain, depsgraph); } -Object *bc_add_object(Main *bmain, Scene *scene, int type, const char *name) +Object *bc_add_object(Main *bmain, Scene *scene, ViewLayer *view_layer, int type, const char *name) { Object *ob = BKE_object_add_only_object(bmain, type, name); ob->data = BKE_object_obdata_add_from_type(bmain, type, name); ob->lay = scene->lay; - DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); + DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); - BKE_scene_base_select(scene, BKE_scene_base_add(scene, ob)); + LayerCollection *layer_collection = BKE_layer_collection_get_active(view_layer); + BKE_collection_object_add(bmain, layer_collection->collection, ob); + + Base *base = BKE_view_layer_base_find(view_layer, ob); + /* TODO: is setting active needed? */ + BKE_view_layer_base_select_and_set_active(view_layer, base); return ob; } Mesh *bc_get_mesh_copy( - Main *bmain, Scene *scene, Object *ob, BC_export_mesh_type export_mesh_type, bool apply_modifiers, bool triangulate) + BlenderContext &blender_context, + Object *ob, + BC_export_mesh_type export_mesh_type, + bool apply_modifiers, + bool triangulate) { - Mesh *tmpmesh; CustomDataMask mask = CD_MASK_MESH; Mesh *mesh = (Mesh *)ob->data; - DerivedMesh *dm = NULL; + Mesh *tmpmesh = NULL; if (apply_modifiers) { +#if 0 /* Not supported by new system currently... */ switch (export_mesh_type) { case BC_MESH_TYPE_VIEW: { - dm = mesh_create_derived_view(scene, ob, mask); + dm = mesh_create_derived_view(depsgraph, scene, ob, mask); break; } case BC_MESH_TYPE_RENDER: { - dm = mesh_create_derived_render(scene, ob, mask); + dm = mesh_create_derived_render(depsgraph, scene, ob, mask); break; } } +#else + Depsgraph *depsgraph = blender_context.get_depsgraph(); + Scene *scene = blender_context.get_scene(); + tmpmesh = mesh_get_eval_final(depsgraph, scene, ob, mask); +#endif } else { - dm = mesh_create_derived((Mesh *)ob->data, NULL); + tmpmesh = mesh; } - tmpmesh = BKE_mesh_add(bmain, "ColladaMesh"); // name is not important here - DM_to_mesh(dm, tmpmesh, ob, CD_MASK_MESH, true); - tmpmesh->flag = mesh->flag; + BKE_id_copy_ex(NULL, &tmpmesh->id, (ID **)&tmpmesh, + LIB_ID_CREATE_NO_MAIN | + LIB_ID_CREATE_NO_USER_REFCOUNT | + LIB_ID_CREATE_NO_DEG_TAG | + LIB_ID_COPY_NO_PREVIEW, + false); if (triangulate) { bc_triangulate_mesh(tmpmesh); @@ -213,12 +322,14 @@ Object *bc_get_assigned_armature(Object *ob) return ob_arm; } -// Returns the highest selected ancestor -// returns NULL if no ancestor is selected -// IMPORTANT: This function expects that -// all exported objects have set: -// ob->id.tag & LIB_TAG_DOIT +/* +* Returns the highest selected ancestor +* returns NULL if no ancestor is selected +* IMPORTANT: This function expects that all exported objects have set: +* ob->id.tag & LIB_TAG_DOIT +*/ Object *bc_get_highest_selected_ancestor_or_self(LinkNode *export_set, Object *ob) + { Object *ancestor = ob; while (ob->parent && bc_is_marked(ob->parent)) { @@ -228,16 +339,31 @@ Object *bc_get_highest_selected_ancestor_or_self(LinkNode *export_set, Object *o return ancestor; } - bool bc_is_base_node(LinkNode *export_set, Object *ob) { Object *root = bc_get_highest_selected_ancestor_or_self(export_set, ob); return (root == ob); } -bool bc_is_in_Export_set(LinkNode *export_set, Object *ob) +bool bc_is_in_Export_set(LinkNode *export_set, Object *ob, ViewLayer *view_layer) { - return (BLI_linklist_index(export_set, ob) != -1); + bool to_export = (BLI_linklist_index(export_set, ob) != -1); + + if (!to_export) + { + /* Mark this object as to_export even if it is not in the + export list, but it contains children to export */ + + std::vector<Object *> children; + bc_get_children(children, ob, view_layer); + for (int i = 0; i < children.size(); i++) { + if (bc_is_in_Export_set(export_set, children[i], view_layer)) { + to_export = true; + break; + } + } + } + return to_export; } bool bc_has_object_type(LinkNode *export_set, short obtype) @@ -811,6 +937,133 @@ static bool has_custom_props(Bone *bone, bool enabled, std::string key) } +void bc_enable_fcurves(bAction *act, char *bone_name) +{ + FCurve *fcu; + char prefix[200]; + + if (bone_name) + BLI_snprintf(prefix, sizeof(prefix), "pose.bones[\"%s\"]", bone_name); + + for (fcu = (FCurve *)act->curves.first; fcu; fcu = fcu->next) { + if (bone_name) { + if (STREQLEN(fcu->rna_path, prefix, strlen(prefix))) + fcu->flag &= ~FCURVE_DISABLED; + else + fcu->flag |= FCURVE_DISABLED; + } + else { + fcu->flag &= ~FCURVE_DISABLED; + } + } +} + +bool bc_bone_matrix_local_get(Object *ob, Bone *bone, Matrix &mat, bool for_opensim) +{ + + /* Ok, lets be super cautious and check if the bone exists */ + bPose *pose = ob->pose; + bPoseChannel *pchan = BKE_pose_channel_find_name(pose, bone->name); + if (!pchan) { + return false; + } + + bAction *action = bc_getSceneObjectAction(ob); + bPoseChannel *parchan = pchan->parent; + + bc_enable_fcurves(action, bone->name); + float ipar[4][4]; + + if (bone->parent) { + invert_m4_m4(ipar, parchan->pose_mat); + mul_m4_m4m4(mat, ipar, pchan->pose_mat); + } + else + copy_m4_m4(mat, pchan->pose_mat); + + /* OPEN_SIM_COMPATIBILITY + * AFAIK animation to second life is via BVH, but no + * reason to not have the collada-animation be correct + */ + if (for_opensim) { + float temp[4][4]; + copy_m4_m4(temp, bone->arm_mat); + temp[3][0] = temp[3][1] = temp[3][2] = 0.0f; + invert_m4(temp); + + mul_m4_m4m4(mat, mat, temp); + + if (bone->parent) { + copy_m4_m4(temp, bone->parent->arm_mat); + temp[3][0] = temp[3][1] = temp[3][2] = 0.0f; + + mul_m4_m4m4(mat, temp, mat); + } + } + bc_enable_fcurves(action, NULL); + return true; +} + +bool bc_is_animated(BCMatrixSampleMap &values) +{ + static float MIN_DISTANCE = 0.00001; + + if (values.size() < 2) + return false; // need at least 2 entries to be not flat + + BCMatrixSampleMap::iterator it; + const BCMatrix *refmat = NULL; + for (it = values.begin(); it != values.end(); ++it) { + const BCMatrix *matrix = it->second; + + if (refmat == NULL) { + refmat = matrix; + continue; + } + + if (!matrix->in_range(*refmat, MIN_DISTANCE)) + return true; + } + return false; +} + +bool bc_has_animations(Object *ob) +{ + /* Check for object,lamp and camera transform animations */ + if ((bc_getSceneObjectAction(ob) && bc_getSceneObjectAction(ob)->curves.first) || + (bc_getSceneLampAction(ob) && bc_getSceneLampAction(ob)->curves.first) || + (bc_getSceneCameraAction(ob) && bc_getSceneCameraAction(ob)->curves.first)) + return true; + + //Check Material Effect parameter animations. + for (int a = 0; a < ob->totcol; a++) { + Material *ma = give_current_material(ob, a + 1); + if (!ma) continue; + if (ma->adt && ma->adt->action && ma->adt->action->curves.first) + return true; + } + + Key *key = BKE_key_from_object(ob); + if ((key && key->adt && key->adt->action) && key->adt->action->curves.first) + return true; + + return false; +} + + +bool bc_has_animations(Scene *sce, LinkNode &export_set) +{ + LinkNode *node; + + for (node = &export_set; node; node = node->next) { + Object *ob = (Object *)node->link; + + if (bc_has_animations(ob)) + return true; + } + return false; +} + /** * Check if custom information about bind matrix exists and modify the from_mat * accordingly. @@ -875,8 +1128,11 @@ void bc_create_restpose_mat(const ExportSettings *export_settings, Bone *bone, f void bc_sanitize_mat(float mat[4][4], int precision) { for (int i = 0; i < 4; i++) - for (int j = 0; j < 4; j++) - mat[i][j] = double_round(mat[i][j], precision); + for (int j = 0; j < 4; j++) { + double val = (double)mat[i][j]; + val = double_round(val, precision); + mat[i][j] = (float)val; + } } void bc_sanitize_mat(double mat[4][4], int precision) @@ -898,17 +1154,31 @@ void bc_copy_farray_m4(float *r, float a[4][4]) for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) *r++ = a[i][j]; +} +void bc_copy_darray_m4d(double *r, double a[4][4]) +{ + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + *r++ = a[i][j]; } -/* -* Returns name of Active UV Layer or empty String if no active UV Layer defined. -* Assuming the Object is of type MESH -*/ -std::string bc_get_active_uvlayer_name(Object *ob) +void bc_copy_v44_m4d(std::vector<std::vector<double>> &r, double(&a)[4][4]) { - Mesh *me = (Mesh *)ob->data; - return bc_get_active_uvlayer_name(me); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + r[i][j] = a[i][j]; + } + } +} + +void bc_copy_m4d_v44(double (&r)[4][4], std::vector<std::vector<double>> &a) +{ + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + r[i][j] = a[i][j]; + } + } } /** @@ -926,7 +1196,17 @@ std::string bc_get_active_uvlayer_name(Mesh *me) return ""; } -/* +/** + * Returns name of Active UV Layer or empty String if no active UV Layer defined. + * Assuming the Object is of type MESH + */ +std::string bc_get_active_uvlayer_name(Object *ob) +{ + Mesh *me = (Mesh *)ob->data; + return bc_get_active_uvlayer_name(me); +} + +/** * Returns UV Layer name or empty string if layer index is out of range */ std::string bc_get_uvlayer_name(Mesh *me, int layer) @@ -941,124 +1221,202 @@ std::string bc_get_uvlayer_name(Mesh *me, int layer) return ""; } -/********************************************************************** -* -* Return the list of Mesh objects with assigned UVtextures and Images -* Note: We need to create artificaial materials for each of them -* -***********************************************************************/ -std::set<Object *> bc_getUVTexturedObjects(Scene *sce, bool all_uv_layers) -{ - std::set <Object *> UVObjects; - Base *base = (Base *)sce->base.first; - - while (base) { - Object *ob = base->object; - bool has_uvimage = false; - if (ob->type == OB_MESH) { - Mesh *me = (Mesh *)ob->data; - int active_uv_layer = CustomData_get_active_layer_index(&me->pdata, CD_MTEXPOLY); - - for (int i = 0; i < me->pdata.totlayer && !has_uvimage; i++) { - if (all_uv_layers || active_uv_layer == i) - { - if (me->pdata.layers[i].type == CD_MTEXPOLY) { - MTexPoly *txface = (MTexPoly *)me->pdata.layers[i].data; - MPoly *mpoly = me->mpoly; - for (int j = 0; j < me->totpoly; j++, mpoly++, txface++) { - - Image *ima = txface->tpage; - if (ima != NULL) { - has_uvimage = true; - break; - } - } - } - } - } +std::string bc_find_bonename_in_path(std::string path, std::string probe) +{ + std::string result; + char *boneName = BLI_str_quoted_substrN(path.c_str(), probe.c_str()); + if (boneName) { + result = std::string(boneName); + MEM_freeN(boneName); + } + return result; +} - if (has_uvimage) { - UVObjects.insert(ob); - } - } - base = base->next; +static bNodeTree *prepare_material_nodetree(Material *ma) +{ + if (ma->nodetree == NULL) { + ma->nodetree = ntreeAddTree(NULL, "Shader Nodetree", "ShaderNodeTree"); + ma->use_nodes = true; } - return UVObjects; -} - -/********************************************************************** -* -* Return the list of UV Texture images from all exported Mesh Items -* Note: We need to create one artificial material for each Image. -* -***********************************************************************/ -std::set<Image *> bc_getUVImages(Scene *sce, bool all_uv_layers) -{ - std::set <Image *> UVImages; - Base *base = (Base *)sce->base.first; - - while (base) { - Object *ob = base->object; - bool has_uvimage = false; - if (ob->type == OB_MESH) { - Mesh *me = (Mesh *)ob->data; - int active_uv_layer = CustomData_get_active_layer_index(&me->pdata, CD_MTEXPOLY); - - for (int i = 0; i < me->pdata.totlayer && !has_uvimage; i++) { - if (all_uv_layers || active_uv_layer == i) - { - if (me->pdata.layers[i].type == CD_MTEXPOLY) { - MTexPoly *txface = (MTexPoly *)me->pdata.layers[i].data; - MPoly *mpoly = me->mpoly; - for (int j = 0; j < me->totpoly; j++, mpoly++, txface++) { - - Image *ima = txface->tpage; - if (ima != NULL) { - if (UVImages.find(ima) == UVImages.end()) - UVImages.insert(ima); - } - } - } - } - } + return ma->nodetree; +} + +bNode *bc_add_node(bContext *C, bNodeTree *ntree, int node_type, int locx, int locy, std::string label) +{ + bNode *node = nodeAddStaticNode(C, ntree, node_type); + if (node) { + if (label.length() > 0) { + strcpy(node->label, label.c_str()); } - base = base->next; + node->locx = locx; + node->locy = locy; + node->flag |= NODE_SELECT; } - return UVImages; + return node; } -/********************************************************************** -* -* Return the list of UV Texture images for the given Object -* Note: We need to create one artificial material for each Image. -* -***********************************************************************/ -std::set<Image *> bc_getUVImages(Object *ob, bool all_uv_layers) + +bNode *bc_add_node(bContext *C, bNodeTree *ntree, int node_type, int locx, int locy) { - std::set <Image *> UVImages; + return bc_add_node(C, ntree, node_type, locx, locy, ""); +} - bool has_uvimage = false; - if (ob->type == OB_MESH) { - Mesh *me = (Mesh *)ob->data; - int active_uv_layer = CustomData_get_active_layer_index(&me->pdata, CD_MTEXPOLY); +#if 0 +// experimental, probably not used +static bNodeSocket *bc_group_add_input_socket(bNodeTree *ntree, bNode *to_node, int to_index, std::string label) +{ + bNodeSocket *to_socket = (bNodeSocket *)BLI_findlink(&to_node->inputs, to_index); - for (int i = 0; i < me->pdata.totlayer && !has_uvimage; i++) { - if (all_uv_layers || active_uv_layer == i) - { - if (me->pdata.layers[i].type == CD_MTEXPOLY) { - MTexPoly *txface = (MTexPoly *)me->pdata.layers[i].data; - MPoly *mpoly = me->mpoly; - for (int j = 0; j < me->totpoly; j++, mpoly++, txface++) { - - Image *ima = txface->tpage; - if (ima != NULL) { - if (UVImages.find(ima) == UVImages.end()) - UVImages.insert(ima); - } - } - } + //bNodeSocket *socket = ntreeAddSocketInterfaceFromSocket(ntree, to_node, to_socket); + //return socket; + + bNodeSocket *gsock = ntreeAddSocketInterfaceFromSocket(ntree, to_node, to_socket); + bNode *inputGroup = ntreeFindType(ntree, NODE_GROUP_INPUT); + node_group_input_verify(ntree, inputGroup, (ID *)ntree); + bNodeSocket *newsock = node_group_input_find_socket(inputGroup, gsock->identifier); + nodeAddLink(ntree, inputGroup, newsock, to_node, to_socket); + strcpy(newsock->name, label.c_str()); + return newsock; +} + +static bNodeSocket *bc_group_add_output_socket(bNodeTree *ntree, bNode *from_node, int from_index, std::string label) +{ + bNodeSocket *from_socket = (bNodeSocket *)BLI_findlink(&from_node->outputs, from_index); + + //bNodeSocket *socket = ntreeAddSocketInterfaceFromSocket(ntree, to_node, to_socket); + //return socket; + + bNodeSocket *gsock = ntreeAddSocketInterfaceFromSocket(ntree, from_node, from_socket); + bNode *outputGroup = ntreeFindType(ntree, NODE_GROUP_OUTPUT); + node_group_output_verify(ntree, outputGroup, (ID *)ntree); + bNodeSocket *newsock = node_group_output_find_socket(outputGroup, gsock->identifier); + nodeAddLink(ntree, from_node, from_socket, outputGroup, newsock); + strcpy(newsock->name, label.c_str()); + return newsock; +} + + +void bc_make_group(bContext *C, bNodeTree *ntree, std::map<std::string, bNode *> nmap) +{ + bNode * gnode = node_group_make_from_selected(C, ntree, "ShaderNodeGroup", "ShaderNodeTree"); + bNodeTree *gtree = (bNodeTree *)gnode->id; + + bc_group_add_input_socket(gtree, nmap["main"], 0, "Diffuse"); + bc_group_add_input_socket(gtree, nmap["emission"], 0, "Emission"); + bc_group_add_input_socket(gtree, nmap["mix"], 0, "Transparency"); + bc_group_add_input_socket(gtree, nmap["emission"], 1, "Emission"); + bc_group_add_input_socket(gtree, nmap["main"], 4, "Metallic"); + bc_group_add_input_socket(gtree, nmap["main"], 5, "Specular"); + + bc_group_add_output_socket(gtree, nmap["mix"], 0, "Shader"); +} +#endif + +static void bc_node_add_link(bNodeTree *ntree, bNode *from_node, int from_index, bNode *to_node, int to_index) +{ + bNodeSocket *from_socket = (bNodeSocket *)BLI_findlink(&from_node->outputs, from_index); + bNodeSocket *to_socket = (bNodeSocket *)BLI_findlink(&to_node->inputs, to_index); + + nodeAddLink(ntree, from_node, from_socket, to_node, to_socket); +} + +void bc_add_default_shader(bContext *C, Material *ma) +{ + bNodeTree *ntree = prepare_material_nodetree(ma); + std::map<std::string, bNode *> nmap; +#if 0 + nmap["main"] = bc_add_node(C, ntree, SH_NODE_BSDF_PRINCIPLED, -300, 300); + nmap["emission"] = bc_add_node(C, ntree, SH_NODE_EMISSION, -300, 500, "emission"); + nmap["add"] = bc_add_node(C, ntree, SH_NODE_ADD_SHADER, 100, 400); + nmap["transparent"] = bc_add_node(C, ntree, SH_NODE_BSDF_TRANSPARENT, 100, 200); + nmap["mix"] = bc_add_node(C, ntree, SH_NODE_MIX_SHADER, 400, 300, "transparency"); + nmap["out"] = bc_add_node(C, ntree, SH_NODE_OUTPUT_MATERIAL, 600, 300); + nmap["out"]->flag &= ~NODE_SELECT; + + bc_node_add_link(ntree, nmap["emission"], 0, nmap["add"], 0); + bc_node_add_link(ntree, nmap["main"], 0, nmap["add"], 1); + bc_node_add_link(ntree, nmap["add"], 0, nmap["mix"], 1); + bc_node_add_link(ntree, nmap["transparent"], 0, nmap["mix"], 2); + + bc_node_add_link(ntree, nmap["mix"], 0, nmap["out"], 0); + // experimental, probably not used. + bc_make_group(C, ntree, nmap); +#else +nmap["main"] = bc_add_node(C, ntree, SH_NODE_BSDF_PRINCIPLED, 0, 300); +nmap["out"] = bc_add_node(C, ntree, SH_NODE_OUTPUT_MATERIAL, 300, 300); +bc_node_add_link(ntree, nmap["main"], 0, nmap["out"], 0); +#endif +} + +COLLADASW::ColorOrTexture bc_get_base_color(Material *ma) +{ + bNode *master_shader = bc_get_master_shader(ma); + if (master_shader) { + return bc_get_base_color(master_shader); + } + else { + return bc_get_cot(ma->r, ma->g, ma->b, ma->alpha); + } +} + +COLLADASW::ColorOrTexture bc_get_specular_color(Material *ma, bool use_fallback) +{ + bNode *master_shader = bc_get_master_shader(ma); + if (master_shader) { + return bc_get_specular_color(master_shader); + } + else if (use_fallback) { + return bc_get_cot(ma->specr * ma->spec, ma->specg * ma->spec, ma->specb * ma->spec, 1.0f); + } + else { + return bc_get_cot(0.0, 0.0, 0.0, 1.0); // no specular + } +} + +COLLADASW::ColorOrTexture bc_get_base_color(bNode *shader) +{ + bNodeSocket *socket = nodeFindSocket(shader, SOCK_IN, "Base Color"); + if (socket) + { + bNodeSocketValueRGBA *dcol = (bNodeSocketValueRGBA *)socket->default_value; + float* col = dcol->value; + return bc_get_cot(col[0], col[1], col[2], col[3]); + } + else { + return bc_get_cot(0.8, 0.8, 0.8, 1.0); //default white + } +} + +COLLADASW::ColorOrTexture bc_get_specular_color(bNode *shader) +{ + bNodeSocket *socket = nodeFindSocket(shader, SOCK_IN, "Specular"); + if (socket) + { + bNodeSocketValueRGBA *dcol = (bNodeSocketValueRGBA *)socket->default_value; + float* col = dcol->value; + return bc_get_cot(col[0], col[1], col[2], col[3]); + } + else { + return bc_get_cot(0.8, 0.8, 0.8, 1.0); //default white + } +} + +bNode *bc_get_master_shader(Material *ma) +{ + bNodeTree *nodetree = ma->nodetree; + if (nodetree) { + for (bNode *node = (bNode *)nodetree->nodes.first; node; node = node->next) { + if (node->typeinfo->type == SH_NODE_BSDF_PRINCIPLED) { + return node; } } } - return UVImages; + return NULL; +} + +COLLADASW::ColorOrTexture bc_get_cot(float r, float g, float b, float a) +{ + COLLADASW::Color color(r, g, b, a); + COLLADASW::ColorOrTexture cot(color); + return cot; } |