我想用LibGDX制作复古风格的小游戏,我想让玩家选择几个字符的颜色,所以我想了想加载png索引图像,然后以编程方式更新调色板...我错过了多少错误^^用OpenGL着色器模拟调色板互换(在LibGDX中)
看来,颜色托盘是过去的东西,而且似乎最好的选择来实现类似的结果是通过使用着色器。
这里是一个图像解释什么,我想现在:
我的本意是用2个图像。其中之一,pixel_guy.png
是一个只有6种颜色(这些颜色是它的原始调色板)的PNG图像。另一个图像colortable.png
将是一个6x6像素的png,它包含6个颜色的6个调色板(每行是不同的调色板)。来自第一行像素colortable.png
的颜色将与pixel_guy.png
中使用的颜色相匹配,这将是第一个/原始调色板,其他行将是第2到第6个调色板。我尝试实现的是使用colortable的第一个调色板索引pixelguy颜色,然后通过向着色器发送一个数字(从2到6)来更改调色板。
经过一番研究,我发现了一个post in gamedev stackexchange,显然这是我在找的东西,所以我试着去测试它。
我创建了顶点和片段着色器,并加载了我的纹理(我想要交换的调色板和包含多个调色板的纹理),但输出是意外的白色图像。
我的顶点着色器:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
}
我的片段着色器:
// Fragment shader
// Thanks to Zack The Human https://gamedev.stackexchange.com/questions/43294/creating-a-retro-style-palette-swapping-effect-in-opengl/
uniform sampler2D texture; // Texture to which we'll apply our shader? (should its name be u_texture?)
uniform sampler2D colorTable; // Color table with 6x6 pixels (6 palettes of 6 colors each)
uniform float paletteIndex; // Index that tells the shader which palette to use (passed here from Java)
void main()
{
vec2 pos = gl_TexCoord[0].xy;
vec4 color = texture2D(texture, pos);
vec2 index = vec2(color.r + paletteIndex, 0);
vec4 indexedColor = texture2D(colorTable, index);
gl_FragColor = indexedColor;
}
我用来使纹理结合和通过调色板号码到着色器中的代码:
package com.test.shaderstest;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
public class ShadersTestMain extends ApplicationAdapter {
SpriteBatch batch;
Texture imgPixelGuy;
Texture colorTable;
private ShaderProgram shader;
private String shaderVertIndexPalette, shaderFragIndexPalette;
@Override
public void create() {
batch = new SpriteBatch();
imgPixelGuy = new Texture("pixel_guy.png"); // Texture to which we'll apply our shader
colorTable = new Texture("colortable.png"); // Color table with 6x6 pixels (6 palettes of 6 colors each)
shaderVertIndexPalette = Gdx.files.internal("shaders/indexpalette.vert").readString();
shaderFragIndexPalette = Gdx.files.internal("shaders/indexpalette.frag").readString();
ShaderProgram.pedantic = false; // important since we aren't using some uniforms and attributes that SpriteBatch expects
shader = new ShaderProgram(shaderVertIndexPalette, shaderFragIndexPalette);
if(!shader.isCompiled()) {
System.out.println("Problem compiling shader :(");
}
else{
batch.setShader(shader);
System.out.println("Shader applied :)");
}
shader.begin();
shader.setUniformi("colorTable", 1); // Set an uniform called "colorTable" with index 1
shader.setUniformf("paletteIndex", 2.0f); // Set a float uniform called "paletteIndex" with a value 2.0f, to select the 2nd palette
shader.end();
colorTable.bind(1); // We bind the texture colorTable to the uniform with index 1 called "colorTable"
}
@Override
public void render() {
Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(imgPixelGuy, 0, 0); // Draw the image with the shader applied
batch.end();
}
}
我不知道这是否是将float值传递给片段统一的正确方法。不知道如何使用我尝试使用的代码片段。
编辑:我试图通过TenFour04建议的修改和他们的工作完美:)
这里是预处理精灵
我已经更新了更改的存储库, (Java代码here,片段着色器here),如果有人有兴趣,可以在这里下载:https://bitbucket.org/hcito/libgdxshadertest
编辑2:我刚刚添加到版本库TenFour04建议的最后优化(将调色板索引传递给调用sprite.setColor()方法的R通道中的每个精灵),并且它再次运行完美:)
为什么这会降低选票? – Tenfour04 2014-10-01 12:20:14
不知道:/我知道我的英语不太好,而且我有点笨拙,但我真的很想在这里尽我所能^^ U非常感谢你帮助我,Tenfour :)(顺便说一下,我需要5点声望upvote你的答案^^ U) – hcito 2014-10-01 18:34:45