Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Added support for arrays in uniforms. (#814)
Browse files Browse the repository at this point in the history
* Added support for arrays in uniforms.

* Update gfx.ts

removed the semicolon...

* Update types.ts

---------

Co-authored-by: nanopoison <[email protected]>
  • Loading branch information
tristanperrow and tristanperrow authored Jan 6, 2024
1 parent 62e1f9a commit 79422c2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### v3000.2.1
- added support for arrays in uniforms

## v3000.2
- added `loadMusic()` to load streaming audio (doesn't block in loading screen)

Expand Down
14 changes: 13 additions & 1 deletion src/gfx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,19 @@ export class Shader {
gl.uniform3f(loc, val.r, val.g, val.b)
} else if (val instanceof Vec2) {
gl.uniform2f(loc, val.x, val.y)
}
} else if (Array.isArray(val)) {
const first = val[0]
if (typeof first === "number") {
gl.uniform1fv(loc, val)
} else if (first instanceof Vec2) {
gl.uniform2fv(loc, val.map(v => [v.x, v.y]).flat())
} else if (first instanceof Color) {
gl.uniform3fv(loc, val.map(v => [v.r, v.g, v.b]).flat())
}
} else if (typeof val === "object") {
throw new Error("Unsupported uniform data type.")
// TODO allow for struct like objects to be sent as a uniform
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4882,6 +4882,9 @@ export type UniformValue =
| Vec2
| Color
| Mat4
| number[]
| Vec2[]
| Color[]

export type UniformKey = Exclude<string, "u_tex">
export type Uniform = Record<UniformKey, UniformValue>
Expand Down

0 comments on commit 79422c2

Please sign in to comment.