camera-helper.ts 6.2 KB

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