Source: components/ModelResourceComponent.js

import { ImageWrapType } from '../const';
import { BaseComponent } from './BaseComponent';

const __ = {
	private: Symbol('private'),
}

const _defaultTextureParam = { wrapS: ImageWrapType.Repeat, wrapT: ImageWrapType.Repeat };

/**
 * @class ModelResourceComponent
 * The model resource component.
 * @memberof THING
 * @extends THING.BaseComponent
 */
class ModelResourceComponent extends BaseComponent {

	/**
	 * The model of entity object.
	 */
	constructor() {
		super();

		this._imageTexture = null;

		this._info = null;
	}

	// #region Private Functions

	// #endregion

	// #region BaseComponent Interface

	// #endregion

	load(options, resolve, reject) {
		let object = this.object;

		const extrasData = options.extras || options.external;

		let modelOptions = {};
		if (extrasData && extrasData.modelOptions) {
			Object.assign(modelOptions, extrasData.modelOptions)
		}

		if (object.resource.nodeName) {
			modelOptions.nodeName = object.resource.nodeName;
		}

		if (object.resource.excludeNodeNames) {
			modelOptions.excludeNodeNames = object.resource.excludeNodeNames;
		}

		if (modelOptions.texture) {
			this._imageTexture = this.app.resourceManager.getTextureManager().load(modelOptions.texture, _defaultTextureParam, { flipY: modelOptions.flipY });
			modelOptions['image'] = this._imageTexture.getTextureResource();
		}

		const that = this;
		const onBeforeSetup = modelOptions.onBeforeSetup;

		object.app.resourceManager.loadObjectResource(object, modelOptions, {
			onBeforeSetup: (ev) => {
				onBeforeSetup && onBeforeSetup(ev);
			},
			onSetup: (ev) => {
				let info = ev.info;

				this._info = info;
			},
			onLoad: () => {
				if (that._imageTexture) {
					that._imageTexture.waitForComplete().then(resolve, reject);
				}
				else {
					resolve();
				}
			},
			onError: (ev) => {
				reject(ev);
			}
		});
	}

	onBeforeRemove() {
		this._unloadImageTexture();
	}

	onUnloadResource() {
		this._unloadImageTexture();
	}

	_unloadImageTexture() {
		if (this._imageTexture) {
			this._imageTexture.release();
			this._imageTexture = null;
		}
	}

	get info() {
		return this._info;
	}

}

export { ModelResourceComponent }