在 p5js 中渲染骨架 glp

问题描述 投票:0回答:0

所以我有一种渲染模型的方法从 three.js 到 p5.js 渲染动画模型。但不是骨架,我真的只想将骨架模型变成带有关键帧和网格的常规模型。这可能吗?

我尝试执行骨架计算,但我不知道执行此操作的数学运算,并且计算全部关闭。 这是我的想法EDIThttps://editor.p5js.org/byersolomon/sketches/D4jgx8VRr真的有这个功能。

var updateThis = [];

function updateSkinnedMesh() {
  requestAnimationFrame(updateSkinnedMesh);
  if (updateThis.length == 0) return;
  updateThis.forEach(obj => {
    var bones = obj.skeleton.bones;
    var boneMatrices = obj.skeleton.boneMatrices;
    var skinIndices = obj.geometry.attributes.skinIndex.array;
    var skinWeights = obj.geometry.attributes.skinWeight.array;
    var bindMatrix = obj.bindMatrix;
    var bindMatrixInverse = obj.bindMatrixInverse;

    // Initialize an array to store the transformed vertices
    var vertices = [];

    // Transform the vertices using the bone matrices
    for (var i = 0; i < obj.geometry.attributes.position.count; i++) {
      var vertex = new THREE.Vector3().fromBufferAttribute(obj.geometry.attributes.position, i);
      var skinIndex = skinIndices[i];
      var skinWeight = skinWeights[i];

      var boneMatrix = new THREE.Matrix4().fromArray(boneMatrices, skinIndex * 16);
      var bindMatrixInverse = new THREE.Matrix4().fromArray(boneMatrices);
      bindMatrixInverse.invert();
      var bindVector = vertex.applyMatrix4(bindMatrix);
      var worldVector = bindVector.applyMatrix4(boneMatrix);
      var transformed = worldVector.applyMatrix4(bindMatrixInverse);

      // Apply skinning weights to the transformed vertex
      vertex.lerp(transformed, skinWeight);
      vertices.push(vertex);
    }

    // Update the transformed vertices in the geometry
    var position = new THREE.Float32BufferAttribute(vertices.length * 3, 3);
    for (var i = 0; i < vertices.length; i++) {
      position.setXYZ(i, vertices[i].x, vertices[i].y, vertices[i].z);
    }
    obj.geometry.setAttribute('position', position);

    obj.updateMatrixWorld();
  });
}

但我认为我只是以错误的方式解决这个问题我不是数学家我只是想要一个在线程序或以其他方式计算这些东西并吐出另一个带有关键帧而不是骨架的glb。

p5.js
© www.soinside.com 2019 - 2024. All rights reserved.