Skip to content

Commit

Permalink
refactor(webgl): use shader type consts
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 6, 2024
1 parent 7dfbb81 commit e9f8c4c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 53 deletions.
7 changes: 4 additions & 3 deletions packages/webgl/src/material.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { V3 } from "@thi.ng/shader-ast/api/types";
import type { GLSL } from "./api/glsl.js";
import type { Material } from "./api/material.js";
import type { ShaderUniformSpecs } from "./api/shader.js";
Expand All @@ -9,9 +10,9 @@ export const DEFAULT_MATERIAL: Material = {
};

const TYPES: Record<keyof Material, GLSL> = {
ambientCol: "vec3",
diffuseCol: "vec3",
specularCol: "vec3",
ambientCol: V3,
diffuseCol: V3,
specularCol: V3,
};

export const defMaterial = (
Expand Down
13 changes: 4 additions & 9 deletions packages/webgl/src/multipass.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IObjectOf } from "@thi.ng/api";
import { assert } from "@thi.ng/errors/assert";
import { S2D, V2, V4 } from "@thi.ng/shader-ast/api/types";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { defMain } from "@thi.ng/shader-ast/ast/function";
import { INT0, ivec2 } from "@thi.ng/shader-ast/ast/lit";
Expand Down Expand Up @@ -122,16 +123,13 @@ const initShader = (
vs: pass.vs || PASSTHROUGH_VS,
fs: pass.fs,
attribs: pass.attribs || {
position: "vec2",
position: V2,
},
varying: pass.varying,
uniforms: <ShaderUniformSpecs>{
...pass.uniforms,
...transduce(
map(
(i) =>
<[string, UniformDecl]>[`input${i}`, ["sampler2D", i]]
),
map((i) => <[string, UniformDecl]>[`input${i}`, [S2D, i]]),
assocObj(),
range(numIns)
),
Expand All @@ -140,10 +138,7 @@ const initShader = (
? transduce(
map(
(i) =>
<[string, ShaderOutputSpec]>[
`output${i}`,
["vec4", i],
]
<[string, ShaderOutputSpec]>[`output${i}`, [V4, i]]
),
assocObj(),
range(numOuts)
Expand Down
35 changes: 17 additions & 18 deletions packages/webgl/src/shaders/lambert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "@thi.ng/shader-ast-stdlib/light/lambert";
import { transformMVP } from "@thi.ng/shader-ast-stdlib/matrix/mvp";
import { surfaceNormal } from "@thi.ng/shader-ast-stdlib/matrix/normal";
import { M4, S2D, V2, V3 } from "@thi.ng/shader-ast/api/types";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { defMain } from "@thi.ng/shader-ast/ast/function";
import { vec4 } from "@thi.ng/shader-ast/ast/lit";
Expand Down Expand Up @@ -66,32 +67,30 @@ export const LAMBERT = (opts: Partial<LambertOpts> = {}): ShaderSpec => ({
],
// pre: ALIAS_TEXTURE,
attribs: {
position: "vec3",
normal: "vec3",
...(opts.uv ? { [opts.uv]: "vec2" } : null),
...(opts.color && !opts.instanceColor
? { [opts.color]: "vec3" }
: null),
...(opts.instancePos ? { [opts.instancePos]: "vec3" } : null),
...(opts.instanceColor ? { [opts.instanceColor]: "vec3" } : null),
position: V3,
normal: V3,
...(opts.uv ? { [opts.uv]: V2 } : null),
...(opts.color && !opts.instanceColor ? { [opts.color]: V3 } : null),
...(opts.instancePos ? { [opts.instancePos]: V3 } : null),
...(opts.instanceColor ? { [opts.instanceColor]: V3 } : null),
},
varying: {
vcolor: "vec3",
vnormal: "vec3",
...(opts.uv ? { vuv: "vec2" } : null),
vcolor: V3,
vnormal: V3,
...(opts.uv ? { vuv: V2 } : null),
},
uniforms: {
model: "mat4",
view: "mat4",
proj: "mat4",
normalMat: ["mat4", autoNormalMatrix2()],
lightDir: ["vec3", [0, 1, 0]],
lightCol: ["vec3", [1, 1, 1]],
model: M4,
view: M4,
proj: M4,
normalMat: [M4, autoNormalMatrix2()],
lightDir: [V3, [0, 1, 0]],
lightCol: [V3, [1, 1, 1]],
...defMaterial(
{ diffuseCol: [1, 1, 1], ...opts.material },
{ specularCol: false }
),
...(opts.uv ? { tex: "sampler2D" } : null),
...(opts.uv ? { tex: S2D } : null),
},
state: {
depth: true,
Expand Down
37 changes: 18 additions & 19 deletions packages/webgl/src/shaders/phong.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Sym } from "@thi.ng/shader-ast";
import { F, M4, V3 } from "@thi.ng/shader-ast/api/types";
import { diffuseLighting } from "@thi.ng/shader-ast-stdlib/light/lambert";
import { surfaceNormal } from "@thi.ng/shader-ast-stdlib/matrix/normal";
import { assign } from "@thi.ng/shader-ast/ast/assign";
Expand Down Expand Up @@ -80,29 +81,27 @@ export const PHONG = (opts: Partial<PhongOpts> = {}): ShaderSpec => ({
}),
],
attribs: {
position: "vec3",
normal: "vec3",
...(opts.color && !opts.instanceColor
? { [opts.color]: "vec3" }
: null),
...(opts.instancePos ? { [opts.instancePos]: "vec3" } : null),
...(opts.instanceColor ? { [opts.instanceColor]: "vec3" } : null),
position: V3,
normal: V3,
...(opts.color && !opts.instanceColor ? { [opts.color]: V3 } : null),
...(opts.instancePos ? { [opts.instancePos]: V3 } : null),
...(opts.instanceColor ? { [opts.instanceColor]: V3 } : null),
},
varying: {
vnormal: "vec3",
veye: "vec3",
vlight: "vec3",
vcolor: "vec3",
vnormal: V3,
veye: V3,
vlight: V3,
vcolor: V3,
},
uniforms: {
model: "mat4",
normalMat: ["mat4", autoNormalMatrix1()],
view: "mat4",
proj: "mat4",
shininess: ["float", 32],
eyePos: "vec3",
lightPos: ["vec3", [0, 0, 2]],
lightCol: ["vec3", [1, 1, 1]],
model: M4,
normalMat: [M4, autoNormalMatrix1()],
view: M4,
proj: M4,
shininess: [F, 32],
eyePos: V3,
lightPos: [V3, [0, 0, 2]],
lightCol: [V3, [1, 1, 1]],
...defMaterial(opts.material),
},
state: {
Expand Down
9 changes: 5 additions & 4 deletions packages/webgl/src/shaders/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { S2D, V2 } from "@thi.ng/shader-ast/api/types";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { defMain } from "@thi.ng/shader-ast/ast/function";
import { FLOAT0, FLOAT1, vec4 } from "@thi.ng/shader-ast/ast/lit";
Expand Down Expand Up @@ -27,7 +28,7 @@ export const PASSTHROUGH_FS_UV: ShaderFn = (_, unis, ins, outs) => [
export const FX_SHADER_SPEC: ShaderSpec = {
vs: PASSTHROUGH_VS,
fs: PASSTHROUGH_FS,
attribs: { position: "vec2" },
attribs: { position: V2 },
varying: {},
uniforms: {},
state: { depth: false },
Expand All @@ -37,9 +38,9 @@ export const FX_SHADER_SPEC: ShaderSpec = {
export const FX_SHADER_SPEC_UV: ShaderSpec = {
vs: PASSTHROUGH_VS_UV,
fs: PASSTHROUGH_FS_UV,
attribs: { position: "vec2", uv: "vec2" },
varying: { v_uv: "vec2" },
uniforms: { tex: "sampler2D" },
attribs: { position: V2, uv: V2 },
varying: { v_uv: V2 },
uniforms: { tex: S2D },
state: { depth: false },
ext: {},
};

0 comments on commit e9f8c4c

Please sign in to comment.