scene.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { WebGLContext } from './webgl/context';
  8. import { GraphicsRenderObject, createRenderable } from './render-object';
  9. import { Object3D } from './object3d';
  10. import { Sphere3D } from '../mol-math/geometry';
  11. import { CommitQueue } from './commit-queue';
  12. import { now } from '../mol-util/now';
  13. import { arraySetRemove } from '../mol-util/array';
  14. import { BoundaryHelper } from '../mol-math/geometry/boundary-helper';
  15. import { hash1 } from '../mol-data/util';
  16. import { GraphicsRenderable } from './renderable';
  17. import { GraphicsRenderVariants } from './webgl/render-item';
  18. const boundaryHelper = new BoundaryHelper('98');
  19. function calculateBoundingSphere(renderables: GraphicsRenderable[], boundingSphere: Sphere3D, onlyVisible: boolean): Sphere3D {
  20. boundaryHelper.reset();
  21. for (let i = 0, il = renderables.length; i < il; ++i) {
  22. if (onlyVisible && !renderables[i].state.visible) continue;
  23. const boundingSphere = renderables[i].values.boundingSphere.ref.value;
  24. if (!boundingSphere.radius) continue;
  25. boundaryHelper.includeSphere(boundingSphere);
  26. }
  27. boundaryHelper.finishedIncludeStep();
  28. for (let i = 0, il = renderables.length; i < il; ++i) {
  29. if (onlyVisible && !renderables[i].state.visible) continue;
  30. const boundingSphere = renderables[i].values.boundingSphere.ref.value;
  31. if (!boundingSphere.radius) continue;
  32. boundaryHelper.radiusSphere(boundingSphere);
  33. }
  34. return boundaryHelper.getSphere(boundingSphere);
  35. }
  36. function renderableSort(a: GraphicsRenderable, b: GraphicsRenderable) {
  37. const drawProgramIdA = (a.getProgram('colorBlended') || a.getProgram('colorWboit')).id;
  38. const drawProgramIdB = (b.getProgram('colorBlended') || a.getProgram('colorWboit')).id;
  39. const materialIdA = a.materialId;
  40. const materialIdB = b.materialId;
  41. if (drawProgramIdA !== drawProgramIdB) {
  42. // sort by program id to minimize gl state changes
  43. return drawProgramIdA - drawProgramIdB;
  44. } else if (materialIdA !== materialIdB) {
  45. // sort by material id to minimize gl state changes
  46. return materialIdA - materialIdB;
  47. } else {
  48. return a.id - b.id;
  49. }
  50. }
  51. interface Scene extends Object3D {
  52. readonly count: number
  53. readonly renderables: ReadonlyArray<GraphicsRenderable>
  54. readonly boundingSphere: Sphere3D
  55. readonly boundingSphereVisible: Sphere3D
  56. readonly primitives: Scene.Group
  57. readonly volumes: Scene.Group
  58. /** Returns `true` if some visibility has changed, `false` otherwise. */
  59. syncVisibility: () => boolean
  60. update: (objects: ArrayLike<GraphicsRenderObject> | undefined, keepBoundingSphere: boolean) => void
  61. add: (o: GraphicsRenderObject) => void // GraphicsRenderable
  62. remove: (o: GraphicsRenderObject) => void
  63. commit: (maxTimeMs?: number) => boolean
  64. readonly needsCommit: boolean
  65. has: (o: GraphicsRenderObject) => boolean
  66. clear: () => void
  67. forEach: (callbackFn: (value: GraphicsRenderable, key: GraphicsRenderObject) => void) => void
  68. getMarkerAverage: () => number
  69. }
  70. namespace Scene {
  71. export interface Group extends Object3D {
  72. readonly renderables: ReadonlyArray<GraphicsRenderable>
  73. }
  74. export function create(ctx: WebGLContext, variants = GraphicsRenderVariants): Scene {
  75. const renderableMap = new Map<GraphicsRenderObject, GraphicsRenderable>();
  76. const renderables: GraphicsRenderable[] = [];
  77. const boundingSphere = Sphere3D();
  78. const boundingSphereVisible = Sphere3D();
  79. const primitives: GraphicsRenderable[] = [];
  80. const volumes: GraphicsRenderable[] = [];
  81. let boundingSphereDirty = true;
  82. let boundingSphereVisibleDirty = true;
  83. const object3d = Object3D.create();
  84. const { view, position, direction, up } = object3d;
  85. function add(o: GraphicsRenderObject) {
  86. if (!renderableMap.has(o)) {
  87. const renderable = createRenderable(ctx, o, variants);
  88. renderables.push(renderable);
  89. if (o.type === 'direct-volume') {
  90. volumes.push(renderable);
  91. } else {
  92. primitives.push(renderable);
  93. }
  94. renderableMap.set(o, renderable);
  95. boundingSphereDirty = true;
  96. boundingSphereVisibleDirty = true;
  97. return renderable;
  98. } else {
  99. console.warn(`RenderObject with id '${o.id}' already present`);
  100. return renderableMap.get(o)!;
  101. }
  102. }
  103. function remove(o: GraphicsRenderObject) {
  104. const renderable = renderableMap.get(o);
  105. if (renderable) {
  106. renderable.dispose();
  107. arraySetRemove(renderables, renderable);
  108. arraySetRemove(primitives, renderable);
  109. arraySetRemove(volumes, renderable);
  110. renderableMap.delete(o);
  111. boundingSphereDirty = true;
  112. boundingSphereVisibleDirty = true;
  113. }
  114. }
  115. const commitBulkSize = 100;
  116. function commit(maxTimeMs: number) {
  117. const start = now();
  118. let i = 0;
  119. while (true) {
  120. const o = commitQueue.tryGetRemove();
  121. if (!o) break;
  122. remove(o);
  123. if (++i % commitBulkSize === 0 && now() - start > maxTimeMs) return false;
  124. }
  125. while (true) {
  126. const o = commitQueue.tryGetAdd();
  127. if (!o) break;
  128. add(o);
  129. if (++i % commitBulkSize === 0 && now() - start > maxTimeMs) return false;
  130. }
  131. renderables.sort(renderableSort);
  132. return true;
  133. }
  134. const commitQueue = new CommitQueue();
  135. let visibleHash = -1;
  136. function computeVisibleHash() {
  137. let hash = 23;
  138. for (let i = 0, il = renderables.length; i < il; ++i) {
  139. if (!renderables[i].state.visible) continue;
  140. hash = (31 * hash + renderables[i].id) | 0;
  141. }
  142. hash = hash1(hash);
  143. if (hash === -1) hash = 0;
  144. return hash;
  145. }
  146. function syncVisibility() {
  147. const newVisibleHash = computeVisibleHash();
  148. if (newVisibleHash !== visibleHash) {
  149. boundingSphereVisibleDirty = true;
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }
  155. return {
  156. view, position, direction, up,
  157. renderables,
  158. primitives: { view, position, direction, up, renderables: primitives },
  159. volumes: { view, position, direction, up, renderables: volumes },
  160. syncVisibility,
  161. update(objects, keepBoundingSphere) {
  162. Object3D.update(object3d);
  163. if (objects) {
  164. for (let i = 0, il = objects.length; i < il; ++i) {
  165. renderableMap.get(objects[i])?.update();
  166. }
  167. } else {
  168. for (let i = 0, il = renderables.length; i < il; ++i) {
  169. renderables[i].update();
  170. }
  171. }
  172. if (!keepBoundingSphere) {
  173. boundingSphereDirty = true;
  174. boundingSphereVisibleDirty = true;
  175. } else {
  176. syncVisibility();
  177. }
  178. },
  179. add: (o: GraphicsRenderObject) => commitQueue.add(o),
  180. remove: (o: GraphicsRenderObject) => commitQueue.remove(o),
  181. commit: (maxTime = Number.MAX_VALUE) => commit(maxTime),
  182. get needsCommit() { return !commitQueue.isEmpty; },
  183. has: (o: GraphicsRenderObject) => {
  184. return renderableMap.has(o);
  185. },
  186. clear: () => {
  187. for (let i = 0, il = renderables.length; i < il; ++i) {
  188. renderables[i].dispose();
  189. }
  190. renderables.length = 0;
  191. renderableMap.clear();
  192. boundingSphereDirty = true;
  193. boundingSphereVisibleDirty = true;
  194. },
  195. forEach: (callbackFn: (value: GraphicsRenderable, key: GraphicsRenderObject) => void) => {
  196. renderableMap.forEach(callbackFn);
  197. },
  198. get count() {
  199. return renderables.length;
  200. },
  201. get boundingSphere() {
  202. if (boundingSphereDirty) {
  203. calculateBoundingSphere(renderables, boundingSphere, false);
  204. boundingSphereDirty = false;
  205. }
  206. return boundingSphere;
  207. },
  208. get boundingSphereVisible() {
  209. if (boundingSphereVisibleDirty) {
  210. calculateBoundingSphere(renderables, boundingSphereVisible, true);
  211. boundingSphereVisibleDirty = false;
  212. visibleHash = computeVisibleHash();
  213. }
  214. return boundingSphereVisible;
  215. },
  216. getMarkerAverage() {
  217. if (primitives.length === 0 && volumes.length === 0) return 0;
  218. let markerAverage = 0;
  219. for (let i = 0, il = primitives.length; i < il; ++i) {
  220. markerAverage += primitives[i].values.markerAverage.ref.value;
  221. }
  222. for (let i = 0, il = volumes.length; i < il; ++i) {
  223. markerAverage += volumes[i].values.markerAverage.ref.value;
  224. }
  225. return markerAverage / (primitives.length + volumes.length);
  226. },
  227. };
  228. }
  229. }
  230. export { Scene };