[Tutorial Index] [Prev: Setting the render quality]

Adding a model to the world

If you desire to add a model to an existing choreography "on the fly", you can easily do so by using the arcticLayer::addModel method. First, of course, you need to load the model.

If you're simply adding another instance of a model referenced by the choreography, you can skip this step; all models contained in a choreography, whether active or not, are automatically loaded by the arcticPigs plugin. If it's a completely new model, however, you must explicitly load it:

arctic.doc.loadModel("models/Teeth.model");

You must pass it a parameter containing the relative or absolute URL which points to the model file. This may be done at any time; if you know ahead of time what model(s) you'll need to load, you can do it within the plugin's onCreate handler. You can also load the model on demand, in response to user input. This decreases initial load times, but can cause a delay while you wait for the model to be loaded before adding it to the scene.

Once it's loaded, it will fire an onModelLoaded event message. Although you can call the arcticLayer::addModel method before the model has finished loading, it is preferable to define an onModelLoaded handler, which may contain the code to actually add the model to the scene, set a variable to signify that the model has finished loading, or enable controls which manipulate the model.

<script type="text/javascript" for="arctic" event="onModelLoaded(mdlModel)">
  if (mdlModel.name == "Teeth") {
    hMain.addModel("Teeth");
  }
</script>

You don't want to do something like this:

<script type="text/javascript" for="arctic" event="onModelLoaded(mdlModel)">
  hMain.addModel(mdlModel.name);
</script>

That would create new instances of every model loaded into the choreography, or controls loaded for the right-click control menu!

Even when a model has been loaded and added to the scene, it remains invisible until explicitly activated. Models are also located at the origin when added to the scene. This can easily be altered; a good time to do so is when the onModelInstanced event fires. This event passes a reference to the model just instanced; the name property will contain "Shortcut to ModelName" if it is the first instance of that model in the scene, "Shortcut to ModelName (2)" if it's the second, and so on.

However, models instanced from an existing choreography might not follow the abovementioned naming practices, if the choreography's creator renamed the shortcuts. If the creator dragged a model named "Frob" into the choreography, and then renamed "Shortcut to Frob" to "Thingy", when the choreography's models are instanced by arcticPigs, the model will be named "Frob", and the instance will be named "Thingy". If a second instance of "Frob" is added to the world via scripting, it will have the name "Shortcut to Frob", not "Shortcut to Frob (2)".

<script type="text/javascript" for="arctic" event="onModelInstanced(mdlModel)">
  switch (mdlModel.name) {
    case "Shortcut to Teeth" :
      mdlModel.base.worldPosition.set(0,0,0);
      mdlModel.active = 1;
      break;
    case "Shortcut to Eyeball" :
      mdlModel.base.worldPosition.set(-60,110,0);
      mdlModel.active = 1;
      break;
    case "Shortcut to Eyeball (2)" :
      mdlModel.base.worldPosition.set(60,110,0);
      mdlModel.active = 1;
      break;
  }
</script>

Note: Although the arcticPigs plugin downloads the model's geometry when the loadModel method is called, it doesn't actually download the model's associated texture file (if one exists) until you call addModel. For this reason, you should wait until the onModelInstanced event is fired to set the model's active property to true. It's possible to make the model active immediately after calling addModel, but it may display briefly without its textures if you do that.

Example

In the following example, the model of the teeth is added to an empty choreography (only lights and camera defined) during the initialization phase. Click the Load New Model button to retrieve the "Eyeball" model from the Web site (the Add New Model button will be enabled when the model has completed loading). You can click the latter button twice, to add two copies of the eyeball above the teeth.