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

Is it possible to pass sprite resolution to the shader? #298

Open
Syjgin opened this issue Apr 12, 2021 · 0 comments
Open

Is it possible to pass sprite resolution to the shader? #298

Syjgin opened this issue Apr 12, 2021 · 0 comments

Comments

@Syjgin
Copy link

Syjgin commented Apr 12, 2021

I know, that project development is stopped, but I need fix a legacy code: draw Vignette effect programmatically on the sprite. I started from adaptation this shader: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson3 without sepia and other effects, but still not know, how to pass "resolution" vector analog. Currently, I'm passing sprite's width & height, but result light spot is not on center. Maybe, there are some another way to get normalized texture coordinates?
Shader is copied from PositionColorTextureCoordinatesShaderProgram
Fragment shader is defined as follows:

"precision lowp float;\n" +
                "uniform sampler2D " + ShaderProgramConstants.UNIFORM_TEXTURE_0 + ";\n" +
                "uniform vec2 " + ShaderProgramConstants.UNIFORM_RESOLUTION + ";\n" +
                "varying lowp vec4 " + ShaderProgramConstants.VARYING_COLOR + ";\n" +
                "varying mediump vec2 " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + ";\n" +
                "void main() {\n" +
                "vec2 position = (gl_FragCoord.xy / " + ShaderProgramConstants.UNIFORM_RESOLUTION + ".xy) - vec2(0.5);" +
                "float dist = length(position);" +
                "float vignette = smoothstep(0.5, 0.01, dist);" +
                "vec4 texColor = texture2D(" + ShaderProgramConstants.UNIFORM_TEXTURE_0 + ", " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + ");" +
                "texColor.rgb = mix(texColor.rgb, texColor.rgb * vignette, 0.5);" +
                "gl_FragColor = " + ShaderProgramConstants.VARYING_COLOR + " * texColor;\n" +
                "}"

where UNIFORM_RESOLUTION is 2d array with sprite width & height bound. Maybe, I have to pass another values here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant