Skip to content

Commit

Permalink
More specific types for Texture image and mipmaps
Browse files Browse the repository at this point in the history
Potential fix for issue three-types#124
  • Loading branch information
capnmidnight committed May 20, 2022
1 parent 6caaa2a commit bc39b74
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 31 deletions.
4 changes: 1 addition & 3 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,8 @@
"profile": "https://github.com/atulrnt",
"contributions": [
"code",
],
"code"
]
},
}
],
"skipCi": true,
"contributorsPerLine": 7
Expand Down
4 changes: 2 additions & 2 deletions types/three/src/textures/CanvasTexture.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Texture } from './Texture';
import { Mapping, Wrapping, TextureFilter, PixelFormat, TextureDataType } from '../constants';

export class CanvasTexture extends Texture {
export class CanvasTexture extends Texture<HTMLCanvasElement | OffscreenCanvas> {
/**
* @param canvas
* @param [format=THREE.RGBAFormat]
Expand All @@ -15,7 +15,7 @@ export class CanvasTexture extends Texture {
* @param [encoding=THREE.LinearEncoding]
*/
constructor(
canvas: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap,
canvas: HTMLCanvasElement | OffscreenCanvas,
mapping?: Mapping,
wrapS?: Wrapping,
wrapT?: Wrapping,
Expand Down
7 changes: 2 additions & 5 deletions types/three/src/textures/CompressedTexture.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Texture } from './Texture';
import { Dimensions2D, Texture } from './Texture';
import {
Mapping,
Wrapping,
Expand All @@ -8,7 +8,7 @@ import {
TextureEncoding,
} from '../constants';

export class CompressedTexture extends Texture {
export class CompressedTexture extends Texture<Dimensions2D> {
/**
* @param mipmaps
* @param width
Expand Down Expand Up @@ -38,9 +38,6 @@ export class CompressedTexture extends Texture {
encoding?: TextureEncoding,
);

get image(): { width: number; height: number };
set image(value: { width: number; height: number });

mipmaps: ImageData[];

/**
Expand Down
6 changes: 3 additions & 3 deletions types/three/src/textures/CubeTexture.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Texture } from './Texture';
import { BaseTextureImageType, Texture } from './Texture';
import { Mapping, Wrapping, TextureFilter, PixelFormat, TextureDataType, TextureEncoding } from '../constants';

export class CubeTexture extends Texture {
export class CubeTexture extends Texture<BaseTextureImageType[], CubeTexture> {
/**
* @param [images=[]]
* @param [mapping=THREE.CubeReflectionMapping]
Expand All @@ -15,7 +15,7 @@ export class CubeTexture extends Texture {
* @param [encoding=THREE.LinearEncoding]
*/
constructor(
images?: any[], // HTMLImageElement or HTMLCanvasElement
images?: BaseTextureImageType[],
mapping?: Mapping,
wrapS?: Wrapping,
wrapT?: Wrapping,
Expand Down
4 changes: 2 additions & 2 deletions types/three/src/textures/Data3DTexture.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Texture } from './Texture';
import { DataDimensions3D, Texture } from './Texture';
import { TextureFilter } from '../constants';

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

/**
Expand Down
4 changes: 2 additions & 2 deletions types/three/src/textures/DataArrayTexture.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Texture } from './Texture';
import { DataDimensions3D, Texture } from './Texture';
import { TextureFilter } from '../constants';

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

/**
Expand Down
4 changes: 2 additions & 2 deletions types/three/src/textures/DataTexture.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Texture } from './Texture';
import { DataDimensions2D, Texture } from './Texture';
import { Mapping, Wrapping, TextureFilter, PixelFormat, TextureDataType, TextureEncoding } from '../constants';

export class DataTexture extends Texture {
export class DataTexture extends Texture<DataDimensions2D> {
/**
* @param data
* @param width
Expand Down
7 changes: 2 additions & 5 deletions types/three/src/textures/DepthTexture.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Texture } from './Texture';
import { Dimensions2D, Texture } from './Texture';
import { Mapping, Wrapping, TextureFilter, TextureDataType } from '../constants';

export class DepthTexture extends Texture {
export class DepthTexture extends Texture<Dimensions2D> {
/**
* @param width
* @param height
Expand All @@ -25,9 +25,6 @@ export class DepthTexture extends Texture {
anisotropy?: number,
);

get image(): { width: number; height: number };
set image(value: { width: number; height: number });

/**
* @default false
*/
Expand Down
4 changes: 2 additions & 2 deletions types/three/src/textures/FramebufferTexture.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Texture } from './Texture';
import { Dimensions2D, Texture } from './Texture';
import { PixelFormat } from '../constants';

export class FramebufferTexture extends Texture {
export class FramebufferTexture extends Texture<Dimensions2D> {
readonly isFramebufferTexture: true;

constructor(width: number, height: number, format: PixelFormat);
Expand Down
35 changes: 30 additions & 5 deletions types/three/src/textures/Texture.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,33 @@ import {
TextureDataType,
TextureEncoding,
} from '../constants';
import { CubeTexture } from './CubeTexture';

export class Texture extends EventDispatcher {
export interface Dimensions2D {
width: number;
height: number;
}

export interface TextureData {
data: BufferSource;
}

export interface DataDimensions2D extends Dimensions2D, TextureData {}

export interface DataDimensions3D extends DataDimensions2D {
depth: number;
}

export type BaseTextureMipMapType = TexImageSource | DataDimensions2D | DataDimensions3D;

export type BaseTextureImageType = BaseTextureMipMapType | Dimensions2D;

export type BaseTextureTypes = BaseTextureImageType | BaseTextureImageType[];

export class Texture<
ImageT extends BaseTextureTypes = TexImageSource,
MipMapT extends BaseTextureMipMapType = TexImageSource,
> extends EventDispatcher {
/**
* @param [image]
* @param [mapping=THREE.Texture.DEFAULT_MAPPING]
Expand All @@ -26,7 +51,7 @@ export class Texture extends EventDispatcher {
* @param [encoding=THREE.LinearEncoding]
*/
constructor(
image?: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement,
image?: ImageT,
mapping?: Mapping,
wrapS?: Wrapping,
wrapT?: Wrapping,
Expand Down Expand Up @@ -61,7 +86,7 @@ export class Texture extends EventDispatcher {
* video element as a source for your texture image and continuously update this texture
* as long as video is playing - the {@link VideoTexture} class handles this automatically.
*/
get image(): any;
get image(): ImageT;

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

/**
* @default []
*/
mipmaps: any[]; // ImageData[] for 2D textures and CubeTexture[] for cube textures;
mipmaps: MipMapT[]; // ImageData[] for 2D textures and CubeTexture[] for cube textures;

/**
* @default THREE.Texture.DEFAULT_MAPPING
Expand Down

0 comments on commit bc39b74

Please sign in to comment.