Drawing a gradient in Libgdx(在 Libgdx 中绘制渐变)
问题描述
好的,我有这个代码
@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:
4个颜色参数指定矩形左下角、右下角、右上角和左上角的颜色,让您可以创建渐变.
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.
这篇关于在 Libgdx 中绘制渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Libgdx 中绘制渐变
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01