Skip to content

Commit

Permalink
Editor: Added cache to UITexture.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Feb 15, 2024
1 parent 08101a5 commit 74d7dc8
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions editor/js/libs/ui.three.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { TGALoader } from 'three/addons/loaders/TGALoader.js';
import { UISpan, UIDiv, UIRow, UIButton, UICheckbox, UIText, UINumber } from './ui.js';
import { MoveObjectCommand } from '../commands/MoveObjectCommand.js';

const cache = new Map();

class UITexture extends UISpan {

constructor( editor ) {
Expand Down Expand Up @@ -51,17 +53,28 @@ class UITexture extends UISpan {
const extension = file.name.split( '.' ).pop().toLowerCase();
const reader = new FileReader();

if ( extension === 'hdr' || extension === 'pic' ) {
const hash = `${file.lastModified}_${file.size}_${file.name}`;

if ( cache.has( hash ) ) {

const texture = cache.get( hash );

scope.setValue( texture );

if ( scope.onChangeCallback ) scope.onChangeCallback( texture );

} else if ( extension === 'hdr' || extension === 'pic' ) {

reader.addEventListener( 'load', function ( event ) {

// assuming RGBE/Radiance HDR iamge format
// assuming RGBE/Radiance HDR image format

const loader = new RGBELoader();
loader.load( event.target.result, function ( hdrTexture ) {

hdrTexture.sourceFile = file.name;
hdrTexture.isHDRTexture = true;

cache.set( hash, hdrTexture );

scope.setValue( hdrTexture );

Expand All @@ -83,6 +96,8 @@ class UITexture extends UISpan {
texture.colorSpace = THREE.SRGBColorSpace;
texture.sourceFile = file.name;

cache.set( hash, texture );

scope.setValue( texture );

if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
Expand All @@ -109,6 +124,9 @@ class UITexture extends UISpan {
texture.colorSpace = THREE.SRGBColorSpace;
texture.sourceFile = file.name;
texture.needsUpdate = true;

cache.set( hash, texture );

scope.setValue( texture );

if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
Expand All @@ -131,6 +149,8 @@ class UITexture extends UISpan {
texture.sourceFile = file.name;
texture.needsUpdate = true;

cache.set( hash, texture );

scope.setValue( texture );

if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
Expand Down

0 comments on commit 74d7dc8

Please sign in to comment.