@thi.ng/webgl 6.6.10 → 6.6.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/fbo.js CHANGED
@@ -3,98 +3,103 @@ import { TEX_FORMATS } from "./api/texture.js";
3
3
  import { isGL2Context } from "./checks.js";
4
4
  import { error } from "./error.js";
5
5
  import { RBO } from "./rbo.js";
6
- const GL_COLOR_ATTACHMENT0_WEBGL = 0x8ce0;
7
- const GL_MAX_COLOR_ATTACHMENTS_WEBGL = 0x8cdf;
8
- /**
9
- * WebGL framebuffer wrapper w/ automatic detection & support for multiple
10
- * render targets (color attachments) and optional depth buffer. Multiple
11
- * targets are only supported if the `WEBGL_draw_buffers` extension is
12
- * available. The max. number of attachments can be obtained via the
13
- * `maxAttachments` property of the FBO instance.
14
- *
15
- * ```
16
- * // create FBO w/ 2 render targets
17
- * fbo = new FBO(gl, { tex: [tex1, tex2] });
18
- * ```
19
- *
20
- * https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_draw_buffers
21
- */
22
- export class FBO {
23
- gl;
24
- fbo;
25
- ext;
26
- maxAttachments;
27
- constructor(gl, opts) {
28
- this.gl = gl;
29
- this.fbo = gl.createFramebuffer() || error("error creating FBO");
30
- this.ext =
31
- !isGL2Context(gl) && opts && opts.tex && opts.tex.length > 1
32
- ? gl.getExtension("WEBGL_draw_buffers") ||
33
- error("missing WEBGL_draw_buffers ext")
34
- : undefined;
35
- this.maxAttachments = gl.getParameter(GL_MAX_COLOR_ATTACHMENTS_WEBGL);
36
- opts && this.configure(opts);
6
+ const GL_COLOR_ATTACHMENT0_WEBGL = 36064;
7
+ const GL_MAX_COLOR_ATTACHMENTS_WEBGL = 36063;
8
+ class FBO {
9
+ gl;
10
+ fbo;
11
+ ext;
12
+ maxAttachments;
13
+ constructor(gl, opts) {
14
+ this.gl = gl;
15
+ this.fbo = gl.createFramebuffer() || error("error creating FBO");
16
+ this.ext = !isGL2Context(gl) && opts && opts.tex && opts.tex.length > 1 ? gl.getExtension("WEBGL_draw_buffers") || error("missing WEBGL_draw_buffers ext") : void 0;
17
+ this.maxAttachments = gl.getParameter(GL_MAX_COLOR_ATTACHMENTS_WEBGL);
18
+ opts && this.configure(opts);
19
+ }
20
+ bind() {
21
+ this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.fbo);
22
+ return true;
23
+ }
24
+ unbind() {
25
+ this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null);
26
+ return true;
27
+ }
28
+ release() {
29
+ this.gl.deleteFramebuffer(this.fbo);
30
+ delete this.fbo;
31
+ delete this.ext;
32
+ return true;
33
+ }
34
+ configure(opts, unbind = true) {
35
+ const gl = this.gl;
36
+ this.bind();
37
+ if (opts.tex) {
38
+ assert(
39
+ opts.tex.length < this.maxAttachments,
40
+ `too many attachments (max. ${this.maxAttachments})`
41
+ );
42
+ const attachments = [];
43
+ for (let i = 0; i < opts.tex.length; i++) {
44
+ const tex = opts.tex[i];
45
+ assert(
46
+ !!(TEX_FORMATS[tex.format].render || TEX_FORMATS[tex.format].renderExt),
47
+ `texture #${i} has non-renderable format`
48
+ );
49
+ const attach = GL_COLOR_ATTACHMENT0_WEBGL + i;
50
+ gl.framebufferTexture2D(
51
+ gl.FRAMEBUFFER,
52
+ attach,
53
+ gl.TEXTURE_2D,
54
+ tex.tex,
55
+ 0
56
+ );
57
+ attachments[i] = attach;
58
+ }
59
+ if (this.ext) {
60
+ this.ext.drawBuffersWEBGL(attachments);
61
+ } else if (isGL2Context(gl)) {
62
+ gl.drawBuffers(attachments);
63
+ }
37
64
  }
38
- bind() {
39
- this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.fbo);
40
- return true;
41
- }
42
- unbind() {
43
- this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null);
44
- return true;
65
+ if (opts.depth) {
66
+ opts.depth instanceof RBO ? gl.framebufferRenderbuffer(
67
+ gl.FRAMEBUFFER,
68
+ gl.DEPTH_ATTACHMENT,
69
+ gl.RENDERBUFFER,
70
+ opts.depth.buffer
71
+ ) : gl.framebufferTexture2D(
72
+ gl.FRAMEBUFFER,
73
+ gl.DEPTH_ATTACHMENT,
74
+ gl.TEXTURE_2D,
75
+ opts.depth.tex,
76
+ 0
77
+ );
45
78
  }
46
- release() {
47
- this.gl.deleteFramebuffer(this.fbo);
48
- delete this.fbo;
49
- delete this.ext;
79
+ this.validate();
80
+ return unbind ? this.unbind() : true;
81
+ }
82
+ validate() {
83
+ const gl = this.gl;
84
+ const err = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
85
+ switch (err) {
86
+ case gl.FRAMEBUFFER_COMPLETE:
50
87
  return true;
88
+ case gl.FRAMEBUFFER_UNSUPPORTED:
89
+ error("FBO unsupported");
90
+ case gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
91
+ error("FBO incomplete attachment");
92
+ case gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
93
+ error("FBO incomplete dimensions");
94
+ case gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
95
+ error("FBO incomplete missing attachment");
96
+ default:
97
+ return error(`FBO error: ${err}`);
51
98
  }
52
- configure(opts, unbind = true) {
53
- const gl = this.gl;
54
- this.bind();
55
- if (opts.tex) {
56
- assert(opts.tex.length < this.maxAttachments, `too many attachments (max. ${this.maxAttachments})`);
57
- const attachments = [];
58
- for (let i = 0; i < opts.tex.length; i++) {
59
- const tex = opts.tex[i];
60
- assert(!!(TEX_FORMATS[tex.format].render ||
61
- TEX_FORMATS[tex.format].renderExt), `texture #${i} has non-renderable format`);
62
- const attach = GL_COLOR_ATTACHMENT0_WEBGL + i;
63
- gl.framebufferTexture2D(gl.FRAMEBUFFER, attach, gl.TEXTURE_2D, tex.tex, 0);
64
- attachments[i] = attach;
65
- }
66
- if (this.ext) {
67
- this.ext.drawBuffersWEBGL(attachments);
68
- }
69
- else if (isGL2Context(gl)) {
70
- gl.drawBuffers(attachments);
71
- }
72
- }
73
- if (opts.depth) {
74
- opts.depth instanceof RBO
75
- ? gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, opts.depth.buffer)
76
- : gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, opts.depth.tex, 0);
77
- }
78
- this.validate();
79
- return unbind ? this.unbind() : true;
80
- }
81
- validate() {
82
- const gl = this.gl;
83
- const err = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
84
- switch (err) {
85
- case gl.FRAMEBUFFER_COMPLETE:
86
- return true;
87
- case gl.FRAMEBUFFER_UNSUPPORTED:
88
- error("FBO unsupported");
89
- case gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
90
- error("FBO incomplete attachment");
91
- case gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
92
- error("FBO incomplete dimensions");
93
- case gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
94
- error("FBO incomplete missing attachment");
95
- default:
96
- return error(`FBO error: ${err}`);
97
- }
98
- }
99
+ }
99
100
  }
