projectInstanceAt method

void projectInstanceAt(
  1. int index,
  2. InstancedMesh instancedMesh,
  3. Matrix4 matrixWorld, {
  4. bool forceCameraSave = false,
})

Implementation

void projectInstanceAt(int index, InstancedMesh instancedMesh, Matrix4 matrixWorld, { bool forceCameraSave = false }) {
  if (
    !(instancedMesh.material is GroupMaterial
      ? (instancedMesh.material as GroupMaterial).children.every((m) => m is ProjectedMaterial)
      : instancedMesh.material is ProjectedMaterial)
  ) {
    throw ('The InstancedMesh material must be a ProjectedMaterial');
  }

  if (
    !(instancedMesh.material is GroupMaterial
      ? (instancedMesh.material as GroupMaterial).children.any((m) => m == this)
      : instancedMesh.material == this)
  ) {
    throw ('''The provided InstancedMeshhave't i samenclude thas e material where project() has been called from''');
  }

  if (
    instancedMesh.geometry?.attributes['savedModelMatrix0'] == false ||
    instancedMesh.geometry?.attributes['savedModelMatrix1'] == false ||
    instancedMesh.geometry?.attributes['savedModelMatrix2'] == false ||
    instancedMesh.geometry?.attributes['savedModelMatrix3'] == false
  ) {
    throw ('No allocated data found on the geometry, please call "ProjectedMaterialUtils.allocateProjectionData(geometry, instancesCount)}"');
  }

  instancedMesh.geometry?.attributes['savedModelMatrix0'].setXYZW(
    index,
    matrixWorld.storage[0],
    matrixWorld.storage[1],
    matrixWorld.storage[2],
    matrixWorld.storage[3]
  );
  instancedMesh.geometry?.attributes['savedModelMatrix1'].setXYZW(
    index,
    matrixWorld.storage[4],
    matrixWorld.storage[5],
    matrixWorld.storage[6],
    matrixWorld.storage[7]
  );
  instancedMesh.geometry?.attributes['savedModelMatrix2'].setXYZW(
    index,
    matrixWorld.storage[8],
    matrixWorld.storage[9],
    matrixWorld.storage[10],
    matrixWorld.storage[11]
  );
  instancedMesh.geometry?.attributes['savedModelMatrix3'].setXYZW(
    index,
    matrixWorld.storage[12],
    matrixWorld.storage[13],
    matrixWorld.storage[14],
    matrixWorld.storage[15]
  );

  // if the material is not the first, output just the texture
  if (instancedMesh.material is GroupMaterial) {
    final materialIndex = (instancedMesh.material as GroupMaterial).children.indexOf(this);
    if (!(instancedMesh.material as GroupMaterial).children[materialIndex].transparent) {
      console.warning('''You have to pass "transparent: true" to the ProjectedMaterial if you're working with multiple materials.''');
    }
    if (materialIndex > 0) {
      this.uniforms['backgroundOpacity']['value'] = 0;
    }
  }

  // persist the current camera position and matrices
  // only if it's the first instance since most surely
  // in all other instances the camera won't change
  if (index == 0 || forceCameraSave) {
    _saveCameraMatrices();
  }
}