scene.ts 9.0 KB

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