All files / src/components/VirtualWalkthrough SplatModel.tsx

0% Statements 0/19
0% Branches 0/23
0% Functions 0/3
0% Lines 0/19

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                                                                                                                                                                                                                         
import React, { memo, useEffect } from 'react';
 
import { Entity } from '@playcanvas/react';
import { GSplat } from '@playcanvas/react/components';
import { useApp, useSplat } from '@playcanvas/react/hooks';
 
import BlockingSpinner from './BlockingSpinner';
import SplatCrop from './SplatCrop';
import SplatFadeCrop from './SplatFadeCrop';
import SplatRevealRain from './SplatRevealRain';
import { SplatFormat, detectSplatFormat, splatLoaderFilename } from './splatFormat';
 
export interface SplatModelProps {
  splatSrc: string;
  /**
   * Splat format of `splatSrc`. Inferred from the URL when omitted, which covers bundled `.sog`,
   * unbundled sog (`meta.json`), streamed LOD sog (`lod-meta.json`) and `.ply` sources. Set this
   * explicitly when the URL hides the filename.
   */
  splatFormat?: SplatFormat;
  rotation?: [number, number, number];
  /**
   * Scale applied to the splat entity. Splat models come out of reconstruction in their own
   * arbitrary units, so this is what converts them world units.
   */
  scaleFactor?: number;
  cropAabbMin?: [number, number, number];
  cropAabbMax?: [number, number, number];
  cropEdgeScaleFactor?: number;
  cropFade?: boolean;
  cropFadeDistance?: number;
  revealRain?: boolean;
  /**
   * Maximum allowable budget for splat rendering operations. Only applies to streamed sog models.
   * A value of 0 means no limit. Left at the engine's default when omitted.
   */
  splatBudget?: number;
  /**
   * Caps the device pixel ratio the scene renders at, trading sharpness for fill rate on high-DPI
   * displays. Left at the engine's default when omitted.
   */
  maxPixelRatio?: number;
  onError?: (error: Error) => void;
}
 
const SplatModel = ({
  splatSrc,
  splatFormat,
  rotation,
  scaleFactor = 1,
  cropAabbMin,
  cropAabbMax,
  cropEdgeScaleFactor,
  cropFade = false,
  cropFadeDistance = 0.5,
  revealRain = false,
  splatBudget,
  maxPixelRatio,
  onError,
}: SplatModelProps) => {
  const app = useApp();
 
  // The loader selects its parser from this filename rather than from splatSrc, so it has to name the
  // format we are actually loading. See splatFormat.ts.
  const filename = splatLoaderFilename(splatFormat ?? detectSplatFormat(splatSrc));
  const { asset, loading, error } = useSplat(splatSrc, { file: { filename } });
 
  useEffect(() => {
    if (!app) {
      return;
    }
    if (splatBudget !== undefined) {
      app.scene.gsplat.splatBudget = splatBudget;
    }
    if (maxPixelRatio !== undefined) {
      app.graphicsDevice.maxPixelRatio = maxPixelRatio;
    }
  }, [app, splatBudget, maxPixelRatio]);
 
  useEffect(() => {
    if (error) {
      onError?.(new Error(error));
    }
  }, [error, onError]);
 
  if (loading) {
    return <BlockingSpinner />;
  }
 
  if (!asset) {
    return null;
  }
 
  return (
    <Entity name='splat' rotation={rotation} scale={[scaleFactor, scaleFactor, scaleFactor]}>
      <GSplat asset={asset} unified />
      {(cropAabbMin || cropAabbMax) &&
        (cropFade ? (
          <SplatFadeCrop aabbMin={cropAabbMin} aabbMax={cropAabbMax} fadeDistance={cropFadeDistance} />
        ) : (
          <SplatCrop aabbMin={cropAabbMin} aabbMax={cropAabbMax} edgeScaleFactor={cropEdgeScaleFactor} />
        ))}
      <SplatRevealRain enabled={revealRain} scaleFactor={scaleFactor} />
    </Entity>
  );
};
 
export default memo(SplatModel);