uniform.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 { Mat3, Mat4, Vec2, Vec3, Vec4 } from 'mol-math/linear-algebra'
  7. import { ValueCell } from 'mol-util';
  8. import { GLRenderingContext } from './compat';
  9. import { RenderableSchema } from 'mol-gl/renderable/schema';
  10. export type UniformKindValue = {
  11. 'f': number
  12. 'i': number
  13. 'v2': Vec2
  14. 'v3': Vec3
  15. 'v4': Vec4
  16. 'm3': Mat3
  17. 'm4': Mat4
  18. 't': number
  19. }
  20. export type UniformKind = keyof UniformKindValue
  21. export type UniformType = number | Vec2 | Vec3 | Vec4 | Mat3 | Mat4
  22. export type UniformValues = { [k: string]: ValueCell<UniformType> }
  23. export function setUniform(gl: GLRenderingContext, location: WebGLUniformLocation | null, kind: UniformKind, value: any) {
  24. switch (kind) {
  25. case 'f': gl.uniform1f(location, value); break
  26. case 'i': case 't': gl.uniform1i(location, value); break
  27. case 'v2': gl.uniform2fv(location, value); break
  28. case 'v3': gl.uniform3fv(location, value); break
  29. case 'v4': gl.uniform4fv(location, value); break
  30. case 'm3': gl.uniformMatrix3fv(location, false, value); break
  31. case 'm4': gl.uniformMatrix4fv(location, false, value); break
  32. default: console.error(`unknown uniform kind '${kind}'`)
  33. }
  34. }
  35. export type UniformSetter = (gl: GLRenderingContext, location: number, value: any) => void
  36. export type UniformSetters = { [k: string]: UniformSetter }
  37. function uniform1f (gl: GLRenderingContext, location: number, value: any) { gl.uniform1f(location, value) }
  38. function uniform1i (gl: GLRenderingContext, location: number, value: any) { gl.uniform1i(location, value) }
  39. function uniform2fv (gl: GLRenderingContext, location: number, value: any) { gl.uniform2fv(location, value) }
  40. function uniform3fv (gl: GLRenderingContext, location: number, value: any) { gl.uniform3fv(location, value) }
  41. function uniform4fv (gl: GLRenderingContext, location: number, value: any) { gl.uniform4fv(location, value) }
  42. function uniformMatrix3fv (gl: GLRenderingContext, location: number, value: any) { gl.uniformMatrix3fv(location, false, value) }
  43. function uniformMatrix4fv (gl: GLRenderingContext, location: number, value: any) { gl.uniformMatrix4fv(location, false, value) }
  44. function getUniformSetter(kind: UniformKind) {
  45. switch (kind) {
  46. case 'f': return uniform1f
  47. case 'i': case 't': return uniform1i
  48. case 'v2': return uniform2fv
  49. case 'v3': return uniform3fv
  50. case 'v4': return uniform4fv
  51. case 'm3': return uniformMatrix3fv
  52. case 'm4': return uniformMatrix4fv
  53. }
  54. throw new Error(`unknown uniform kind '${kind}'`)
  55. }
  56. export function getUniformSetters(schema: RenderableSchema) {
  57. const setters: UniformSetters = {}
  58. Object.keys(schema).forEach(k => {
  59. const spec = schema[k]
  60. if (spec.type === 'uniform') {
  61. setters[k] = getUniformSetter(spec.kind as UniformKind)
  62. } else if (spec.type === 'texture') {
  63. setters[k] = getUniformSetter('t')
  64. }
  65. })
  66. return setters
  67. }