All files / src/threef scene-renderer.ts

0% Statements 0/183
0% Branches 0/96
0% Functions 0/16
0% Lines 0/183

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import * as THREE from 'three';
import {ThreeInspector} from './three-inspector';
 
import {DomWatcher} from '../dom/dom-watcher';
import {elementVisibility} from '../dom/element-visibility';
import * as is from '../is/is';
import * as mathf from '../mathf/mathf';
 
export enum SceneResizingAlgo {
  /**
   * This is most commonly used three.js aspect ratio based resizing but
   * doesn't really work great on mobile if you are using the full viewport.
   *
   * Basically doing:
   *  camera.aspect = aspect;
   *  camera.updateProjectionMatrix();
   *
   * Basically, the camera will project out objects to the aspect ratio
   * of the dom element.  It contains Y but clips X.
   *
   * Thinking in terms of css, you can think of this similar to a
   * background-size: contain for height only and background-cover for width.
   *
   * Using this, as you resize your scene, the tops and bottom will never be
   * clipped but the sides can be clipped.  All of this is relative to
   * how you setup your scene as well (camera position, object positions etc)
   *
   * Nuthsell:
   * - If you resize the height, your object will go down in size.
   * - If you resize the width, your objects will not change in size.
   * - Resizing is based on a NDC 0,0 coordinates.
   */
  standardAspect = 'standardAspect',
 
  /**
   * An algo that attempts to recreate a similar effect as background: contain
   * but it's based on zoom.  This require you to pass the resizeScalar value
   * which in this case would be used to multiply the zoom value.
   *
   * This is a less preferred method so consider using 'contain' instead.
   *
   * Nuthsell:
   * - If you resize the height, your object will go down in size.
   * - If you resize the width, your objects will go down in size by changing
   *   the camera zoom.
   * - Resizing is based on a NDC 0,0 coordinates.
   * - resizingScalar can be passed to adust the zoom levels.
   */
  resizeWithZoom = 'resizeWithZoom',
 
  /**
   * This algo can be useful because it calculates the FOV
   * based on a scalar value.
   *
   * This is useful in two scenerios.
   * 1) You want to force an absolute size for your objects.
   *    By passing a resizingScalar that doesn't change,
   *    the projection will always be of a consistent size regardless
   *    of the canvas size.
   *
   *    The easiest way to describe this is to image a window in your house.
   *    The window would be the canvas size (scene size).  By using a resizingScalar,
   *    you can basically create a scene where your house window size might
   *    change but what is projected out the window is exaclty the same size.
   *    It more like peaking into another world without the world changing.
   *
   *    To use this, set your resizingAlgo to 'resizeWithFov' and also pass
   *    a resizingScalar (something like 200 or 1000).  When you do this,
   *    you'll notice that your objects are always the same size.
   *
   *    If you have breakpoints in your project, could could change the
   *    resizingScalar based on your breakpoints and the objects in your scene,
   *    will nicely resizing and you have control over how it looks.
   *
   * 2) Knowing the characteristics described in 1,
   *    you can make a responsive scene by changing adjusting the resizingScalar.
   *    The basis of how it scales can change depending on how you want to
   *    adjust the scalar.
   *
   *    This provides you with the ability to resize the scene objects based on
   *    axis
   *    - x (element.offsetWidth)
   *    - y (element.offsetHeight)
   *    - x or y (Math.min(element.offsetWidth, element.offsetHeight))
   *
   * ```ts
   *  const scene = new THREE.Scene();
   *  const element = document.getElementById('my-element');
   *  sceneRenderer.addScene({
   *    resizingAlgo: 'resizingWithFov',
   *    domElement: element,
   *    // Update the scalar prior to resizing.
   *    onBeforeResize: ()=> {
   *     // Resizing the objects in your scene based on the current width of the
   *     // dom element.
   *     // If you do this, the objects will get smaller as you narrow your browser.
   *     // (or the element width gets smaller)
   *     scene.userData.resizingScalar = element.offsetWidth * 0.2
   *
   *
   *     // Resizing the objects in your scene based on the current height of the
   *     // dom element.
   *     // If you do this, the objects will get smaller as you shorten your browser
   *     // (or the element height gets smaller).
   *     scene.userData.resizingScalar = element.offsetHeight * 0.2;
   *
   *
   *     // Resize both in x and y scales.
   *     scene.userData.resizingScalar = Math.min(element.offsetHeight, element.offsetWidth) * 0.2;
   *   }
   * })
   *
   * ```
   */
  resizeWithFov = 'resizeWithFov',
 
