Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dither fade example #421

Merged
merged 18 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions example/fadingTiles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta charset="utf-8"/>

<title>Dither Fade Tiles</title>

<style>
* {
margin: 0;
padding: 0;
}

html {
overflow: hidden;
font-family: Arial, Helvetica, sans-serif;
user-select: none;
}

canvas {
image-rendering: pixelated;
outline: none;
}

#info {
position: absolute;
top: 0;
left: 0;
width: 100%;
text-align: center;
padding: 5px;
pointer-events: none;
line-height: 1.5em;
}

#info, #info a {
color: #234;
}

#info a {
pointer-events: all;
}
</style>
</head>
<body>
<div id="info">
Demonstration of tiles use a dither fade to change, smoothing out the transition.
</div>
<script src="./fadingTiles.js"></script>
</body>
</html>
143 changes: 143 additions & 0 deletions example/fadingTiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {
FadeTilesRenderer,
} from './src/FadeTilesRenderer.js';
import {
Scene,
DirectionalLight,
AmbientLight,
WebGLRenderer,
PerspectiveCamera,
Group,
} from 'three';
import { FlyOrbitControls } from './FlyOrbitControls.js';
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';

let camera, controls, scene, renderer;
let groundTiles, skyTiles, tilesParent;

const params = {

reinstantiateTiles,
useFade: true,
errorTarget: 12,
fadeDuration: 0.5,
renderScale: 1,
fadingGroundTiles: '0 tiles',

};

init();
render();

function init() {

scene = new Scene();

// primary camera view
renderer = new WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0xd8cec0 );

document.body.appendChild( renderer.domElement );
renderer.domElement.tabIndex = 1;

camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
camera.position.set( 20, 10, 20 );

// controls
controls = new FlyOrbitControls( camera, renderer.domElement );
controls.screenSpacePanning = false;
controls.minDistance = 1;
controls.maxDistance = 2000;
controls.maxPolarAngle = Math.PI / 2;
controls.baseSpeed = 0.1;
controls.fastSpeed = 0.2;

// lights
const dirLight = new DirectionalLight( 0xffffff );
dirLight.position.set( 1, 2, 3 );
scene.add( dirLight );

const ambLight = new AmbientLight( 0xffffff, 0.2 );
scene.add( ambLight );

tilesParent = new Group();
tilesParent.rotation.set( Math.PI / 2, 0, 0 );
scene.add( tilesParent );

reinstantiateTiles();

onWindowResize();
window.addEventListener( 'resize', onWindowResize, false );

const gui = new GUI();
gui.add( params, 'useFade' );
gui.add( params, 'errorTarget', 0, 1000 );
gui.add( params, 'fadeDuration', 0, 5 );
gui.add( params, 'renderScale', 0.1, 1.0, 0.05 ).onChange( v => renderer.setPixelRatio( v * window.devicePixelRatio ) );

const textController = gui.add( params, 'fadingGroundTiles' ).listen().disable();
textController.domElement.style.opacity = 1.0;

gui.add( params, 'reinstantiateTiles' );

gui.open();

}

function reinstantiateTiles() {

if ( groundTiles ) {

groundTiles.dispose();
skyTiles.dispose();

}

groundTiles = new FadeTilesRenderer( 'https://raw.githubusercontent.com/NASA-AMMOS/3DTilesSampleData/master/msl-dingo-gap/0528_0260184_to_s64o256_colorize/0528_0260184_to_s64o256_colorize/0528_0260184_to_s64o256_colorize_tileset.json' );
groundTiles.fetchOptions.mode = 'cors';
groundTiles.lruCache.minSize = 900;
groundTiles.lruCache.maxSize = 1300;
groundTiles.errorTarget = 12;

skyTiles = new FadeTilesRenderer( 'https://raw.githubusercontent.com/NASA-AMMOS/3DTilesSampleData/master/msl-dingo-gap/0528_0260184_to_s64o256_colorize/0528_0260184_to_s64o256_sky/0528_0260184_to_s64o256_sky_tileset.json' );
skyTiles.fetchOptions.mode = 'cors';
skyTiles.lruCache = groundTiles.lruCache;

tilesParent.add( groundTiles.group, skyTiles.group );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );

}

function render() {

requestAnimationFrame( render );

camera.updateMatrixWorld();

groundTiles.errorTarget = params.errorTarget;

groundTiles.setCamera( camera );
groundTiles.setResolutionFromRenderer( camera, renderer );
groundTiles.update();

skyTiles.setCamera( camera );
skyTiles.setResolutionFromRenderer( camera, renderer );
skyTiles.update();

groundTiles.fadeDuration = params.useFade ? params.fadeDuration * 1000 : 0;
skyTiles.fadeDuration = params.useFade ? params.fadeDuration * 1000 : 0;

renderer.render( scene, camera );

params.fadingGroundTiles = groundTiles._fadeGroup.children.length + ' tiles';

}
Loading
Loading