WhitestormJS/whs.js

DataTextureModule

Open

#214 opened on Mar 22, 2017

View on GitHub
 (7 comments) (0 reactions) (1 assignee)JavaScript (425 forks)batch import
HacktoberfestMODULE

Repository metrics

Stars
 (6,070 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I have a module for data textures from whs v1. I think either a DataTextureModule and TextureModule (see #213) can exist, or there can be one generic one. Here is the code for reference, I will make a v2 version soon. Also included some utils I used, but probably would not want those in the module. But maybe the data texture module could have some param options which are callback functions used to construct the texture. Something that gives you (x,y) of current position, and you return a [r, g, b, a], etc.

class DataTexture {
  static defaults = {
    data: undefined,
    width: DEFAULT_WIDTH,
    height: DEFAULT_HEIGHT,
    mapping: THREE.UVMapping,
    format: THREE.RGBFormat,
    wrapS: THREE.ClampToEdgeWrapping,
    wrapT: THREE.ClampToEdgeWrapping,
    minFilter: THREE.LinearFilter, // NearestFilter,
    magFilter: THREE.LinearFilter,
    type: THREE.UnsignedByteType,
    anisotropy: 1, // see renderer.getMaxAnisotropy()
    needsUpdate: false, // flag this when modifying texture (e.g. wrap, encoding)
    encoding: THREE.LinearEncoding, // sRGB, RGBE, RGBM, RGBD, LogLuv and Gamma
    onUpdate: () => {}
  }

  constructor (params = {}) {
    params = Object.assign({}, DataTexture.defaults, params)

    const dataTexture = new THREE.DataTexture(
      params.data,
      params.width, params.height,
      params.format,
      params.type,
      params.mapping,
      params.wrapS, params.wrapT,
      params.magFilter, params.minFilter,
      params.anisotropy
    )

    dataTexture.needsUpdate = true

    this.dataTexture = dataTexture

    return this.dataTexture
  }
}

// Utils
export const generateTextureArray = ({
  width = DEFAULT_WIDTH,
  height = DEFAULT_HEIGHT,
  format = 'rgba'
} = {}) => {
  if (typeof(width) !== 'number' || typeof(height) !== 'number') {
    throw new Error('width and height must be numbers')
  }

  if (typeof(format.length) !== 'number') {
    throw new Error('format.length must be a number')
  }

  return new Uint8Array(width * height * format.length)
}

export const dataMaker = ({
  width = DEFAULT_WIDTH,
  height = DEFAULT_HEIGHT,
  format = 'rgba',
  cb = function (i) {
    const val = Math.random() * 255
    const opacity = 1

    return [val, val, val, opacity]
  }
} = {}) => {
  const data = generateTextureArray({ width, height, format })

  for (let i = 0; i < data.length; i += format.length) {
    // cb receives i and should return an array of same length as format
    const datum = cb(i)

    for (let j = 0; j < format.length; j++) {
      data[i + j] = datum[j]
    }
  }

  return data
}

export default DataTexture
Version:
  • v2.x.x
  • v1.x.x
Issue type:
  • Bug
  • Proposal/Enhancement
  • Question

Desktop
  • Chrome
  • Chrome Canary
  • Chrome dev-channel
  • Firefox
  • Opera
  • Microsoft IE
  • Microsoft Edge
Android
  • Chrome
  • Firefox
  • Opera
IOS
  • Chrome
  • Firefox
  • Opera

Contributor guide