updates to threejsdrawer, bugxfixes/improvements

This commit is contained in:
Tom 2023-03-13 17:43:35 -04:00
parent ade59513df
commit 6d1895364f
2 changed files with 46 additions and 25 deletions

View File

@ -65,7 +65,7 @@ export class ThreeJSDrawer extends OpenSeadragon.DrawerBase{
// this._stats && this._stats.end(); // this._stats && this._stats.end();
} }
render(){ render(){
// this._stats && this._stats.begin(); this._stats && this._stats.begin();
let numItems = this.viewer.world.getItemCount(); let numItems = this.viewer.world.getItemCount();
this._outputContext.clearRect(0, 0, this._outputCanvas.width, this._outputCanvas.height); this._outputContext.clearRect(0, 0, this._outputCanvas.width, this._outputCanvas.height);
@ -77,6 +77,7 @@ export class ThreeJSDrawer extends OpenSeadragon.DrawerBase{
if(item.wrapHorizontal || item.wrapVertical){ if(item.wrapHorizontal || item.wrapVertical){
createWrappingGrid(scene, item); createWrappingGrid(scene, item);
} else { } else {
cleanupObject(scene.userData.wrappedCopies);
scene.userData.wrappedCopies.clear(); scene.userData.wrappedCopies.clear();
} }
this._renderer.render(scene, this._camera); //renders to this._renderingCanvas this._renderer.render(scene, this._camera); //renders to this._renderingCanvas
@ -101,8 +102,8 @@ export class ThreeJSDrawer extends OpenSeadragon.DrawerBase{
this.renderFrame(); this.renderFrame();
} }
// this._stats && this._stats.end(); this._stats && this._stats.end();
// console.log(this._renderer.info.memory, this._renderer.info.render.triangles); console.log(this._renderer.info.memory, this._renderer.info.render.triangles);
} }
renderContinuously(continuously){ renderContinuously(continuously){
if(continuously){ if(continuously){
@ -315,13 +316,14 @@ export class ThreeJSDrawer extends OpenSeadragon.DrawerBase{
} }
_tileUnloadedHandler(event){ _tileUnloadedHandler(event){
console.log('Tile unloaded',event);
let tile = event.tile; let tile = event.tile;
if(!this._tileMap[tile.cacheKey]){ if(!this._tileMap[tile.cacheKey]){
//already cleaned up //already cleaned up
return; return;
} }
cleanupObject(this._tileMap[tile.cacheKey], true);
this._updateTiledImageRendering(event.tiledImage, tile); this._updateTiledImageRendering(event.tiledImage, tile);
cleanupObject(this._tileMap[tile.cacheKey]);
delete this._tileMap[tile.cacheKey]; delete this._tileMap[tile.cacheKey];
} }
@ -755,44 +757,60 @@ function addMaterialToMesh(mesh, material, opacity){
} }
function createWrappingGrid(scene, tiledImage){ function createWrappingGrid(scene, tiledImage){
let container = scene.userData.wrappedCopies;
container.clear(); // TO DO: This is a problematic approach and needs to be debugged/reworked.
// Tiles of wrapped images are the wrong resolution because higher-res
// images only get loaded into the main panel, and cloning the entire
// image every frame is too expensive
//calculate how to tile the space //calculate how to tile the space
let tiledImageBounds = tiledImage.getBounds(); let tiledImageBounds = tiledImage.getBoundsNoRotate();
let imgBounds = {x: 0, y: 0, width: tiledImageBounds.width, height: tiledImageBounds.height }; let imgBounds = {x: 0, y: 0, width: tiledImageBounds.width, height: tiledImageBounds.height };
let drawArea = tiledImage.viewer.viewport.getBounds(true); let drawArea = tiledImage.viewer.viewport.getBounds(true);
let center = drawArea.getCenter(); let center = drawArea.getCenter();
let halfDiag = Math.sqrt(drawArea.width * drawArea.width + drawArea.height * drawArea.height); let halfDiag = Math.sqrt(drawArea.width * drawArea.width + drawArea.height * drawArea.height);
let extraBuffer = imgBounds.width + imgBounds.height; let left = center.x - halfDiag;
let left = center.x - halfDiag - extraBuffer; let right = center.x + halfDiag;
let right = center.x + halfDiag + extraBuffer; let top = center.y - halfDiag;
let top = center.y - halfDiag - extraBuffer; let bottom = center.y + halfDiag;
let bottom = center.y + halfDiag + extraBuffer;
let xMin = tiledImage.wrapHorizontal ? Math.floor(left / imgBounds.width) : imgBounds.x; let xMin = tiledImage.wrapHorizontal ? Math.floor(left / imgBounds.width) : imgBounds.x;
let yMin = tiledImage.wrapVertical ? Math.floor(top / imgBounds.height) : imgBounds.y; let yMin = tiledImage.wrapVertical ? Math.floor(top / imgBounds.height) : imgBounds.y;
let xMax = tiledImage.wrapHorizontal ? Math.floor(right / imgBounds.width) : imgBounds.x; let xMax = tiledImage.wrapHorizontal ? Math.floor(right / imgBounds.width) : imgBounds.x;
let yMax = tiledImage.wrapVertical ? Math.floor(bottom / imgBounds.height) : imgBounds.y; let yMax = tiledImage.wrapVertical ? Math.floor(bottom / imgBounds.height) : imgBounds.y;
for(let x = xMin; x <= xMax; x += 1){ let container = scene.userData.wrappedCopies;
for(let y = yMin; y <= yMax; y += 1){ if( container.userData.xMin !== xMin ||
if(x == 0 && y == 0) { container.userData.xMax !== xMax ||
continue; container.userData.yMin !== yMin ||
container.userData.yMax !== yMax
){
// container.clear();
cleanupObject(container);
container.clear();
for(let x = xMin; x <= xMax; x += 1){
for(let y = yMin; y <= yMax; y += 1){
if(x == 0 && y == 0) {
continue;
}
let clone = scene.userData.tileContainer.clone();
clone.position.x += x * imgBounds.width;
clone.position.y += y * imgBounds.height;
container.add(clone);
} }
let clone = scene.userData.tileContainer.clone();
clone.position.x += x;
clone.position.y += y;
container.add(clone);
} }
} }
} }
function cleanupObject(object){ function cleanupObject(object, cleanupTextures){
if(object.children && object.children.forEach){ if(object.children && object.children.forEach){
object.children.forEach(cleanupObject); object.children.forEach(cleanupObject, cleanupTextures);
} }
if(object.dispose){ if(object.dispose){
object.dispose(); object.dispose();
@ -800,6 +818,9 @@ function cleanupObject(object){
if(object.geometry){ if(object.geometry){
object.geometry.dispose(); object.geometry.dispose();
} }
if(object.map && cleanupTextures){
object.map.dispose();
}
} }
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136 // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136

View File

@ -20,9 +20,9 @@ const labels = {
} }
var stats = null; var stats = null;
// var stats = new Stats(); var stats = new Stats();
// stats.showPanel( 1 ); // 0: fps, 1: ms, 2: mb, 3+: custom stats.showPanel( 1 ); // 0: fps, 1: ms, 2: mb, 3+: custom
// document.body.appendChild( stats.dom ); document.body.appendChild( stats.dom );
//Double viewer setup for comparison - CanvasDrawer and ThreeJSDrawer //Double viewer setup for comparison - CanvasDrawer and ThreeJSDrawer