πŸ€‘ Indie game storeπŸ™Œ Free gamesπŸ˜‚ Fun games😨 Horror games
πŸ‘· Game development🎨 AssetsπŸ“š Comics
πŸŽ‰ Sales🎁 Bundles

The use of Uniforms (in Shaders)

A topic by JTE created 1 year ago Views: 539 Replies: 4
Viewing posts 1 to 5
(Edited 1 time)

I have a shader which requires knowledge of the image width and height in order to properly apply itself. So I add a vec2 uniform to hold such information, and ...

Oh. It looks like I'm going to have to duplicate this shader in full for every individual texture I want to apply it to because there is no way to change the uniforms at runtime. The TypeScript class for shaders is a big opaque dummy you can't do anything with, and the setup to apply a shader to a sprite in a scene or by script doesn't allow for any further inputs besides the shader path to apply.

:( Disappointment.

(+1)

Don't jump to conclusions too quickly :)

It's maybe not very clear (and it obviously lacks the documentation needed) but you can access uniforms on a spriteRenderer or a modelRenderer in code: `this.actor.spriteRenderer.getUniforms();`. This API is about to change but as v0.18.1, as soon as you modify the value you get, it will be applied to the shader.

In the next version, it will be `this.actor.spriteRenderer.uniforms.getFloat(nameofuniform);` or `this.actor.spriteRenderer.uniforms.setFloat(nameofuniform, value);` to match better the overall API in Superpowers.

Hopefully, that helps you!

(Edited 6 times)

Right... Thanks. Now I just have to figure out how to debug to discern what type of structure I'm dealing with here.

Edit: Ah... It seems I am dealing with a THREE.Vector2 ... Which is a type that TypeScript won't even let me create myself? But that means that I'm just looking to modify uniforms["textureSize"].value.x and y. Okay, I can live with that.

And in the next version it'll be more in line with the rest of the API and less raw direct access? Sounds good to me. :)

Edit2: Buuut now it looks like I've just moved from having to make a new copy of the shader for every texture, to having to make a new copy of the behavior script for every texture, because I can't find a way to get the sprite width and height...

Oh wait, isn't that more or less what the "Grid Size" is? Right, right, I guess that will have to do, as long as I stick to single-sprite textures for this shader it'll be fine...

Edit11: Actually I think it may be thrown off a bit by THREE.js automatically resizing the texture to be a power of two anyway... Gah. So many little troubles.

Yeah grid size is what you're looking for, you don't need to make several behaviors, you can do something like:

let size = this.actor.spriteRenderer.getGridSize(); // size.width and size.height

Just a quick thing regarding your first message: let's try and have a positive attitude when encountering limitations or confusing stuff in Superpowers. Rather than affirming that there's no way to do something and voicing disappointment, you could have more neutrally asked 1) whether it was possible or if it was planned 2) explaining why it matters to you. We're all in this together and if there's a bug or missing feature, it's always possible to look into adding it.

Edit11: Actually I think it may be thrown off a bit by THREE.js automatically resizing the texture to be a power of two anyway... Gah. So many little troubles.

Ah right, that's something we've talked of working around by storing the image in a power-of-two at runtime. Maybe! Here's an issue to discuss it, if anyone has input, please share.