loadNode method

dynamic loadNode(
  1. dynamic nodeIndex
)

Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#nodes-and-hierarchy @param {number} nodeIndex @return {Promise

Implementation

loadNode(nodeIndex) async {
  var json = this.json;
  var extensions = this.extensions;
  var parser = this;

  Map<String, dynamic> nodeDef = json["nodes"][nodeIndex];

  // reserve node's name before its dependencies, so the root has the intended name.
  var nodeName = nodeDef["name"] != null ? parser.createUniqueName(nodeDef["name"]) : '';

  var pending = [];

  var meshPromise = await parser._invokeOne((ext) {
    return ext.createNodeMesh != null ? ext.createNodeMesh(nodeIndex) : null;
  });

  if (meshPromise != null) {
    pending.add(meshPromise);
  }
  // if ( nodeDef["mesh"] != null ) {
  //   var mesh = await parser.getDependency( 'mesh', nodeDef["mesh"] );
  //   var node = await parser._getNodeRef( parser.meshCache, nodeDef["mesh"], mesh );
  //   // if weights are provided on the node, override weights on the mesh.
  //   if ( nodeDef["weights"] != null ) {
  //     node.traverse( ( o ) {
  //       if ( ! o.isMesh ) return;
  //       for ( var i = 0, il = nodeDef["weights"].length; i < il; i ++ ) {
  //         o.morphTargetInfluences[ i ] = nodeDef["weights"][ i ];
  //       }
  //     } );
  //   }
  //   pending.add(node);
  // }

  if (nodeDef["camera"] != null) {
    var camera = await parser.getDependency('camera', nodeDef["camera"]);

    pending.add(await parser._getNodeRef(parser.cameraCache, nodeDef["camera"], camera));
  }

  // parser._invokeAll( ( ext ) async {
  //   return ext.createNodeAttachment != null ? await ext.createNodeAttachment( nodeIndex ) : null;
  // } ).forEach( ( promise ) {
  //   pending.add( promise );
  // } );

  List results = await parser._invokeAll((ext) async {
    return ext.createNodeAttachment != null ? await ext.createNodeAttachment(nodeIndex) : null;
  });

  var objects = [];

  for (var element in pending) {
    objects.add(element);
  }

  for (var element in results) {
    objects.add(element);
  }

  var node;

  // .isBone isn't in glTF spec. See ._markDefs
  if (nodeDef["isBone"] == true) {
    node = Bone();
  } else if (objects.length > 1) {
    node = Group();
  } else if (objects.length == 1) {
    node = objects[0];
  } else {
    node = Object3D();
  }

  if (objects.isEmpty || node != objects[0]) {
    for (var i = 0, il = objects.length; i < il; i++) {
      node.add(objects[i]);
    }
  }

  if (nodeDef["name"] != null) {
    node.userData["name"] = nodeDef["name"];
    node.name = nodeName;
  }

  assignExtrasToUserData(node, nodeDef);

  if (nodeDef["extensions"] != null) addUnknownExtensionsToUserData(extensions, node, nodeDef);

  if (nodeDef["matrix"] != null) {
    var matrix = Matrix4();
    matrix.fromArray(List<num>.from(nodeDef["matrix"]));
    node.applyMatrix4(matrix);
  } else {
    if (nodeDef["translation"] != null) {
      node.position.fromArray(List<num>.from(nodeDef["translation"]));
    }

    if (nodeDef["rotation"] != null) {
      node.quaternion.fromArray(List<num>.from(nodeDef["rotation"]));
    }

    if (nodeDef["scale"] != null) {
      node.scale.fromArray(List<num>.from(nodeDef["scale"]));
    }
  }

  parser.associations[node] = {"type": 'nodes', "index": nodeIndex};

  return node;
}