[Bf-blender-cvs] [6e5f271a006] collada2.8: added initial support for material import (wip)

Gaia Clary noreply at git.blender.org
Tue Nov 6 16:55:17 CET 2018


Commit: 6e5f271a006b4cd5193507c9de94ca0f462e132a
Author: Gaia Clary
Date:   Tue Nov 6 16:41:21 2018 +0100
Branches: collada2.8
https://developer.blender.org/rB6e5f271a006b4cd5193507c9de94ca0f462e132a

added initial support for material import (wip)

===================================================================

M	source/blender/collada/AnimationImporter.cpp
M	source/blender/collada/AnimationImporter.h
M	source/blender/collada/DocumentImporter.cpp
M	source/blender/collada/DocumentImporter.h
M	source/blender/collada/EffectExporter.cpp
M	source/blender/collada/EffectExporter.h
M	source/blender/collada/collada_utils.cpp
M	source/blender/collada/collada_utils.h

===================================================================

diff --git a/source/blender/collada/AnimationImporter.cpp b/source/blender/collada/AnimationImporter.cpp
index 23e54fa1e85..b581c6647ba 100644
--- a/source/blender/collada/AnimationImporter.cpp
+++ b/source/blender/collada/AnimationImporter.cpp
@@ -1303,7 +1303,7 @@ AnimationImporter::AnimMix *AnimationImporter::get_animation_type(const COLLADAF
 			continue;
 		}
 		else {
-			types->transform = types->transform | NODE_TRANSFORM;
+			types->transform = types->transform | BC_NODE_TRANSFORM;
 			break;
 		}
 	}
diff --git a/source/blender/collada/AnimationImporter.h b/source/blender/collada/AnimationImporter.h
index cfee9d12af2..4fa4864cbb0 100644
--- a/source/blender/collada/AnimationImporter.h
+++ b/source/blender/collada/AnimationImporter.h
@@ -125,8 +125,8 @@ private:
 
 	enum AnimationType
 		{
-			INANIMATE = 0,
-			NODE_TRANSFORM = 1,
+			BC_INANIMATE = 0,
+			BC_NODE_TRANSFORM = 1
 		};
 
 	struct AnimMix
diff --git a/source/blender/collada/DocumentImporter.cpp b/source/blender/collada/DocumentImporter.cpp
index 82133c42e56..6a72f86f64f 100644
--- a/source/blender/collada/DocumentImporter.cpp
+++ b/source/blender/collada/DocumentImporter.cpp
@@ -764,39 +764,11 @@ bool DocumentImporter::writeMaterial(const COLLADAFW::Material *cmat)
 	return true;
 }
 
+
 void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
 {
-	COLLADAFW::EffectCommon::ShaderType shader = ef->getShaderType();
+	add_material_shader(ef, ma);
 
-	// TODO: add back texture and extended material parameter support
-
-	// blinn
-	if (shader == COLLADAFW::EffectCommon::SHADER_BLINN) {
-#if 0
-		ma->spec_shader = MA_SPEC_BLINN;
-		ma->spec = ef->getShininess().getFloatValue();
-#endif
-	}
-	// phong
-	else if (shader == COLLADAFW::EffectCommon::SHADER_PHONG) {
-#if 0
-		ma->spec_shader = MA_SPEC_PHONG;
-		ma->har = ef->getShininess().getFloatValue();
-#endif
-	}
-	// lambert
-	else if (shader == COLLADAFW::EffectCommon::SHADER_LAMBERT) {
-#if 0
-		ma->diff_shader = MA_DIFF_LAMBERT;
-#endif
-	}
-	// default - lambert
-	else {
-#if 0
-		ma->diff_shader = MA_DIFF_LAMBERT;
-		fprintf(stderr, "Current shader type is not supported, default to lambert.\n");
-#endif
-	}
 	// reflectivity
 	ma->metallic = ef->getReflectivity().getFloatValue();
 	// index of refraction
@@ -896,6 +868,38 @@ void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Materia
 	}
 }
 