  /**
   * An algo that "attempts" to recreate contain type effects.
   * Read below on the Resizing Algo section for more information on
   * this algo.
   */
  contain = 'contain',
 
  /**
   * An algo that "attempts" to recreate cover type effects.
   * Read below on the Resizing Algo section for more information on
   * this algo.
   */
  cover = 'cover',
 
  /**
   * Indicates that scene-renders should not attempt to resize and fix
   * camera aspect on resizing.  This will be handled manually.  Scene
   * defaults to manual if nothing is specified for resizingAlgo.
   *
   *
   * An example of manually handing the resizing.
   * ```
   *
   *       this.sceneRenderer.addScene({
   *           resizingAlgo: 'manual',
   *           scene: scene,
   *           camera: camera,
   *           domElement: element,
   *           onResize() {
   *               camera.aspect = element.offsetWidth / element.offsetHeight;
   *               camera.updateProjectionMatrix();
   *           }
   *       });
   *
   * ```
   */
  manual = 'manual',
}
 
export interface SceneConfig {
  scene: THREE.Scene;
  camera: THREE.Camera;
  domElement: HTMLElement;
  // A onetime callback executed when module is added to the renderer.
  onInit?: Function;
  // A callback to execute prior to this scene being renders.
  onBeforeRender?: Function;
  // A callback to execute after this scene gets renders.
  // This can be considered a clean up of actions that maybe done during
  // onBeforeRender.  For example, a scene may alter the renderer temporarily
  // in onBeforeRender (such as adding gammaOutput).  Each scene should clean up
  // after itself so that it undoes any actions.
  onAfterRender?: Function;
  // A callback to executed on each resize event.
  onBeforeResize?: Function;
  // A callback to executed on each resize event.
  onResize?: Function;
  // A callback to executed on each dispose
  onDipose?: Function;
 
  // Add debugging to the scene.
  debug?: Boolean;
 
  // The type of resizing that should be used.
  resizingAlgo: string;
  // A scalar value used to control resizing when an applicable resizingAlgo
  // is specified.
  resizingScalar?: number;
  // Optional object that can be passed to certain resizing
  resizingOptions?: object;
}
 
export interface SceneRendererConfig {
  /**
   * The root element to create the main canvas.
   * Optional and typically this would default to the document.body.
   */
  rootElement?: HTMLElement;
 
  /**
   * The standard THREE.WEbGLRenderer options such as antialias:true.
   * If you leave this empty, sceneRender will default to it's internal
   * options for renderer which makes best effort to cover most cases.
   */
  rendererOptions?: Object;
 
  /**
   * Whether to use absolute positioning of the master canvas.
   * Defaults to false.
   * This can have performance benefits but as of now, it is experimental.
   */
  useAbsolutePositioning?: boolean;
}
 
