mdx-m3-viewer
Version:
A browser WebGL model viewer. Mainly focused on models of the games Warcraft 3 and Starcraft 2.
57 lines (47 loc) • 1.49 kB
text/typescript
import { HandlerResource } from './handlerresource';
import Bounds from './bounds';
import ModelInstance from './modelinstance';
/**
* A model.
*/
export default abstract class Model extends HandlerResource {
/**
* An array of instances that were created before the model loaded.
*
* When the model loads, the instances are loaded, and the array is cleared.
*/
preloadedInstances: ModelInstance[] = [];
bounds: Bounds = new Bounds();
/**
* Create the actual instance object and return it.
*
* The given type can be used to select between instance classes, if there are more than one.
*/
abstract createInstance(): ModelInstance;
/**
* Adds a new instance to this model, and returns it.
*/
addInstance() {
let instance = this.createInstance();
if (this.ok) {
instance.load();
} else {
this.preloadedInstances.push(instance);
}
return instance;
}
lateLoad() {
for (let instance of this.preloadedInstances) {
instance.load();
// If an instance was created and attached to a scene before the model finished loading, it was rejected by the scene.
// Therefore re-add it now that the model is loaded.
let scene = instance.scene;
if (scene) {
instance.scene = null;
scene.addInstance(instance);
}
}
// Remove references to the instances.
this.preloadedInstances.length = 0;
}
}