All files / Sources/IO/Geometry/GLTFImporter Extensions.js

3.7% Statements 1/27
0% Branches 0/10
0% Functions 0/7
3.84% Lines 1/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104              1x                                                                                                                                                                                                
import macro from 'vtk.js/Sources/macros';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkDracoReader from 'vtk.js/Sources/IO/Geometry/DracoReader';
import vtkLight from 'vtk.js/Sources/Rendering/Core/Light';
 
import { MIN_LIGHT_ATTENUATION } from 'vtk.js/Sources/IO/Geometry/GLTFImporter/Constants';
 
const { vtkWarningMacro } = macro;
 
/**
 * Handles the KHR_materials_unlit extension.
 *
 * @param {object} extension - The KHR_materials_unlit extension object.
 * @param {vtkProperty} property - The vtkProperty instance to update.
 */
export function handleKHRMaterialsUnlit(extension, property) {
  property.setLighting(true);
}
 
/**
 * Handles the KHR_materials_ior extension.
 *
 * @param {object} extension - The KHR_materials_unlit extension object.
 * @param {vtkProperty} property - The vtkProperty instance to update.
 */
export function handleKHRMaterialsIor(extension, property) {
  property.setBaseIOR(extension.ior);
}
 
/**
 * Handles the KHR_materials_specular extension.
 * @param {object} extension - The KHR_materials_specular extension object.
 * @param {vtkProperty} property - The vtkProperty instance to update.
 */
export function handleKHRMaterialsSpecular(extension, property) {
  property.setSpecular(extension.specularFactor);
  property.setSpecularColor(extension.specularColorFactor);
}
 
/**
 * Handles the KHR_lights_punctual extension.
 *
 * @param {object} extension - The KHR_lights_punctual extension object.
 * @param {vtkRenderer} renderer - The vtkRenderer instance to add the light to.
 */
export function handleKHRLightsPunctual(extension, transformMatrix, model) {
  const { light } = extension;
 
  const { color, intensity, range, spot, type } = light;
 
  const l = vtkLight.newInstance({
    color: color || [1, 1, 1],
    intensity: intensity || 1.0,
  });
 
  // Apply the global transform to the light
  l.setTransformMatrix(transformMatrix);
 
  // Handle range
  if (range > 0) {
    // Set quadratic values to get attenuation(range) ~= MIN_LIGHT_ATTENUATION
    l.setAttenuationValues(1, 0, 1.0 / (range * range * MIN_LIGHT_ATTENUATION));
  }
 
  switch (type) {
    case 'directional':
      l.setPositional(false);
      break;
    case 'point':
      l.setPositional(true);
      l.setConeAngle(90);
      break;
    case 'spot':
      l.setPositional(true);
      l.setConeAngle(vtkMath.radiansFromDegrees(spot.outerConeAngle));
      break;
    default:
      vtkWarningMacro(`Unsupported light type: ${type}`);
  }
 
  model.lights.set(light.name, l);
}
 
/**
 * Handles the KHR_draco_mesh_compression extension.
 *
 * @param {object} extension - The KHR_draco_mesh_compression extension object.
 */
export async function handleKHRDracoMeshCompression(extension) {
  const reader = vtkDracoReader.newInstance();
  reader.parse(extension.bufferView);
  return reader.getOutputData();
}
 
/**
 * Handles the KHR_materials_variants extension.
 *
 * @param {object} extension - The KHR_materials_variants extension object.
 * @param {object} model - The model object to update with variant information.
 */
export function handleKHRMaterialsVariants(extension, model) {
  model.variants = extension.variants.map((v) => v.name);
}