/**
 * A class that attempts to get around max webGL context issues by having a
 * single renderer on the page.
 *
 * At a high level the issue is that you can't have more than 8 or so webGL
 * contexts as a general browser limitation.  Each context also can't share
 * resources.   So on pages where you might want to have like multiple webGL
 * canvases, you hit a road block.
 *
 * The solution here is to have a single canvas that covers the entire page (100vw x 100vh)
 * This single renderer would accept three.js scenes, which each define
 * a root html element to indicate the size and position to render.
 *
 * These scenes get reigstered to the sceneRenderer which upon calling,
 * sceneRenderer, will check the location of each registered scene dom element
 * and render it at the right place.
 *
 * This means that you could potentially export out a single gltf file with
 * multiple scenes and load it once to render on various DOM elements allowing
 * you to recycle geometry and avoid the webGL context limitations.
 *
 * For example usage, see:
 * @see /examples/three-scene-renderer.js
 * @see /examples/three-scene-renderer2.js
 *
 *
 * A high level example:
 *
 * ```
 * // Create an instance of SceneRenderer
 * this.sceneRenderer = new SceneRenderer({});
 *
 * // Add as many scenes as you like.
 *  this.sceneRenderer.addScene({
 *      // The resizing algo.
 *      resizingAlgo: 'resizeWithFov',
 *      resizingScalar: 1.0,
 *      scene: scene,
 *      camera: camera,
 *      // The dom element that this scene should render and size to.
 *      domElement: document.getElementById('my-element'),
 *      onBeforeRender: (renderer, scene, camera)=> {
 *           // Add some animations or whatever you need on render.
 *           scene.children[ 0 ].rotation.y = Date.now() * 0.001;
 *      },
 *      onAfterRender: (renderer, scene, camera)=> {
 *          // This is used for clean up.  For example, you may
 *          // have had a need to alter the settings of the renderer.
 *          // You would clean up here.
 *      }
 *      onBeforeResize: (renderer, scene, camera)=> {
 *         // Gets fired prior to resizing algo gets calculated.
 *
 *         // Update any of your scene options.
 *         scene.userData.resizingScalar = 2.0;
 *      },
 *      onResize(renderer, scene, camera) {
 *         // Gets fired after resizing algo gets calculated.
 *         // Depending on the algo you use, the camera.fov and aspect
 *         // would have been altered by this stage.
 *         // If you need access to pre alter values, then use
 *         // onBeforeResize
 *      }
 *     });
 *  });
 *
 * // Whereever you have RAF setup,
 * this.sceneRenderer.render();
 *
 * // Later dispose to clean up.
 * this.sceneRenderer.dispose();
 *
 * ```
 *
 *
 * ## Resizing Algos
 * There are various resizing algos available.  The current recommendation is
 * to use 'contain' or 'cover' where possible as this has been designed to most closely
 * reflect how css background-contain would work.
 *
 * You can also specify your own logic by setting resizingAlgo to 'manual'.
 *
 *
 * Using 'Contain' algo
 * The resizingAlgo contain accepts a few parameters.  Because in a 3d scene,
 * size is really relative to the current camera position and object sizes,
 * it is up to you to define the "bounds" of which should be contained in
 * your scene.  This is different per project but is specified in webGL world
 * scale.
 *
 * Contain requires the following resizing options:
 * - scalarX - a float representing the horizontal scale of the bounding box
 * - scalarY - a float representing the vertical scalar of the bounding box.
 *
 * The easiest way to figure out what values to use is to imagine
 * a box with an image on your screen and basically, you are defining
 * that box size and telling the scene-renderer to never crop that image (contain).
 *
 * Start with the shape you want to draw.  If you say wanted a
 * tall image, perhaps it's like 1200x1800 pixels.  Try using
 * those values but reducing it by a factor of 1000 (for example).
 *
 * scaleX: 1200 / 1000
 * scaleY: 1800 / 1000
 *
 * The actual values of scaleX and scaleY will depend on your scene
 * object sizes and what you need.  What scaleX and scaleY define
 * are the relative size of the area that should be contained.
 *
 * As a side note you can also achieve a quick "contain" effect
 * by using the 'resizeWithFov' option and passing a single resizingScalar.
 *
 * useFov
 * By default, size adjustments are performed by changing the camera zoom level.
 * This has an added benefit of correctly resizing text if you are calculating
 * text scale using threef.toDomCoordinates method.  It also doesn't skew
 * perspective.
 *
 * If you don't want to adjust zoom for some reason, you can opt to change
 * scene size by alterning the useFov to a true value.  This will adjust the
 * camera FOV as needed.  If your scene is orthographic or 2d based, you won't
 * see a difference but in 3d scene it will alter and skew perspective.
 *
 * Alignment options:
 * By default, without any alignment options, your scene
 * will scale at center / center point.  However, you
 * can define it so that it scales aligned to top, bottom,
 * left or right similar to how it would work in css.
 *
 * top - a value between 0-1 to note the vertical position of the final box
 * bottom - a value between 0-1 to note the vertical position of the final box
 * left - a value between 0-1 to note the horizontal position of the final box
 * right - a value between 0-1 to note the horizontal position of the final box
 *
 * top / bottom can't exist together
 * left / right can't exist together
 *
 * An example of using 'contain' algo
 *
 * ```ts
 *  this.sceneRenderer.addScene({
 *      // The resizing algo.
 *      resizingAlgo: 'contain',
 *      resizingOptions: {
 *           useFov: false, // Whether to use FOV instead of zoom
 *           scalarX: 2.6,
 *           scalarY: 3.8,
 *           top: 0 // Align this to the top
 *           left: 0 // Align this to the left
 *      },
 *     });
 *  });
 * ```
 *
 *
 * Using 'Cover' algo
 * Cover works in a very similar way to contain, you need to specify your
 * scalarX and scalarY values to tell scene-renderer to respect that box to
 * calculate the cover values.  You can specify top, bottom, left and right
 * values to cover scale from the specific axis.
 *
 * ```ts
 *  this.sceneRenderer.addScene({
 *      // The resizing algo.
 *      resizingAlgo: 'cover',
 *      resizingOptions: {
 *           useFov: false, // Whether to use FOV instead of zoom
 *           scalarX: 2.6,
 *           scalarY: 3.8,
 *           top: 0 // Align this to the top
 *           left: 0 // Align this to the left
 *      },
 *     });
 *  });
 *
 *
 * Advanced Resizing
 *
 *
 * Here is an example of changing scalar and aspect ratio,
 * based on your mobile versus desktop layout.
 *
 * ```ts
 *
 *  const sizeScalar = 0.2;
 *  this.sceneRenderer.addScene({
 *      // The resizing algo.
 *      resizingAlgo: 'cover',
 *      // Required - you should define the resizing options.
 *      resizingOptions: {
 *         scalarX: null,
 *         scalarY: null,
 *         top: null,
 *      },
 *      onBeforeResize: (renderer, scene, camera)=> {
 *         if(window.innerWidth < 800) {
 *             scene.userData.resizingOptions.scalarX = 400 * sizeScalar;
 *             scene.userData.resizingOptions.scalarY = 800 * sizeScalar;
 *             scene.userData.resizingOptions.top = 0;
 *         } else {
 *             scene.userData.resizingOptions.scalarX = 1920 * sizeScalar;
 *             scene.userData.resizingOptions.scalarY = 1080 * sizeScalar;
 *             scene.userData.resizingOptions.top = 0.5;
 *         }
 *      },
 *     });
 *  });
 *
 * ```
 *
 * ### Applying Degu Shader Chunks
 *
 * You can apply shader chunks as follows:
 * ```
 *import {deguShape2d} from
 * 'degu/lib/shaders/three-shader-chunks/degu-shape2d';
 *import {deguMathf} from
 * 'degu/lib/shaders/three-shader-chunks/degu-mathf';
 *   // Apply shader chunks.
 *   deguMathf(this.sceneRenderer.getThree());
 *   deguShape2d(this.sceneRenderer.getThree());
 *
 * ```
 *
 *
 *
 * @see examples/three-scene-renderer2.html for examples of the resizign strategies.
 *
 *
 * @see https://stackoverflow.com/questions/41919341/is-there-a-limit-to-the-number-of-three-webglrenderer-instances-in-a-page
 * @see https://stackoverflow.com/questions/30608723/is-it-possible-to-enable-unbounded-number-of-renderers-in-three-js/30633132#30633132
 * @see https://threejs.org/examples/webgl_multiple_elements.html
 */
