camera-helper.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { WebGLContext } from '../../mol-gl/webgl/context';
  7. import Scene from '../../mol-gl/scene';
  8. import { Camera } from '../camera';
  9. import { MeshBuilder } from '../../mol-geo/geometry/mesh/mesh-builder';
  10. import { Vec3, Mat4 } from '../../mol-math/linear-algebra';
  11. import { addSphere } from '../../mol-geo/geometry/mesh/builder/sphere';
  12. import { GraphicsRenderObject } from '../../mol-gl/render-object';
  13. import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
  14. import { ColorNames } from '../../mol-util/color/names';
  15. import { addCylinder } from '../../mol-geo/geometry/mesh/builder/cylinder';
  16. import { Viewport } from '../camera/util';
  17. import { Sphere3D } from '../../mol-math/geometry';
  18. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  19. import produce from 'immer';
  20. import { Shape } from '../../mol-model/shape';
  21. // TODO add scale line/grid
  22. const AxesParams = {
  23. ...Mesh.Params,
  24. alpha: { ...Mesh.Params.alpha, defaultValue: 0.33 },
  25. ignoreLight: { ...Mesh.Params.ignoreLight, defaultValue: true },
  26. colorX: PD.Color(ColorNames.red, { isEssential: true }),
  27. colorY: PD.Color(ColorNames.green, { isEssential: true }),
  28. colorZ: PD.Color(ColorNames.blue, { isEssential: true }),
  29. scale: PD.Numeric(0.33, { min: 0.1, max: 2, step: 0.1 }, { isEssential: true }),
  30. };
  31. type AxesParams = typeof AxesParams
  32. type AxesProps = PD.Values<AxesParams>
  33. export const CameraHelperParams = {
  34. axes: PD.MappedStatic('on', {
  35. on: PD.Group(AxesParams),
  36. off: PD.Group({})
  37. }, { cycle: true, description: 'Show camera orientation axes' }),
  38. };
  39. export type CameraHelperParams = typeof CameraHelperParams
  40. export type CameraHelperProps = PD.Values<CameraHelperParams>
  41. export class CameraHelper {
  42. scene: Scene
  43. camera: Camera
  44. props: CameraHelperProps = {
  45. axes: { name: 'off', params: {} }
  46. }
  47. private renderObject: GraphicsRenderObject | undefined
  48. constructor(private webgl: WebGLContext, props: Partial<CameraHelperProps> = {}) {
  49. this.scene = Scene.create(webgl);
  50. this.camera = new Camera();
  51. Vec3.set(this.camera.up, 0, 1, 0);
  52. Vec3.set(this.camera.target, 0, 0, 0);
  53. this.setProps(props);
  54. }
  55. setProps(props: Partial<CameraHelperProps>) {
  56. this.props = produce(this.props, p => {
  57. if (props.axes !== undefined) {
  58. p.axes.name = props.axes.name;
  59. if (props.axes.name === 'on') {
  60. this.scene.clear();
  61. const params = { ...props.axes.params, scale: props.axes.params.scale * this.webgl.pixelRatio };
  62. this.renderObject = createAxesRenderObject(params);
  63. this.scene.add(this.renderObject);
  64. this.scene.commit();
  65. Vec3.set(this.camera.position, 0, 0, params.scale * 200);
  66. Mat4.lookAt(this.camera.view, this.camera.position, this.camera.target, this.camera.up);
  67. p.axes.params = { ...props.axes.params };
  68. }
  69. }
  70. });
  71. }
  72. get isEnabled() {
  73. return this.props.axes.name === 'on';
  74. }
  75. update(camera: Camera) {
  76. if (!this.renderObject) return;
  77. updateCamera(this.camera, camera.viewport);
  78. Mat4.extractRotation(this.scene.view, camera.view);
  79. const r = this.renderObject.values.boundingSphere.ref.value.radius;
  80. Mat4.setTranslation(this.scene.view, Vec3.create(
  81. -camera.viewport.width / 2 + r,
  82. -camera.viewport.height / 2 + r,
  83. 0
  84. ));
  85. }
  86. }
  87. function updateCamera(camera: Camera, viewport: Viewport) {
  88. const { near, far } = camera;
  89. const fullLeft = -(viewport.width - viewport.x) / 2;
  90. const fullRight = (viewport.width - viewport.x) / 2;
  91. const fullTop = (viewport.height - viewport.y) / 2;
  92. const fullBottom = -(viewport.height - viewport.y) / 2;
  93. const dx = (fullRight - fullLeft) / 2;
  94. const dy = (fullTop - fullBottom) / 2;
  95. const cx = (fullRight + fullLeft) / 2;
  96. const cy = (fullTop + fullBottom) / 2;
  97. const left = cx - dx;
  98. const right = cx + dx;
  99. const top = cy + dy;
  100. const bottom = cy - dy;
  101. Mat4.ortho(camera.projection, left, right, top, bottom, near, far);
  102. }
  103. function createAxesMesh(scale: number, mesh?: Mesh) {
  104. const state = MeshBuilder.createState(512, 256, mesh);
  105. const radius = 0.05 * scale;
  106. const x = Vec3.scale(Vec3(), Vec3.unitX, scale);
  107. const y = Vec3.scale(Vec3(), Vec3.unitY, scale);
  108. const z = Vec3.scale(Vec3(), Vec3.unitZ, scale);
  109. const cylinderProps = { radiusTop: radius, radiusBottom: radius, radialSegments: 32 };
  110. state.currentGroup = 0;
  111. addSphere(state, Vec3.origin, radius, 2);
  112. state.currentGroup = 1;
  113. addSphere(state, x, radius, 2);
  114. addCylinder(state, Vec3.origin, x, 1, cylinderProps);
  115. state.currentGroup = 2;
  116. addSphere(state, y, radius, 2);
  117. addCylinder(state, Vec3.origin, y, 1, cylinderProps);
  118. state.currentGroup = 3;
  119. addSphere(state, z, radius, 2);
  120. addCylinder(state, Vec3.origin, z, 1, cylinderProps);
  121. return MeshBuilder.getMesh(state);
  122. }
  123. function getAxesShape(props: AxesProps, shape?: Shape<Mesh>) {
  124. const scale = 100 * props.scale;
  125. const mesh = createAxesMesh(scale, shape?.geometry);
  126. mesh.setBoundingSphere(Sphere3D.create(Vec3.create(scale / 2, scale / 2, scale / 2), scale + scale / 4));
  127. const getColor = (groupId: number) => {
  128. switch (groupId) {
  129. case 1: return props.colorX;
  130. case 2: return props.colorY;
  131. case 3: return props.colorZ;
  132. default: return ColorNames.grey;
  133. }
  134. };
  135. return Shape.create('axes', {}, mesh, getColor, () => 1, () => '');
  136. }
  137. function createAxesRenderObject(props: AxesProps) {
  138. const shape = getAxesShape(props);
  139. return Shape.createRenderObject(shape, props);
  140. }