Skip to content

Commit

Permalink
Add loading bar to demos
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Jan 21, 2025
1 parent 681effa commit c6af123
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions example/r3f/atmosphere.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
} from '@takram/three-atmosphere/r3f';
import { Dithering, LensFlare, } from '@takram/three-geospatial-effects/r3f';
import { toCreasedNormals } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
import { TilesLoadingBar } from './components/TilesLoadingBar.jsx';

// Plugin to generate creased normals for the tiles
class TileCreasedNormalsPlugin {
Expand Down Expand Up @@ -89,6 +90,7 @@ function App() {
{/* Add compass gizmo */}
<CompassGizmo overrideRenderLoop={ false } />

<TilesLoadingBar />
<GlobeTilesAtmosphere />
</TilesRenderer>

Expand Down
37 changes: 37 additions & 0 deletions example/r3f/components/TilesLoadingBar.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CanvasDOMOverlay, TilesRendererContext } from '3d-tiles-renderer/r3f';
import { useFrame } from '@react-three/fiber';
import { useContext, useRef } from 'react';

export function TilesLoadingBar( props ) {

const tiles = useContext( TilesRendererContext );
const domRef = useRef();

useFrame( () => {

const element = domRef.current;
if ( element && tiles ) {

element.style.width = `${ tiles.loadProgress * 100 }%`;
element.style.opacity = tiles.loadProgress === 1 ? 0 : 1;
element.style.transition = tiles.loadProgress === 1 ? 'opacity 0.5s linear' : '';

}

} );

return <CanvasDOMOverlay
ref={ domRef }
style={ {
position: 'absolute',
left: 0,
bottom: 0,
height: '2px',
background: 'white',
// transition: 'opacity 0.2s linear',
opacity: 0,
width: '100%',
} }
/>;

}
3 changes: 3 additions & 0 deletions example/r3f/globe.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Canvas, useFrame } from '@react-three/fiber';
import { Environment } from '@react-three/drei';
import { useControls } from 'leva';
import { MathUtils, Vector3 } from 'three';
import { TilesLoadingBar } from './components/TilesLoadingBar.jsx';

const dracoLoader = new DRACOLoader().setDecoderPath( 'https://www.gstatic.com/draco/v1/decoders/' );
const vec1 = new Vector3();
Expand Down Expand Up @@ -121,6 +122,8 @@ function App() {

{/* Add compass gizmo */}
<CompassGizmo />

<TilesLoadingBar />
</TilesRenderer>

{/* other r3f staging */}
Expand Down
10 changes: 5 additions & 5 deletions src/r3f/components/CanvasDOMOverlay.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMemo, useEffect, StrictMode } from 'react';
import { useMemo, useEffect, StrictMode, forwardRef } from 'react';
import { createRoot } from 'react-dom/client';
import { useThree } from '@react-three/fiber';

// Utility class for overlaying dom elements on top of the canvas
export function CanvasDOMOverlay( { children, ...rest } ) {
export const CanvasDOMOverlay = forwardRef( function CanvasDOMOverlay( { children, ...rest }, ref ) {

// create the dom element and react root
const [ gl ] = useThree( state => [ state.gl ] );
Expand All @@ -27,15 +27,15 @@ export function CanvasDOMOverlay( { children, ...rest } ) {

};

}, [ container ] );
}, [ container, gl.domElement.parentNode ] );

// render the children into the container
root.render(
<StrictMode>
<div { ...rest }>
<div { ...rest } ref={ ref }>
{ children }
</div>
</StrictMode>
);

}
} );

0 comments on commit c6af123

Please sign in to comment.