Skip to content

Commit 74d7dc8

Browse files
committed
Editor: Added cache to UITexture.
1 parent 08101a5 commit 74d7dc8

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

editor/js/libs/ui.three.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { TGALoader } from 'three/addons/loaders/TGALoader.js';
77
import { UISpan, UIDiv, UIRow, UIButton, UICheckbox, UIText, UINumber } from './ui.js';
88
import { MoveObjectCommand } from '../commands/MoveObjectCommand.js';
99

10+
const cache = new Map();
11+
1012
class UITexture extends UISpan {
1113

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

54-
if ( extension === 'hdr' || extension === 'pic' ) {
56+
const hash = `${file.lastModified}_${file.size}_${file.name}`;
57+
58+
if ( cache.has( hash ) ) {
59+
60+
const texture = cache.get( hash );
61+
62+
scope.setValue( texture );
63+
64+
if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
65+
66+
} else if ( extension === 'hdr' || extension === 'pic' ) {
5567

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

58-
// assuming RGBE/Radiance HDR iamge format
70+
// assuming RGBE/Radiance HDR image format
5971

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

6375
hdrTexture.sourceFile = file.name;
64-
hdrTexture.isHDRTexture = true;
76+
77+
cache.set( hash, hdrTexture );
6578

6679
scope.setValue( hdrTexture );
6780

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

99+
cache.set( hash, texture );
100+
86101
scope.setValue( texture );
87102

88103
if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
@@ -109,6 +124,9 @@ class UITexture extends UISpan {
109124
texture.colorSpace = THREE.SRGBColorSpace;
110125
texture.sourceFile = file.name;
111126
texture.needsUpdate = true;
127+
128+
cache.set( hash, texture );
129+
112130
scope.setValue( texture );
113131

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

152+
cache.set( hash, texture );
153+
134154
scope.setValue( texture );
135155

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

0 commit comments

Comments
 (0)