------------------------------------------------------------------------------
| 369 | |
| 370 | //------------------------------------------------------------------------------ |
| 371 | bool BuildMultiBlockDatasetFromMesh(vtkGLTFDocumentLoader::Model& m, unsigned int meshId, |
| 372 | vtkSmartPointer<vtkMultiBlockDataSet> parentDataSet, |
| 373 | vtkSmartPointer<vtkMultiBlockDataSet> meshDataSet, std::string& dataSetName, |
| 374 | vtkSmartPointer<vtkMatrix4x4> globalTransform, |
| 375 | const std::vector<vtkSmartPointer<vtkMatrix4x4>>& jointMats, bool applyDeformations, |
| 376 | std::vector<float>* morphingWeights, int outputPointsPrecision) |
| 377 | { |
| 378 | if (meshId >= m.Meshes.size()) |
| 379 | { |
| 380 | vtkErrorWithObjectMacro(nullptr, "Invalid mesh index " << meshId); |
| 381 | return false; |
| 382 | } |
| 383 | bool createNewPolyData = false; |
| 384 | // If meshDataSet is not defined, create it and append it to the parent dataset. |
| 385 | if (!meshDataSet && !createNewPolyData) |
| 386 | { |
| 387 | createNewPolyData = true; |
| 388 | meshDataSet = vtkSmartPointer<vtkMultiBlockDataSet>::New(); |
| 389 | parentDataSet->SetBlock(parentDataSet->GetNumberOfBlocks(), meshDataSet); |
| 390 | parentDataSet->GetMetaData(parentDataSet->GetNumberOfBlocks() - 1) |
| 391 | ->Set(vtkCompositeDataSet::NAME(), dataSetName); |
| 392 | } |
| 393 | |
| 394 | vtkGLTFDocumentLoader::Mesh mesh = m.Meshes[meshId]; |
| 395 | |
| 396 | int blockId = 0; |
| 397 | for (auto& primitive : mesh.Primitives) |
| 398 | { |
| 399 | vtkSmartPointer<vtkPolyData> meshPolyData; |
| 400 | |
| 401 | // Even though no weights are defined in the node, meshes may contain default weights |
| 402 | if ((morphingWeights == nullptr || morphingWeights->empty()) && !mesh.Weights.empty()) |
| 403 | { |
| 404 | morphingWeights = &(mesh.Weights); |
| 405 | } |
| 406 | // Apply deformations (skins and morph targets to each primitive's geometry, then add the |
| 407 | // resulting polydata to the parent dataSet) |
| 408 | if (applyDeformations) |
| 409 | { |
| 410 | meshPolyData = vtkSmartPointer<vtkPolyData>::New(); |
| 411 | meshPolyData->ShallowCopy(primitive.Geometry); |
| 412 | // Add material information to fieldData |
| 413 | AddMaterialToFieldData(primitive.Material, meshPolyData->GetFieldData(), m); |
| 414 | |
| 415 | vtkNew<vtkTransformPolyDataFilter> filter; |
| 416 | filter->SetOutputPointsPrecision(outputPointsPrecision); |
| 417 | |
| 418 | // Morphing |
| 419 | if (morphingWeights != nullptr && !morphingWeights->empty()) |
| 420 | { |
| 421 | // Number of weights should be equal to the number of targets |
| 422 | if (morphingWeights->size() != primitive.Targets.size()) |
| 423 | { |
| 424 | vtkErrorWithObjectMacro(nullptr, "Invalid number of morphing weights"); |
| 425 | return false; |
| 426 | } |
| 427 | ApplyMorphingToPolyData( |
| 428 | primitive.Targets, morphingWeights, primitive.Geometry, meshPolyData); |
no test coverage detected