All files / engine/Source/Scene/GltfPipeline ForEach.js

75% Statements 156/208
50.98% Branches 52/102
87.5% Functions 42/48
75.36% Lines 153/203

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425                              1x 52x 52x 66x 66x 66x   64x                     1x 33874x 20640x 20640x 34610x 34610x   34610x                     1x 33657x 33657x 44x     33613x     1x 1689x     1x 24x 24x 15x 15x     15x       5x 5x   5x             15x       15x                                           1x 1655x 1655x 1723x 1836x     5137x 4998x 4998x   4998x             1836x       1836x 16x     28x 28x 28x   28x                     1x 1647x 1647x 1718x 1831x 1831x 1773x 1773x   1773x               1x 3323x     1x 130x 130x     1x 130x 130x     1x 7838x     1x 1660x     1x 8x     1x 4529x     1x 1685x     1x 12x 12x       5x     12x 30x 30x   30x             1x 5002x     1x 5194x 5194x 5194x 5194x 5533x 5533x   5533x             1x 1871x 1871x 5172x 5172x   5172x             1x 1856x 1856x 10x 10x 16x   16x             1x 16x 28x 28x 28x   28x             1x 1671x     1x                                                     1x             1x 8x             8x     1x 1647x     1x 8x     1x 4529x 1x           4528x     1x 13x     1x                             1x 4x 4x 4x 4x   4x             1x 4x 4x 20x 20x   20x             1x 12x 12x 72x 72x   72x             1x 32x             32x     1x 16x        
import usesExtension from "./usesExtension.js";
import defined from "../../Core/defined.js";
 
/**
 * Contains traversal functions for processing elements of the glTF hierarchy.
 * @constructor
 *
 * @private
 */
function ForEach() {}
 
/**
 * Fallback for glTF 1.0
 * @private
 */
ForEach.objectLegacy = function (objects, handler) {
  Eif (defined(objects)) {
    for (const objectId in objects) {
      Eif (Object.prototype.hasOwnProperty.call(objects, objectId)) {
        const object = objects[objectId];
        const value = handler(object, objectId);
 
        Iif (defined(value)) {
          return value;
        }
      }
    }
  }
};
 
/**
 * @private
 */
ForEach.object = function (arrayOfObjects, handler) {
  if (defined(arrayOfObjects)) {
    const length = arrayOfObjects.length;
    for (let i = 0; i < length; i++) {
      const object = arrayOfObjects[i];
      const value = handler(object, i);
 
      Iif (defined(value)) {
        return value;
      }
    }
  }
};
 
/**
 * Supports glTF 1.0 and 2.0
 * @private
 */
ForEach.topLevel = function (gltf, name, handler) {
  const gltfProperty = gltf[name];
  if (defined(gltfProperty) && !Array.isArray(gltfProperty)) {
    return ForEach.objectLegacy(gltfProperty, handler);
  }
 
  return ForEach.object(gltfProperty, handler);
};
 
ForEach.accessor = function (gltf, handler) {
  return ForEach.topLevel(gltf, "accessors", handler);
};
 
ForEach.accessorWithSemantic = function (gltf, semantic, handler) {
  const visited = {};
  return ForEach.mesh(gltf, function (mesh) {
    return ForEach.meshPrimitive(mesh, function (primitive) {
      const valueForEach = ForEach.meshPrimitiveAttribute(
        primitive,
        function (accessorId, attributeSemantic) {
          if (
            attributeSemantic.indexOf(semantic) === 0 &&
            !defined(visited[accessorId])
          ) {
            visited[accessorId] = true;
            const value = handler(accessorId);
 
            Iif (defined(value)) {
              return value;
            }
          }
        },
      );
 
      Iif (defined(valueForEach)) {
        return valueForEach;
      }
 
      return ForEach.meshPrimitiveTarget(primitive, function (target) {
        return ForEach.meshPrimitiveTargetAttribute(
          target,
          function (accessorId, attributeSemantic) {
            if (
              attributeSemantic.indexOf(semantic) === 0 &&
              !defined(visited[accessorId])
            ) {
              visited[accessorId] = true;
              const value = handler(accessorId);
 
              if (defined(value)) {
                return value;
              }
            }
          },
        );
      });
    });
  });
};
 
