libGdx自己传入顶点绘制纹理
2022-10-21 本文已影响0人
大旺旺的弟弟小旺旺
写shader
1.vertex
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
attribute vec2 a_texCoord1;
varying vec4 v_color;
uniform mat4 u_projTrans;
varying vec2 v_textCoords;
varying vec2 v_textCoords1;
void main() {
v_color = a_color;
v_color.a = v_color.a * (255.0/254.0);
v_textCoords = a_texCoord0;
v_textCoords1 = a_texCoord1;
gl_Position = u_projTrans * a_position;
}
- frag
#ifdef GL_ES
precision mediump float;
#endif
//input from vertex shader
varying vec4 v_color;
uniform vec4 v_color1;
varying vec2 v_textCoords;
uniform sampler2D u_texture;
uniform sampler2D u_texture2;
uniform float time;
varying vec2 v_textCoords1;
void main() {
vec4 tempColor = texture2D(u_texture, v_textCoords);
vec4 tempColor2 = texture2D(u_texture2, v_textCoords1);
// vec4 tempHuang = vec4(229.0F/255, 208.0F/255, 190.0F/255,1);
// tempHuang.a = 1.0-time*0.4;
// gl_FragColor = vec4(tempColor.rgb,v_textCoords1.x * 1+0.5); //+ tempHuang;
gl_FragColor = mix(tempColor2,tempColor,0.5);
}
3.绘制代码
float arr[] = new float[]{
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
};
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4*2*4);
byteBuffer.order(ByteOrder.nativeOrder());
floatBuffer = byteBuffer.asFloatBuffer();
floatBuffer.put(arr);
texture = Asset.getAsset().getTexture("1234.png");
if (shadowProgram == null) {
shadowProgram = new ShaderProgram(
Gdx.files.internal("shader/hui.vert"),
Gdx.files.internal("shader/Demo8.frag"));
}
Image group = new Image(new Texture("bgTest.png")){
private float time = 0;
@Override
public void act(float delta) {
super.act(delta);
time+=delta;
}
@Override
public void draw(Batch batch, float parentAlpha) {
if (shadowProgram!=null) {
batch.setShader(shadowProgram);
int timeLocation = shadowProgram.getUniformLocation("time");
int u_t = shadowProgram.getUniformLocation("u_texture2");
int v_color1 = shadowProgram.getUniformLocation("v_color1");
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE1);
texture.bind();
shadowProgram.setUniformi(u_t,1);
int a_texCoord1 = shadowProgram.getAttributeLocation("a_texCoord1");
shadowProgram.enableVertexAttribute(a_texCoord1);
floatBuffer.position(0);
shadowProgram.setVertexAttribute(a_texCoord1,2,Gdx.gl20.GL_FLOAT,false,0,floatBuffer);
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0);
shadowProgram.setUniformf(v_color1,1,1,1,1);
shadowProgram.setUniformf(timeLocation,time*1F);
super.draw(batch, parentAlpha);
batch.setShader(null);
}else {
super.draw(batch, parentAlpha);
}
}
};
// Image image = new Image(Asset.getAsset().getTexture("1234.png"));
// addActor(image);
// image.setSize(group.getWidth(),group.getHeight());
group.setPosition(0,0);
addActor(group);