export class SceneRenderer {
  /**
   * The internal instance of domWatcher.
   */
  private watcher: DomWatcher;
 
  /**
   * The root element to attach the renderer to.  This would
   * typically just be the document body.
   */
  private rootElement: HTMLElement;
 
  /**
   * The main canvas used to render scenes.  This would cover the whole screen.
   */
  private canvas: HTMLCanvasElement;
 
  /**
   * The dpr.
   */
  private dpr: number;
 
  /**
   * The Three webGLRenderer.
   */
  private renderer: THREE.WebGLRenderer;
 
  /**
   * An internal list of scenes that need to be renderered.
   */
  private scenes: Array<THREE.Scene>;
 
  /**
   * The last known root element width (usually window size)
   */
  private width = 0;
 
  /**
   * The last known root element height (usually window size)
   */
  private height = 0;
 
  /**
   *  The z-index of the main canvas.
   */
  private zIndex: number;
 
  /**
   * Whether to use absolute position on the root canvas or no.
   */
  private useAbsolutePositioning: boolean;
 
  constructor(config: SceneRendererConfig) {
    this.watcher = new DomWatcher();
 
    this.watcher.add({
      element: window,
      callback: this.onResize.bind(this),
      eventOptions: {passive: true},
      on: 'smartResize',
    });
 
    this.useAbsolutePositioning = !!config.useAbsolutePositioning;
 
    this.rootElement = config.rootElement || document.body;
    this.dpr = window.devicePixelRatio || 1;
    this.scenes = [];
 
    // Generate canvas.
    this.zIndex = 0;
    this.canvas = document.createElement('canvas');
    this.canvas.style.pointerEvents = 'none';
    if (this.useAbsolutePositioning) {
      this.canvas.style.position = 'absolute';
    } else {
      this.canvas.style.position = 'fixed';
    }
    this.canvas.style.left = '0px';
    this.canvas.style.top = '0px';
 
    this.setZIndex(this.zIndex);
 
    this.rootElement.append(this.canvas);
 
    // Initial renderer setup.
    this.renderer = new THREE.WebGLRenderer(
      config.rendererOptions || {
        canvas: this.canvas,
        antialias: true,
        alpha: true,
      }
    );
    // Set to transparency.
    this.renderer.setClearColor(0xffffff, 0);
    this.renderer.setPixelRatio(this.dpr);
 
    this.onResize();
  }
 
