Skip to content

Commit a5b3c30

Browse files
committed
Texture and MipMap types
1 parent 4ecb654 commit a5b3c30

21 files changed

+1920
-1823
lines changed

.all-contributorsrc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,8 @@
349349
"profile": "https://github.com/atulrnt",
350350
"contributions": [
351351
"code",
352-
],
353-
"code"
354352
]
355-
},
353+
}
356354
],
357355
"skipCi": true,
358356
"contributorsPerLine": 7

types/three/examples/jsm/loaders/DDSLoader.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LoadingManager, CompressedTextureLoader, PixelFormat, CompressedPixelFormat } from '../../../src/Three';
22

33
export interface DDS {
4-
mipmaps: object[];
4+
mipmaps: ImageData[];
55
width: number;
66
height: number;
77
format: PixelFormat | CompressedPixelFormat;

types/three/examples/jsm/loaders/KTXLoader.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LoadingManager, CompressedTextureLoader, PixelFormat, CompressedPixelFormat } from '../../../src/Three';
22

33
export interface KTX {
4-
mipmaps: object[];
4+
mipmaps: ImageData[];
55
width: number;
66
height: number;
77
format: PixelFormat | CompressedPixelFormat;

types/three/examples/jsm/loaders/PVRLoader.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LoadingManager, CompressedTextureLoader, CompressedPixelFormat } from '../../../src/Three';
22

33
export interface PVR {
4-
mipmaps: object[];
4+
mipmaps: ImageData[];
55
width: number;
66
height: number;
77
format: CompressedPixelFormat;

types/three/src/renderers/WebGL3DRenderTarget.d.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Data3DTexture } from '../textures/Data3DTexture';
2+
import { DataDimensions3D } from '../textures/Texture';
23
import { WebGLRenderTarget } from './WebGLRenderTarget';
34

