nicolasgramlich / AndEngine

Free Android 2D OpenGL Game Engine
http://www.andengine.org
Apache License 2.0
3.18k stars 1.35k forks source link

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

Open Syjgin opened 3 years ago

Syjgin commented 3 years ago

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?