setData()

in modules/core/src/lib/attribute/data-column.js [186:251]


  setData(opts) {
    const {state} = this;
    if (ArrayBuffer.isView(opts)) {
      opts = {value: opts};
    } else if (opts instanceof Buffer) {
      opts = {buffer: opts};
    }

    const accessor = {...this.settings, ...opts};
    state.bufferAccessor = accessor;

    if (opts.constant) {
      // set constant
      let value = opts.value;
      value = this._normalizeValue(value, [], 0);
      if (this.settings.normalized) {
        value = this._normalizeConstant(value);
      }
      const hasChanged = !state.constant || !this._areValuesEqual(value, this.value);

      if (!hasChanged) {
        return false;
      }
      state.externalBuffer = null;
      state.constant = true;
      this.value = value;
    } else if (opts.buffer) {
      const buffer = opts.buffer;
      state.externalBuffer = buffer;
      state.constant = false;
      this.value = opts.value;
      const isBuffer64Bit = opts.value instanceof Float64Array;

      // Copy the type of the buffer into the accessor
      accessor.type = opts.type || buffer.accessor.type;
      accessor.bytesPerElement = buffer.accessor.BYTES_PER_ELEMENT * (isBuffer64Bit ? 2 : 1);
      accessor.stride = getStride(accessor);
    } else if (opts.value) {
      this._checkExternalBuffer(opts);

      let value = opts.value;
      state.externalBuffer = null;
      state.constant = false;
      this.value = value;

      accessor.bytesPerElement = value.BYTES_PER_ELEMENT;
      accessor.stride = getStride(accessor);

      const {buffer, byteOffset} = this;

      if (this.doublePrecision && value instanceof Float64Array) {
        value = toDoublePrecisionArray(value, accessor);
      }
      // TODO: support offset in buffer.setData?
      if (buffer.byteLength < value.byteLength + byteOffset) {
        // Over allocation is required because shader attributes may have bigger offsets
        buffer.reallocate((value.byteLength + byteOffset) * 2);
      }
      // Hack: force Buffer to infer data type
      buffer.setAccessor(null);
      buffer.subData({data: value, offset: byteOffset});
      accessor.type = opts.type || buffer.accessor.type;
    }

    return true;
  }