[Bf-blender-cvs] [199017ac14c] collada2.8: feature: Collada: Added initial support for texture import
Gaia Clary
noreply at git.blender.org
Wed Nov 14 14:53:56 CET 2018
Commit: 199017ac14cf850978d7ed5ed64b655ba1257ece
Author: Gaia Clary
Date: Tue Nov 6 23:24:42 2018 +0100
Branches: collada2.8
https://developer.blender.org/rB199017ac14cf850978d7ed5ed64b655ba1257ece
feature: Collada: Added initial support for texture import
===================================================================
M source/blender/collada/CMakeLists.txt
M source/blender/collada/DocumentImporter.cpp
M source/blender/collada/DocumentImporter.h
M source/blender/collada/ErrorHandler.cpp
A source/blender/collada/Materials.cpp
A source/blender/collada/Materials.h
===================================================================
diff --git a/source/blender/collada/CMakeLists.txt b/source/blender/collada/CMakeLists.txt
index 2aca4cd57e5..240493ae5a9 100644
--- a/source/blender/collada/CMakeLists.txt
+++ b/source/blender/collada/CMakeLists.txt
@@ -72,6 +72,7 @@ set(SRC
MaterialExporter.cpp
MeshImporter.cpp
SkinInfo.cpp
+ Materials.cpp
SceneExporter.cpp
TransformReader.cpp
TransformWriter.cpp
@@ -103,6 +104,7 @@ set(SRC
LightExporter.h
MaterialExporter.h
MeshImporter.h
+ Materials.h
SkinInfo.h
SceneExporter.h
TransformReader.h
diff --git a/source/blender/collada/DocumentImporter.cpp b/source/blender/collada/DocumentImporter.cpp
index 6a72f86f64f..f1fdf8f83c8 100644
--- a/source/blender/collada/DocumentImporter.cpp
+++ b/source/blender/collada/DocumentImporter.cpp
@@ -92,7 +92,7 @@ extern "C" {
#include "collada_internal.h"
#include "collada_utils.h"
-
+#include "materials.h"
/*
* COLLADA Importer limitations:
@@ -767,31 +767,14 @@ bool DocumentImporter::writeMaterial(const COLLADAFW::Material *cmat)
void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
{
- add_material_shader(ef, ma);
+ MaterialNode matNode = MaterialNode(mContext, ef, ma, uid_image_map);
- // reflectivity
- ma->metallic = ef->getReflectivity().getFloatValue();
- // index of refraction
-#if 0
- ma->ang = ef->getIndexOfRefraction().getFloatValue();
-#endif
+ matNode.set_reflectivity(ef->getReflectivity().getFloatValue());
+ matNode.set_ior(ef->getIndexOfRefraction().getFloatValue());
+ matNode.set_diffuse(ef->getDiffuse());
COLLADAFW::Color col;
- // DIFFUSE
- // color
- if (ef->getDiffuse().isColor()) {
- col = ef->getDiffuse().getColor();
- ma->r = col.getRed();
- ma->g = col.getGreen();
- ma->b = col.getBlue();
- }
- // texture
- else if (ef->getDiffuse().isTexture()) {
-#if 0
- COLLADAFW::Texture ctex = ef->getDiffuse().getTexture();
-#endif
- }
// AMBIENT
// color
if (ef->getAmbient().isColor()) {
@@ -805,6 +788,7 @@ void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Materia
COLLADAFW::Texture ctex = ef->getAmbient().getTexture();
#endif
}
+
// SPECULAR
// color
if (ef->getSpecular().isColor()) {
@@ -819,6 +803,7 @@ void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Materia
COLLADAFW::Texture ctex = ef->getSpecular().getTexture();
#endif
}
+
// REFLECTIVE
// color
if (ef->getReflective().isColor()) {
@@ -868,38 +853,6 @@ 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 eeceff45f0c..5d5c5df2aaf 100644
--- a/source/blender/collada/DocumentImporter.h
+++ b/source/blender/collada/DocumentImporter.h
@@ -173,7 +173,6 @@ private:
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/ErrorHandler.cpp b/source/blender/collada/ErrorHandler.cpp
index 22ec432d237..d567127879c 100644
--- a/source/blender/collada/ErrorHandler.cpp
+++ b/source/blender/collada/ErrorHandler.cpp
@@ -69,7 +69,7 @@ bool ErrorHandler::handleError(const COLLADASaxFWL::IError *error)
}
}
- if (parserError.getErrorType() == GeneratedSaxParser::ParserError::ERROR_VALIDATION_SEQUENCE_PREVIOUS_SIBLING_NOT_PRESENT) {
+ else if (parserError.getErrorType() == GeneratedSaxParser::ParserError::ERROR_VALIDATION_SEQUENCE_PREVIOUS_SIBLING_NOT_PRESENT) {
if (!(STREQ(parserError.getElement(), "extra") &&
STREQ(parserError.getAdditionalText().c_str(), "sibling: fx_profile_abstract")))
{
@@ -77,10 +77,12 @@ bool ErrorHandler::handleError(const COLLADASaxFWL::IError *error)
}
}
- if (parserError.getErrorType() == GeneratedSaxParser::ParserError::ERROR_COULD_NOT_OPEN_FILE) {
+ else if (parserError.getErrorType() == GeneratedSaxParser::ParserError::ERROR_COULD_NOT_OPEN_FILE) {
isError = true;
error_context = "File access";
}
+
+ else isError = (parserError.getSeverity() != COLLADASaxFWL::IError::SEVERITY_ERROR_NONCRITICAL);
}
else if (error->getErrorClass() == COLLADASaxFWL::IError::ERROR_SAXFWL) {
error_context = "Sax FWL";
diff --git a/source/blender/collada/Materials.cpp b/source/blender/collada/Materials.cpp
new file mode 100644
index 00000000000..38e43aaedca
--- /dev/null
+++ b/source/blender/collada/Materials.cpp
@@ -0,0 +1,167 @@
+/*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* Contributor(s): Gaia Clary.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#include "Materials.h"
+
+MaterialNode::MaterialNode(bContext *C, COLLADAFW::EffectCommon *ef, Material *ma, Image_map &uid_image_map) :
+ mContext(C),
+ effect(ef),
+ material(ma),
+ uid_image_map(uid_image_map)
+{
+ ntree = prepare_material_nodetree();
+ setShaderType();
+
+ 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
+ shader_node = bc_add_node(SH_NODE_BSDF_PRINCIPLED, 0, 300, "");
+ output_node = bc_add_node(SH_NODE_OUTPUT_MATERIAL, 300, 300, "");
+ bc_node_add_link(shader_node, 0, output_node, 0);
+#endif
+}
+
+void MaterialNode::setShaderType()
+{
+#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
+}
+
+bNodeTree *MaterialNode::prepare_material_nodetree()
+{
+ if (material->nodetree == NULL) {
+ material->nodetree = ntreeAddTree(NULL, "Shader Nodetree", "ShaderNodeTree");
+ material->use_nodes = true;
+ }
+ return material->nodetree;
+}
+
+bNode *MaterialNode::bc_add_node(int node_type, int locx, int locy, std::string label)
+{
+ bNode *node = nodeAddStaticNode(mContext, 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;
+}
+
+void MaterialNode::bc_node_add_link(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 MaterialNode::set_reflectivity(float val)
+{
+ material->metallic = val;
+ bNodeSocket *socket = (bNodeSocket *)BLI_findlink(&shader_node->inputs, BC_PBR_METALLIC);
+ *(float *)socket->default_value = val;
+}
+
+void MaterialNode::set_ior(float val)
+{
+ bNodeSocket *socket = (bNodeSocket *)BLI_findlink(&shader_node->inputs, BC_PBR_IOR);
+ *(float *)socket->default_value = val;
+}
+
+void MaterialNode::set_diffuse(COLLADAFW::ColorOrTexture &cot)
+{
+ if (cot.isColor()) {
+ COLLADAFW::Color col = cot.getColor();
+ material->r = col.getRed();
+ material->g = col.getGreen();
+ material->b = col.getBlue();
+
+ bNodeSocket *so
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list