ForEach.accessorContainingVertexAttributeData = function (gltf, handler) {
  const visited = {};
  return ForEach.mesh(gltf, function (mesh) {
    return ForEach.meshPrimitive(mesh, function (primitive) {
      const valueForEach = ForEach.meshPrimitiveAttribute(
        primitive,
        function (accessorId) {
          if (!defined(visited[accessorId])) {
            visited[accessorId] = true;
            const value = handler(accessorId);
 
            Iif (defined(value)) {
              return value;
            }
          }
        },
      );
 
      Iif (defined(valueForEach)) {
        return valueForEach;
      }
 
      return ForEach.meshPrimitiveTarget(primitive, function (target) {
        return ForEach.meshPrimitiveTargetAttribute(
          target,
          function (accessorId) {
            Eif (!defined(visited[accessorId])) {
              visited[accessorId] = true;
              const value = handler(accessorId);
 
              Iif (defined(value)) {
                return value;
              }
            }
          },
        );
      });
    });
  });
};
 
ForEach.accessorContainingIndexData = function (gltf, handler) {
  const visited = {};
  return ForEach.mesh(gltf, function (mesh) {
    return ForEach.meshPrimitive(mesh, function (primitive) {
      const indices = primitive.indices;
      if (defined(indices) && !defined(visited[indices])) {
        visited[indices] = true;
        const value = handler(indices);
 
        Iif (defined(value)) {
          return value;
        }
      }
    });
  });
};
 
ForEach.animation = function (gltf, handler) {
  return ForEach.topLevel(gltf, "animations", handler);
};
 
ForEach.animationChannel = function (animation, handler) {
  const channels = animation.channels;
  return ForEach.object(channels, handler);
};
 
ForEach.animationSampler = function (animation, handler) {
  const samplers = animation.samplers;
  return ForEach.object(samplers, handler);
};
 
ForEach.buffer = function (gltf, handler) {
  return ForEach.topLevel(gltf, "buffers", handler);
};
 
ForEach.bufferView = function (gltf, handler) {
  return ForEach.topLevel(gltf, "bufferViews", handler);
};
 
ForEach.camera = function (gltf, handler) {
  return ForEach.topLevel(gltf, "cameras", handler);
};
 
ForEach.image = function (gltf, handler) {
  return ForEach.topLevel(gltf, "images", handler);
};
 
ForEach.material = function (gltf, handler) {
  return ForEach.topLevel(gltf, "materials", handler);
};
 
ForEach.materialValue = function (material, handler) {
  let values = material.values;
  if (
    defined(material.extensions) &&
    defined(material.extensions.KHR_techniques_webgl)
  ) {
    values = material.extensions.KHR_techniques_webgl.values;
  }
 
  for (const name in values) {
    Eif (Object.prototype.hasOwnProperty.call(values, name)) {
      const value = handler(values[name], name);
 
      Iif (defined(value)) {
        return value;
      }
    }
  }
};
 
ForEach.mesh = function (gltf, handler) {
  return ForEach.topLevel(gltf, "meshes", handler);
};
 
ForEach.meshPrimitive = function (mesh, handler) {
  const primitives = mesh.primitives;
  Eif (defined(primitives)) {
    const primitivesLength = primitives.length;
    for (let i = 0; i < primitivesLength; i++) {
      const primitive = primitives[i];
      const value = handler(primitive, i);
 
      Iif (defined(value)) {
        return value;
      }
    }
  }
};
 
ForEach.meshPrimitiveAttribute = function (primitive, handler) {
  const attributes = primitive.attributes;
  for (const semantic in attributes) {
    Eif (Object.prototype.hasOwnProperty.call(attributes, semantic)) {
      const value = handler(attributes[semantic], semantic);
 
      Iif (defined(value)) {
        return value;
      }
    }
  }
};
 
ForEach.meshPrimitiveTarget = function (primitive, handler) {
  const targets = primitive.targets;
  if (defined(targets)) {
    const length = targets.length;
    for (let i = 0; i < length; ++i) {
      const value = handler(targets[i], i);
 
      Iif (defined(value)) {
        return value;
      }
    }
  }
};
 