  /**
   * Gets the THREE object.
   */
  public getThree() {
    return THREE;
  }
 
  /**
   * Forces a resize event on the scene-renderer.
   */
  public resize() {
    this.onResize();
  }
 
  /**
   * Handles resizing events.
   */
  private onResize() {
    this.width = window.innerWidth;
    this.height = window.innerHeight;
 
    this.scenes.forEach((scene: THREE.Scene) => {
      const element = scene.userData.domElement;
 
      scene.userData.onBeforeResize &&
        scene.userData.onBeforeResize(
          this.getRenderer(),
          scene,
          scene.userData.camera
        );
 
      // Now for each, figure out the right resizing strategy.
      const camera = scene.userData.camera;
 
      const h = element.offsetHeight;
      const w = element.offsetWidth;
      const aspect = w / h;
 
      // Contain zoom algo.
      if (scene.userData.resizingAlgo === SceneResizingAlgo.resizeWithZoom) {
        const scalar = scene.userData.resizingScalar || 1.0;
        if (aspect <= 1) {
          camera.zoom = aspect * scalar;
        } else {
          camera.zoom = 1;
        }
        camera.aspect = aspect;
        camera.updateProjectionMatrix();
      }
 
      // Standard Aspect algo
      if (scene.userData.resizingAlgo === SceneResizingAlgo.standardAspect) {
        camera.aspect = aspect;
        camera.updateProjectionMatrix();
      }
 
      // Resize with FOV
      // Uses resizingScalar.
      if (scene.userData.resizingAlgo === SceneResizingAlgo.resizeWithFov) {
        const z = scene.userData.resizingScalar || 1.0;
        camera.fov = Math.atan(h / 2 / z) * 2 * THREE.Math.RAD2DEG;
        camera.aspect = aspect;
        camera.updateProjectionMatrix();
      }
 
      // "Contain"  Algo.
      // Uses resizingOptions
      if (scene.userData.resizingAlgo === SceneResizingAlgo.contain) {
        if (
          !scene.userData.resizingOptions ||
          !scene.userData.resizingOptions.scalarX ||
          !scene.userData.resizingOptions.scalarY
        ) {
          throw new Error(
            'Requested contain algo without the correct resizing options'
          );
        }
 
        // Fake contain.  Control the x,y scalar values to control
        // resize points.
        const scalarX = scene.userData.resizingOptions.scalarX;
        const scalarY = scene.userData.resizingOptions.scalarY;
 
        // Based on the provided scalars, implement a basic
        // contain algo adjusting FOV.  Now this box area will
        // be contained.
        const hFov = Math.atan(h / 2 / (w * scalarY)) * 2 * THREE.Math.RAD2DEG;
        const vFov = Math.atan(h / 2 / (h * scalarX)) * 2 * THREE.Math.RAD2DEG;
        const virtualFov = Math.max(vFov, hFov);
 
        // Use Fov
        if (scene.userData.resizingOptions.useFov) {
          camera.fov = virtualFov;
        } else {
          // Use zoom
          camera.zoom = camera.fov / virtualFov;
        }
 
        // Calculate the virtual aspect ratio.
        let virtualBox;
        const virtualBoxAspect = scalarY / scalarX;
 
        // Default Camera offsets in pixels.
        let xOffset = 0;
        let yOffset = 0;
 
        // Offset options
        let top = scene.userData.resizingOptions.top;
        let bottom = scene.userData.resizingOptions.bottom;
        let left = scene.userData.resizingOptions.left;
        let right = scene.userData.resizingOptions.right;
 
        // Vertical offset calculations
        if (is.defined(top) || is.defined(bottom)) {
          // Create a virtual box based on the provided aspects.
          virtualBox = {
            width: w,
            height: w * virtualBoxAspect,
          };
 
          const diffY = Math.max(0, (h - virtualBox.height) / 2);
 
          if (is.defined(top)) {
            // A top value ranging from 0-1 would actually
            // be an offset from the top of the scene to
            // the vertical center of the scene.  In order
            // to make this work more like a traditional css format
            // where 0 is the top and 1 is fully put the bounding
            // box a the bottom, we double the value.
            top *= 2;
            yOffset = diffY * (1 - top);
          }
 
          if (is.defined(bottom)) {
            bottom *= 2;
            yOffset = -diffY * (1 - bottom);
          }
        }
 
        // Horizontal offset calculations
        if (is.defined(left) || is.defined(right)) {
          // Create a virtual box based on the provided aspects.
          virtualBox = {
            width: h / virtualBoxAspect,
            height: h,
          };
 
          const diffX = Math.max(0, (w - virtualBox.width) / 2);
          if (is.defined(left)) {
            left *= 2;
            xOffset = diffX * (1 - left);
          }
          if (is.defined(right)) {
            right *= 2;
            xOffset = -diffX * (1 - right);
          }
        }
 
        // Apply camera offsets
        camera.setViewOffset(w, h, xOffset, yOffset, w, h);
 
        // Update camera aspect
        camera.aspect = aspect;
        camera.updateProjectionMatrix();
      }
 
      // "Cover"  Algo.
      // Uses resizingOptions
      if (scene.userData.resizingAlgo === SceneResizingAlgo.cover) {
        if (
          !scene.userData.resizingOptions ||
          !scene.userData.resizingOptions.scalarX ||
          !scene.userData.resizingOptions.scalarY
        ) {
          throw new Error(
            'Requested cover algo without the correct resizing options'
          );
        }
 
        const scalarX = scene.userData.resizingOptions.scalarX;
        const scalarY = scene.userData.resizingOptions.scalarY;
        let virtualBox;
        const virtualBoxAspect = scalarY / scalarX;
 
        // Based on the provided scalars, implement a basic
        // cover algo adjusting FOV.  Now this box area will
        // be cover with center / center.
        const hFov = Math.atan(h / 2 / (w * scalarY)) * 2 * THREE.Math.RAD2DEG;
        const vFov = Math.atan(h / 2 / (h * scalarX)) * 2 * THREE.Math.RAD2DEG;
        const virtualFov = Math.min(vFov, hFov);
 
        // Use Fov
        if (scene.userData.resizingOptions.useFov) {
          camera.fov = virtualFov;
        } else {
          // Use zoom
          camera.zoom = camera.fov / virtualFov;
        }
 
        // Offset options
        let xOffset = 0;
        let yOffset = 0;
        let top = scene.userData.resizingOptions.top;
        let bottom = scene.userData.resizingOptions.bottom;
        let left = scene.userData.resizingOptions.left;
        let right = scene.userData.resizingOptions.right;
        // Vertical offset calculations
        if (is.defined(top) || is.defined(bottom)) {
          // Create a virtual box based on the provided aspects.
          virtualBox = {
            width: h / virtualBoxAspect,
            height: h,
          };
 
          // Calculate the approximate virutal box cover box scale.
          const coverBox = mathf.calculateBackgroundCover(
            {width: w, height: h},
            virtualBox
          );
 
          const diffY = Math.max(0, (coverBox.height - h) / 2);
 
          if (is.defined(top)) {
            top *= 2;
            yOffset = -diffY * (1 - top);
          }
 
          if (is.defined(bottom)) {
            bottom *= 2;
            yOffset = diffY * (1 - bottom);
          }
        }
 
        // Horizontal offset calculations
        if (is.defined(left) || is.defined(right)) {
          // Create a virtual box based on the provided aspects.
          virtualBox = {
            width: w,
            height: w * virtualBoxAspect,
          };
 
          const coverBox = mathf.calculateBackgroundCover(
            {width: w, height: h},
            virtualBox
          );
 
          const diffX = Math.max(0, (coverBox.width - w) / 2);
          if (is.defined(left)) {
            left *= 2;
            xOffset = -diffX * (1 - left);
          }
 
          if (is.defined(right)) {
            right *= 2;
            xOffset = diffX * (1 - right);
          }
        }
 
        camera.setViewOffset(w, h, xOffset, yOffset, w, h);
        camera.aspect = aspect;
        camera.updateProjectionMatrix();
      }
 
      scene.userData.onResize &&
        scene.userData.onResize(
          this.getRenderer(),
          scene,
          scene.userData.camera
        );
    });
 
    this.renderer.setSize(this.width, this.height);
    this.render();
  }
 