+void DocumentImporter::add_material_shader(COLLADAFW::EffectCommon *ef, Material *ma)
+{
+	bc_add_default_shader(mContext, ma);
+
+#if 0
+	COLLADAFW::EffectCommon::ShaderType shader = ef->getShaderType();
+	// Currently we only support PBR based shaders
+	// TODO: simulate the effects with PBR
+
+	// blinn
+	if (shader == COLLADAFW::EffectCommon::SHADER_BLINN) {
+		ma->spec_shader = MA_SPEC_BLINN;
+		ma->spec = ef->getShininess().getFloatValue();
+	}
+	// phong
+	else if (shader == COLLADAFW::EffectCommon::SHADER_PHONG) {
+		ma->spec_shader = MA_SPEC_PHONG;
+		ma->har = ef->getShininess().getFloatValue();
+	}
+	// lambert
+	else if (shader == COLLADAFW::EffectCommon::SHADER_LAMBERT) {
+		ma->diff_shader = MA_DIFF_LAMBERT;
+	}
+	// default - lambert
+	else {
+		ma->diff_shader = MA_DIFF_LAMBERT;
+		fprintf(stderr, "Current shader type is not supported, default to lambert.\n");
+	}
+#endif
+
+}
+
 /** When this method is called, the writer must write the effect.
  * \return The writer should return true, if writing succeeded, false otherwise.*/
 
diff --git a/source/blender/collada/DocumentImporter.h b/source/blender/collada/DocumentImporter.h
index caac933c3c8..eeceff45f0c 100644
--- a/source/blender/collada/DocumentImporter.h
+++ b/source/blender/collada/DocumentImporter.h
@@ -172,6 +172,8 @@ private:
 	std::string import_from_version;
 
 	void report_unknown_reference(const COLLADAFW::Node &node, const std::string object_type);
+
+	void add_material_shader(COLLADAFW::EffectCommon *ef, Material *ma);
 };
 
 #endif
diff --git a/source/blender/collada/EffectExporter.cpp b/source/blender/collada/EffectExporter.cpp
index a9bddbbc527..94436b68351 100644
--- a/source/blender/collada/EffectExporter.cpp
+++ b/source/blender/collada/EffectExporter.cpp
@@ -96,10 +96,34 @@ void EffectsExporter::exportEffects(Scene *sce)
 	}
 }
 
-void EffectsExporter::writeLambert(COLLADASW::EffectProfile &ep, Material *ma)
+void EffectsExporter::set_shader_type(COLLADASW::EffectProfile &ep, Material *ma)
 {
-	COLLADASW::ColorOrTexture cot;
-	ep.setShaderType(COLLADASW::EffectProfile::LAMBERT);
+	ep.setShaderType(COLLADASW::EffectProfile::LAMBERT); //XXX check if BLINN and PHONG can be supported as well
+}
+
+void EffectsExporter::set_transparency(COLLADASW::EffectProfile &ep, Material *ma)
+{
+	if (ma->alpha == 1.0f) {
+		return; // have no transparency
+	}
+
+	// Tod: because we are in A_ONE mode transparency is calculated like this:
+	COLLADASW::ColorOrTexture cot = getcol(1.0f, 1.0f, 1.0f, ma->alpha);
+	ep.setTransparent(cot);
+	ep.setOpaque(COLLADASW::EffectProfile::A_ONE);
+}
+void EffectsExporter::set_diffuse_color(COLLADASW::EffectProfile &ep, Material *ma)
+{
+	// get diffuse color
+	COLLADASW::ColorOrTexture cot = bc_get_base_color(ma);
+	ep.setDiffuse(cot, false, "diffuse");
+}
+
+void EffectsExporter::set_specular_color(COLLADASW::EffectProfile &ep, Material *ma)
+{
+	bool use_fallback = ep.getShaderType() != COLLADASW::EffectProfile::LAMBERT;
+	COLLADASW::ColorOrTexture cot = bc_get_specular_color(ma, use_fallback);
+	ep.setSpecular(cot, false, "specular");
 }
 
 void EffectsExporter::operator()(Material *ma, Object *ob)
@@ -111,33 +135,19 @@ void EffectsExporter::operator()(Material *ma, Object *ob)
 	COLLADASW::EffectProfile ep(mSW);
 	ep.setProfileType(COLLADASW::EffectProfile::COMMON);
 	ep.openProfile();
-	writeLambert(ep, ma);
+	set_shader_type(ep, ma);
 
 	COLLADASW::ColorOrTexture cot;
 
