2#include <GLPhotorealisticRender.h>
4#define NULL_TIME (-9599.99)
5#define NULL_POSITION (-959959.9956)
6#define TINYGLTF_IMPLEMENTATION
8#include "tinygltf/tiny_gltf.h"
11#include "ImageLoader.h"
15 template<typename uint>
16 __global__ void C_Shape_PointCounter(
18 DArray<uint> point_ShapeIds,
21 IMPLEMENT_CLASS(BoundingBoxOfTextureMesh);
23 BoundingBoxOfTextureMesh::BoundingBoxOfTextureMesh()
26 auto callback = std::make_shared<FCallBackFunc>(std::bind(&BoundingBoxOfTextureMesh::shapeIdChanged, this));
28 this->varShapeId()->attach(callback);
31 void BoundingBoxOfTextureMesh::compute()
36 void BoundingBoxOfTextureMesh::shapeIdChanged()
38 uint shapeId = this->varShapeId()->getValue();
39 auto mesh = this->inTextureMesh()->constDataPtr();
46 if (this->outBoundingBox()->isEmpty())
48 this->outBoundingBox()->allocate();
51 auto& es = this->outBoundingBox()->getData();
53 if (shapeId < mesh->shapes().size())
55 auto bb = mesh->shapes()[shapeId]->boundingBox;
57 this->varCenter()->setValue((bb.v0 + bb.v1) / 2);
58 this->varLowerBound()->setValue(bb.v0);
59 this->varUpperBound()->setValue(bb.v1);
61 std::vector<Vec3f> vertices;
64 Vec3f v1 = Vec3f(bb.v0.x, bb.v0.y, bb.v1.z);
65 Vec3f v2 = Vec3f(bb.v1.x, bb.v0.y, bb.v1.z);
66 Vec3f v3 = Vec3f(bb.v1.x, bb.v0.y, bb.v0.z);
68 Vec3f v4 = Vec3f(bb.v0.x, bb.v1.y, bb.v0.z);
69 Vec3f v5 = Vec3f(bb.v0.x, bb.v1.y, bb.v1.z);
71 Vec3f v7 = Vec3f(bb.v1.x, bb.v1.y, bb.v0.z);
73 vertices.push_back(v0);
74 vertices.push_back(v1);
75 vertices.push_back(v2);
76 vertices.push_back(v3);
77 vertices.push_back(v4);
78 vertices.push_back(v5);
79 vertices.push_back(v6);
80 vertices.push_back(v7);
82 std::vector<TopologyModule::Edge> edges;
83 edges.push_back(TopologyModule::Edge(0, 1));
84 edges.push_back(TopologyModule::Edge(1, 2));
85 edges.push_back(TopologyModule::Edge(2, 3));
86 edges.push_back(TopologyModule::Edge(3, 0));
88 edges.push_back(TopologyModule::Edge(4, 5));
89 edges.push_back(TopologyModule::Edge(5, 6));
90 edges.push_back(TopologyModule::Edge(6, 7));
91 edges.push_back(TopologyModule::Edge(7, 4));
93 edges.push_back(TopologyModule::Edge(0, 4));
94 edges.push_back(TopologyModule::Edge(1, 5));
95 edges.push_back(TopologyModule::Edge(2, 6));
96 edges.push_back(TopologyModule::Edge(3, 7));
98 es.setPoints(vertices);
107 template<typename TDataType>
108 GltfLoader<TDataType>::GltfLoader()
110 auto callback = std::make_shared<FCallBackFunc>(std::bind(&GltfLoader<TDataType>::varChanged, this));
111 auto animationCallback = std::make_shared<FCallBackFunc>(std::bind(&GltfLoader<TDataType>::varAnimation, this));
113 this->stateJointSet()->setDataPtr(std::make_shared<EdgeSet<DataType3f>>());
114 this->stateShapeCenter()->setDataPtr(std::make_shared<PointSet<DataType3f>>());
116 this->varImportAnimation()->attach(callback);
117 this->varImportAnimation()->attach(animationCallback);
118 this->varFileName()->attach(callback);
119 this->varAnimationSpeed()->setRange(0.02,10);
121 auto callbackTransform = std::make_shared<FCallBackFunc>(std::bind(&GltfLoader<TDataType>::updateTransform, this));
123 this->varLocation()->attach(callbackTransform);
124 this->varScale()->attach(callbackTransform);
125 this->varRotation()->attach(callbackTransform);
126 this->varUseInstanceTransform()->attach(callbackTransform);
129 this->stateTextureMesh()->setDataPtr(std::make_shared<TextureMesh>());
131 auto render = std::make_shared<GLPhotorealisticRender>();
132 this->stateTextureMesh()->connect(render->inTextureMesh());
133 this->graphicsPipeline()->pushModule(render);
136 auto callbackRender = std::make_shared<FCallBackFunc>(std::bind(&GltfLoader<TDataType>::varRenderChanged, this));
137 this->varJointRadius()->attach(callbackRender);
139 jointPointRender = std::make_shared<GLPointVisualModule>();
140 jointPointRender->setColor(Color(1.0f, 0.0f, 0.0f));
141 jointPointRender->varPointSize()->setValue(this->varJointRadius()->getValue());
142 jointPointRender->setVisible(true);
143 this->stateJointSet()->connect(jointPointRender->inPointSet());
144 this->graphicsPipeline()->pushModule(jointPointRender);
146 jointLineRender = std::make_shared<GLWireframeVisualModule>();
147 jointLineRender->varBaseColor()->setValue(Color(0, 1, 0));
148 jointLineRender->setVisible(true);
149 jointLineRender->varRadius()->setValue(this->varJointRadius()->getValue() / 3);
150 jointLineRender->varRenderMode()->setCurrentKey(GLWireframeVisualModule::EEdgeMode::CYLINDER);
151 this->stateJointSet()->connect(jointLineRender->inEdgeSet());
152 this->graphicsPipeline()->pushModule(jointLineRender);
154 this->stateAnimation()->setDataPtr(std::make_shared<JointAnimationInfo>());
155 this->stateAnimation()->promoteOuput();
157 auto glShapeCenter = std::make_shared<GLPointVisualModule>();
158 glShapeCenter->setColor(Color(1.0f, 1.0f, 0.0f));
159 glShapeCenter->varPointSize()->setValue(this->varJointRadius()->getValue() * 2);
160 glShapeCenter->setVisible(true);
161 this->stateShapeCenter()->connect(glShapeCenter->inPointSet());
162 this->graphicsPipeline()->pushModule(glShapeCenter);
164 auto showBoundingBox = std::make_shared<BoundingBoxOfTextureMesh>();
165 this->stateTextureMesh()->connect(showBoundingBox->inTextureMesh());
166 this->graphicsPipeline()->pushModule(showBoundingBox);
168 auto bbRender = std::make_shared<GLWireframeVisualModule>();
169 showBoundingBox->outBoundingBox()->connect(bbRender->inEdgeSet());
170 this->graphicsPipeline()->pushModule(bbRender);
172 this->stateSkin()->setDataPtr(std::make_shared<SkinInfo>());
173 this->stateJointsData()->setDataPtr(std::make_shared<JointInfo>());
175 this->stateTextureMesh()->promoteOuput();
177 this->setForceUpdate(false);
181 template<typename TDataType>
182 void GltfLoader<TDataType>::varAnimation()
184 auto importAnimation = this->varImportAnimation()->getValue();
186 this->setForceUpdate(true);
188 this->setForceUpdate(false);
195 template<typename TDataType>
196 void GltfLoader<TDataType>::varChanged()
198 if (this->varFileName()->isEmpty())
201 this->updateTransformState();
203 printf("!!!!!!!!!!!!!!!!! Import GLTF !!!!!!!!!!!!!!!!!!!!!!!!\n\n\n");
205 this->InitializationData();
208 using namespace tinygltf;
210 auto newModel = new Model;
216 std::string filename = this->varFileName()->getValue().string();
219 bool ret = loader.LoadASCIIFromFile(newModel, &err, &warn, filename);
222 printf("Warn: %s\n", warn.c_str());
226 printf("Err: %s\n", err.c_str());
230 printf("Failed to parse glTF\n");
235 std::map<joint, std::vector<Real>> joint_T_Time;
236 std::map<joint, std::vector<Real>> joint_S_Time;
237 std::map<joint, std::vector<Real>> joint_R_Time;
238 std::map<joint, std::vector<Vec3f>> joint_T_f_anim;
239 std::map<joint, std::vector<Quat<float>>> joint_R_f_anim;
240 std::map<joint, std::vector<Vec3f>> joint_S_f_anim;
242 importAnimation(*newModel, joint_output, joint_input, joint_T_f_anim, joint_T_Time, joint_S_f_anim, joint_S_Time, joint_R_f_anim, joint_R_Time);
244 for (int i = 0; i< newModel->nodes.size();i++)
246 node_Name[i] = newModel->nodes[i].name;
251 std::map<scene, std::vector<int>> Scene_Nodes;
252 for (size_t i = 0; i < newModel->scenes.size(); i++)
254 std::vector<int> vecS_Roots;
255 vecS_Roots = newModel->scenes[i].nodes;
256 Scene_Nodes[i] = vecS_Roots;
258 std::vector<int> all_Nodes;
259 std::map<joint, std::vector<int>> nodeId_Dir;
262 std::vector<int> all_Meshs;
263 std::map<int, std::vector<int>> meshId_Dir;
265 getNodesAndHierarchy(*newModel, Scene_Nodes, all_Nodes, nodeId_Dir); //jointId_joint_Dir //update private: std::map<joint, std::vector<int>> jointId_joint_Dir;
267 updateJoint_Mesh_Camera_Dir(*newModel, jointNum, meshNum, jointId_joint_Dir, all_Joints, all_Nodes, nodeId_Dir, meshId_Dir, all_Meshs, maxJointId);
269 std::vector<std::vector<int>> joint_child; //build edgeset;
271 //get Local Transform T S R M
272 getJointsTransformData(all_Nodes, joint_child, joint_rotation, joint_scale, joint_translation, joint_matrix, *newModel);
275 d_joints.assign(all_Joints);
277 //get InverseBindMatrix (Global)
278 this->buildInverseBindMatrices(all_Joints);
280 std::vector<Mat4f> localMatrix;
281 localMatrix.resize(maxJointId + 1);
283 for (auto jId : all_Joints)
285 localMatrix[jId] = joint_matrix[jId];
288 this->stateJointLocalMatrix()->assign(localMatrix);
291 // get joint World Location
292 printf("************ Set Joint ************\n");
294 std::vector<Coord> jointVertices;
295 std::map<int, int> jointId_VId;
297 for (size_t j = 0; j < jointNum; j++)
299 joint jId = all_Joints[j];
300 jointId_VId[jId] = jointVertices.size();
302 jointVertices.push_back(getVertexLocationWithJointTransform(jId, Vec3f(0, 0, 0), joint_matrix));
307 this->stateJointSet()->getDataPtr()->setPoints(jointVertices);
308 std::vector<TopologyModule::Edge> edges;
310 for (size_t i = 0; i < jointNum; i++)
312 for (auto childId : joint_child[all_Joints[i]])
314 edges.push_back(TopologyModule::Edge(i, jointId_VId[childId]));
317 this->stateJointSet()->getDataPtr()->setEdges(edges);
319 jointVertices.clear();
322 auto texMesh = this->stateTextureMesh()->getDataPtr();
324 loadGLTFMaterial(*newModel, texMesh, filename);
327 loadGLTFShape(*newModel, texMesh, filename, &initialPosition,&initialNormal, &d_mesh_Matrix,&d_shape_meshId, this->stateSkin()->getDataPtr());
330 this->updateTransform();
332 this->stateSkin()->getDataPtr()->mesh = texMesh;
334 this->stateSkin()->getDataPtr()->initialPosition = initialPosition;
336 this->stateSkin()->getDataPtr()->initialNormal = initialNormal;
340 this->stateJointsData()->getDataPtr()->UpdateJointInfo(
341 this->stateJointInverseBindMatrix()->getData(),
342 this->stateJointLocalMatrix()->getData(),
343 this->stateJointWorldMatrix()->getData(),
351 this->stateJointsData()->getDataPtr()->setJointName(joint_Name);
353 this->stateAnimation()->getDataPtr()->setAnimationData(
360 this->stateJointsData()->getDataPtr()
363 this->stateAnimation()->getDataPtr()->setLoop(false);
365 this->updateAnimation(0);
370 // ***************************** function *************************** //
372 template<typename TDataType>
373 void GltfLoader<TDataType>::updateTransform()
375 //updateModelTransformMatrix
376 this->updateTransformState();
377 auto animation = this->stateAnimation()->getDataPtr();
378 if (all_Joints.size()) //Animation
380 if (varImportAnimation()->getValue() && (!animation->mJoint_Index_Translation.empty() && !animation->mJoint_Index_Rotation.empty() && !animation->mJoint_Index_Scale.empty()))
381 updateAnimation(this->stateFrameNumber()->getValue());
385 if (this->stateTextureMesh()->getDataPtr()->vertices().size())
390 cuExecute(this->stateTextureMesh()->getDataPtr()->vertices().size(),
393 this->stateTextureMesh()->getDataPtr()->vertices(),
395 this->stateTextureMesh()->getDataPtr()->normals(),
397 this->stateTextureMesh()->getDataPtr()->shapeIds(),
402 //Move by VarTransform
409 auto shapeNum = this->stateTextureMesh()->getDataPtr()->shapes().size();
411 CArray<Coord> c_shapeCenter;
412 c_shapeCenter.resize(shapeNum);
414 for (uint i = 0; i < shapeNum; i++)
417 counter.resize(this->stateTextureMesh()->getDataPtr()->vertices().size());
420 cuExecute(this->stateTextureMesh()->getDataPtr()->vertices().size(),
421 C_Shape_PointCounter,
423 this->stateTextureMesh()->getDataPtr()->shapeIds(),
427 Reduction<int> reduce;
428 int num = reduce.accumulate(counter.begin(), counter.size());
430 DArray<Coord> targetPoints;
431 targetPoints.resize(num);
434 scan.exclusive(counter.begin(), counter.size());
436 cuExecute(this->stateTextureMesh()->getDataPtr()->vertices().size(),
439 this->stateTextureMesh()->getDataPtr()->vertices(),
444 Reduction<Coord> reduceBounding;
446 auto& bounding = this->stateTextureMesh()->getDataPtr()->shapes()[i]->boundingBox;
447 Coord lo = reduceBounding.minimum(targetPoints.begin(), targetPoints.size());
448 Coord hi = reduceBounding.maximum(targetPoints.begin(), targetPoints.size());
452 this->stateTextureMesh()->getDataPtr()->shapes()[i]->boundingTransform.translation() = (lo + hi) / 2;
454 c_shapeCenter[i] = (lo + hi) / 2;
456 targetPoints.clear();
461 d_ShapeCenter.assign(c_shapeCenter); // Used to "ToCenter"
462 unCenterPosition.assign(this->stateTextureMesh()->getDataPtr()->vertices());
465 if (varUseInstanceTransform()->getValue())
467 cuExecute(this->stateTextureMesh()->getDataPtr()->vertices().size(),
470 this->stateTextureMesh()->getDataPtr()->vertices(),
471 this->stateTextureMesh()->getDataPtr()->shapeIds(),
475 auto& reShapes = this->stateTextureMesh()->getDataPtr()->shapes();
477 for (size_t i = 0; i < shapeNum; i++)
479 reShapes[i]->boundingTransform.translation() = reShapes[i]->boundingTransform.translation() + this->varLocation()->getValue();
484 auto& reShapes = this->stateTextureMesh()->getDataPtr()->shapes();
486 for (size_t i = 0; i < shapeNum; i++)
488 reShapes[i]->boundingTransform.translation() = Vec3f(0);
492 this->stateShapeCenter()->getDataPtr()->setPoints(d_ShapeCenter);
496 template<typename TDataType>
497 void GltfLoader<TDataType>::updateStates()
499 ParametricModel<TDataType>::updateStates();
500 auto animation = this->stateAnimation()->getDataPtr();
502 if (joint_output.empty() || !this->varImportAnimation()->getValue() || (animation->mJoint_Index_Rotation.empty()&& animation->mJoint_Index_Translation.empty()&&animation->mJoint_Index_Scale.empty()))
505 updateAnimation(this->stateFrameNumber()->getValue());
506 auto jointInfo = this->stateJointsData()->getDataPtr();
508 this->stateJointsData()->getDataPtr()->UpdateJointInfo(
509 this->stateJointInverseBindMatrix()->getData(),
510 this->stateJointLocalMatrix()->getData(),
511 this->stateJointWorldMatrix()->getData(),
521 template<typename TDataType>
522 void GltfLoader<TDataType>::updateAnimation(int frameNumber)
524 if (joint_output.empty() || all_Joints.empty() || joint_matrix.empty())
527 auto mesh = this->stateTextureMesh()->getDataPtr();
530 this->stateAnimation()->getDataPtr()->updateAnimationPose(this->stateElapsedTime()->getValue()* this->varAnimationSpeed()->getValue());
534 cuExecute(all_Joints.size(),
536 this->stateJointSet()->getDataPtr()->getPoints(),
537 this->stateJointsData()->getDataPtr()->mJointWorldMatrix,
539 this->stateTransform()->getValue()
545 auto& skinInfo = this->stateSkin()->getData();
548 for (size_t i = 0; i < skinInfo.size(); i++)//
550 auto& bindJoint0 = skinInfo.V_jointID_0[i];
551 auto& bindJoint1 = skinInfo.V_jointID_1[i];
553 auto& bindWeight0 = skinInfo.V_jointWeight_0[i];
554 auto& bindWeight1 = skinInfo.V_jointWeight_1[i];
556 for (size_t j = 0; j < skinInfo.skin_VerticeRange[i].size(); j++)
559 Vec2u& range = skinInfo.skin_VerticeRange[i][j];
561 skinAnimation(initialPosition,
563 this->stateJointInverseBindMatrix()->getData(),
564 this->stateJointsData()->getDataPtr()->mJointWorldMatrix,
570 this->stateTransform()->getValue(),
580 this->stateJointInverseBindMatrix()->getData(),
581 this->stateJointsData()->getDataPtr()->mJointWorldMatrix,
587 this->stateTransform()->getValue(),
600 template<typename TDataType>
601 Vec3f GltfLoader<TDataType>::getVertexLocationWithJointTransform(joint jointId, Vec3f inPoint, std::map<joint, Mat4f> jMatrix)
604 Vec3f result = Vec3f(0);
606 const std::vector<int>& jD = getJointDirByJointIndex(jointId, jointId_joint_Dir);
608 Mat4f tempMatrix = Mat4f::identityMatrix();
611 for (int k = jD.size() - 1; k >= 0; k--)
613 joint select = jD[k];
614 tempMatrix *= jMatrix[select]; //
618 Vec4f jointLocation = tempMatrix * Vec4f(inPoint[0], inPoint[1], inPoint[2], 1);
619 result = Coord(jointLocation[0], jointLocation[1], jointLocation[2]);
625 template<typename TDataType>
626 void GltfLoader<TDataType>::buildInverseBindMatrices(const std::vector<joint>& all_Joints)
629 std::map<joint, Mat4f> tempJointMatrix = joint_matrix;
630 std::vector<Mat4f> temp;
632 temp.resize(maxJointId + 1);
634 for (size_t i = 0; i < maxJointId + 1; i++)
636 //temp.push_back(Mat4f::identityMatrix());
637 temp[i] = Mat4f::identityMatrix();
643 for (size_t i = 0; i < all_Joints.size(); i++)
645 joint jointId = all_Joints[i];
647 const std::vector<int>& jD = getJointDirByJointIndex(jointId, jointId_joint_Dir);
649 Mat4f tempMatrix = Mat4f::identityMatrix();
652 for (int k = 0; k < jD.size(); k++)
654 joint select = jD[k];
656 Vec3f tempVT = Vec3f(0, 0, 0);
657 Vec3f tempVS = Vec3f(1, 1, 1);
658 Quat<float> tempQR = Quat<float>(Mat3f::identityMatrix());
660 if (joint_input.find(select) != joint_input.end())
662 tempQR = joint_rotation[select];
664 tempVT = joint_translation[select];
666 tempVS = joint_scale[select];
668 Mat4f mT = Mat4f(1, 0, 0, tempVT[0], 0, 1, 0, tempVT[1], 0, 0, 1, tempVT[2], 0, 0, 0, 1);
669 Mat4f mS = Mat4f(tempVS[0], 0, 0, 0, 0, tempVS[1], 0, 0, 0, 0, tempVS[2], 0, 0, 0, 0, 1);
670 Mat4f mR = tempQR.toMatrix4x4();
673 tempJointMatrix[select] = mT * mS * mR;
676 tempMatrix *= tempJointMatrix[select].inverse();
680 joint_inverseBindMatrix[jointId] = (tempMatrix);
684 temp[jointId] = tempMatrix;
688 this->stateJointInverseBindMatrix()->assign(temp);
693 template<typename TDataType>
694 Vec3f GltfLoader<TDataType>::getmeshPointDeformByJoint(joint jointId, Coord worldPosition, std::map<joint, Mat4f> jMatrix)
696 Vec3f offest = worldPosition;
698 Vec4f v_bone_space = joint_inverseBindMatrix[jointId] * Vec4f(offest[0], offest[1], offest[2], 1);//
700 auto v_world_space = getVertexLocationWithJointTransform(jointId, Vec3f(v_bone_space[0], v_bone_space[1], v_bone_space[2]), jMatrix);
702 return v_world_space;
705 template<typename TDataType>
706 void GltfLoader<TDataType>::updateTransformState()
708 Vec3f location = this->varLocation()->getValue();
709 Vec3f scale = this->varScale()->getValue();
710 Mat4f mT = Mat4f(1, 0, 0, location[0], 0, 1, 0, location[1], 0, 0, 1, location[2], 0, 0, 0, 1);
711 Mat4f mS = Mat4f(scale[0], 0, 0, 0, 0, scale[1], 0, 0, 0, 0, scale[2], 0, 0, 0, 0, 1);
712 Mat4f mR = this->computeQuaternion().toMatrix4x4();
713 Mat4f transform = mT * mS * mR;
715 this->stateTransform()->setValue(transform);
721 template< typename Coord, typename Mat4f>
722 __global__ void StaticMeshTransform(
723 DArray<Coord> initialPosition,
724 DArray<Coord> Position,
728 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
729 if (pId >= Position.size()) return;
731 Vec4f temp = transform * Vec4f(initialPosition[pId][0], initialPosition[pId][1], initialPosition[pId][2], 1);
732 Position[pId] = Vec3f(temp[0], temp[1], temp[2]);
737 template< typename Coord, typename Mat4f, typename Vec3f >
738 __global__ void ShapeTransform(
739 DArray<Coord> intialPosition,
740 DArray<Vec3f> worldPosition,
741 DArray<Coord> intialNormal,
742 DArray<Vec3f> Normal,
743 DArray<Mat4f> WorldMatrix,
744 DArray<uint> vertexId_shape,
745 DArray<int> shapeId_MeshId
748 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
749 if (pId >= intialPosition.size()) return;
751 int shape = vertexId_shape[pId];
753 int MeshId = shapeId_MeshId[shape];
755 Vec4f tempV = Vec4f(intialPosition[pId][0], intialPosition[pId][1], intialPosition[pId][2], 1);
756 Vec4f tempN = Vec4f(intialNormal[pId][0], intialNormal[pId][1], intialNormal[pId][2], 0);
759 auto iP = intialPosition[pId];
762 tempV = WorldMatrix[MeshId] * tempV;
763 tempN = WorldMatrix[MeshId] * tempN;
765 worldPosition[pId] = Coord(tempV[0], tempV[1], tempV[2]);
766 Normal[pId] = Coord(tempN[0], tempN[1], tempN[2]);
769 auto iP = worldPosition[pId];
779 template< typename Coord, typename Vec4f, typename Mat4f ,typename Vec2u>
780 __global__ void PointsAnimation(
781 DArray<Coord> intialPosition,
782 DArray<Coord> worldPosition,
783 DArray<Mat4f> joint_inverseBindMatrix,
784 DArray<Mat4f> WorldMatrix,
786 DArray<Vec4f> bind_joints_0,
787 DArray<Vec4f> bind_joints_1,
788 DArray<Vec4f> weights_0,
789 DArray<Vec4f> weights_1,
797 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
798 if (pId >= worldPosition.size()) return;
800 if (pId<range[0] || pId>range[1])
803 Vec4f result = Vec4f(0, 0, 0, float(!isNormal));
805 int skinInfoVertexId = pId - range[0];
809 bool j0 = bind_joints_0.size();
810 bool j1 = bind_joints_1.size();
814 for (unsigned int i = 0; i < 4; i++)
816 int jointId = int(bind_joints_0[skinInfoVertexId][i]);
817 Real weight = weights_0[skinInfoVertexId][i];
819 offest = intialPosition[pId];
820 Vec4f v_bone_space = joint_inverseBindMatrix[jointId] * Vec4f(offest[0], offest[1], offest[2], float(!isNormal));//
822 result += (transform * WorldMatrix[jointId] * v_bone_space) * weight;
827 for (unsigned int i = 0; i < 4; i++)
829 int jointId = int(bind_joints_1[skinInfoVertexId][i]);
830 Real weight = weights_1[skinInfoVertexId][i];
832 offest = intialPosition[pId];
833 Vec4f v_bone_space = joint_inverseBindMatrix[jointId] * Vec4f(offest[0], offest[1], offest[2], float(!isNormal));//
835 result += (WorldMatrix[jointId] * v_bone_space) * weight;
839 //result = transform * result;
843 worldPosition[pId][0] = result[0];
844 worldPosition[pId][1] = result[1];
845 worldPosition[pId][2] = result[2];
849 worldPosition[pId] = worldPosition[pId].normalize();
853 template< typename Coord, typename Mat4f>
854 __global__ void jointAnimation(
855 DArray<Coord> worldPosition,
856 DArray<Mat4f> WorldMatrix,
861 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
862 if (pId >= joints.size()) return;
864 Vec4f result = Vec4f(0, 0, 0, 1);
866 int jointId = joints[pId];
867 result = transform * WorldMatrix[jointId] * result;
869 //result = transform * result;
871 worldPosition[pId][0] = result[0];
872 worldPosition[pId][1] = result[1];
873 worldPosition[pId][2] = result[2];
879 template<typename TDataType>
880 void GltfLoader<TDataType>::InitializationData()
882 joint_rotation.clear();
884 joint_translation.clear();
885 joint_matrix.clear();
886 jointId_joint_Dir.clear();
889 joint_output.clear();
891 joint_inverseBindMatrix.clear();
892 joint_AnimaMatrix.clear();
896 initialPosition.clear();
898 initialNormal.clear();
904 d_mesh_Matrix.clear();
905 d_shape_meshId.clear();
906 unCenterPosition.clear();
914 this->stateInitialMatrix()->clear();
915 this->stateJointInverseBindMatrix()->clear();
916 this->stateJointLocalMatrix()->clear();
918 this->stateJointWorldMatrix()->clear();
919 this->stateTexCoord_0()->clear();
920 this->stateTexCoord_1()->clear();
921 this->stateTextureMesh()->getDataPtr()->clear();
927 template<typename TDataType>
928 GltfLoader<TDataType>::~GltfLoader()
930 InitializationData();
932 initialPosition.clear();
933 initialNormal.clear();
937 template<typename TDataType>
938 void GltfLoader<TDataType>::varRenderChanged()
940 jointLineRender->varRadius()->setValue(this->varJointRadius()->getValue() / 2);
941 jointPointRender->varPointSize()->setValue(this->varJointRadius()->getValue());
946 template< typename Coord, typename uint>
947 __global__ void ShapeToCenter(
948 DArray<Coord> iniPos,
949 DArray<Coord> finalPos,
950 DArray<uint> shapeId,
954 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
955 if (pId >= iniPos.size()) return;
957 finalPos[pId] = iniPos[pId] - t[shapeId[pId]];
958 Vec4f P = Vec4f(finalPos[pId][0], finalPos[pId][1], finalPos[pId][2], 1);
960 finalPos[pId] = Coord(P[0], P[1], P[2]);
966 template< typename Coord, typename uint>
967 __global__ void initialCenterCoord(
969 DArray<uint> shapeId,
971 DArray<Coord> iniPoint,
975 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
976 if (pId >= Pos.size()) return;
978 for (int i = 0; i < shapeNum; i++)
980 if (shapeId[pId] == i && pointId[i] == -1)
983 iniPoint[i] = Pos[pId];
990 template<typename uint>
991 __global__ void C_Shape_PointCounter(
993 DArray<uint> point_ShapeIds,
997 uint tId = threadIdx.x + blockDim.x * blockIdx.x;
998 if (tId >= point_ShapeIds.size()) return;
1000 counter[tId] = (point_ShapeIds[tId]== target) ? 1 : 0;
1004 template<typename Coord>
1005 __global__ void C_SetupPoints(
1006 DArray<Coord> newPos,
1011 uint tId = threadIdx.x + blockDim.x * blockIdx.x;
1012 if (tId >= pos.size()) return;
1014 if (tId < pos.size() - 1 && radix[tId] != radix[tId + 1])
1016 newPos[radix[tId]] = pos[tId];
1018 else if (tId == pos.size() - 1 && pos.size() > 2)
1020 if(radix[tId] != radix[tId - 1])
1021 newPos[radix[tId]] = pos[tId];
1027 DEFINE_CLASS(GltfLoader);