[Bf-blender-cvs] [d7901ed6070] blender-v2.93-release: COLLADA: Support for alpha color in vertex data. Many thanks to the original Author of this patch: Christian Aguilera

Gaia Clary noreply at git.blender.org
Sat Jul 2 22:44:03 CEST 2022


Commit: d7901ed6070874685579775f78d0775692cba816
Author: Gaia Clary
Date:   Thu Jun 30 22:12:38 2022 +0200
Branches: blender-v2.93-release
https://developer.blender.org/rBd7901ed6070874685579775f78d0775692cba816

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
<float_array id="Plane-mesh-colors-Col-array" count="24">1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1</float_array>
```

**With** the patch, the first and the second exported projects retain
the alpha values painted previously:

```lang=xml
<float_array id="Plane-mesh-colors-Col-array" count="24">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</float_array>
```

Reviewed By: cristian64, SonnyCampbell_Unity
Authored by: Christian Aguilera

Differential Revision: https://developer.blender.org/D14246

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

M	source/blender/io/collada/MeshImporter.cpp

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

diff --git a/source/blender/io/collada/MeshImporter.cpp b/source/blender/io/collada/MeshImporter.cpp
index 172f9a468b4..ccea6a06db1 100644
--- a/source/blender/io/collada/MeshImporter.cpp
+++ b/source/blender/io/collada/MeshImporter.cpp
@@ -159,6 +159,27 @@ VCOLDataWrapper::VCOLDataWrapper(COLLADAFW::MeshVertexData &vdata) : mVData(&vda
 {
 }
 
+template<typename T>
+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);
@@ -169,25 +190,14 @@ void VCOLDataWrapper::get_vcol(int v_index, MLoopCol *mloopcol)
   switch (mVData->getType()) {
     case COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT: {
       COLLADAFW::ArrayPrimitiveType<float> *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<COLLADAFW::ArrayPrimitiveType<float> *>(values, mloopcol, v_index, stride);
     } break;
 
     case COLLADAFW::MeshVertexData::DATA_TYPE_DOUBLE: {
       COLLADAFW::ArrayPrimitiveType<double> *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<COLLADAFW::ArrayPrimitiveType<double> *>(values, mloopcol, v_index, stride);
     } break;
+
     default:
       fprintf(stderr, "VCOLDataWrapper.getvcol(): unknown data type\n");
   }



More information about the Bf-blender-cvs mailing list