scene.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Copyright (c) 2018-2021 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. }
  69. namespace Scene {
  70. export interface Group extends Object3D {
  71. readonly renderables: ReadonlyArray<GraphicsRenderable>
  72. }
  73. export function create(ctx: WebGLContext, variants = GraphicsRenderVariants): Scene {
  74. const renderableMap = new Map<GraphicsRenderObject, GraphicsRenderable>();
  75. const renderables: GraphicsRenderable[] = [];
  76. const boundingSphere = Sphere3D();
  77. const boundingSphereVisible = Sphere3D();
  78. const primitives: GraphicsRenderable[] = [];
  79. const volumes: GraphicsRenderable[] = [];
  80. let boundingSphereDirty = true;
  81. let boundingSphereVisibleDirty = true;
  82. const object3d = Object3D.create();
  83. const { view, position, direction, up } = object3d;
  84. function add(o: GraphicsRenderObject) {
  85. if (!renderableMap.has(o)) {
  86. const renderable = createRenderable(ctx, o, variants);
  87. renderables.push(renderable);
  88. if (o.type === 'direct-volume') {
  89. volumes.push(renderable);
  90. } else {
  91. primitives.push(renderable);
  92. }
  93. renderableMap.set(o, renderable);
  94. boundingSphereDirty = true;
  95. boundingSphereVisibleDirty = true;
  96. return renderable;
  97. } else {
  98. console.warn(`RenderObject with id '${o.id}' already present`);
  99. return renderableMap.get(o)!;
  100. }
  101. }
  102. function remove(o: GraphicsRenderObject) {
  103. const renderable = renderableMap.get(o);
  104. if (renderable) {
  105. renderable.dispose();
  106. arraySetRemove(renderables, renderable);
  107. arraySetRemove(primitives, renderable);
  108. arraySetRemove(volumes, renderable);
  109. renderableMap.delete(o);
  110. boundingSphereDirty = true;
  111. boundingSphereVisibleDirty = true;
  112. }
  113. }
  114. const commitBulkSize = 100;
  115. function commit(maxTimeMs: number) {
  116. const start = now();
  117. let i = 0;
  118. while (true) {
  119. const o = commitQueue.tryGetRemove();
  120. if (!o) break;
  121. remove(o);
  122. if (++i % commitBulkSize === 0 && now() - start > maxTimeMs) return false;
  123. }
  124. while (true) {
  125. const o = commitQueue.tryGetAdd();
  126. if (!o) break;
  127. add(o);
  128. if (++i % commitBulkSize === 0 && now() - start > maxTimeMs) return false;
  129. }
  130. renderables.sort(renderableSort);
  131. return true;
  132. }
  133. const commitQueue = new CommitQueue();
  134. let visibleHash = -1;
  135. function computeVisibleHash() {
  136. let hash = 23;
  137. for (let i = 0, il = renderables.length; i < il; ++i) {
  138. if (!renderables[i].state.visible) continue;
  139. hash = (31 * hash + renderables[i].id) | 0;
  140. }
  141. hash = hash1(hash);
  142. if (hash === -1) hash = 0;
  143. return hash;
  144. }
  145. function syncVisibility() {
  146. const newVisibleHash = computeVisibleHash();
  147. if (newVisibleHash !== visibleHash) {
  148. boundingSphereVisibleDirty = true;
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. return {
  155. view, position, direction, up,
  156. renderables,
  157. primitives: { view, position, direction, up, renderables: primitives },
  158. volumes: { view, position, direction, up, renderables: volumes },
  159. syncVisibility,
  160. update(objects, keepBoundingSphere) {
  161. Object3D.update(object3d);
  162. if (objects) {
  163. for (let i = 0, il = objects.length; i < il; ++i) {
  164. renderableMap.get(objects[i])?.update();
  165. }
  166. } else {
  167. for (let i = 0, il = renderables.length; i < il; ++i) {
  168. renderables[i].update();
  169. }
  170. }
  171. if (!keepBoundingSphere) {
  172. boundingSphereDirty = true;
  173. boundingSphereVisibleDirty = true;
  174. } else {
  175. syncVisibility();
  176. }
  177. },
  178. add: (o: GraphicsRenderObject) => commitQueue.add(o),
  179. remove: (o: GraphicsRenderObject) => commitQueue.remove(o),
  180. commit: (maxTime = Number.MAX_VALUE) => commit(maxTime),
  181. get needsCommit() { return !commitQueue.isEmpty; },
  182. has: (o: GraphicsRenderObject) => {
  183. return renderableMap.has(o);
  184. },
  185. clear: () => {
  186. for (let i = 0, il = renderables.length; i < il; ++i) {
  187. renderables[i].dispose();
  188. }
  189. renderables.length = 0;
  190. renderableMap.clear();
  191. boundingSphereDirty = true;
  192. boundingSphereVisibleDirty = true;
  193. },
  194. forEach: (callbackFn: (value: GraphicsRenderable, key: GraphicsRenderObject) => void) => {
  195. renderableMap.forEach(callbackFn);
  196. },
  197. get count() {
  198. return renderables.length;
  199. },
  200. get boundingSphere() {
  201. if (boundingSphereDirty) {
  202. calculateBoundingSphere(renderables, boundingSphere, false);
  203. boundingSphereDirty = false;
  204. }
  205. return boundingSphere;
  206. },
  207. get boundingSphereVisible() {
  208. if (boundingSphereVisibleDirty) {
  209. calculateBoundingSphere(renderables, boundingSphereVisible, true);
  210. boundingSphereVisibleDirty = false;
  211. visibleHash = computeVisibleHash();
  212. }
  213. return boundingSphereVisible;
  214. }
  215. };
  216. }
  217. }
  218. export { Scene };