"use strict";
cc._RF.push(module, '18d1c5ObBtCeaysyO5bMIgb', 'TmoAssembler');
// Script/TmoAssembler.js

"use strict";

exports.__esModule = true;
exports.TmoAssembler = void 0;

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

var gfx = cc.gfx; // 颜色去除 + 物件图集合批

var vfmtPosUvIndex = new gfx.VertexFormat([{
  name: gfx.ATTR_POSITION,
  type: gfx.ATTR_TYPE_FLOAT32,
  num: 2
}, {
  name: gfx.ATTR_UV0,
  type: gfx.ATTR_TYPE_FLOAT32,
  num: 2
}, {
  name: "a_texture_idx",
  type: gfx.ATTR_TYPE_FLOAT32,
  num: 1
}]); // 仅物件图集合批

var vfmtPosUvColorIndex = new gfx.VertexFormat([{
  name: gfx.ATTR_POSITION,
  type: gfx.ATTR_TYPE_FLOAT32,
  num: 2
}, {
  name: gfx.ATTR_UV0,
  type: gfx.ATTR_TYPE_FLOAT32,
  num: 2
}, {
  name: "a_texture_idx",
  type: gfx.ATTR_TYPE_FLOAT32,
  num: 1
}, {
  name: gfx.ATTR_COLOR,
  type: gfx.ATTR_TYPE_UINT8,
  num: 4,
  normalize: true
}]);

