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; // SpriteBatch , ABGR int float. // - NAN , , (0-254) // , float 1, . // libgdx . v_color.a = v_color.a * (255.0/254.0); v_texCoords = a_texCoord0; // , // - // gl_Position gl_Position = u_projTrans * a_position; }
//#ifdef , . (GL_ES) // () .(highp – ; mediump – ; lowp – ) #ifdef GL_ES #define LOWP lowp precision mediump float; #else #define LOWP #endif varying LOWP vec4 v_color; varying vec2 v_texCoords; // sampler2D glsl uniform sampler2D u_texture; void main(){ gl_FragColor = v_color * texture2D(u_texture, v_texCoords);// }
// shaderProgram=new ShaderProgram(Gdx.files.internal("shaders/default.vert"),Gdx.files.internal("shaders/default.frag")); // vertexShader fragmentShader String shaderProgram=new ShaderProgram(vertexShader,fragmentShader);
ShaderProgram.pedantic = false;
shaderProgram.dispose().
// gl_FragColor = v_color * texture2D(u_texture, v_texCoords); // , gl_FragColor.rgb=1.0-gl_FragColor.rgb;
#ifdef GL_ES #define LOWP lowp precision mediump float; #else #define LOWP #endif varying LOWP vec4 v_color; varying vec2 v_texCoords; uniform sampler2D u_texture; void main(){ // gl_FragColor = v_color * texture2D(u_texture, v_texCoords); // , gl_FragColor.rgb=1.0-gl_FragColor.rgb; }
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; import com.badlogic.gdx.scenes.scene2d.Stage; public class ShaderDemo extends ApplicationAdapter { SpriteBatch batch; Texture img; ShaderProgram shader; @Override public void create() { batch = new SpriteBatch(); img = new Texture("badlogic.jpg"); // , , ShaderProgram.pedantic = false; shader = new ShaderProgram(Gdx.files.internal("shaders/default.vert"), (Gdx.files.internal("shaders/invertColors.frag"))); if (!shader.isCompiled()) { System.err.println(shader.getLog()); System.exit(0); } batch = new SpriteBatch(1000); batch.setShader(shader); } @Override public void render() { Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); batch.draw(img, 0, 0,Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); batch.end(); } @Override public void dispose() { // , batch.dispose(); shader.dispose(); img.dispose(); } }
Source: https://habr.com/ru/post/274813/
All Articles