-	// transparency
-	if (ma->alpha != 1.0f) {
-		// Tod: because we are in A_ONE mode transparency is calculated like this:
-		cot = getcol(1.0f, 1.0f, 1.0f, ma->alpha);
-		ep.setTransparent(cot);
-		ep.setOpaque(COLLADASW::EffectProfile::A_ONE);
-	}
+	set_transparency(ep, ma);
+	set_diffuse_color(ep, ma);
+	set_specular_color(ep, ma);
 
 	// emission
 #if 0
 	cot = getcol(ma->emit, ma->emit, ma->emit, 1.0f);
 #endif
 
-	// diffuse
-	cot = getcol(ma->r, ma->g, ma->b, 1.0f);
-	ep.setDiffuse(cot, false, "diffuse");
-
-	// specular
-	if (ep.getShaderType() != COLLADASW::EffectProfile::LAMBERT) {
-		cot = getcol(ma->specr * ma->spec, ma->specg * ma->spec, ma->specb * ma->spec, 1.0f);
-		ep.setSpecular(cot, false, "specular");
-	}
-
 	// XXX make this more readable if possible
 
 #if 0
diff --git a/source/blender/collada/EffectExporter.h b/source/blender/collada/EffectExporter.h
index a1395bfde9f..9afccd2620d 100644
--- a/source/blender/collada/EffectExporter.h
+++ b/source/blender/collada/EffectExporter.h
@@ -58,7 +58,11 @@ public:
 
 	COLLADASW::ColorOrTexture getcol(float r, float g, float b, float a);
 private:
-	void writeLambert(COLLADASW::EffectProfile &ep, Material *ma);
+	void set_shader_type(COLLADASW::EffectProfile &ep, Material *ma);
+	void set_transparency(COLLADASW::EffectProfile &ep, Material *ma);
+	void set_diffuse_color(COLLADASW::EffectProfile &ep, Material *ma);
+	void set_specular_color(COLLADASW::EffectProfile &ep, Material *ma);
+
 	void writeTextures(COLLADASW::EffectProfile &ep,
 			std::string &key,
 			COLLADASW::Sampler *sampler,
diff --git a/source/blender/collada/collada_utils.cpp b/source/blender/collada/collada_utils.cpp
index 36bb5997ddb..4d8f4b15783 100644
--- a/source/blender/collada/collada_utils.cpp
+++ b/source/blender/collada/collada_utils.cpp
@@ -33,6 +33,7 @@
 #include "COLLADAFWMeshVertexData.h"
 
 #include <set>
+#include <string>
 extern "C" {
 #include "DNA_modifier_types.h"
 #include "DNA_customdata_types.h"
@@ -45,6 +46,7 @@ extern "C" {
 
 #include "BLI_math.h"
 #include "BLI_linklist.h"
+#include "BLI_listbase.h"
 
 #include "BKE_action.h"
 #include "BKE_context.h"
@@ -52,6 +54,7 @@ extern "C" {
 #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"
@@ -62,6 +65,7 @@ extern "C" {
 
 #include "ED_armature.h"
 #include "ED_screen.h"
+#include "ED_node.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -73,6 +77,9 @@ extern "C" {
 
 #include "DEG_depsgraph.h"
 #include "DEG_depsgraph_query.h"
+#if 0
+#include "NOD_common.h"
+#endif
 }
 
 #include "collada_utils.h"
@@ -1211,4 +1218,193 @@ std::string bc_find_bonename_in_path(std::string path, std::string probe)
 		MEM_freeN(boneName);
 	}
 	return result;
-}
\ No newline at end of file
+}
+
+static bNodeTree *prepare_material_nodetree(Material *ma)
+{
+	if (ma->nodetree == NULL) {
+		ma->nodetree = ntreeAddTree(NULL, "Shader Nodetree", "ShaderNodeTree");
+		ma->use_nodes = true;
+	}
+	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());
+		}
+		node->locx = locx;
+		node->locy = locy;
+		node->flag |= NODE_SELECT;
+	}
+	return node;
+}
+
+
+bNode *bc_add_node(bContext *C, bNodeTree *ntree, int node_type, int locx, int locy)
+{
+	return bc_add_node(C, ntree, node_type, locx, locy, "");
+}
+
+#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);
+	
+	//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_sock

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list