|
29 | 29 |
|
30 | 30 | <script> |
31 | 31 | var file_loader, texture_loader, rgbe_loader, basis_loader, exr_loader, hdrJPGLoader; |
32 | | - var ktx_loader, ktx2_loader, lottie_loader, lottie, tga_loader, dds_loader; |
| 32 | + var d_compress, ktx_loader, ktx2_loader, lottie_loader, lottie, tga_loader, dds_loader; |
33 | 33 | var THREE, fflate, renderer, css_renderer, css2d_renderer, css2d_object; |
34 | 34 | var manager, scene, camera, mesh, render_requested, count = 0; |
35 | 35 | var controls, gizmo, mouse_down, mouse_wheel, events_initialized; |
|
49 | 49 | import { KTXLoader } from "three/addons/loaders/KTXLoader.min.js"; |
50 | 50 | import { KTX2Loader } from "three/addons/loaders/KTX2Loader.min.js"; |
51 | 51 | import { BasisTextureLoader } from "../static/jsm/loaders/BasisTextureLoader.js"; |
| 52 | + import { decompress } from "three/addons/utils/WebGLTextureUtils.min.js"; |
52 | 53 |
|
53 | 54 | THREE = three_js; |
54 | 55 | fflate = fflate_js; |
| 56 | + d_compress = decompress; |
55 | 57 |
|
56 | 58 | manager = new THREE.LoadingManager(); |
57 | 59 |
|
|
1260 | 1262 | } |
1261 | 1263 | else if (fn_uc.endsWith('.KTX')) { |
1262 | 1264 | await new Promise( resolve => { |
1263 | | - ktx_loader.load( URL.createObjectURL( fi.files[ i ] ), function( texture ) { |
| 1265 | + ktx_loader.load( URL.createObjectURL( fi.files[ i ] ), async function( texture ) { |
1264 | 1266 | URL.revokeObjectURL( fi.files[ i ] ); |
1265 | 1267 |
|
1266 | | - resolve( texture_files.push( { flip: true, tex: texture } ), texture.dispose() ); |
| 1268 | + // Convert to PNG so it shows on most platforms |
| 1269 | + // Unsupported formats might log a warning and then throw error |
| 1270 | + |
| 1271 | + let canvas_tex = await d_compress( texture ); |
| 1272 | + |
| 1273 | + texture.dispose(); |
| 1274 | + |
| 1275 | + texture_loader.load( canvas_tex.image.toDataURL( 'image/png', 1 ), function( png_tex ) { |
| 1276 | + resolve( texture_files.push( { flip: true, tex: png_tex } ), canvas_tex.dispose(), png_tex.dispose() ); |
| 1277 | + }); |
1267 | 1278 | }, function ( xhr ) { // onProgress |
1268 | 1279 | }, function ( error ) { // onError |
1269 | 1280 | URL.revokeObjectURL( fi.files[ i ] ); |
|
1273 | 1284 | } |
1274 | 1285 | else if (fn_uc.endsWith('.KTX2')) { |
1275 | 1286 | await new Promise( resolve => { |
1276 | | - ktx2_loader.load( URL.createObjectURL( fi.files[ i ] ), function( texture ) { |
| 1287 | + ktx2_loader.load( URL.createObjectURL( fi.files[ i ] ), async function( texture ) { |
1277 | 1288 | URL.revokeObjectURL( fi.files[ i ] ); |
1278 | 1289 |
|
1279 | | - resolve( texture_files.push( { flip: true, tex: texture } ), texture.dispose() ); |
| 1290 | + // Convert to PNG so it shows on most platforms |
| 1291 | + // Unsupported formats might log a warning and then throw error |
| 1292 | + |
| 1293 | + if (texture.colorSpace === 'display-p3') texture.colorSpace = THREE.SRGBColorSpace; |
| 1294 | + |
| 1295 | + let canvas_tex = await d_compress( texture ); |
| 1296 | + |
| 1297 | + texture.dispose(); |
| 1298 | + |
| 1299 | + texture_loader.load( canvas_tex.image.toDataURL( 'image/png', 1 ), function( png_tex ) { |
| 1300 | + resolve( texture_files.push( { flip: true, tex: png_tex } ), canvas_tex.dispose(), png_tex.dispose() ); |
| 1301 | + }); |
1280 | 1302 | }, function ( xhr ) { // onProgress |
1281 | 1303 | }, function ( error ) { // onError |
1282 | 1304 | URL.revokeObjectURL( fi.files[ i ] ); |
|
1908 | 1930 | // KTX2 or KTX |
1909 | 1931 | case 'q0': |
1910 | 1932 | await new Promise( resolve => { |
1911 | | - ktx2_loader.load( 'data:image/ktx2;base64,' + str, function( texture ) { |
1912 | | - resolve( texture_files.push( { flip: true, tex: texture } ) ); |
| 1933 | + ktx2_loader.load( 'data:image/ktx2;base64,' + str, async function( texture ) { |
| 1934 | + // Convert to PNG so it shows on most platforms |
| 1935 | + // Unsupported formats might log a warning and then throw error |
| 1936 | + |
| 1937 | + if (texture.colorSpace === 'display-p3') texture.colorSpace = THREE.SRGBColorSpace; |
| 1938 | + |
| 1939 | + let canvas_tex = await d_compress( texture ); |
| 1940 | + |
| 1941 | + texture.dispose(); |
| 1942 | + |
| 1943 | + texture_loader.load( canvas_tex.image.toDataURL( 'image/png', 1 ), function( png_tex ) { |
| 1944 | + resolve( texture_files.push( { flip: true, tex: png_tex } ), canvas_tex.dispose(), png_tex.dispose() ); |
| 1945 | + }); |
1913 | 1946 | }, function ( xhr ) { |
1914 | 1947 | }, function ( error ) { |
1915 | 1948 | console.log( 'Not a base64 KTX2 texture, trying to load base64 KTX instead...' ); |
1916 | 1949 |
|
1917 | | - ktx_loader.load( 'data:image/ktx;base64,' + str, function( texture ) { |
1918 | | - resolve( texture_files.push( { flip: true, tex: texture } ) ); |
| 1950 | + ktx_loader.load( 'data:image/ktx;base64,' + str, async function( texture ) { |
| 1951 | + // Convert to PNG so it shows on most platforms |
| 1952 | + // Unsupported formats might log a warning and then throw error |
| 1953 | + |
| 1954 | + let canvas_tex = await d_compress( texture ); |
| 1955 | + |
| 1956 | + texture.dispose(); |
| 1957 | + |
| 1958 | + texture_loader.load( canvas_tex.image.toDataURL( 'image/png', 1 ), function( png_tex ) { |
| 1959 | + resolve( texture_files.push( { flip: true, tex: png_tex } ), canvas_tex.dispose(), png_tex.dispose() ); |
| 1960 | + }); |
1919 | 1961 | }, function ( xhr ) { |
1920 | 1962 | }, function ( error ) { |
1921 | 1963 | handle_error( error ); |
|
2142 | 2184 | }); |
2143 | 2185 | } else if (url_uc.endsWith('.KTX')) { |
2144 | 2186 | await new Promise( resolve => { |
2145 | | - ktx_loader.load( url, function( texture ) { |
2146 | | - resolve( texture_files.push( { flip: true, tex: texture } ) ); |
| 2187 | + ktx_loader.load( url, async function( texture ) { |
| 2188 | + // Convert to PNG so it shows on most platforms |
| 2189 | + // Unsupported formats might log a warning and then throw error |
| 2190 | + |
| 2191 | + let canvas_tex = await d_compress( texture ); |
| 2192 | + |
| 2193 | + texture.dispose(); |
| 2194 | + |
| 2195 | + texture_loader.load( canvas_tex.image.toDataURL( 'image/png', 1 ), function( png_tex ) { |
| 2196 | + resolve( texture_files.push( { flip: true, tex: png_tex } ), canvas_tex.dispose(), png_tex.dispose() ); |
| 2197 | + }); |
2147 | 2198 | }, function ( xhr ) { |
2148 | 2199 | }, function ( error ) { |
2149 | 2200 | handle_error( error ); |
2150 | 2201 | }); |
2151 | 2202 | }); |
2152 | 2203 | } else if (url_uc.endsWith('.KTX2')) { |
2153 | 2204 | await new Promise( resolve => { |
2154 | | - ktx2_loader.load( url, function( texture ) { |
2155 | | - resolve( texture_files.push( { flip: true, tex: texture } ) ); |
| 2205 | + ktx2_loader.load( url, async function( texture ) { |
| 2206 | + // Convert to PNG so it shows on most platforms |
| 2207 | + // Unsupported formats might log a warning and then throw error |
| 2208 | + |
| 2209 | + if (texture.colorSpace === 'display-p3') texture.colorSpace = THREE.SRGBColorSpace; |
| 2210 | + |
| 2211 | + let canvas_tex = await d_compress( texture ); |
| 2212 | + |
| 2213 | + texture.dispose(); |
| 2214 | + |
| 2215 | + texture_loader.load( canvas_tex.image.toDataURL( 'image/png', 1 ), function( png_tex ) { |
| 2216 | + resolve( texture_files.push( { flip: true, tex: png_tex } ), canvas_tex.dispose(), png_tex.dispose() ); |
| 2217 | + }); |
2156 | 2218 | }, function ( xhr ) { |
2157 | 2219 | }, function ( error ) { |
2158 | 2220 | handle_error( error ); |
|
2470 | 2532 | }); |
2471 | 2533 | } else if (url_uc.endsWith('.KTX')) { |
2472 | 2534 | await new Promise( resolve => { |
2473 | | - ktx_loader.load( selected_url_file, function( texture ) { |
2474 | | - resolve( texture_files.push( { flip: true, tex: texture } ) ); |
| 2535 | + ktx_loader.load( selected_url_file, async function( texture ) { |
| 2536 | + // Convert to PNG so it shows on most platforms |
| 2537 | + // Unsupported formats might log a warning and then throw error |
| 2538 | + |
| 2539 | + let canvas_tex = await d_compress( texture ); |
| 2540 | + |
| 2541 | + texture.dispose(); |
| 2542 | + |
| 2543 | + texture_loader.load( canvas_tex.image.toDataURL( 'image/png', 1 ), function( png_tex ) { |
| 2544 | + resolve( texture_files.push( { flip: true, tex: png_tex } ), canvas_tex.dispose(), png_tex.dispose() ); |
| 2545 | + }); |
2475 | 2546 | }, function ( xhr ) { |
2476 | 2547 | }, function ( error ) { |
2477 | 2548 | handle_error( error ); |
2478 | 2549 | }); |
2479 | 2550 | }); |
2480 | 2551 | } else if (url_uc.endsWith('.KTX2')) { |
2481 | 2552 | await new Promise( resolve => { |
2482 | | - ktx2_loader.load( selected_url_file, function( texture ) { |
2483 | | - resolve( texture_files.push( { flip: true, tex: texture } ) ); |
| 2553 | + ktx2_loader.load( selected_url_file, async function( texture ) { |
| 2554 | + // Convert to PNG so it shows on most platforms |
| 2555 | + // Unsupported formats might log a warning and then throw error |
| 2556 | + |
| 2557 | + if (texture.colorSpace === 'display-p3') texture.colorSpace = THREE.SRGBColorSpace; |
| 2558 | + |
| 2559 | + let canvas_tex = await d_compress( texture ); |
| 2560 | + |
| 2561 | + texture.dispose(); |
| 2562 | + |
| 2563 | + texture_loader.load( canvas_tex.image.toDataURL( 'image/png', 1 ), function( png_tex ) { |
| 2564 | + resolve( texture_files.push( { flip: true, tex: png_tex } ), canvas_tex.dispose(), png_tex.dispose() ); |
| 2565 | + }); |
2484 | 2566 | }, function ( xhr ) { |
2485 | 2567 | }, function ( error ) { |
2486 | 2568 | handle_error( error ); |
|
0 commit comments