state.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 REGL = require('regl');
  7. import * as glContext from 'mol-gl/context'
  8. import { Camera } from 'mol-gl/camera'
  9. import { Vec3 } from 'mol-math/linear-algebra'
  10. export default class State {
  11. regl: REGL.Regl
  12. initRegl (container: HTMLDivElement) {
  13. const regl = glContext.create({
  14. container,
  15. extensions: [
  16. 'OES_texture_float',
  17. 'OES_texture_float_linear',
  18. // 'ext_disjoint_timer_query',
  19. 'EXT_blend_minmax'
  20. ],
  21. // profile: true
  22. })
  23. const camera = Camera.create(regl, container, {
  24. center: Vec3.create(0, 0, 0)
  25. })
  26. const drawTriangle = regl({
  27. frag: `
  28. void main() {
  29. gl_FragColor = vec4(1, 0, 0, 1);
  30. }`,
  31. vert: `
  32. precision mediump float;
  33. uniform mat4 projection, view;
  34. attribute vec3 position;
  35. void main () {
  36. gl_Position = projection * view * vec4(position, 1.0);
  37. }`,
  38. attributes: {
  39. position: [[0, -1, 0], [-1, 0, 0], [1, 1, 0]]
  40. },
  41. count: 3
  42. })
  43. regl.frame(() => {
  44. camera.update((state: any) => {
  45. if (!camera.isDirty()) return
  46. console.log(state, camera)
  47. regl.clear({color: [0, 0, 0, 1]})
  48. drawTriangle()
  49. }, undefined)
  50. })
  51. this.regl = regl
  52. }
  53. constructor() {
  54. }
  55. }