Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a convenient hook for Postprocessors to update #2416

Closed
eonarheim opened this issue Jul 16, 2022 · 4 comments · Fixed by #2667
Closed

Add a convenient hook for Postprocessors to update #2416

eonarheim opened this issue Jul 16, 2022 · 4 comments · Fixed by #2667
Labels
feature Label applied to new feature requests stale This issue or PR has not had any activity recently

Comments

@eonarheim
Copy link
Member

Context

Currently there is no lifecycle hook for postprocessors to update over time.

See discussion #2415

Proposal

Add some sort of hook that allows users to update uniforms after the shader has been bound.

Perhaps a onPreRender() or update? Not sure what makes the most sense?

@mattjennings
Copy link
Contributor

mattjennings commented Jul 16, 2022

I have personally been doing something like this:

class MyActor extends ex.Actor {
    effect = new SomePostProcessor()

    onInitialize(engine) {
          engine.graphicsContext.addPostProcessor(this.effect)
          const shader = this.effect.getShader()

          // set initial uniforms, otherwise shader errors
          shader.use()
          shader.setUniformFloat('u_time', 0)
    }

    onPreUpdate() {
          const shader = this.effect.getShader()
          shader.use()
          shader.setUniformFloat('u_time', someValue)
    }
}

which I honestly haven't minded to this point, but this is about as complex as my usage has gotten.

A lifecycle hook that allows me to set the uniforms once instead of both in initialize and an update loop would be nice. Possibly something that abstractsshader.use() when setting uniforms would be handy too (but I imagine there's good reason to have that called separately).

I guess another thought I have is, if u_time is a common thing to pass into shaders, should that be provided automatically as elapsed time of the shader?

@eonarheim
Copy link
Member Author

@mattjennings I like what you've done here! That's a pretty convenient work around at the moment.

I think it makes sense to have a shader specific lifecycle hook for the situation you describe. Perhaps something specific like onUpdate(...) which would be called every frame and be guaranteed to have shader.use() constext setup? And maybe a onInitialize(...) which would be called once?

It's probably worth including a few well known uniforms by default (definitely u_time for sure). I use ShaderToy a good deal to develop shader code, maybe borrowing some from their list would be useful? At minimum it would help when sourcing shader code into excalibur?

image

Things that look useful on first glance might be iTime, iTimeDelta, iMouse, iResoultion`?

I'd want to also document a way to include additional textures into the composition as well.

@mattjennings
Copy link
Contributor

That all sounds good to me!

A set of well-known uniforms from ShaderToy would be awesome. It seems to be the go-to for examples, and it would really help newcomers like myself with migrating to Excalibur.

@eonarheim eonarheim added the feature Label applied to new feature requests label Jul 28, 2022
@github-actions
Copy link

This issue hasn't had any recent activity lately and is being marked as stale automatically.

@github-actions github-actions bot added the stale This issue or PR has not had any activity recently label Sep 27, 2022
eonarheim added a commit that referenced this issue Jun 26, 2023
Closes #2416 

This PR adds some new default uniforms supplied to postprocessors automatically if they are present in the source
* `uniform float u_time_ms` - total playback time in milliseconds
* `uniform float u_elapsed_ms` - the elapsed time from the last frame in milliseconds
* `uniform vec2 u_resolution` - the resolution of the canvas (in pixels)

Custom uniforms can now be set in an optional `onUpdate()` function on postprocessors, the internal `shader.use()` is called for users.


Example using the CRT post processor, see gist https://gist.github.com/eonarheim/5ebb7f9161c10894935479e0caa0f6c7

<img width="677" alt="image" src="https://github.com/excaliburjs/Excalibur/assets/612071/07521591-934b-4aec-b77b-a01c8488025b">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Label applied to new feature requests stale This issue or PR has not had any activity recently
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants