util.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. export function calculateTextureInfo (n: number, itemSize: number) {
  7. const sqN = Math.sqrt(n * itemSize)
  8. let width = Math.ceil(sqN)
  9. width = width + (itemSize - (width % itemSize)) % itemSize
  10. const height = width > 0 ? Math.ceil(n * itemSize / width) : 0
  11. return { width, height, length: width * height * itemSize }
  12. }
  13. export interface TextureImage {
  14. array: Uint8Array
  15. width: number
  16. height: number
  17. }
  18. export function createColorTexture (n: number): TextureImage {
  19. const { length, width, height } = calculateTextureInfo(n, 3)
  20. return { array: new Uint8Array(length), width, height }
  21. }
  22. export const emptyTexture = { array: new Uint8Array(0), width: 0, height: 0 }
  23. export function fillSerial<T extends Helpers.NumberArray> (array: T) {
  24. const n = array.length
  25. for (let i = 0; i < n; ++i) array[ i ] = i
  26. return array
  27. }