Source: primitives/texturePrimitive.js

import { TextureArray } from '../texture/textureArray';
import { ZincObject } from './zincObject';
/**
 * Provides a base class object which stores textures and rendering object.
 *
 * @class
 * @author Alan Wu
 * @return {TexturePrimitive}
 */
const TexturePrimitive = function (textureIn) {
  ZincObject.call(this);
  this.isTexturePrimitive = true;
  this.texture = textureIn;
  //List of textureArray object, which is used for
  //time variation for textureSlides object
  this.textureList = [];
  if (this.texture) {
    this.textureList.push(this.texture);
  }

  /**
   * Load texture data into this primitves.
   *
   * @param {Object} textureData - contains the informations about the textures.
   * @param {Function} finishCallback - User's function to be called once texture's
   *  is loaded.
   */
  this.load = (textureData, finishCallback, isInline) => {
    if (textureData) {
      if (textureData.images && textureData.images.source) {
        const texture = new TextureArray();
        const imgArray = [];
        textureData.images.source.forEach((img) => {
          imgArray.push(img);
        });
        const _this = this;
        texture.loadFromImages(imgArray).then(() => {
          _this.texture = texture;
          _this.textureList.push(this.texture);
          _this.initialise(textureData, finishCallback);
        });
      }
    }
  };

  this.addTextureArray = (tArray) => {
    if (this.textureList.length === 0 && this.texture) {
      this.textureList.push(this.texture);
    }
    if (tArray && tArray.isTextureArray) {
      this.textureList.push(tArray);
    }
  };

  /**
   * Initialise a texture based on the provided textureData, this should be used
   * internally only.
   *
   * @param {Object} textureData - contains the informations about the textures.
   * @param {Function} finishCallback - User's function to be called once texture's
   *  is loaded.
   */
  this.initialise = (textureData, finishCallback) => {
    if (finishCallback != undefined && typeof finishCallback == 'function') {
      finishCallback(this);
    }
  };
};

TexturePrimitive.prototype = Object.create(ZincObject.prototype);
TexturePrimitive.prototype.constructor = TexturePrimitive;

/**
 * Free the textures and the other allocated memory.
 */
TexturePrimitive.prototype.dispose = function () {
  const textures = new Set(this.textureList);
  if (this.texture) textures.add(this.texture);
  textures.forEach((texture) => texture.dispose());
  this.texture = undefined;
  this.textureList = [];
  ZincObject.prototype.dispose.call(this);
};
export { TexturePrimitive };