45
/**
56
* Represents a three-dimensional render target.
67
*/
7-
export class WebGL3DRenderTarget extends WebGLRenderTarget {
8+
export class WebGL3DRenderTarget extends WebGLRenderTarget<DataDimensions3D, DataDimensions3D, Data3DTexture> {
89
/**
910
* Creates a new WebGL3DRenderTarget.
1011
*
@@ -19,10 +20,5 @@ export class WebGL3DRenderTarget extends WebGLRenderTarget {
1920
*/
2021
depth: number;
2122

22-
/**
23-
* The texture property is overwritten with an instance of {@link Data3DTexture}.
24-
*/
25-
texture: Data3DTexture;
26-
2723
readonly isWebGL3DRenderTarget: true;
2824
}

types/three/src/renderers/WebGLArrayRenderTarget.d.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { DataArrayTexture } from '../textures/DataArrayTexture';
2+
import { DataDimensions3D } from '../textures/Texture';
23
import { WebGLRenderTarget } from './WebGLRenderTarget';
34

45
/**
56
* This type of render target represents an array of textures.
67
*/
7-
export class WebGLArrayRenderTarget extends WebGLRenderTarget {
8+
export class WebGLArrayRenderTarget extends WebGLRenderTarget<DataDimensions3D, DataDimensions3D, DataArrayTexture> {
89
/**
910
* Creates a new WebGLArrayRenderTarget.
1011
*
@@ -19,10 +20,5 @@ export class WebGLArrayRenderTarget extends WebGLRenderTarget {
1920
*/
2021
depth: number;
2122

22-
/**
23-
* The texture property is overwritten with an instance of {@link DataArrayTexture}.
24-
*/
25-
texture: DataArrayTexture;
26-
2723
readonly isWebGLArrayRenderTarget: true;
2824
}

types/three/src/renderers/WebGLCubeRenderTarget.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { WebGLRenderTargetOptions, WebGLRenderTarget } from './WebGLRenderTarget';
22
import { WebGLRenderer } from './WebGLRenderer';
3-
import { Texture } from './../textures/Texture';
3+
import { BaseTextureImageType, Texture } from './../textures/Texture';
44
import { CubeTexture } from './../textures/CubeTexture';
55

6-
export class WebGLCubeRenderTarget extends WebGLRenderTarget {
6+
export class WebGLCubeRenderTarget extends WebGLRenderTarget<BaseTextureImageType[], CubeTexture, CubeTexture> {
77
constructor(size: number, options?: WebGLRenderTargetOptions);
88

9-
texture: CubeTexture;
10-
119
fromEquirectangularTexture(renderer: WebGLRenderer, texture: Texture): this;
1210

1311
clear(renderer: WebGLRenderer, color: boolean, depth: boolean, stencil: boolean): void;

types/three/src/renderers/WebGLRenderTarget.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Vector4 } from './../math/Vector4';
2-
import { Texture } from './../textures/Texture';
2+
import { MipMapType, Texture, TextureImageTypes } from './../textures/Texture';
33
import { DepthTexture } from './../textures/DepthTexture';
44
import { EventDispatcher } from './../core/EventDispatcher';
55
import { Wrapping, TextureFilter, TextureDataType, TextureEncoding } from '../constants';
@@ -25,7 +25,11 @@ export interface WebGLRenderTargetOptions {
2525
samples?: number;
2626
}
2727

28-
export class WebGLRenderTarget extends EventDispatcher {
28+
export class WebGLRenderTarget<
29+
ImageT extends TextureImageTypes = TextureImageTypes,
30+
MipMapT extends MipMapType = MipMapType,
31+
TextureT extends Texture<ImageT, MipMapT> = Texture<ImageT, MipMapT>,
32+
> extends EventDispatcher {
2933
constructor(width: number, height: number, options?: WebGLRenderTargetOptions);
3034

3135
width: number;
@@ -38,7 +42,7 @@ export class WebGLRenderTarget extends EventDispatcher {
3842
*/
3943
scissorTest: boolean;
4044
viewport: Vector4;
41-
texture: Texture;
45+
texture: TextureT;
4246

4347
/**
4448
* @default true

types/three/src/textures/CanvasTexture.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Texture } from './Texture';
22
import { Mapping, Wrapping, TextureFilter, PixelFormat, TextureDataType } from '../constants';
3+
import { OffscreenCanvas } from '../renderers/WebGLRenderer';
34

4-
export class CanvasTexture extends Texture {
5+
export class CanvasTexture extends Texture<HTMLCanvasElement> {
56
/**
67
* @param canvas
78
* @param [format=THREE.RGBAFormat]
@@ -15,7 +16,7 @@ export class CanvasTexture extends Texture {
1516
* @param [encoding=THREE.LinearEncoding]
1617
*/
1718
constructor(
18-
canvas: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap,
19+
canvas: HTMLCanvasElement | OffscreenCanvas,
1920
mapping?: Mapping,
2021
wrapS?: Wrapping,
2122
wrapT?: Wrapping,

types/three/src/textures/CompressedTexture.d.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Texture } from './Texture';
1+
import { Dimensions2D, Texture } from './Texture';
22
import {
33
Mapping,
44
Wrapping,
@@ -8,7 +8,7 @@ import {
88
TextureEncoding,
99
} from '../constants';
1010

11-
export class CompressedTexture extends Texture {
11+
export class CompressedTexture extends Texture<Dimensions2D> {
1212
/**
1313
* @param mipmaps
1414
* @param width
@@ -38,9 +38,6 @@ export class CompressedTexture extends Texture {
3838
encoding?: TextureEncoding,
3939
);
4040

41-
get image(): { width: number; height: number };
42-
set image(value: { width: number; height: number });
43-
4441
mipmaps: ImageData[];
4542

4643
/**

types/three/src/textures/CubeTexture.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Texture } from './Texture';
1+
import { BaseTextureImageType, Texture } from './Texture';
22
import { Mapping, Wrapping, TextureFilter, PixelFormat, TextureDataType, TextureEncoding } from '../constants';
33

4-
export class CubeTexture extends Texture {
4+
export class CubeTexture extends Texture<BaseTextureImageType[], CubeTexture> {
55
/**
66
* @param [images=[]]
77
* @param [mapping=THREE.CubeReflectionMapping]
@@ -15,7 +15,7 @@ export class CubeTexture extends Texture {
1515
* @param [encoding=THREE.LinearEncoding]
1616
*/
1717
constructor(
18-
images?: any[], // HTMLImageElement or HTMLCanvasElement
18+
images?: BaseTextureImageType[],
1919
mapping?: Mapping,
2020
wrapS?: Wrapping,
2121
wrapT?: Wrapping,

types/three/src/textures/Data3DTexture.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Texture } from './Texture';
1+
import { DataDimensions3D, Texture } from './Texture';
22
import { TextureFilter } from '../constants';
33

4-
export class Data3DTexture extends Texture {
4+
export class Data3DTexture extends Texture<DataDimensions3D, DataDimensions3D> {
55
constructor(data: BufferSource, width: number, height: number, depth: number);
66

77
/**

types/three/src/textures/DataArrayTexture.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Texture } from './Texture';
1+
import { DataDimensions3D, Texture } from './Texture';
22
import { TextureFilter } from '../constants';
33

4-
export class DataArrayTexture extends Texture {
4+
export class DataArrayTexture extends Texture<DataDimensions3D, DataDimensions3D> {
55
constructor(data?: BufferSource, width?: number, height?: number, depth?: number);
66

77
/**

types/three/src/textures/DataTexture.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Texture } from './Texture';
22
import { Mapping, Wrapping, TextureFilter, PixelFormat, TextureDataType, TextureEncoding } from '../constants';
33

4-
export class DataTexture extends Texture {
4+
export class DataTexture extends Texture<ImageData> {
55
/**
66
* @param data
77
* @param width

types/three/src/textures/DepthTexture.d.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Texture } from './Texture';
1+
import { Dimensions2D, Texture } from './Texture';
22
import { Mapping, Wrapping, TextureFilter, TextureDataType } from '../constants';
33

4-
export class DepthTexture extends Texture {
4+
export class DepthTexture extends Texture<Dimensions2D> {
55
/**
66
* @param width
77
* @param height
@@ -25,9 +25,6 @@ export class DepthTexture extends Texture {
2525
anisotropy?: number,
2626
);
2727

28-
get image(): { width: number; height: number };
29-
set image(value: { width: number; height: number });
30-
3128
/**
3229
* @default false
3330
*/

types/three/src/textures/FramebufferTexture.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Texture } from './Texture';
1+
import { Dimensions2D, Texture } from './Texture';
22
import { PixelFormat } from '../constants';
33

4-
export class FramebufferTexture extends Texture {
4+
export class FramebufferTexture extends Texture<Dimensions2D> {
55
readonly isFramebufferTexture: true;
66

77
constructor(width: number, height: number, format: PixelFormat);

types/three/src/textures/Texture.d.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,32 @@ import {
1111
TextureDataType,
1212
TextureEncoding,
1313
} from '../constants';
14+
import { CubeTexture } from './CubeTexture';
15+
import { OffscreenCanvas } from '../renderers/WebGLRenderer';
1416

15-
export class Texture extends EventDispatcher {
17+
export interface Dimensions2D {
18+
width: number;
19+
height: number;
20+
}
21+
22+
export interface DataDimensions3D extends ImageData {
23+
depth: number;
24+
}
25+
26+
export type BaseTextureImageType = TexImageSource | OffscreenCanvas | DataDimensions3D | Dimensions2D;
27+
28+
export type TextureImageTypes = BaseTextureImageType | BaseTextureImageType[];
29+
30+
export interface ManualMipMapType<ImageSource extends TexImageSource = TexImageSource> extends Dimensions2D {
31+
data: ImageSource;
32+
}
33+
34+
export type MipMapType = ImageData | ManualMipMapType | CubeTexture;
35+
36+
export class Texture<
37+
ImageT extends TextureImageTypes = TextureImageTypes,
38+
MipMapT extends MipMapType = MipMapType,
39+
> extends EventDispatcher {
1640
/**
1741
* @param [image]
1842
* @param [mapping=THREE.Texture.DEFAULT_MAPPING]
@@ -26,7 +50,7 @@ export class Texture extends EventDispatcher {
2650
* @param [encoding=THREE.LinearEncoding]
2751
*/
2852
constructor(
29-
image?: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement,
53+
image?: ImageT,
3054
mapping?: Mapping,
3155
wrapS?: Wrapping,
3256
wrapT?: Wrapping,
@@ -61,7 +85,7 @@ export class Texture extends EventDispatcher {
6185
* video element as a source for your texture image and continuously update this texture
6286
* as long as video is playing - the {@link VideoTexture} class handles this automatically.
6387
*/
64-
get image(): any;
88+
get image(): ImageT;
6589

6690
/**
6791
* An image object, typically created using the {@link TextureLoader.load} method.
@@ -71,12 +95,12 @@ export class Texture extends EventDispatcher {
7195
* video element as a source for your texture image and continuously update this texture
7296
* as long as video is playing - the {@link VideoTexture} class handles this automatically.
7397
*/
74-
set image(data: any);
98+
set image(data: ImageT);
7599

76100
/**
77101
* @default []
78102
*/
79-
mipmaps: any[]; // ImageData[] for 2D textures and CubeTexture[] for cube textures;
103+
mipmaps: MipMapT[]; // ImageData[] for 2D textures and CubeTexture[] for cube textures;
80104

81105
/**
82106
* @default THREE.Texture.DEFAULT_MAPPING

types/three/src/textures/VideoTexture.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Texture } from './Texture';
22
import { Mapping, Wrapping, TextureFilter, PixelFormat, TextureDataType } from '../constants';
33

4-
export class VideoTexture extends Texture {
4+
export class VideoTexture extends Texture<HTMLVideoElement> {
55
/**
66
* @param video
77
* @param [mapping=THREE.Texture.DEFAULT_MAPPING]

types/three/test/postprocessing/postprocessing-savepass.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as THREE from 'three';
22
import { SavePass } from 'three/examples/jsm/postprocessing/SavePass';
33

44
let pass = new SavePass(); // $ExpectType SavePass
5-
let rt = pass.renderTarget; // $ExpectType WebGLRenderTarget
5+
let rt = pass.renderTarget; // $ExpectType WebGLRenderTarget<TextureImageTypes, MipMapType, Texture<TextureImageTypes, MipMapType>>
66

77
pass = new SavePass(new THREE.WebGLRenderTarget(128, 128)); // $ExpectType SavePass
8-
rt = pass.renderTarget; // $ExpectType WebGLRenderTarget
8+
rt = pass.renderTarget; // $ExpectType WebGLRenderTarget<TextureImageTypes, MipMapType, Texture<TextureImageTypes, MipMapType>>

types/three/test/textures/textures-source.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,57 @@ imageLoader.load('/path/to/image.png', image => {
1111
material.map.source = source;
1212
material.map.offset.x = 0.5;
1313
});
14+
15+
function assertNever(_: never): void {
16+
throw new Error('Assertion failed');
17+
}
18+
19+
(function testTextureFromImage() {
20+
const image = document.createElement('img');
21+
const imageTex = new THREE.Texture(image);
22+
const img = imageTex.image; // $ExpectType HTMLImageElement
23+
if (!(img instanceof HTMLImageElement)) {
24+
assertNever(img);
25+
}
26+
})();
27+
28+
(function testTextureFromCanvas() {
29+
const canvas = document.createElement('canvas');
30+
const canvasTex = new THREE.Texture(canvas);
31+
const canv = canvasTex.image; // $ExpectType HTMLCanvasElement
32+
if (!(canv instanceof HTMLCanvasElement)) {
33+
assertNever(canv);
34+
}
35+
})();
36+
37+
(function testTextureFromVideo() {
38+
const video = document.createElement('video');
39+
const videoTex = new THREE.VideoTexture(video);
40+
const vid = videoTex.image; // $ExpectType HTMLVideoElement
41+
if (!(vid instanceof HTMLVideoElement)) {
42+
assertNever(vid);
43+
}
44+
})();
45+
46+
(function testCanvasTexture() {
47+
const canvas = makeCanvas();
48+
const canvTex = new THREE.CanvasTexture(canvas);
49+
const img = canvTex.image; // $ExpectType HTMLCanvasElement
50+
if (!(img instanceof HTMLCanvasElement)) {
51+
assertNever(img);
52+
}
53+
})();
54+
55+
function makeCanvas() {
56+
const canvas = document.createElement('canvas');
57+
canvas.width = 1024;
58+
canvas.height = 1024;
59+
const g = canvas.getContext('2d');
60+
if (g) {
61+
g.fillStyle = 'red';
62+
g.fillRect(0, 0, 512, 512);
63+
g.fillStyle = 'blue';
64+
g.fillRect(512, 512, 512, 512);
65+
}
66+
return canvas;
67+
}

0 commit comments

Comments
 (0)