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 THREE.CompressedArrayTexture #24745

Merged
merged 21 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bc9be5f
wip
RenaudRohlinger Oct 4, 2022
8d23278
Merge branch 'mrdoob:dev' into texture2darray_compressed
RenaudRohlinger Oct 4, 2022
c5c3157
texture array is working with hardcoded values
RenaudRohlinger Oct 5, 2022
32c1a67
KTX2Loader: Decode additional layers from array textures.
donmccurdy Oct 5, 2022
49a37a9
added compressedarraytexture
RenaudRohlinger Oct 6, 2022
c129db1
Merge remote-tracking branch 'upstream/dev' into texture2darray_compr…
RenaudRohlinger Oct 6, 2022
fbe1b6b
fix merge
RenaudRohlinger Oct 6, 2022
5e8f9ce
fixed level issues
RenaudRohlinger Oct 6, 2022
4eab10a
fix copyTextureToTexture3D, updated compressedarraytexture arguments,…
RenaudRohlinger Oct 7, 2022
739e3d0
remove build
RenaudRohlinger Oct 7, 2022
61a141d
fix deepscan
RenaudRohlinger Oct 7, 2022
c6bcf87
Update WebGLTextures.js
Mugen87 Oct 10, 2022
bc639f2
Update webgl2_texture2darray_compressed.html
Mugen87 Oct 15, 2022
e860e3e
Update webgl2_texture2darray_compressed.html
Mugen87 Oct 15, 2022
1eddd13
replaced with simple example
RenaudRohlinger Oct 17, 2022
13186da
remove build
RenaudRohlinger Oct 17, 2022
6f35ef2
Update webgl2_texture2darray_compressed.html
Mugen87 Oct 17, 2022
949dfb5
Update webgl2_texture2darray_compressed.html
Mugen87 Oct 17, 2022
5e9ea94
make the example framerate independent
RenaudRohlinger Oct 17, 2022
ce9a92f
fix init texture2darray for compressed texture in webglrenderer
RenaudRohlinger Oct 18, 2022
4ad7887
Update webgl2_texture2darray_compressed.html
Mugen87 Oct 18, 2022
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
75 changes: 75 additions & 0 deletions docs/api/en/textures/CompressedArrayTexture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="../../../" />
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>
[page:CompressedTexture] &rarr;

<h1>[name]</h1>

