@thi.ng/webgl 6.6.11 → 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.
@@ -1,4 +1,8 @@
1
- import { diffuseLighting, halfLambert, lambert, } from "@thi.ng/shader-ast-stdlib/light/lambert";
1
+ import {
2
+ diffuseLighting,
3
+ halfLambert,
4
+ lambert
5
+ } from "@thi.ng/shader-ast-stdlib/light/lambert";
2
6
  import { transformMVP } from "@thi.ng/shader-ast-stdlib/matrix/mvp";
3
7
  import { surfaceNormal } from "@thi.ng/shader-ast-stdlib/matrix/normal";
4
8
  import { assign } from "@thi.ng/shader-ast/ast/assign";
@@ -11,51 +15,78 @@ import { texture } from "@thi.ng/shader-ast/builtin/texture";
11
15
  import { defMaterial } from "../material.js";
12
16
  import { autoNormalMatrix2 } from "../matrices.js";
13
17
  import { colorAttrib, positionAttrib } from "../utils.js";
14
- export const LAMBERT = (opts = {}) => ({
15
- vs: (gl, unis, ins, outs) => [
16
- defMain(() => [
17
- opts.uv ? assign(outs.vuv, ins[opts.uv]) : null,
18
- assign(outs.vcolor, colorAttrib(opts, ins, unis.diffuseCol)),
19
- assign(outs.vnormal, surfaceNormal(ins.normal, unis.normalMat)),
20
- assign(gl.gl_Position, transformMVP(positionAttrib(opts, ins), unis.model, unis.view, unis.proj)),
21
- ]),
22
- ],
23
- fs: (_, unis, ins, outs) => [
24
- defMain(() => [
25
- assign(outs.fragColor, vec4(diffuseLighting((opts.bidir !== false ? halfLambert : lambert)(normalize(ins.vnormal), unis.lightDir), opts.uv
26
- ? mul($(texture(unis.tex, ins.vuv), "xyz"), ins.vcolor)
27
- : ins.vcolor, unis.lightCol, unis.ambientCol), 1)),
28
- ]),
29
- ],
30
- // pre: ALIAS_TEXTURE,
31
- attribs: {
32
- position: "vec3",
33
- normal: "vec3",
34
- ...(opts.uv ? { [opts.uv]: "vec2" } : null),
35
- ...(opts.color && !opts.instanceColor
36
- ? { [opts.color]: "vec3" }
37
- : null),
38
- ...(opts.instancePos ? { [opts.instancePos]: "vec3" } : null),
39
- ...(opts.instanceColor ? { [opts.instanceColor]: "vec3" } : null),
40
- },
41
- varying: {
42
- vcolor: "vec3",
43
- vnormal: "vec3",
44
- ...(opts.uv ? { vuv: "vec2" } : null),
45
- },
46
- uniforms: {
47
- model: "mat4",
48
- view: "mat4",
49
- proj: "mat4",
50
- normalMat: ["mat4", autoNormalMatrix2()],
51
- lightDir: ["vec3", [0, 1, 0]],
52
- lightCol: ["vec3", [1, 1, 1]],
53
- ...defMaterial({ diffuseCol: [1, 1, 1], ...opts.material }, { specularCol: false }),
54
- ...(opts.uv ? { tex: "sampler2D" } : null),
55
- },
56
- state: {
57
- depth: true,
58
- cull: true,
59
- ...opts.state,
60
- },
18
+ const LAMBERT = (opts = {}) => ({
19
+ vs: (gl, unis, ins, outs) => [
20
+ defMain(() => [
21
+ opts.uv ? assign(outs.vuv, ins[opts.uv]) : null,
22
+ assign(outs.vcolor, colorAttrib(opts, ins, unis.diffuseCol)),
23
+ assign(outs.vnormal, surfaceNormal(ins.normal, unis.normalMat)),
24
+ assign(
25
+ gl.gl_Position,
26
+ transformMVP(
27
+ positionAttrib(opts, ins),
28
+ unis.model,
29
+ unis.view,
30
+ unis.proj
31
+ )
32
+ )
33
+ ])
34
+ ],
35
+ fs: (_, unis, ins, outs) => [
36
+ defMain(() => [
37
+ assign(
38
+ outs.fragColor,
39
+ vec4(
40
+ diffuseLighting(
41
+ (opts.bidir !== false ? halfLambert : lambert)(
42
+ normalize(ins.vnormal),
43
+ unis.lightDir
44
+ ),
45
+ opts.uv ? mul(
46
+ $(texture(unis.tex, ins.vuv), "xyz"),
47
+ ins.vcolor
48
+ ) : ins.vcolor,
49
+ unis.lightCol,
50
+ unis.ambientCol
51
+ ),
52
+ 1
53
+ )
54
+ )
55
+ ])
56
+ ],
57
+ // pre: ALIAS_TEXTURE,
58
+ attribs: {
59
+ position: "vec3",
60
+ normal: "vec3",
61
+ ...opts.uv ? { [opts.uv]: "vec2" } : null,
62
+ ...opts.color && !opts.instanceColor ? { [opts.color]: "vec3" } : null,
63
+ ...opts.instancePos ? { [opts.instancePos]: "vec3" } : null,
64
+ ...opts.instanceColor ? { [opts.instanceColor]: "vec3" } : null
65
+ },
66
+ varying: {
67
+ vcolor: "vec3",
68
+ vnormal: "vec3",
69
+ ...opts.uv ? { vuv: "vec2" } : null
70
+ },
71
+ uniforms: {
72
+ model: "mat4",
73
+ view: "mat4",
74
+ proj: "mat4",
75
+ normalMat: ["mat4", autoNormalMatrix2()],
76
+ lightDir: ["vec3", [0, 1, 0]],
77
+ lightCol: ["vec3", [1, 1, 1]],
78
+ ...defMaterial(
79
+ { diffuseCol: [1, 1, 1], ...opts.material },
80
+ { specularCol: false }
81
+ ),
82
+ ...opts.uv ? { tex: "sampler2D" } : null
83
+ },
84
+ state: {
85
+ depth: true,
86
+ cull: true,
87
+ ...opts.state
88
+ }
61
89
  });
90
+ export {
91
+ LAMBERT
92
+ };
package/shaders/phong.js CHANGED
@@ -11,64 +11,96 @@ import { dot, max, normalize, pow } from "@thi.ng/shader-ast/builtin/math";
11
11
  import { defMaterial } from "../material.js";
12
12
  import { autoNormalMatrix1 } from "../matrices.js";
13
13
  import { colorAttrib, positionAttrib } from "../utils.js";
14
- export const PHONG = (opts = {}) => ({
15
- vs: (gl, unis, ins, outs) => [
16
- defMain(() => {
17
- let worldPos;
18
- return [
19
- (worldPos = sym(mul(unis.model, vec4(positionAttrib(opts, ins), 1)))),
20
- assign(outs.vnormal, surfaceNormal(ins.normal, unis.normalMat)),
21
- assign(outs.vlight, sub(unis.lightPos, $(worldPos, "xyz"))),
22
- assign(outs.veye, sub(unis.eyePos, $(worldPos, "xyz"))),
23
- assign(outs.vcolor, colorAttrib(opts, ins, unis.diffuseCol)),
24
- assign(gl.gl_Position, mul(mul(unis.proj, unis.view), worldPos)),
25
- ];
26
- }),
27
- ],
28
- fs: (_, unis, ins, outs) => [
29
- defMain(() => {
30
- let normal;
31
- let light;
32
- let directional;
33
- let specular;
34
- return [
35
- (normal = sym(normalize(ins.vnormal))),
36
- (light = sym(normalize(ins.vlight))),
37
- (directional = sym(max(dot(normal, light), FLOAT0))),
38
- (specular = sym(ternary(gt(directional, FLOAT0), pow(dot(normal, normalize(add(light, normalize(ins.veye)))), unis.shininess), FLOAT0))),
39
- assign(outs.fragColor, vec4(add(diffuseLighting(directional, ins.vcolor, unis.lightCol, unis.ambientCol), mul(unis.specularCol, specular)), 1)),
40
- ];
41
- }),
42
- ],
43
- attribs: {
44
- position: "vec3",
45
- normal: "vec3",
46
- ...(opts.color && !opts.instanceColor
47
- ? { [opts.color]: "vec3" }
48
- : null),
49
- ...(opts.instancePos ? { [opts.instancePos]: "vec3" } : null),
50
- ...(opts.instanceColor ? { [opts.instanceColor]: "vec3" } : null),
51
- },
52
- varying: {
53
- vnormal: "vec3",
54
- veye: "vec3",
55
- vlight: "vec3",
56
- vcolor: "vec3",
57
- },
58
- uniforms: {
59
- model: "mat4",
60
- normalMat: ["mat4", autoNormalMatrix1()],
61
- view: "mat4",
62
- proj: "mat4",
63
- shininess: ["float", 32],
64
- eyePos: "vec3",
65
- lightPos: ["vec3", [0, 0, 2]],
66
- lightCol: ["vec3", [1, 1, 1]],
67
- ...defMaterial(opts.material),
68
- },
69
- state: {
70
- depth: true,
71
- cull: true,
72
- ...opts.state,
73
- },
14
+ const PHONG = (opts = {}) => ({
15
+ vs: (gl, unis, ins, outs) => [
16
+ defMain(() => {
17
+ let worldPos;
18
+ return [
19
+ worldPos = sym(
20
+ mul(unis.model, vec4(positionAttrib(opts, ins), 1))
21
+ ),
22
+ assign(outs.vnormal, surfaceNormal(ins.normal, unis.normalMat)),
23
+ assign(outs.vlight, sub(unis.lightPos, $(worldPos, "xyz"))),
24
+ assign(outs.veye, sub(unis.eyePos, $(worldPos, "xyz"))),
25
+ assign(outs.vcolor, colorAttrib(opts, ins, unis.diffuseCol)),
26
+ assign(
27
+ gl.gl_Position,
28
+ mul(mul(unis.proj, unis.view), worldPos)
29
+ )
30
+ ];
31
+ })
32
+ ],
33
+ fs: (_, unis, ins, outs) => [
34
+ defMain(() => {
35
+ let normal;
36
+ let light;
37
+ let directional;
38
+ let specular;
39
+ return [
40
+ normal = sym(normalize(ins.vnormal)),
41
+ light = sym(normalize(ins.vlight)),
42
+ directional = sym(max(dot(normal, light), FLOAT0)),
43
+ specular = sym(
44
+ ternary(
45
+ gt(directional, FLOAT0),
46
+ pow(
47
+ dot(
48
+ normal,
49
+ normalize(add(light, normalize(ins.veye)))
50
+ ),
51
+ unis.shininess
52
+ ),
53
+ FLOAT0
54
+ )
55
+ ),
56
+ assign(
57
+ outs.fragColor,
58
+ vec4(
59
+ add(
60
+ diffuseLighting(
61
+ directional,
62
+ ins.vcolor,
63
+ unis.lightCol,
64
+ unis.ambientCol
65
+ ),
66
+ mul(unis.specularCol, specular)
67
+ ),
68
+ 1
69
+ )
70
+ )
71
+ ];
72
+ })
73
+ ],
74
+ attribs: {
75
+ position: "vec3",
76
+ normal: "vec3",
77
+ ...opts.color && !opts.instanceColor ? { [opts.color]: "vec3" } : null,
78
+ ...opts.instancePos ? { [opts.instancePos]: "vec3" } : null,
79
+ ...opts.instanceColor ? { [opts.instanceColor]: "vec3" } : null
80
+ },
81
+ varying: {
82
+ vnormal: "vec3",
83
+ veye: "vec3",
84
+ vlight: "vec3",
85
+ vcolor: "vec3"
86
+ },
87
+ uniforms: {
88
+ model: "mat4",
89
+ normalMat: ["mat4", autoNormalMatrix1()],
90
+ view: "mat4",
91
+ proj: "mat4",
92
+ shininess: ["float", 32],
93
+ eyePos: "vec3",
94
+ lightPos: ["vec3", [0, 0, 2]],
95
+ lightCol: ["vec3", [1, 1, 1]],
96
+ ...defMaterial(opts.material)
97
+ },
98
+ state: {
99
+ depth: true,
100
+ cull: true,
101
+ ...opts.state
102
+ }
74
103
  });
104
+ export {
105
+ PHONG
106
+ };
@@ -3,36 +3,44 @@ import { defMain } from "@thi.ng/shader-ast/ast/function";
3
3
  import { FLOAT0, FLOAT1, vec4 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { $xy } from "@thi.ng/shader-ast/ast/swizzle";
5
5
  import { texture } from "@thi.ng/shader-ast/builtin/texture";
6
- export const PASSTHROUGH_VS = (gl, _, ins) => [
7
- defMain(() => [assign(gl.gl_Position, vec4(ins.position, FLOAT0, FLOAT1))]),
6
+ const PASSTHROUGH_VS = (gl, _, ins) => [
7
+ defMain(() => [assign(gl.gl_Position, vec4(ins.position, FLOAT0, FLOAT1))])
8
8
  ];
9
- export const PASSTHROUGH_VS_UV = (gl, _, ins, outs) => [
10
- defMain(() => [
11
- assign(outs.v_uv, ins.uv),
12
- assign(gl.gl_Position, vec4(ins.position, FLOAT0, FLOAT1)),
13
- ]),
9
+ const PASSTHROUGH_VS_UV = (gl, _, ins, outs) => [
10
+ defMain(() => [
11
+ assign(outs.v_uv, ins.uv),
12
+ assign(gl.gl_Position, vec4(ins.position, FLOAT0, FLOAT1))
13
+ ])
14
14
  ];
15
- export const PASSTHROUGH_FS = (gl, _, __, outs) => [
16
- defMain(() => [assign(outs.fragColor, $xy(gl.gl_FragCoord))]),
15
+ const PASSTHROUGH_FS = (gl, _, __, outs) => [
16
+ defMain(() => [assign(outs.fragColor, $xy(gl.gl_FragCoord))])
17
17
  ];
18
- export const PASSTHROUGH_FS_UV = (_, unis, ins, outs) => [
19
- defMain(() => [assign(outs.fragColor, texture(unis.tex, ins.v_uv))]),
18
+ const PASSTHROUGH_FS_UV = (_, unis, ins, outs) => [
19
+ defMain(() => [assign(outs.fragColor, texture(unis.tex, ins.v_uv))])
20
20
  ];
21
- export const FX_SHADER_SPEC = {
22
- vs: PASSTHROUGH_VS,
23
- fs: PASSTHROUGH_FS,
24
- attribs: { position: "vec2" },
25
- varying: {},
26
- uniforms: {},
27
- state: { depth: false },
28
- ext: {},
21
+ const FX_SHADER_SPEC = {
22
+ vs: PASSTHROUGH_VS,
23
+ fs: PASSTHROUGH_FS,
24
+ attribs: { position: "vec2" },
25
+ varying: {},
26
+ uniforms: {},
27
+ state: { depth: false },
28
+ ext: {}
29
29
  };
30
- export const FX_SHADER_SPEC_UV = {
31
- vs: PASSTHROUGH_VS_UV,
32
- fs: PASSTHROUGH_FS_UV,
33
- attribs: { position: "vec2", uv: "vec2" },
34
- varying: { v_uv: "vec2" },
35
- uniforms: { tex: "sampler2D" },
36
- state: { depth: false },
37
- ext: {},
30
+ const FX_SHADER_SPEC_UV = {
31
+ vs: PASSTHROUGH_VS_UV,
32
+ fs: PASSTHROUGH_FS_UV,
33
+ attribs: { position: "vec2", uv: "vec2" },
34
+ varying: { v_uv: "vec2" },
35
+ uniforms: { tex: "sampler2D" },
36
+ state: { depth: false },
37
+ ext: {}
38
+ };
39
+ export {
40
+ FX_SHADER_SPEC,
41
+ FX_SHADER_SPEC_UV,
42
+ PASSTHROUGH_FS,
43
+ PASSTHROUGH_FS_UV,
44
+ PASSTHROUGH_VS,
45
+ PASSTHROUGH_VS_UV
38
46
  };
package/syntax.js CHANGED
@@ -1,91 +1,69 @@
1
1
  import { isArray } from "@thi.ng/checks/is-array";
2
2
  import { GLSLVersion } from "@thi.ng/shader-ast-glsl/api";
3
- export const PREFIXES = {
4
- a: "a_",
5
- v: "v_",
6
- u: "u_",
7
- o: "o_",
3
+ const PREFIXES = {
4
+ a: "a_",
5
+ v: "v_",
6
+ u: "u_",
7
+ o: "o_"
8
8
  };
9
- export const NO_PREFIXES = {
10
- a: "",
11
- v: "",
12
- u: "",
13
- o: "",
9
+ const NO_PREFIXES = {
10
+ a: "",
11
+ v: "",
12
+ u: "",
13
+ o: ""
14
14
  };
15
- /**
16
- * GLSL data declaration code generators.
17
- */
18
- export const SYNTAX = {
19
- /**
20
- * WebGL (GLSL ES 1.0)
21
- */
22
- [GLSLVersion.GLES_100]: {
23
- number: 100,
24
- attrib: (id, type, pre) => `attribute ${isArray(type) ? type[0] : type} ${pre.a}${id};`,
25
- varying: {
26
- vs: (id, type, pre) => arrayDecl("varying", type, pre.v + id),
27
- fs: (id, type, pre) => arrayDecl("varying", type, pre.v + id),
28
- },
29
- uniform: (id, u, pre) => arrayDecl("uniform", u, pre.u + id),
30
- output: (id, type, pre) => isArray(type)
31
- ? `#define ${pre.o}${id} gl_FragData[${type[1]}]`
32
- : "",
15
+ const SYNTAX = {
16
+ /**
17
+ * WebGL (GLSL ES 1.0)
18
+ */
19
+ [GLSLVersion.GLES_100]: {
20
+ number: 100,
21
+ attrib: (id, type, pre) => `attribute ${isArray(type) ? type[0] : type} ${pre.a}${id};`,
22
+ varying: {
23
+ vs: (id, type, pre) => arrayDecl("varying", type, pre.v + id),
24
+ fs: (id, type, pre) => arrayDecl("varying", type, pre.v + id)
33
25
  },
34
- /**
35
- * WebGL 2 (GLSL ES 3)
36
- */
37
- [GLSLVersion.GLES_300]: {
38
- number: 300,
39
- attrib: (id, type, pre) => isArray(type)
40
- ? `layout(location=${type[1]}) in ${type[0]} ${pre.a}${id};`
41
- : `in ${type} ${pre.a}${id};`,
42
- varying: {
43
- vs: (id, type, pre) => arrayDecl("out", type, pre.v + id),
44
- fs: (id, type, pre) => arrayDecl("in", type, pre.v + id),
45
- },
46
- uniform: (id, u, pre) => arrayDecl("uniform", u, pre.u + id),
47
- output: (id, type, pre) => isArray(type)
48
- ? `layout(location=${type[1]}) out ${type[0]} ${pre.o}${id};`
49
- : `out ${type} ${pre.o}${id};`,
26
+ uniform: (id, u, pre) => arrayDecl("uniform", u, pre.u + id),
27
+ output: (id, type, pre) => isArray(type) ? `#define ${pre.o}${id} gl_FragData[${type[1]}]` : ""
28
+ },
29
+ /**
30
+ * WebGL 2 (GLSL ES 3)
31
+ */
32
+ [GLSLVersion.GLES_300]: {
33
+ number: 300,
34
+ attrib: (id, type, pre) => isArray(type) ? `layout(location=${type[1]}) in ${type[0]} ${pre.a}${id};` : `in ${type} ${pre.a}${id};`,
35
+ varying: {
36
+ vs: (id, type, pre) => arrayDecl("out", type, pre.v + id),
37
+ fs: (id, type, pre) => arrayDecl("in", type, pre.v + id)
50
38
  },
39
+ uniform: (id, u, pre) => arrayDecl("uniform", u, pre.u + id),
40
+ output: (id, type, pre) => isArray(type) ? `layout(location=${type[1]}) out ${type[0]} ${pre.o}${id};` : `out ${type} ${pre.o}${id};`
41
+ }
51
42
  };
52
43
  const arrayDecl = (qualifier, decl, id) => {
53
- const type = isArray(decl) ? decl[0] : decl;
54
- return type.indexOf("[]") > 0
55
- ? `${qualifier} ${type.replace("[]", "")} ${id}[${decl[1]}];`
56
- : `${qualifier} ${type} ${id};`;
44
+ const type = isArray(decl) ? decl[0] : decl;
45
+ return type.indexOf("[]") > 0 ? `${qualifier} ${type.replace("[]", "")} ${id}[${decl[1]}];` : `${qualifier} ${type} ${id};`;
57
46
  };
58
- /**
59
- * GLSL preprocessor macro for conditional execution based on `__VERSION__`.
60
- *
61
- * @param ver -
62
- * @param ok -
63
- * @param fail -
64
- */
65
- export const VERSION_CHECK = (ver, ok, fail = "") => {
66
- let cmp = ">=";
67
- if (!ok) {
68
- ok = fail;
69
- fail = null;
70
- cmp = "<";
71
- }
72
- return `#if __VERSION__ ${cmp} ${ver}
73
- ${ok}${fail ? `\n#else\n${fail}` : ""}
47
+ const VERSION_CHECK = (ver, ok, fail = "") => {
48
+ let cmp = ">=";
49
+ if (!ok) {
50
+ ok = fail;
51
+ fail = null;
52
+ cmp = "<";
53
+ }
54
+ return `#if __VERSION__ ${cmp} ${ver}
55
+ ${ok}${fail ? `
56
+ #else
57
+ ${fail}` : ""}
74
58
  #endif`;
75
59
  };
76
- export const ALIAS_TEXTURE = VERSION_CHECK(300, "", "#define texture texture2D");
77
- /**
78
- * GLSL version specific fragment shader output. If `__VERSION__ >= 300`
79
- * assigns `body` to `out`, else to `gl_FragColor`.
80
- *
81
- * @param body -
82
- * @param out -
83
- */
84
- export const EXPORT_FRAGCOL = (body = "col", out = "o_fragColor") => VERSION_CHECK(300, `${out}=${body};`, `gl_FragColor=${body};`);
85
- /**
86
- * Default GLSL prelude.
87
- */
88
- export const GLSL_HEADER = `#ifdef GL_FRAGMENT_PRECISION_HIGH
60
+ const ALIAS_TEXTURE = VERSION_CHECK(
61
+ 300,
62
+ "",
63
+ "#define texture texture2D"
64
+ );
65
+ const EXPORT_FRAGCOL = (body = "col", out = "o_fragColor") => VERSION_CHECK(300, `${out}=${body};`, `gl_FragColor=${body};`);
66
+ const GLSL_HEADER = `#ifdef GL_FRAGMENT_PRECISION_HIGH
89
67
  precision highp int;
90
68
  precision highp float;
91
69
  #else
@@ -102,3 +80,12 @@ precision mediump float;
102
80
  #define HALF_PI 1.570796326794896
103
81
  #endif
104
82
  `;
83
+ export {
84
+ ALIAS_TEXTURE,
85
+ EXPORT_FRAGCOL,
86
+ GLSL_HEADER,
87
+ NO_PREFIXES,
88
+ PREFIXES,
89
+ SYNTAX,
90
+ VERSION_CHECK
91
+ };