100
- export const defFBO = (gl, opts) => new FBO(gl, opts);
101
+ const defFBO = (gl, opts) => new FBO(gl, opts);
102
+ export {
103
+ FBO,
104
+ defFBO
105
+ };
package/geo/cube.js CHANGED
@@ -1,28 +1,30 @@
1
1
  import { DrawMode } from "../api/model.js";
2
- // prettier-ignore
3
- export const defCubeModel = (opts) => {
4
- opts = { size: 1, normal: true, uv: true, ...opts };
5
- const s = opts.size;
6
- const spec = {
7
- attribs: {
8
- position: {
9
- data: new Float32Array([s, s, -s, s, s, s, s, -s, s, s, -s, -s, -s, s, s, -s, s, -s, -s, -s, -s, -s, -s, s, -s, s, s, s, s, s, s, s, -s, -s, s, -s, -s, -s, -s, s, -s, -s, s, -s, s, -s, -s, s, s, s, s, -s, s, s, -s, -s, s, s, -s, s, -s, s, -s, s, s, -s, s, -s, -s, -s, -s, -s])
10
- },
11
- },
12
- indices: {
13
- data: new Uint16Array([0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23])
14
- },
15
- uniforms: {},
16
- shader: null,
17
- mode: DrawMode.TRIANGLES,
18
- num: 36
19
- };
20
- opts.normal && (spec.attribs.normal = {
21
- data: new Float32Array([1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1])
22
- });
23
- opts.uv && (spec.attribs.uv = {
24
- data: new Float32Array([1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1]),
25
- size: 2
26
- });
27
- return spec;
2
+ const defCubeModel = (opts) => {
3
+ opts = { size: 1, normal: true, uv: true, ...opts };
4
+ const s = opts.size;
5
+ const spec = {
6
+ attribs: {
7
+ position: {
8
+ data: new Float32Array([s, s, -s, s, s, s, s, -s, s, s, -s, -s, -s, s, s, -s, s, -s, -s, -s, -s, -s, -s, s, -s, s, s, s, s, s, s, s, -s, -s, s, -s, -s, -s, -s, s, -s, -s, s, -s, s, -s, -s, s, s, s, s, -s, s, s, -s, -s, s, s, -s, s, -s, s, -s, s, s, -s, s, -s, -s, -s, -s, -s])
9
+ }
10
+ },
11
+ indices: {
12
+ data: new Uint16Array([0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23])
13
+ },
14
+ uniforms: {},
15
+ shader: null,
16
+ mode: DrawMode.TRIANGLES,
17
+ num: 36
18
+ };
19
+ opts.normal && (spec.attribs.normal = {
20
+ data: new Float32Array([1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1])
21
+ });
22
+ opts.uv && (spec.attribs.uv = {
23
+ data: new Float32Array([1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1]),
24
+ size: 2
25
+ });
26
+ return spec;
27
+ };
28
+ export {
29
+ defCubeModel
28
30
  };
package/geo/quad.js CHANGED
@@ -1,35 +1,36 @@
1
1
  import { DrawMode } from "../api/model.js";
2
- export const defQuadModel = (opts) => {
3
- let { size, uv, center } = { size: 2, uv: true, center: true, ...opts };
4
- size *= 0.5;
5
- const o = center ? 0 : size;
6
- return {
7
- attribs: {
8
- position: {
9
- data: new Float32Array([
10
- o - size,
11
- o - size,
12
- o + size,
13
- o - size,
14
- o - size,
15
- o + size,
16
- o + size,
17
- o + size,
18
- ]),
19
- size: 2,
20
- },
21
- ...(uv
22
- ? {
23
- uv: {
24
- data: new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]),
25
- size: 2,
26
- },
27
- }
28
- : null),
29
- },
30
- uniforms: {},
31
- shader: null,
32
- mode: DrawMode.TRIANGLE_STRIP,
33
- num: 4,
34
- };
2
+ const defQuadModel = (opts) => {
3
+ let { size, uv, center } = { size: 2, uv: true, center: true, ...opts };
4
+ size *= 0.5;
5
+ const o = center ? 0 : size;
6
+ return {
7
+ attribs: {
8
+ position: {
9
+ data: new Float32Array([
10
+ o - size,
11
+ o - size,
12
+ o + size,
13
+ o - size,
14
+ o - size,
15
+ o + size,
16
+ o + size,
17
+ o + size
18
+ ]),
19
+ size: 2
20
+ },
21
+ ...uv ? {
22
+ uv: {
23
+ data: new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]),
24
+ size: 2
25
+ }
26
+ } : null
27
+ },
28
+ uniforms: {},
29
+ shader: null,
30
+ mode: DrawMode.TRIANGLE_STRIP,
31
+ num: 4
32
+ };
33
+ };
34
+ export {
35
+ defQuadModel
35
36
  };
