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 KTX2 Texture support for editor #27016

Merged
merged 8 commits into from
Oct 25, 2023
Merged
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
37 changes: 33 additions & 4 deletions editor/js/libs/ui.three.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as THREE from 'three';

import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
import { TGALoader } from 'three/addons/loaders/TGALoader.js';

Expand Down Expand Up @@ -93,6 +94,34 @@ class UITexture extends UISpan {

reader.readAsDataURL( file );

} else if ( extension === 'ktx2' ) {

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

const arrayBuffer = event.target.result;
const blobURL = URL.createObjectURL( new Blob( [ arrayBuffer ] ) );
const tempRenderer = new THREE.WebGLRenderer();
const ktx2Loader = new KTX2Loader();
ktx2Loader.setTranscoderPath( '../../examples/jsm/libs/basis/' );
ktx2Loader.detectSupport( tempRenderer );

ktx2Loader.load( blobURL, function ( texture ) {

texture.colorSpace = THREE.SRGBColorSpace;
texture.sourceFile = file.name;
texture.needsUpdate = true;
scope.setValue( texture );

if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
ktx2Loader.dispose();
tempRenderer.dispose();

} );

} );

reader.readAsArrayBuffer( file );

} else if ( file.type.match( 'image.*' ) ) {

reader.addEventListener( 'load', function ( event ) {
Expand Down Expand Up @@ -155,14 +184,14 @@ class UITexture extends UISpan {
canvas.title = texture.sourceFile;
const scale = canvas.width / image.width;

if ( image.data === undefined ) {
if ( texture.isDataTexture || texture.isCompressedTexture ) {

context.drawImage( image, 0, 0, image.width * scale, image.height * scale );
const canvas2 = renderToCanvas( texture );
context.drawImage( canvas2, 0, 0, image.width * scale, image.height * scale );

} else {

const canvas2 = renderToCanvas( texture );
context.drawImage( canvas2, 0, 0, image.width * scale, image.height * scale );
context.drawImage( image, 0, 0, image.width * scale, image.height * scale );

}

Expand Down