LibGDX - How to render two textures with different scales in one shader?

I am trying to scale a texture created in FBO with a different scale than another texture, and at the same time display both textures in the fragment shader so that I can combine them.

I followed this tutorial to create a light map for my game, and I got a light map that works great, but I want to convert the light map to a shader so that parallaxed objects in the background get a proportionally scaled light map, so that " The 3D "distance to the light source does not seem to affect their lighting, but their 2D distance does.

My best guess on how to do this is to scale the lightmap in place at a distance from the parallaxed object Z. The distance Z in my game is directly proportional to the distance of the 2D object to the camera.

Here are examples depicting what happens with a normal light mask, and what is the desired effect. The light map here consists of a yellow radial gradient located on the yellow, non-parallaxed "sun" on the right:

Lightmap scaling description

Animated Current Lighting Example

An animated example of alleged lighting.

Usually in my game I scale parallax objects by zooming in on a camera connected to SpriteBatch through something like this in LibGDX:

Array<Something> objects = new Array<Something>();
SpriteBatch batch = new SpriteBatch();
OrthographicCamera camera = new OrthographicCamera(screenWidth, screenHeight);

//{start loop, add objects, run logic, etc.}
...

for(int i=0;i<objects.size;i++){
   camera.zoom = objects.get(i).z;
   camera.update();
   batch.setProjectionMatrix(camera.combined);
   objects.get(i).sprite.draw(batch);
}

, , . LibGDX, :

void main() {
    v_color = a_color;
    v_texCoords = a_texCoord0; 

    gl_Position =  u_projTrans * a_position; 
}

, , , . , , :

void main() {
    vec4 diffuseColor = texture2D(u_texture, v_texCoords);

    vec2 lightCoord = (gl_FragCoord.xy / resolution.xy);
    vec4 light = texture2D(u_lightmap, lightCoord);

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

    gl_FragColor = v_color * vec4(finalColor, diffuseColor.a);
}

, , , OpenGL ES 2 GLSL .

-, ? , ?

Edit:

, , , FBO , (Android).

, , do , , , , SpriteBatch. , ? , ?

+3
1

, , , Something : , batch.setProjectionMatrix(), , , . z, batch.setProjectionMatrix(), z batch.draw(). , .

: -, , , . fbo.begin() , cam.zoom cam.zoom .

lightmap, . , , . :

//including the changes explained way above, where objectLayers is an 
//Array<LayerWrapper>, where LayerWrapper contains a zoom value and 
//an Array<Something>
for(int i=0;i<objectLayers.size;i++){
    LayerWrapper layer = objectLayers.get(i);
    Array<Something> layerObjects = layer.objects;

    camera.zoom = layer.zoom;
    camera.update();
    batch.setProjectionMatrix(camera.combined);

    //update the zoom level in the shader you're using with the sprite batch
    customShader.bind();
    customShader.setUniformf("u_zoom", layer.zoom);

    for(Something object : layerObjects ){
        object.sprite.draw(batch);
    }
}

, uniform float u_zoom; varying vec2 v_lightCoords;. . , , , (, ).

:

void main()
{
    v_color = a_color;
    v_texCoords = a_texCoord0;
    vec4 screenSpacePosition = u_projTrans * a_position;
    gl_Position =  screenSpacePosition;

    v_lightCoords = (screenSpacePosition.xy * u_zoom) * 0.5 + 0.5;
}

v_lightCoords , , :   vec4 Light = texture2D(u_lightmap, v_lightCoords); uniform vec2 resolution;, .

+1

All Articles