  /**
   * Updates the z-index of the main renderer canvas.
   */
  public setZIndex(zIndex: number) {
    this.zIndex = zIndex;
    this.canvas.style.zIndex = this.zIndex + '';
  }
 
  /**
   * Returns the last known size of root canvas.  Usually this is usually the window size
   * since the main renderer canvas covers the whole page.
   */
  public getSize() {
    return {
      width: this.width,
      height: this.height,
      aspect: this.width / this.height,
    };
  }
 
  /**
   * Add a THREE.Scene to be rendered when the given domElement is visible
   * in the scene.
   */
  public addScene(sceneConfig: SceneConfig, forceResize = false) {
    if (!sceneConfig.domElement || !sceneConfig.scene || !sceneConfig.camera) {
      console.warn(
        'SceneRenderer could not register scene.  It is missing either the scene, camere or domElement'
      );
      return;
    }
 
    const scene = sceneConfig.scene;
 
    if (sceneConfig.debug) {
      this.setupDebugging(sceneConfig);
    }
 
    // Set the scene config data to the scene.
    scene.userData = sceneConfig;
 
    // Add ev to able to later check if the associated domElement is visible
    // in the viewport.
    scene.userData.ev = elementVisibility.inview(sceneConfig.domElement);
 
    this.scenes.push(scene);
 
    // Run init.
    scene.userData.onInit &&
      scene.userData.onInit(this.getRenderer(), scene, scene.userData.camera);
 
    if (forceResize) {
      window.setTimeout(() => {
        this.resize();
      });
    }
  }
 
