Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] SetObjectShaderConstantByName() for 2D sprites?

Author
Message
TomToad
6
Years of Service
User Offline
Joined: 6th Jan 2018
Location:
Posted: 19th Dec 2018 23:58
I have several sprites that share the same shader, but the uniform variable needs a different value for each sprite. I see that 3D objects have SetObectShaderConstantByName() command, but don't see one for sprites. I can either make the sprite visible, change the shader with SetShaderConstantByName(), then DrawSprite() then hide the sprite again so it doesn't cover what's drawn. Or I can load a copy of the shader for each sprite and change each copy individually. Both ways drastically affect performance. Is there something I'm missing here?

The author of this post has marked a post as an answer.

Go to answer

puzzler2018
User Banned
Posted: 20th Dec 2018 00:06 Edited at: 20th Dec 2018 00:09
Sprites are 2D not 3D - or my shader knowledge is zilch still
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 20th Dec 2018 05:18 Edited at: 20th Dec 2018 05:31
I wrote a few shaders for my defense game. All of them are applied to sprites.
To set the values I use SetShaderConstantByName(shaderID, "shaderConstantName",value1, value2, value3, value4) and it works perfectly.
TomToad
6
Years of Service
User Offline
Joined: 6th Jan 2018
Location:
Posted: 20th Dec 2018 07:11
@GarBenjamin
I am aware of the SetShaderConstantByName() function. The problem is that it cannot be set for individual sprites. For example, sprite 1 might need the value1 set to 10, sprite 2 need it set to 15, etc... If you try and set it to individual sprites, only the last value used will be in effect when calling Sync().
3d objects have SetObjectShaderConstantByName(), which sets the values for each 3d object individually, I don't see an equivalent function for sprite shaders.
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 20th Dec 2018 11:16
In fact a sprite uses everything a 3D object uses so you could use a plane instead and set it up with loadshader but use a orthographic projection matrix and maybe use a few uniforms from sprites.
It's a bit inconvinient but with the options you described above its everything we can do right now.
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 20th Dec 2018 13:29 Edited at: 20th Dec 2018 18:15
Oh right Tom. Yeah I don't know anyway around that. For my use I just created multiple shaders. Of course not the ideal way but like Janbo said I think it is the only one of a couple options we have.

There might be way some way to assign values only to a specific instance of a shader on a specific sprite but I didn't find it if there is. Couldn't waste time on it so did what I needed to move on.

Might be a good feature request.
TomToad
6
Years of Service
User Offline
Joined: 6th Jan 2018
Location:
Posted: 21st Dec 2018 15:49 Edited at: 21st Dec 2018 15:51
This post has been marked by the post author as the answer.
Found a workaround. I can use SetSpriteAdditionalImage() to send information to the shader using the image as a type of 2D array. Only problem is that I would have to piece each component together if I need values outside of the 0.0-1.0 range. The performance is slightly slower than using SetShaderConstantByName, but far better than any of the other solutions I tried.

For those who are interested, here is an example I created showing this solution.
check.ps

AGK code
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 21st Dec 2018 21:58
Very clever!

Login to post a reply

Server time is: 2024-11-23 08:56:06
Your offset time is: 2024-11-23 08:56:06