From 106d937a4e1ebafff244c67f208e4c651a11ed3a Mon Sep 17 00:00:00 2001 From: Gaia Clary Date: Thu, 30 Jun 2022 22:12:38 +0200 Subject: COLLADA: Support for alpha color in vertex data. Many thanks to the original Author of this patch: Christian Aguilera The COLLADA importer was silently ignoring the alpha component in the vertex data. The `stride` variable holds the component count (3 for RGB; 4 for RGBA), and can be used for honouring the alpha channel in the vertex data. Test plan: - Open Blender. - Clear the scene. - Add a plane. - Enter **Vertex Paint** mode. - Switch to the **Erase Alpha** blending mode. - Select a tone of gray. - Turn strength down to less than 1 - Paint [some of] the vertices of the plane. - Export project as a COLLADA file (`.dae`). - Clear the scene. - Re-import the COLLADA file again. - Export the project again (with different name). **Without** this patch, the second exported project will have lost the alpha component in their vertex data: ```lang=xml, counterexample 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ``` **With** the patch, the first and the second exported projects retain the alpha values painted previously: ```lang=xml 1 1 1 1 1 1 1 0.5490196 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0.5490196 ``` Reviewed By: cristian64, SonnyCampbell_Unity Authored by: Christian Aguilera Differential Revision: https://developer.blender.org/D14246 --- source/blender/io/collada/MeshImporter.cpp | 38 +++++++++++++++++++----------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'source/blender/io/collada/MeshImporter.cpp') diff --git a/source/blender/io/collada/MeshImporter.cpp b/source/blender/io/collada/MeshImporter.cpp index 6e109353be8..fa0348fbcf2 100644 --- a/source/blender/io/collada/MeshImporter.cpp +++ b/source/blender/io/collada/MeshImporter.cpp @@ -145,6 +145,27 @@ VCOLDataWrapper::VCOLDataWrapper(COLLADAFW::MeshVertexData &vdata) : mVData(&vda { } +template +static void colladaAddColor(T values, MLoopCol *mloopcol, int v_index, int stride) +{ + if (values->empty() || values->getCount() < (v_index + 1) * stride) { + fprintf(stderr, + "VCOLDataWrapper.getvcol(): Out of Bounds error: index %d points outside value " + "list of length %zd (with stride=%d) \n", + v_index, + values->getCount(), + stride); + return; + } + + mloopcol->r = unit_float_to_uchar_clamp((*values)[v_index * stride]); + mloopcol->g = unit_float_to_uchar_clamp((*values)[v_index * stride + 1]); + mloopcol->b = unit_float_to_uchar_clamp((*values)[v_index * stride + 2]); + if (stride == 4) { + mloopcol->a = unit_float_to_uchar_clamp((*values)[v_index * stride + 3]); + } +} + void VCOLDataWrapper::get_vcol(int v_index, MLoopCol *mloopcol) { int stride = mVData->getStride(0); @@ -155,25 +176,14 @@ void VCOLDataWrapper::get_vcol(int v_index, MLoopCol *mloopcol) switch (mVData->getType()) { case COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT: { COLLADAFW::ArrayPrimitiveType *values = mVData->getFloatValues(); - if (values->empty() || values->getCount() <= (v_index * stride + 2)) { - return; /* XXX: need to create an error instead. */ - } - - mloopcol->r = unit_float_to_uchar_clamp((*values)[v_index * stride]); - mloopcol->g = unit_float_to_uchar_clamp((*values)[v_index * stride + 1]); - mloopcol->b = unit_float_to_uchar_clamp((*values)[v_index * stride + 2]); + colladaAddColor *>(values, mloopcol, v_index, stride); } break; case COLLADAFW::MeshVertexData::DATA_TYPE_DOUBLE: { COLLADAFW::ArrayPrimitiveType *values = mVData->getDoubleValues(); - if (values->empty() || values->getCount() <= (v_index * stride + 2)) { - return; /* XXX: need to create an error instead. */ - } - - mloopcol->r = unit_float_to_uchar_clamp((*values)[v_index * stride]); - mloopcol->g = unit_float_to_uchar_clamp((*values)[v_index * stride + 1]); - mloopcol->b = unit_float_to_uchar_clamp((*values)[v_index * stride + 2]); + colladaAddColor *>(values, mloopcol, v_index, stride); } break; + default: fprintf(stderr, "VCOLDataWrapper.getvcol(): unknown data type\n"); } -- cgit v1.2.3