<p class="desc">
Creates an texture 2D array based on data in compressed form, for example from a [link:https://en.wikipedia.org/wiki/DirectDraw_Surface DDS] file.<br /><br />


For use with the [page:CompressedTextureLoader CompressedTextureLoader].
</p>


<h2>Constructor</h2>


<h3>[name]( [param:Array mipmaps], [param:Number width], [param:Number height], [param:Constant format], [param:Constant type] )</h3>
<p>
[page:Array mipmaps] -- The mipmaps array should contain objects with data, width and height. The mipmaps should be of the correct format and type.<br />

[page:Number width] -- The width of the biggest mipmap.<br />

[page:Number height] -- The height of the biggest mipmap.<br />

[page:Number depth] -- The number of layers of the 2D array texture.<br />

[page:Constant format] -- The format used in the mipmaps.
See [page:Textures ST3C Compressed Texture Formats],
[page:Textures PVRTC Compressed Texture Formats] and
[page:Textures ETC Compressed Texture Format] for other choices.<br />

[page:Constant type] -- Default is [page:Textures THREE.UnsignedByteType].
See [page:Textures type constants] for other choices.<br />

</p>


<h2>Properties</h2>

See the base [page:CompressedTexture CompressedTexture] class for common properties.

<h3>[property:number wrapR]</h3>
<p>
This defines how the texture is wrapped in the depth direction.<br />
The default is [page:Textures THREE.ClampToEdgeWrapping], where the edge is clamped to the outer edge texels.
The other two choices are [page:Textures THREE.RepeatWrapping] and [page:Textures THREE.MirroredRepeatWrapping].
See the [page:Textures texture constants] page for details.
</p>

<h3>[property:Boolean isCompressedArrayTexture]</h3>
<p>
Read-only flag to check if a given object is of type [name].
</p>

<h2>Methods</h2>

<p>
See the base [page:CompressedTexture CompressedTexture] class for common methods.
</p>

<h2>Source</h2>

<p>
[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
</p>
</body>
</html>
75 changes: 75 additions & 0 deletions docs/api/zh/textures/CompressedArrayTexture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="../../../" />
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>
[page:CompressedTexture] &rarr;

<h1>[name]</h1>

<p class="desc">
Creates an texture 2D array based on data in compressed form, for example from a [link:https://en.wikipedia.org/wiki/DirectDraw_Surface DDS] file.<br /><br />


For use with the [page:CompressedTextureLoader CompressedTextureLoader].
</p>


<h2>Constructor</h2>


<h3>[name]( [param:Array mipmaps], [param:Number width], [param:Number height], [param:Constant format], [param:Constant type] )</h3>
<p>
[page:Array mipmaps] -- The mipmaps array should contain objects with data, width and height. The mipmaps should be of the correct format and type.<br />

[page:Number width] -- The width of the biggest mipmap.<br />

[page:Number height] -- The height of the biggest mipmap.<br />

[page:Number depth] -- The number of layers of the 2D array texture.<br />

[page:Constant format] -- The format used in the mipmaps.
See [page:Textures ST3C Compressed Texture Formats],
[page:Textures PVRTC Compressed Texture Formats] and
[page:Textures ETC Compressed Texture Format] for other choices.<br />

[page:Constant type] -- Default is [page:Textures THREE.UnsignedByteType].
See [page:Textures type constants] for other choices.<br />

</p>


<h2>Properties</h2>

See the base [page:CompressedTexture CompressedTexture] class for common properties.

<h3>[property:number wrapR]</h3>
<p>
This defines how the texture is wrapped in the depth direction.<br />
The default is [page:Textures THREE.ClampToEdgeWrapping], where the edge is clamped to the outer edge texels.
The other two choices are [page:Textures THREE.RepeatWrapping] and [page:Textures THREE.MirroredRepeatWrapping].
See the [page:Textures texture constants] page for details.
</p>

<h3>[property:Boolean isCompressedArrayTexture]</h3>
<p>
Read-only flag to check if a given object is of type [name].
</p>

<h2>Methods</h2>

<p>
See the base [page:CompressedTexture CompressedTexture] class for common methods.
</p>

<h2>Source</h2>

<p>
[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
</p>
</body>
</html>
2 changes: 2 additions & 0 deletions docs/list.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@
"Textures": {
"CanvasTexture": "api/en/textures/CanvasTexture",
"CompressedTexture": "api/en/textures/CompressedTexture",
"CompressedArrayTexture": "api/en/textures/CompressedArrayTexture",
"CubeTexture": "api/en/textures/CubeTexture",
"Data3DTexture": "api/en/textures/Data3DTexture",
"DataArrayTexture": "api/en/textures/DataArrayTexture",
Expand Down Expand Up @@ -814,6 +815,7 @@
"纹理贴图": {
"CanvasTexture": "api/zh/textures/CanvasTexture",
"CompressedTexture": "api/zh/textures/CompressedTexture",
"CompressedArrayTexture": "api/zh/textures/CompressedArrayTexture",
"CubeTexture": "api/zh/textures/CubeTexture",
"DataArrayTexture": "api/zh/textures/DataArrayTexture",
"Data3DTexture": "api/zh/textures/Data3DTexture",
Expand Down
1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@
"webgl2_multiple_rendertargets",
"webgl2_multisampled_renderbuffers",
"webgl2_rendertarget_texture2darray",
"webgl2_texture2darray_compressed",
"webgl2_ubo",
"webgl2_volume_cloud",
"webgl2_volume_instancing",
Expand Down
95 changes: 67 additions & 28 deletions examples/jsm/loaders/KTX2Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import {
CompressedTexture,
CompressedArrayTexture,
Data3DTexture,
DataTexture,
FileLoader,
Expand All @@ -35,7 +36,7 @@ import {
RGBAFormat,
RGFormat,
sRGBEncoding,
UnsignedByteType
UnsignedByteType,
} from 'three';
import { WorkerPool } from '../utils/WorkerPool.js';
import {
Expand Down Expand Up @@ -236,16 +237,21 @@ class KTX2Loader extends Loader {

}

_createTextureFrom( transcodeResult ) {
_createTextureFrom( transcodeResult, container ) {

const { mipmaps, width, height, format, type, error, dfdTransferFn, dfdFlags } = transcodeResult;

if ( type === 'error' ) return Promise.reject( error );

const texture = new CompressedTexture( mipmaps, width, height, format, UnsignedByteType );
const texture = container.layerCount > 1
? new CompressedArrayTexture( mipmaps, width, height, container.layerCount, format, UnsignedByteType )
: new CompressedTexture( mipmaps, width, height, format, UnsignedByteType );


texture.minFilter = mipmaps.length === 1 ? LinearFilter : LinearMipmapLinearFilter;
texture.magFilter = LinearFilter;
texture.generateMipmaps = false;

texture.needsUpdate = true;
texture.encoding = dfdTransferFn === KHR_DF_TRANSFER_SRGB ? sRGBEncoding : LinearEncoding;
texture.premultiplyAlpha = !! ( dfdFlags & KHR_DF_FLAG_ALPHA_PREMULTIPLIED );
Expand All @@ -257,7 +263,7 @@ class KTX2Loader extends Loader {
/**
* @param {ArrayBuffer} buffer
* @param {object?} config
* @return {Promise<CompressedTexture|DataTexture|Data3DTexture>}
* @return {Promise<CompressedTexture|CompressedArrayTexture|DataTexture|Data3DTexture>}
*/
_createTexture( buffer, config = {} ) {

Expand All @@ -270,13 +276,12 @@ class KTX2Loader extends Loader {
}

//

const taskConfig = config;
const texturePending = this.init().then( () => {

return this.workerPool.postMessage( { type: 'transcode', buffer, taskConfig: taskConfig }, [ buffer ] );

} ).then( ( e ) => this._createTextureFrom( e.data ) );
} ).then( ( e ) => this._createTextureFrom( e.data, container ) );

// Cache the task result.
_taskCache.set( buffer, { promise: texturePending } );
Expand Down Expand Up @@ -437,6 +442,7 @@ KTX2Loader.BasisWorker = function () {
const basisFormat = ktx2File.isUASTC() ? BasisFormat.UASTC_4x4 : BasisFormat.ETC1S;
const width = ktx2File.getWidth();
const height = ktx2File.getHeight();
const layers = ktx2File.getLayers() || 1;
const levels = ktx2File.getLevels();
const hasAlpha = ktx2File.getHasAlpha();
const dfdTransferFn = ktx2File.getDFDTransferFunc();
Expand All @@ -462,30 +468,39 @@ KTX2Loader.BasisWorker = function () {

for ( let mip = 0; mip < levels; mip ++ ) {

const levelInfo = ktx2File.getImageLevelInfo( mip, 0, 0 );
const mipWidth = levelInfo.origWidth;
const mipHeight = levelInfo.origHeight;
const dst = new Uint8Array( ktx2File.getImageTranscodedSizeInBytes( mip, 0, 0, transcoderFormat ) );

const status = ktx2File.transcodeImage(
dst,
mip,
0,
0,
transcoderFormat,
0,
- 1,
- 1,
);
const layerMips = [];

let mipWidth, mipHeight;

for ( let layer = 0; layer < layers; layer ++ ) {

const levelInfo = ktx2File.getImageLevelInfo( mip, layer, 0 );
mipWidth = levelInfo.origWidth;
mipHeight = levelInfo.origHeight;
const dst = new Uint8Array( ktx2File.getImageTranscodedSizeInBytes( mip, layer, 0, transcoderFormat ) );
const status = ktx2File.transcodeImage(
dst,
mip,
layer,
0,
transcoderFormat,
0,
- 1,
- 1,
);

if ( ! status ) {

if ( ! status ) {
cleanup();
throw new Error( 'THREE.KTX2Loader: .transcodeImage failed.' );

cleanup();
throw new Error( 'THREE.KTX2Loader: .transcodeImage failed.' );
}

layerMips.push( dst );

}

mipmaps.push( { data: dst, width: mipWidth, height: mipHeight } );
mipmaps.push( { data: concat( layerMips ), width: mipWidth, height: mipHeight } );

}

Expand Down Expand Up @@ -612,6 +627,33 @@ KTX2Loader.BasisWorker = function () {

}

/** Concatenates N byte arrays. */
function concat( arrays ) {

let totalByteLength = 0;

for ( const array of arrays ) {

totalByteLength += array.byteLength;

}

const result = new Uint8Array( totalByteLength );

let byteOffset = 0;

for ( const array of arrays ) {

result.set( array, byteOffset );

byteOffset += array.byteLength;

}

return result;

}

};

//
Expand Down Expand Up @@ -673,8 +715,6 @@ async function createDataTexture( container ) {

}

//

const level = container.levels[ 0 ];

let levelData;
Expand Down Expand Up @@ -731,7 +771,6 @@ async function createDataTexture( container ) {
view = levelData;

}

//

const texture = pixelDepth === 0
Expand Down
Binary file added examples/models/gltf/Sougen/body_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/models/gltf/Sougen/body_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/models/gltf/Sougen/color.ktx
Binary file not shown.
Binary file added examples/models/gltf/Sougen/head_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/models/gltf/Sougen/head_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/models/gltf/Sougen/normal.ktx
Binary file not shown.
Binary file added examples/models/gltf/Sougen/sougen.glb
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading