我正在用shaperenderer的rect绘制颜色渐变。 这样会产生柔和的渐变。
是否有一种量化颜色的方法。例如,我喜欢最小颜色和最大颜色之间只有10种不同的颜色。
是否可以通过LIBGDX的预定义功能而无需自行绘制?
答案 0 :(得分:0)
一种实现此目标的方法(如果可以节省CPU周期)可以是手动滚动ShapeRenderer::rect
的版本,该版本可以通过绘制多个矩形并在四种颜色之间进行插值来实现。
private static Color quantizedColor = new Color();
private static void rectQuantized(ShapeRenderer shapeRenderer, float x, float y, float width, float height, Color color1, Color color2, Color color3, Color color4, int steps) {
float dx = width / steps;
float dy = height / steps;
for(int sy = 0; sy < steps; ++sy) {
float py = (float)sy / (steps - 1);
for(int sx = 0; sx < steps; ++sx) {
float px = (float)sx / (steps - 1);
float minR = (1.0f - px) * color1.r + px * color2.r;
float maxR = (1.0f - px) * color4.r + px * color3.r;
float minG = (1.0f - px) * color1.g + px * color2.g;
float maxG = (1.0f - px) * color4.g + px * color3.g;
float minB = (1.0f - px) * color1.b + px * color2.b;
float maxB = (1.0f - px) * color4.b + px * color3.b;
float minA = (1.0f - px) * color1.a + px * color2.a;
float maxA = (1.0f - px) * color4.a + px * color3.a;
quantizedColor.set(minR + (maxR - minR) * py, minG + (maxG - minG) * py, minB + (maxB - minB) * py, minA + (maxA - minA) * py);
shapeRenderer.setColor(quantizedColor);
shapeRenderer.rect(x + dx * sx, y + dy * sy, dx, dy);
}
}
}
使用上面的代码(将200x200 rect量化为16步)看起来像这样(使用左侧的标准ShapeRenderer::rect
调用进行比较)
上述的完全正常运行的 libGDX 示例:
package com.bornander.sandbox;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTile;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
public class SandboxGdxGame extends ApplicationAdapter {
private ShapeRenderer shapeRenderer;
private OrthographicCamera camera;
@Override
public void create () {
shapeRenderer = new ShapeRenderer();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
private static Color quantizedColor = new Color();
private static void rectQuantized(ShapeRenderer shapeRenderer, float x, float y, float width, float height, Color color1, Color color2, Color color3, Color color4, int steps) {
float dx = width / steps;
float dy = height / steps;
for(int sy = 0; sy < steps; ++sy) {
float py = (float)sy / (steps - 1);
for(int sx = 0; sx < steps; ++sx) {
float px = (float)sx / (steps - 1);
float minR = (1.0f - px) * color1.r + px * color2.r;
float maxR = (1.0f - px) * color4.r + px * color3.r;
float minG = (1.0f - px) * color1.g + px * color2.g;
float maxG = (1.0f - px) * color4.g + px * color3.g;
float minB = (1.0f - px) * color1.b + px * color2.b;
float maxB = (1.0f - px) * color4.b + px * color3.b;
float minA = (1.0f - px) * color1.a + px * color2.a;
float maxA = (1.0f - px) * color4.a + px * color3.a;
quantizedColor.set(minR + (maxR - minR) * py, minG + (maxG - minG) * py, minB + (maxB - minB) * py, minA + (maxA - minA) * py);
shapeRenderer.setColor(quantizedColor);
shapeRenderer.rect(x + dx * sx, y + dy * sy, dx, dy);
}
}
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.Filled);
rectQuantized(shapeRenderer, 0, -100, 200, 200, Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, 16);
shapeRenderer.rect(-200, -100, 200, 200, Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
shapeRenderer.end();
}
}