绘制在Libgdx渐变Libgdx

2023-09-06 01:29:48 作者:乱可爱一把

好吧,我有这个code

Ok I have this code

@Override
public void render() {
    // do not update game world when paused
    if (!paused) {
        // Update game world by the time that has passed
        // since last render frame
        worldController.update(Gdx.graphics.getDeltaTime());
    }
    // Sets the clear screen color to: Cornflower Blue
    Gdx.gl.glClearColor(0x64/255.0f, 0x95/255.0f, 0xed/255.0f,
            0xff/255.0f);
    // Clears the screen
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    // Render game world to screen
    worldRenderer.render();
}

和它绘制一个浅蓝色背景到屏幕上。我试图创建一个梯度,它从一个深蓝色的顶部,以向底部浅蓝色。有没有一种简单的方法来做到这一点?我是新来Libgdx,和OpenGL所以我想从一本书来学习,但我似乎无法找到这个问题的答案之一。我听说画一个大广场和具有顶点不同颜色的,但我不确定如何做到这一点。

And it draws a light blue background onto the screen. I am attempting to create a gradient that goes from a dark blue at the top, to a light blue towards the bottom. Is there a simple way to do this? I'm new to Libgdx, and OpenGL so i'm trying to learn from a book but I can't seem to find the answer to this one. I've heard of drawing a big square and having the vertices different colors, but I'm unsure of how to do this.

推荐答案

在libGDX中,ShapeRenderer对象包含一个的drawRect()方法,它的参数为它的位置和大小还有四种颜色。这些颜色被转换为4角梯度。如果你想有一个垂直梯度,才使上角一种颜色和底角另一种颜色。事情是这样的:

In libGDX, the ShapeRenderer object contains a drawRect() method that takes arguments for its position and size as well as four colors. Those colors are converted to a 4-corners gradient. If you want a vertical gradient, just make the top corners one color and the bottom corners another color. Something like this:

shapeRenderer.filledRect(x, y, width, height, lightBlue, lightBlue, darkBlue, darkBlue);

这是为ShapeRenderer的API:

From the API for ShapeRenderer:

四色参数指定颜色为左下,右下,右上和矩形的左上角,允许您创建渐变。

The 4 color parameters specify the color for the bottom left, bottom right, top right and top left corner of the rectangle, allowing you to create gradients.