var TmoAssembler = /*#__PURE__*/function (_cc$Assembler) {
  _inheritsLoose(TmoAssembler, _cc$Assembler);

  function TmoAssembler() {
    return _cc$Assembler.apply(this, arguments) || this;
  }

  var _proto = TmoAssembler.prototype;

  _proto.init = function init(comp) {
    _cc$Assembler.prototype.init.call(this, comp);

    this._noColor = comp.isNoColor();
    this.updateColor = this._noColor ? undefined : this.updateColorFunc;
    this.uvOffset = 2;
    this.colorOffset = 5;
    this.textureIndexOffset = 4;
    this.floatsPerVert = this._noColor ? 5 : 6;
    this.indicesCount = 6;
    this.verticesCount = 4;
    this._renderData = new cc.RenderData();

    this._renderData.init(this);

    this.initLocal();
    this.initData();
  };

  _proto.initLocal = function initLocal() {
    this._local = [];
    this._local.length = 4;
  };

  _proto.initData = function initData() {
    var data = this._renderData;
    data.createFlexData(0, this.verticesCount, this.indicesCount, this.getVfmt()); // 填充顶点索引信息

    var indices = data.iDatas[0];
    var count = indices.length / 6;

    for (var i = 0, idx = 0; i < count; i++) {
      var vertextID = i * 4;
      indices[idx++] = vertextID;
      indices[idx++] = vertextID + 1;
      indices[idx++] = vertextID + 2;
      indices[idx++] = vertextID + 1;
      indices[idx++] = vertextID + 3;
      indices[idx++] = vertextID + 2;
    }
  };

  _proto.getBuffer = function getBuffer() {
    return cc.renderer._handle.getBuffer("mesh", this.getVfmt());
  };

  _proto.getVfmt = function getVfmt() {
    return this._noColor ? vfmtPosUvIndex : vfmtPosUvColorIndex;
  };

  _proto.updateRenderData = function updateRenderData(sprite) {
    this.packToDynamicAtlas(sprite, sprite._spriteFrame);

    if (sprite._vertsDirty) {
      this.updateUVs(sprite);
      this.updateVerts(sprite);
      this.updateTextureIdx(sprite);
      sprite._vertsDirty = false;
    }
  }
  /**
   * 填充textureIndex数据
   * @param {cc.Sprite} sprite 
   */
  ;

  _proto.updateTextureIdx = function updateTextureIdx(sprite) {
    var textureIdx = sprite._textureIdx;
    var verts = this._renderData.vDatas[0];
    var verticesCount = this.verticesCount;
    var floatsPerVert = this.floatsPerVert;
    var textureIndexOffset = this.textureIndexOffset;

    for (var i = 0; i < verticesCount; i++) {
      var index = i * floatsPerVert + textureIndexOffset;
      verts[index] = textureIdx;
    }
  };

  _proto.updateUVs = function updateUVs(sprite) {
    var uv = sprite._spriteFrame.uv;
    var uvOffset = this.uvOffset;
    var floatsPerVert = this.floatsPerVert;
    var verts = this._renderData.vDatas[0];

    for (var i = 0; i < 4; i++) {
      var srcOffset = i * 2;
      var dstOffset = floatsPerVert * i + uvOffset;
      verts[dstOffset] = uv[srcOffset];
      verts[dstOffset + 1] = uv[srcOffset + 1];
    }
  };

  _proto.updateVerts = function updateVerts(sprite) {
    var node = sprite.node,
        cw = node.width,
        ch = node.height,
        appx = node.anchorX * cw,
        appy = node.anchorY * ch,
        l,
        b,
        r,
        t;

    if (sprite.trim) {
      l = -appx;
      b = -appy;
      r = cw - appx;
      t = ch - appy;
    } else {
      var frame = sprite.spriteFrame,
          ow = frame._originalSize.width,
          oh = frame._originalSize.height,
          rw = frame._rect.width,
          rh = frame._rect.height,
          offset = frame._offset,
          scaleX = cw / ow,
          scaleY = ch / oh;
      var trimLeft = offset.x + (ow - rw) / 2;
      var trimRight = offset.x - (ow - rw) / 2;
      var trimBottom = offset.y + (oh - rh) / 2;
      var trimTop = offset.y - (oh - rh) / 2;
      l = trimLeft * scaleX - appx;
      b = trimBottom * scaleY - appy;
      r = cw + trimRight * scaleX - appx;
      t = ch + trimTop * scaleY - appy;
    }

    var local = this._local;
    local[0] = l;
    local[1] = b;
    local[2] = r;
    local[3] = t;
    this.updateWorldVerts(sprite);
  };

  _proto.updateColorFunc = function updateColorFunc(comp, color) {
    var uintVerts = this._renderData.uintVDatas[0];
    if (!uintVerts) return;
    color = color != null ? color : comp.node.color._val;
    var floatsPerVert = this.floatsPerVert;
    var colorOffset = this.colorOffset;

    for (var i = colorOffset, l = uintVerts.length; i < l; i += floatsPerVert) {
      uintVerts[i] = color;
    }
  };

  _proto.updateWorldVerts = function updateWorldVerts(comp) {
    if (CC_NATIVERENDERER) {
      // 原生平台兼容代码 复制于jsb-engine.js中的cc.Assembler2D.prototype.updateWorldVerts
      var local = this._local;
      var verts = this._renderData.vDatas[0];
      var vl = local[0],
          vr = local[2],
          vb = local[1],
          vt = local[3];
      var floatsPerVert = this.floatsPerVert;
      var vertexOffset = 0; // left bottom

      verts[vertexOffset] = vl;
      verts[vertexOffset + 1] = vb;
      vertexOffset += floatsPerVert; // right bottom

      verts[vertexOffset] = vr;
      verts[vertexOffset + 1] = vb;
      vertexOffset += floatsPerVert; // left top

      verts[vertexOffset] = vl;
      verts[vertexOffset + 1] = vt;
      vertexOffset += floatsPerVert; // right top

      verts[vertexOffset] = vr;
      verts[vertexOffset + 1] = vt;
    } else {
      var _local = this._local;
      var _verts = this._renderData.vDatas[0];
      var matrix = comp.node._worldMatrix;
      var matrixm = matrix.m,
          a = matrixm[0],
          b = matrixm[1],
          c = matrixm[4],
          d = matrixm[5],
          tx = matrixm[12],
          ty = matrixm[13];
      var _vl = _local[0],
          _vr = _local[2],
          _vb = _local[1],
          _vt = _local[3];
      var _floatsPerVert = this.floatsPerVert;
      var _vertexOffset = 0;
      var justTranslate = a === 1 && b === 0 && c === 0 && d === 1;

      if (justTranslate) {
        // left bottom
        _verts[_vertexOffset] = _vl + tx;
        _verts[_vertexOffset + 1] = _vb + ty;
        _vertexOffset += _floatsPerVert; // right bottom

        _verts[_vertexOffset] = _vr + tx;
        _verts[_vertexOffset + 1] = _vb + ty;
        _vertexOffset += _floatsPerVert; // left top

        _verts[_vertexOffset] = _vl + tx;
        _verts[_vertexOffset + 1] = _vt + ty;
        _vertexOffset += _floatsPerVert; // right top

        _verts[_vertexOffset] = _vr + tx;
        _verts[_vertexOffset + 1] = _vt + ty;
      } else {
        var al = a * _vl,
            ar = a * _vr,
            bl = b * _vl,
            br = b * _vr,
            cb = c * _vb,
            ct = c * _vt,
            db = d * _vb,
            dt = d * _vt; // left bottom

        _verts[_vertexOffset] = al + cb + tx;
        _verts[_vertexOffset + 1] = bl + db + ty;
        _vertexOffset += _floatsPerVert; // right bottom

        _verts[_vertexOffset] = ar + cb + tx;
        _verts[_vertexOffset + 1] = br + db + ty;
        _vertexOffset += _floatsPerVert; // left top

        _verts[_vertexOffset] = al + ct + tx;
        _verts[_vertexOffset + 1] = bl + dt + ty;
        _vertexOffset += _floatsPerVert; // right top

        _verts[_vertexOffset] = ar + ct + tx;
        _verts[_vertexOffset + 1] = br + dt + ty;
      }
    }
  };

  _proto.fillBuffers = function fillBuffers(comp, renderer) {
    if (renderer.worldMatDirty) {
      this.updateWorldVerts(comp);
    }

    var renderData = this._renderData;
    var vData = renderData.vDatas[0];
    var iData = renderData.iDatas[0];
    var buffer = this.getBuffer(renderer);
    var offsetInfo = buffer.request(this.verticesCount, this.indicesCount); // buffer data may be realloc, need get reference after request.
    // fill vertices

    var vertexOffset = offsetInfo.byteOffset >> 2,
        vbuf = buffer._vData;

    if (vData.length + vertexOffset > vbuf.length) {
      vbuf.set(vData.subarray(0, vbuf.length - vertexOffset), vertexOffset);
    } else {
      vbuf.set(vData, vertexOffset);
    } // fill indices


    var ibuf = buffer._iData,
        indiceOffset = offsetInfo.indiceOffset,
        vertexId = offsetInfo.vertexOffset;

    for (var i = 0, l = iData.length; i < l; i++) {
      ibuf[indiceOffset++] = vertexId + iData[i];
    }
  };

  _proto.packToDynamicAtlas = function packToDynamicAtlas(comp, frame) {
    if (CC_TEST) return;

    if (!frame._original && cc.dynamicAtlasManager && frame._texture.packable) {
      var packedFrame = cc.dynamicAtlasManager.insertSpriteFrame(frame);

      if (packedFrame) {
        frame._setDynamicAtlasFrame(packedFrame);
      }
    }

    var material = comp._materials[0];
    if (!material) return;

    if (material.getProperty('texture') !== frame._texture) {
      // texture was packed to dynamic atlas, should update uvs
      comp._vertsDirty = true;

      comp._updateMaterial();
    }
  };

  _createClass(TmoAssembler, [{
    key: "verticesFloats",
    get: function get() {
      return this.verticesCount * this.floatsPerVert;
    }
  }]);

  return TmoAssembler;
}(cc.Assembler);

exports.TmoAssembler = TmoAssembler;

cc._RF.pop();