  /**
   * Sets up debugging via GUIF.
   * @param scene
   */
  private setupDebugging(sceneConfig: SceneConfig) {
    const scene = sceneConfig.scene;
    const axesHelper = new THREE.AxesHelper(50);
    scene.add(axesHelper);
 
    new ThreeInspector(sceneConfig.domElement);
  }
 
  /**
   * Gets the internal webGLRenderer.
   */
  public getRenderer(): THREE.WebGLRenderer {
    return this.renderer;
  }
 
  /**
   * Renders out each scene.
   */
  public render(): void {
    // See note on: https://stackoverflow.com/questions/30608723/is-it-possible-to-enable-unbounded-number-of-renderers-in-three-js/30633132#30633132
    // Rather than using fixed, this keeps the canvas in sync.
    if (this.useAbsolutePositioning) {
      this.canvas.style.transform = `translateY(${window.scrollY}px)`;
    }
 
    this.renderer.setScissorTest(false);
    this.renderer.clear();
    this.renderer.setScissorTest(true);
 
    // Go through each scene and it's position and render it out in
    // pieces.
    this.scenes.forEach((scene: THREE.Scene) => {
      // If the scene is out of view, cull.
      if (!scene.userData.ev.state().inview) {
        return;
      }
 
      const element = scene.userData.domElement;
      // Get the element position relative to the page's viewport
      const rect = element.getBoundingClientRect();
      // set the viewport
      const width = rect.right - rect.left;
      const height = rect.bottom - rect.top;
      const left = rect.left;
      const bottom = this.renderer.domElement.clientHeight - rect.bottom;
 
      this.renderer.setViewport(left, bottom, width, height);
      this.renderer.setScissor(left, bottom, width, height);
 
      scene.userData.onBeforeRender &&
        scene.userData.onBeforeRender(
          this.getRenderer(),
          scene,
          scene.userData.camera
        );
 
      this.renderer.render(scene, scene.userData.camera);
 
      scene.userData.onAfterRender &&
        scene.userData.onAfterRender(
          this.getRenderer(),
          scene,
          scene.userData.camera
        );
    });
  }
 
  public dispose() {
    // Clean up scenes.
    this.scenes.forEach((scene: THREE.Scene) => {
      // Dispose element visiblity.
      scene.userData.ev.dispose();
 
      // Remove dom association.
      scene.userData.element = null;
 
      scene.userData.onDispose && scene.userData.onDispose();
    });
 
    // Remove canvas from rootElement.
    this.canvas.parentElement?.removeChild(this.canvas);
  }
}