package/logger.js CHANGED
@@ -1,3 +1,7 @@
1
1
  import { NULL_LOGGER } from "@thi.ng/logger/null";
2
- export let LOGGER = NULL_LOGGER;
3
- export const setLogger = (logger) => (LOGGER = logger);
2
+ let LOGGER = NULL_LOGGER;
3
+ const setLogger = (logger) => LOGGER = logger;
4
+ export {
5
+ LOGGER,
6
+ setLogger
7
+ };
package/material.js CHANGED
@@ -1,15 +1,18 @@
1
- export const DEFAULT_MATERIAL = {
2
- ambientCol: [0.1, 0.1, 0.1],
3
- diffuseCol: [0.8, 0.8, 0.8],
4
- specularCol: [1, 1, 1],
1
+ const DEFAULT_MATERIAL = {
2
+ ambientCol: [0.1, 0.1, 0.1],
3
+ diffuseCol: [0.8, 0.8, 0.8],
4
+ specularCol: [1, 1, 1]
5
5
  };
6
6
  const TYPES = {
7
- ambientCol: "vec3",
8
- diffuseCol: "vec3",
9
- specularCol: "vec3",
7
+ ambientCol: "vec3",
8
+ diffuseCol: "vec3",
9
+ specularCol: "vec3"
10
10
  };
11
- export const defMaterial = (mat = {}, flags = {}, base = DEFAULT_MATERIAL) => Object.keys(base).reduce((acc, id) => {
12
- flags[id] !== false &&
13
- (acc[id] = [TYPES[id], mat[id] || base[id]]);
14
- return acc;
11
+ const defMaterial = (mat = {}, flags = {}, base = DEFAULT_MATERIAL) => Object.keys(base).reduce((acc, id) => {
12
+ flags[id] !== false && (acc[id] = [TYPES[id], mat[id] || base[id]]);
13
+ return acc;
15
14
  }, {});
15
+ export {
16
+ DEFAULT_MATERIAL,
17
+ defMaterial
18
+ };
package/matrices.js CHANGED
@@ -4,23 +4,16 @@ import { mulM44 } from "@thi.ng/matrices/mulm";
4
4
  import { normal44 } from "@thi.ng/matrices/normal-mat";
5
5
  import { ortho } from "@thi.ng/matrices/ortho";
6
6
  const $ = (a, b, id) => a[id] || b[id]?.defaultVal || IDENT44;
7
- /**
8
- * Computes the inverse transpose of given 4x4 matrix uniform, i.e.
9
- * `transpose(invert(m))`.
10
- *
11
- * @param model -
12
- */
13
- export const autoNormalMatrix1 = (model = "model") => (shaderU, specU) => normal44([], $(specU, shaderU, model));
14
- /**
15
- * Computes the inverse transpose of the matrix product of given 4x4
16
- * matrix uniforms, i.e. `transpose(invert(view * model))`.
17
- *
18
- * @param model -
19
- * @param view -
20
- */
21
- export const autoNormalMatrix2 = (model = "model", view = "view") => (shaderU, specU) => (normal44(null, mulM44([], $(specU, shaderU, view), $(specU, shaderU, model))));
22
- export function screen2d(a, b) {
23
- return isNumber(a)
24
- ? ortho([], 0, a, b, 0, -1, 1)
25
- : ortho([], 0, a.drawingBufferWidth, a.drawingBufferHeight, 0, -1, 1);
7
+ const autoNormalMatrix1 = (model = "model") => (shaderU, specU) => normal44([], $(specU, shaderU, model));
8
+ const autoNormalMatrix2 = (model = "model", view = "view") => (shaderU, specU) => normal44(
9
+ null,
10
+ mulM44([], $(specU, shaderU, view), $(specU, shaderU, model))
11
+ );
12
+ function screen2d(a, b) {
13
+ return isNumber(a) ? ortho([], 0, a, b, 0, -1, 1) : ortho([], 0, a.drawingBufferWidth, a.drawingBufferHeight, 0, -1, 1);
26
14
  }
15
+ export {
16
+ autoNormalMatrix1,
17
+ autoNormalMatrix2,
18
+ screen2d
19
+ };