threepipe
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

conversion.ts 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {Color, DataTexture, DataUtils, LinearSRGBColorSpace, RGBAFormat, UnsignedByteType, Vector4} from 'three'
  2. export function dataTextureFromColor(color: Color) {
  3. const dataTexture = new DataTexture(new Uint8Array([Math.floor(color.r * 255), Math.floor(color.g * 255), Math.floor(color.b * 255), 255]), 1, 1, RGBAFormat, UnsignedByteType)
  4. dataTexture.needsUpdate = true
  5. dataTexture.colorSpace = LinearSRGBColorSpace
  6. return dataTexture
  7. }
  8. export function dataTextureFromVec4(color: Vector4) {
  9. const dataTexture = new DataTexture(new Uint8Array([Math.floor(color.x * 255), Math.floor(color.y * 255), Math.floor(color.z * 255), Math.floor(color.w * 255)]), 1, 1, RGBAFormat, UnsignedByteType)
  10. dataTexture.needsUpdate = true
  11. return dataTexture
  12. }
  13. /**
  14. * Convert half float buffer to rgbe
  15. * adapted from https://github.com/enkimute/hdrpng.js/blob/3a62b3ae2940189777df9f669df5ece3e78d9c16/hdrpng.js#L235
  16. * channels = 4 for RGBA data or 3 for RGB data. buffer from THREE.DataTexture
  17. * @param buffer
  18. * @param channels
  19. * @param res
  20. */
  21. export function halfFloatToRgbe(buffer: Uint16Array, channels = 3, res?: Uint8ClampedArray): Uint8ClampedArray {
  22. let r, g, b, v, s
  23. const l = buffer.byteLength / (channels * 2) | 0
  24. res = res || new Uint8ClampedArray(l * 4)
  25. for (let i = 0;i < l;i++) {
  26. r = DataUtils.fromHalfFloat(buffer[i * channels]) * 65504
  27. g = DataUtils.fromHalfFloat(buffer[i * channels + 1]) * 65504
  28. b = DataUtils.fromHalfFloat(buffer[i * channels + 2]) * 65504
  29. v = Math.max(Math.max(r, g), b)
  30. const e = Math.ceil(Math.log2(v)); s = Math.pow(2, e - 8)
  31. res[i * 4] = r / s | 0
  32. res[i * 4 + 1] = g / s | 0
  33. res[i * 4 + 2] = b / s | 0
  34. res[i * 4 + 3] = e + 128
  35. }
  36. return res
  37. }