ForEach.meshPrimitiveTargetAttribute = function (target, handler) {
  for (const semantic in target) {
    Eif (Object.prototype.hasOwnProperty.call(target, semantic)) {
      const accessorId = target[semantic];
      const value = handler(accessorId, semantic);
 
      Iif (defined(value)) {
        return value;
      }
    }
  }
};
 
ForEach.node = function (gltf, handler) {
  return ForEach.topLevel(gltf, "nodes", handler);
};
 
ForEach.nodeInTree = function (gltf, nodeIds, handler) {
  const nodes = gltf.nodes;
  if (defined(nodes)) {
    const length = nodeIds.length;
    for (let i = 0; i < length; i++) {
      const nodeId = nodeIds[i];
      const node = nodes[nodeId];
      if (defined(node)) {
        let value = handler(node, nodeId);
 
        if (defined(value)) {
          return value;
        }
 
        const children = node.children;
        if (defined(children)) {
          value = ForEach.nodeInTree(gltf, children, handler);
 
          if (defined(value)) {
            return value;
          }
        }
      }
    }
  }
};
 
ForEach.nodeInScene = function (gltf, scene, handler) {
  const sceneNodeIds = scene.nodes;
  if (defined(sceneNodeIds)) {
    return ForEach.nodeInTree(gltf, sceneNodeIds, handler);
  }
};
 
ForEach.program = function (gltf, handler) {
  Iif (usesExtension(gltf, "KHR_techniques_webgl")) {
    return ForEach.object(
      gltf.extensions.KHR_techniques_webgl.programs,
      handler,
    );
  }
 
  return ForEach.topLevel(gltf, "programs", handler);
};
 
ForEach.sampler = function (gltf, handler) {
  return ForEach.topLevel(gltf, "samplers", handler);
};
 
ForEach.scene = function (gltf, handler) {
  return ForEach.topLevel(gltf, "scenes", handler);
};
 
ForEach.shader = function (gltf, handler) {
  if (usesExtension(gltf, "KHR_techniques_webgl")) {
    return ForEach.object(
      gltf.extensions.KHR_techniques_webgl.shaders,
      handler,
    );
  }
 
  return ForEach.topLevel(gltf, "shaders", handler);
};
 
ForEach.skin = function (gltf, handler) {
  return ForEach.topLevel(gltf, "skins", handler);
};
 
ForEach.skinJoint = function (skin, handler) {
  const joints = skin.joints;
  if (defined(joints)) {
    const jointsLength = joints.length;
    for (let i = 0; i < jointsLength; i++) {
      const joint = joints[i];
      const value = handler(joint);
 
      if (defined(value)) {
        return value;
      }
    }
  }
};
 
ForEach.techniqueAttribute = function (technique, handler) {
  const attributes = technique.attributes;
  for (const attributeName in attributes) {
    Eif (Object.prototype.hasOwnProperty.call(attributes, attributeName)) {
      const value = handler(attributes[attributeName], attributeName);
 
      Iif (defined(value)) {
        return value;
      }
    }
  }
};
 
ForEach.techniqueUniform = function (technique, handler) {
  const uniforms = technique.uniforms;
  for (const uniformName in uniforms) {
    Eif (Object.prototype.hasOwnProperty.call(uniforms, uniformName)) {
      const value = handler(uniforms[uniformName], uniformName);
 
      Iif (defined(value)) {
        return value;
      }
    }
  }
};
 
ForEach.techniqueParameter = function (technique, handler) {
  const parameters = technique.parameters;
  for (const parameterName in parameters) {
    Eif (Object.prototype.hasOwnProperty.call(parameters, parameterName)) {
      const value = handler(parameters[parameterName], parameterName);
 
      Iif (defined(value)) {
        return value;
      }
    }
  }
};
 
ForEach.technique = function (gltf, handler) {
  Iif (usesExtension(gltf, "KHR_techniques_webgl")) {
    return ForEach.object(
      gltf.extensions.KHR_techniques_webgl.techniques,
      handler,
    );
  }
 
  return ForEach.topLevel(gltf, "techniques", handler);
};
 
ForEach.texture = function (gltf, handler) {
  return ForEach.topLevel(gltf, "textures", handler);
};
 
export default ForEach;