2015-01-30 5 views
0

Я делаю пошаговую игру с Libgdx.Наложение огней с шейдерами в игре LibGDX

Я пытаюсь создать туман войны для битв, добавив маску на карту и карту на каждую ячейку в зоне боя.

Для этого мне нужно наложить огни, но я не могу.

Результат в игре:

enter image description here

Java-код визуализации:

//draw the light to the FBO 
fbo.begin(); 
batch.setProjectionMatrix(cam.combined); 
batch.setShader(defaultShader); 
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

batch.begin(); 
float lightSize = lightOscillate? (4.75f + 0.25f * (float)Math.sin(zAngle) + .2f*MathUtils.random()):5.0f; 
//Draw light 1 
batch.draw(light, 0, 0, lightSize, lightSize); 

//Draw light 2 
batch.draw(light, 2, 2, lightSize, lightSize); 

batch.end(); 
fbo.end(); 

//draw the actual scene 
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
batch.setProjectionMatrix(cam.combined); 
batch.setShader(finalShader); 
batch.begin(); 
fbo.getColorBufferTexture().bind(1); //this is important! bind the FBO to the 2nd texture unit 
light.bind(0); //we force the binding of a texture on first texture unit to avoid artefacts 
      //this is because our default and ambiant shader dont use multi texturing... 
      //youc can basically bind anything, it doesnt matter 
tilemap.render(batch, dt); 
batch.end(); 

фрагмент кода шейдера:

varying LOWP vec4 vColor; 
varying vec2 vTexCoord; 

//texture samplers 
uniform sampler2D u_texture; //diffuse map 
uniform sampler2D u_lightmap; //light map 

//additional parameters for the shader 
uniform vec2 resolution; //resolution of screen 
uniform LOWP vec4 ambientColor; //ambient RGB, alpha channel is intensity 

void main() { 
    vec4 diffuseColor = texture2D(u_texture, vTexCoord); 
    vec2 lighCoord = gl_FragCoord.xy/resolution.xy; 
    vec4 light = texture2D(u_lightmap, lighCoord); 

    vec3 ambient = ambientColor.rgb * ambientColor.a; 
    vec3 intensity = ambient + light.rgb; 
    vec3 finalColor = diffuseColor.rgb * intensity; 

    gl_FragColor = vColor * light; //vec4(finalColor, diffuseColor.a); 
} 
+0

Похоже, что вам нужно включить альфа-смешивание. – user3256930

+0

Я попытался добавить 'Gdx.gl20.glEnable (GL20.GL_BLEND); \t \t Gdx.gl20.glBlendFunc (GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); 'после batch.begin() и метода create(), но он ничего не делает. – OmiCroh

ответ

0

Я нашел решение. Необходимо было включить альфа-смешение.

Здесь правильно делают код:

public void render() { 
    final float dt = Gdx.graphics.getRawDeltaTime(); 

    Gdx.gl.glClearColor(0f,0f,0f,1f); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

    batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE); 

    //draw the light to the FBO 
    fbo.begin(); 
    Gdx.gl.glClearColor(0f,0f,0f,0f); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     batch.setProjectionMatrix(cam.combined); 
     batch.setShader(defaultShader); 

     batch.begin(); 
     float lightSize = lightOscillate? (4.75f + 0.25f * (float)Math.sin(zAngle) + .2f*MathUtils.random()):5.0f; 
     //Draw light 1 
     batch.draw(light, 0, 0, lightSize, lightSize); 

     //Draw light 2 
     batch.draw(light, 2, 2, lightSize, lightSize); 

     batch.end(); 
    fbo.end(); 

    batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); 

    //draw the actual scene 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    batch.setProjectionMatrix(cam.combined); 
    batch.setShader(finalShader); 
    batch.begin(); 
    fbo.getColorBufferTexture().bind(1); //this is important! bind the FBO to the 2nd texture unit 
    light.bind(0); //we force the binding of a texture on first texture unit to avoid artefacts 
        //this is because our default and ambiant shader dont use multi texturing... 
        //youc can basically bind anything, it doesnt matter 
    tilemap.render(batch, dt); 
    batch.end(); 
} 
Смежные вопросы