복고 스타일의 작은 게임을 만들기 위해 LibGDX를 사용하려고합니다. 플레이어에게 여러 문자의 색상을 선택하게하고 싶습니다. PNG 인덱스 이미지로드 및 프로그래밍 방식으로 팔레트 업데이트 중 ... UOpenGL 쉐이더로 팔레트 바꾸기 시뮬레이션하기 (LibGDX에서)
컬러 팔레트가 과거와 비슷한 것처럼 보였습니다. 비슷한 결과를 얻는 가장 좋은 방법은 셰이더를 사용하는 것입니다 .
내 의도는이 개 이미지를 사용하는 것입니다
여기에 내가 지금 노력하고있어 설명하는 이미지입니다. 그중 하나 인 pixel_guy.png
은 6 색 (이 색은 원래 팔레트) 인 png 이미지입니다. 다른 이미지 colortable.png
은 각각 6 색 팔레트 (각 행은 다른 팔레트)를 포함하는 6x6 픽셀 png입니다. colortable.png
의 첫 번째 픽셀 행의 색은 pixel_guy.png
에서 사용 된 색과 일치합니다 (첫 번째/원래 색 팔레트가 될 것이며 다른 행은 2-6 팔레트가 될 것입니다.) 달성하려는 것은 colortable의 첫 번째 팔레트를 사용하는 것입니다. index pixelguy colors를 선택하고 셰이더에 숫자 (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();
}
}
을
플로트 값을 조각의 유니폼에 전달하는 올바른 방법인지는 알 수 없습니다. 내가 사용하려고 시도한 코드 스 니펫이 어떻게 작동하는지에 대해 확실하지 않습니다. 편집
: 나는 TenFour04가 제시 한 변화를 시도하고 완벽하게 일 :
다음는 사전 처리 된 스프라이트
내가 변화와 저장소를 업데이트 한 것입니다 (자바 코드 here, 조각 쉐이더 here), 누군가가 관심이 있으시면 여기에서 다운로드하십시오 : https://bitbucket.org/hcito/libgdxshadertest
편집 2 : 방금 TenFour04가 조언 한 마지막 최적화를 저장소에 추가했습니다. (Sprite.setColor() 메서드를 호출하는 R 채널 내의 각 스프라이트에 팔레트 색인을 전달하기 위해) 그리고 다시 완벽하게 작동했습니다.
왜 투표가 중단됩니까? – Tenfour04
모르겠다 :/나는 내 영어가별로 좋지 않다는 것을 알고 있고, 조금 어색하고 전부다.하지만 나는 정말로 내 최선을 다하려고 노력하고있다. ^^ U 저를 도와 주셔서 대단히 감사합니다, Tenfour :) 그건 그렇고, 당신의 대답을 upvote하는 데 5 포인트가 더 필요합니다 ^^ U) – hcito