Skip to content

Commit

Permalink
feat(webgl): update Texture.config() default handling
Browse files Browse the repository at this point in the history
- add defaults for `filter` & `wrap` opts
- add TextureOpts docstrings
  • Loading branch information
postspectacular committed Feb 12, 2020
1 parent 6e22af4 commit 4c62d87
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 32 deletions.
77 changes: 77 additions & 0 deletions packages/webgl/src/api/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,22 +646,99 @@ export type ReadableTextureFormat =
// );

export interface TextureOpts {
/**
* If this value is null or a typedarray then size options (i.e.
* `width`, `height`, `depth`) MUST be given (the latter only if
* `target` is TEXTURE_3D).
*/
image: ArrayBufferView | TexImageSource | null;
/**
* @defaultValue TextureTarget.TEXTURE_2D
*/
target: TextureTarget;
/**
* Only needed if overriding `format`'s default type.
*/
type: TextureType;
/**
* @defaultValue TextureFilter.NEAREST
*/
filter: TextureFilter | [TextureFilter, TextureFilter?];
/**
* @defaultValue TextureRepeat.CLAMP
*/
wrap: TextureRepeat | [TextureRepeat, TextureRepeat?, TextureRepeat?];
/**
* Min/max level-of-detail values.
*
* @defaultValue none
*/
lod: [number, number?];
/**
* Min/max mipmap levels (ints)
*
* @defaultValue none
*/
minMaxLevel: [number, number];
/**
* Mipmap level to configure (e.g. if providing custom mipmaps)
*
* @defaultValue 0
*/
level: number;
/**
* @defaultValue TextureFormat.RGBA
*/
format: TextureFormat;
/**
* Texture width in pixels. Only used if `image` is null or a typed
* array.
*
* @defaultValue none
*/
width: number;
/**
* Texture height in pixels. Only used if `image` is null or a typed
* array.
*
* @defaultValue none
*/
height: number;
/**
* Texture depth in pixels. Only used if `target` is
* `TextureTarget.TEXTURE_3D`
*
* @defaultValue none
*/
depth: number;
/**
* True, if mipmaps should be generated.
*
* @defaultValue false
*/
mipmap: boolean;
/**
* True, if source data should be flipped along its vertical axis.
*
* @defaultValue none
*/
flip: boolean;
/**
* True, if the source data's color channels should be
* pre-multiplied with the alpha channel.
*
* @defaultValue none
*/
premultiply: boolean;
/**
* True, if given `image` is only defining a sub-image (i.e. partial
* update of a previously configured texture). If true, also uses
* `pos` option.
*/
sub: boolean;
/**
* Pixel position offset for `sub` image updates.
*/
pos: number[];
}

Expand Down
67 changes: 35 additions & 32 deletions packages/webgl/src/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { isArray } from "@thi.ng/checks";
import {
ITexture,
TEX_FORMATS,
TextureFilter,
TextureFormat,
TextureOpts,
TextureRepeat,
TextureTarget,
TextureType
} from "./api/texture";
Expand Down Expand Up @@ -143,48 +145,49 @@ export class Texture implements ITexture {
(<any>opts.image).height
];
}
gl.texImage2D(target, level, format, baseFormat, type, <
TexImageSource
>opts.image);
gl.texImage2D(
target,
level,
format,
baseFormat,
type,
<TexImageSource>opts.image
);
}
}
}
}

opts.mipmap && gl.generateMipmap(target);

if (opts.filter) {
const flt = opts.filter;
if (isArray(flt)) {
t1 = flt[0];
t2 = flt[1]!;
} else {
t1 = t2 = flt;
}
t1 && gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, t1);
t2 && gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, t2);
const flt = opts.filter || TextureFilter.NEAREST;
if (isArray(flt)) {
t1 = flt[0];
t2 = flt[1]!;
} else {
t1 = t2 = flt;
}
t1 && gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, t1);
t2 && gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, t2);

if (opts.wrap) {
const wrap = opts.wrap;
if (isArray(wrap)) {
t1 = wrap[0];
t2 = wrap[1]!;
t3 = wrap[2]!;
} else {
t1 = t2 = t3 = wrap;
}
t1 && gl.texParameteri(target, gl.TEXTURE_WRAP_S, t1);
t2 && gl.texParameteri(target, gl.TEXTURE_WRAP_T, t2);
t3 &&
isGL2 &&
target === (<WebGL2RenderingContext>gl).TEXTURE_3D &&
gl.texParameteri(
target,
(<WebGL2RenderingContext>gl).TEXTURE_WRAP_R,
t3
);
const wrap = opts.wrap || TextureRepeat.CLAMP;
if (isArray(wrap)) {
t1 = wrap[0];
t2 = wrap[1]!;
t3 = wrap[2]!;
} else {
t1 = t2 = t3 = wrap;
}
t1 && gl.texParameteri(target, gl.TEXTURE_WRAP_S, t1);
t2 && gl.texParameteri(target, gl.TEXTURE_WRAP_T, t2);
t3 &&
isGL2 &&
target === (<WebGL2RenderingContext>gl).TEXTURE_3D &&
gl.texParameteri(
target,
(<WebGL2RenderingContext>gl).TEXTURE_WRAP_R,
t3
);

if (opts.lod) {
const [t1, t2] = opts.lod;
Expand Down

0 comments on commit 4c62d87

Please sign in to comment.