gpt4 book ai didi

java - libGDX 着色器坐标和相机位置

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:59:06 24 4
gpt4 key购买 nike

我正在使用 libGDX、Tiled Map 和 Box2D 开发平台游戏。我想应用一些着色器。

我想要一个贴在英雄身上的晕影效果。对于晕影效果,我使用了 this tutorial .我遇到的问题是:如果相机是固定的,我得到的正是我想要的。小插图坚持英雄。但是,如果摄像机移动(这发生在滚动平台游戏中),晕影效果就不会粘在英雄身上。

Here is a video that illustrates my problem

看来我必须考虑相机位置来计算晕影效果的坐标,但我不知道如何。这是我用于小插图的片段着色器:

varying vec4 v_color;   
varying vec2 v_texCoord0;

uniform vec2 u_resolution;
uniform float u_PosX, u_PosY;
uniform sampler2D u_sampler2D;

const float outerRadius = 10, innerRadius = 1, intensity = .5;

void main(){
vec4 color = texture2D(u_sampler2D, v_texCoord0) * v_color;

vec2 relativePosition = gl_FragCoord.xy / u_resolution - vec2(u_PosX, u_PosY); //Position of the vignette
relativePosition.x *= u_resolution.x / u_resolution.y; //The vignette is a circle
float len = length(relativePosition);
float vignette = smoothstep(outerRadius, innerRadius, len);
color.rgb = mix(color.rgb, color.rgb * vignette, intensity);

gl_FragColor = color;
}

在游戏画面中,相关代码为:

public TestScreen(final MyGdxGame gam){    

camera = new MyCamera();
camera.setToOrtho(false, width, height);
camera.update();

vertexShader = Gdx.files.internal("Shaders/vVignette.glsl").readString();
fragmentShader = Gdx.files.internal("Shaders/fVignette.glsl").readString();
shaderProgram = new ShaderProgram(vertexShader,fragmentShader);

shaderProgram.begin();
shaderProgram.setUniformf("u_resolution", camera.viewportWidth, camera.viewportHeight);
shaderProgram.end();
}


public void render(float delta) {
Gdx.gl.glClearColor(0.1f,0.1f,0.1f,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();

//calculation of the position of the effect depending on the position of the hero
posX = (Gdx.graphics.getWidth()/camera.viewportWidth) * hero.bodyHero.getPosition().x/camera.viewportWidth;
posY = (Gdx.graphics.getHeight()/camera.viewportHeight) * hero.bodyHero.getPosition().y/camera.viewportHeight;

shaderProgram.begin();
shaderProgram.setUniformf("u_PosX", posX);
shaderProgram.setUniformf("u_PosY", posY);
shaderProgram.end();
}

正如我所说,如果相机不移动,此代码将完美运行。但是由于当相机移动时它不起作用,我认为问题与相机投影有关,我在 render() 中尝试了这段代码,但没有取得更好的成功 :

Vector3 PosHero = new Vector3();
PosHero.set(hero.bodyHero.getPosition().x, hero.bodyHero.getPosition().y, 0);
camera.unproject(PosHero);

posX = (Gdx.graphics.getWidth()/camera.viewportWidth) * PosHero.x/camera.viewportWidth;
posY = (Gdx.graphics.getHeight()/camera.viewportHeight) * PosHero.y/camera.viewportHeight;

shaderProgram.begin();
shaderProgram.setUniformf("u_PosX", posX);
shaderProgram.setUniformf("u_PosY", posY);
shaderProgram.end();

最佳答案

天哪...我为此苦苦挣扎了将近 2 周,我的问题是一个简单的错误:

正如我所想,问题与相机投影有关。在我在消息末尾提供的代码中,我使用了 camera.unproject(PosHero);,但我需要改用 camera.project(PosHero);!

最后是我用来为片段着色器提供正确坐标的代码:

Vector3 PosHero = new Vector3();
PosHero.set(hero.bodyHero.getPosition().x, hero.bodyHero.getPosition().y, 0);
camera.project(PosHero);

posX = PosHero.x/camera.viewportWidth;
posY = PosHero.y/camera.viewportHeight;

shaderProgram.begin();
shaderProgram.setUniformf("u_PosX", posX);
shaderProgram.setUniformf("u_PosY", posY);
shaderProgram.end();

关于java - libGDX 着色器坐标和相机位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33460392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com