updateVertexPositionBuffer method

void updateVertexPositionBuffer(
  1. Float32List buffer,
  2. bool isSkinnedDeformInWorld
)

Implementation

void updateVertexPositionBuffer(
    Float32List buffer, bool isSkinnedDeformInWorld) {
  Mat2D worldTransform = this.worldTransform;
  int readIdx = 0;
  int writeIdx = 0;

  Float32List? v = _animationDeformedVertices != null
      ? _animationDeformedVertices
      : _vertices;
  int stride = _animationDeformedVertices != null ? 2 : vertexStride;

  if (skin != null) {
    Float32List? boneTransforms = skin!.boneMatrices;

    //Mat2D inverseWorldTransform = Mat2D.Invert(new Mat2D(), worldTransform);
    Float32List influenceMatrix =
        Float32List.fromList([0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);

    // if(this.name == "evolution_1_0001s_0003_evolution_1_weapo")
    // {
    // //	print("TEST!");
    // 	int boneIndexOffset = vertexBoneIndexOffset;
    // 	int weightOffset = vertexBoneWeightOffset;
    // 	for(int i = 0; i < _vertexCount; i++)
    // 	{
    // 		for(int wi = 0; wi < 4; wi++)
    // 		{
    // 			int boneIndex = _vertices[boneIndexOffset+wi].toInt();
    // 			double weight = _vertices[weightOffset+wi];
    // 			if(boneIndex == 1)
    // 			{
    // 				_vertices[weightOffset+wi] = 1.0;
    // 			}
    // 			else if(boneIndex == 2)
    // 			{
    // 				_vertices[weightOffset+wi] = 0.0;
    // 			}
    // 			//print("BI $boneIndex $weight");
    // 		}
    // 		boneIndexOffset += vertexStride;
    // 		weightOffset += vertexStride;
    // 	}
    // }
    int boneIndexOffset = vertexBoneIndexOffset;
    int weightOffset = vertexBoneWeightOffset;
    for (int i = 0; i < _vertexCount; i++) {
      double x = v![readIdx];
      double y = v[readIdx + 1];

      double px, py;

      if (_animationDeformedVertices != null && isSkinnedDeformInWorld) {
        px = x;
        py = y;
      } else {
        px =
            worldTransform[0] * x + worldTransform[2] * y + worldTransform[4];
        py =
            worldTransform[1] * x + worldTransform[3] * y + worldTransform[5];
      }

      influenceMatrix[0] = influenceMatrix[1] = influenceMatrix[2] =
          influenceMatrix[3] = influenceMatrix[4] = influenceMatrix[5] = 0.0;

      for (int wi = 0; wi < 4; wi++) {
        int boneIndex = _vertices![boneIndexOffset + wi].toInt();
        double weight = _vertices![weightOffset + wi];

        int boneTransformIndex = boneIndex * 6;
        if (boneIndex <= connectedBones!.length) {
          for (int j = 0; j < 6; j++) {
            influenceMatrix[j] +=
                boneTransforms[boneTransformIndex + j] * weight;
          }
        }
      }

      x = influenceMatrix[0] * px +
          influenceMatrix[2] * py +
          influenceMatrix[4];
      y = influenceMatrix[1] * px +
          influenceMatrix[3] * py +
          influenceMatrix[5];

      readIdx += stride;
      boneIndexOffset += vertexStride;
      weightOffset += vertexStride;

      buffer[writeIdx++] = x;
      buffer[writeIdx++] = y;
    }
  } else {
    for (int i = 0; i < _vertexCount; i++) {
      buffer[writeIdx++] = v![readIdx];
      buffer[writeIdx++] = v[readIdx + 1];
      readIdx += stride;
    }
  }
}