texture.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. import { Context } from './context'
  7. import { TextureImage } from '../renderable/util';
  8. import { ValueCell } from 'mol-util';
  9. import { RenderableSchema } from '../renderable/schema';
  10. import { idFactory } from 'mol-util/id-factory';
  11. const getNextTextureId = idFactory()
  12. export type TextureFormat = 'rgb' | 'rgba'
  13. export type TextureType = 'ubyte' | 'uint'
  14. export function getFormat(ctx: Context, format: TextureFormat) {
  15. const { gl } = ctx
  16. switch (format) {
  17. case 'rgb': return gl.RGB
  18. case 'rgba': return gl.RGBA
  19. }
  20. }
  21. export function getType(ctx: Context, type: TextureType) {
  22. const { gl } = ctx
  23. switch (type) {
  24. case 'ubyte': return gl.UNSIGNED_BYTE
  25. case 'uint': return gl.UNSIGNED_INT
  26. }
  27. }
  28. export interface Texture {
  29. readonly id: number
  30. readonly texture: WebGLTexture
  31. readonly format: number
  32. readonly type: number
  33. readonly width: number
  34. readonly height: number
  35. load: (image: TextureImage) => void
  36. bind: (id: TextureId) => void
  37. unbind: (id: TextureId) => void
  38. destroy: () => void
  39. }
  40. export type TextureId = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
  41. export type TextureValues = { [k: string]: ValueCell<TextureImage> }
  42. export type Textures = { [k: string]: Texture }
  43. export function createTexture(ctx: Context, _format: TextureFormat, _type: TextureType): Texture {
  44. const id = getNextTextureId()
  45. const { gl } = ctx
  46. const texture = gl.createTexture()
  47. if (texture === null) {
  48. throw new Error('Could not create WebGL texture')
  49. }
  50. const magFilter = gl.NEAREST
  51. const minFilter = gl.NEAREST
  52. const format = getFormat(ctx, _format)
  53. const type = getType(ctx, _type)
  54. let _width = 0
  55. let _height = 0
  56. let destroyed = false
  57. ctx.textureCount += 1
  58. return {
  59. id,
  60. texture,
  61. format,
  62. type,
  63. get width () { return _width },
  64. get height () { return _height },
  65. load: (image: TextureImage) => {
  66. const { array, width, height } = image
  67. gl.bindTexture(gl.TEXTURE_2D, texture)
  68. // unpack alignment of 1 since we use textures only for data
  69. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
  70. gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, type, array)
  71. _width = width
  72. _height = height
  73. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter)
  74. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter)
  75. // clamp-to-edge needed for non-power-of-two textures
  76. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  77. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  78. gl.bindTexture(gl.TEXTURE_2D, null)
  79. },
  80. bind: (id: TextureId) => {
  81. gl.activeTexture(gl.TEXTURE0 + id)
  82. gl.bindTexture(gl.TEXTURE_2D, texture)
  83. },
  84. unbind: (id: TextureId) => {
  85. gl.activeTexture(gl.TEXTURE0 + id)
  86. gl.bindTexture(gl.TEXTURE_2D, null)
  87. },
  88. destroy: () => {
  89. if (destroyed) return
  90. gl.deleteTexture(texture)
  91. destroyed = true
  92. ctx.textureCount -= 1
  93. }
  94. }
  95. }
  96. export function createTextures(ctx: Context, schema: RenderableSchema, values: TextureValues) {
  97. const textures: Textures = {}
  98. Object.keys(schema).forEach((k, i) => {
  99. const spec = schema[k]
  100. if (spec.type === 'texture') {
  101. const texture = createTexture(ctx, spec.format, spec.dataType)
  102. texture.load(values[k].ref.value)
  103. textures[k] = texture
  104. }
  105. })
  106. return textures
  107. }