use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.
the class DistanceFieldFont method createDistanceFieldShader.
/** Returns a new instance of the distance field shader, see https://github.com/libgdx/libgdx/wiki/Distance-field-fonts if the
* u_smoothing uniform > 0.0. Otherwise the same code as the default SpriteBatch shader is used. */
public static ShaderProgram createDistanceFieldShader() {
String vertexShader = //
"attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" + "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + //
";\n" + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + //
"0;\n" + //
"uniform mat4 u_projTrans;\n" + //
"varying vec4 v_color;\n" + //
"varying vec2 v_texCoords;\n" + //
"\n" + //
"void main() {\n" + " v_color = " + ShaderProgram.COLOR_ATTRIBUTE + //
";\n" + //
" v_color.a = v_color.a * (255.0/254.0);\n" + " v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + //
"0;\n" + " gl_Position = u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + //
";\n" + "}\n";
String fragmentShader = //
"#ifdef GL_ES\n" + //
" precision mediump float;\n" + //
" precision mediump int;\n" + //
"#endif\n" + //
"\n" + //
"uniform sampler2D u_texture;\n" + //
"uniform float u_smoothing;\n" + //
"varying vec4 v_color;\n" + //
"varying vec2 v_texCoords;\n" + //
"\n" + //
"void main() {\n" + //
" if (u_smoothing > 0.0) {\n" + //
" float smoothing = 0.25 / u_smoothing;\n" + //
" float distance = texture2D(u_texture, v_texCoords).a;\n" + //
" float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);\n" + //
" gl_FragColor = vec4(v_color.rgb, alpha * v_color.a);\n" + //
" } else {\n" + //
" gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" + //
" }\n" + "}\n";
ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
if (shader.isCompiled() == false)
throw new IllegalArgumentException("Error compiling distance field shader: " + shader.getLog());
return shader;
}
use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.
the class ShaderProgramLoader method loadSync.
@Override
public ShaderProgram loadSync(AssetManager manager, String fileName, FileHandle file, ShaderProgramParameter parameter) {
String vertFileName = null, fragFileName = null;
if (parameter != null) {
if (parameter.vertexFile != null)
vertFileName = parameter.vertexFile;
if (parameter.fragmentFile != null)
fragFileName = parameter.fragmentFile;
}
if (vertFileName == null && fileName.endsWith(fragmentFileSuffix)) {
vertFileName = fileName.substring(0, fileName.length() - fragmentFileSuffix.length()) + vertexFileSuffix;
}
if (fragFileName == null && fileName.endsWith(vertexFileSuffix)) {
fragFileName = fileName.substring(0, fileName.length() - vertexFileSuffix.length()) + fragmentFileSuffix;
}
FileHandle vertexFile = vertFileName == null ? file : resolve(vertFileName);
FileHandle fragmentFile = fragFileName == null ? file : resolve(fragFileName);
String vertexCode = vertexFile.readString();
String fragmentCode = vertexFile.equals(fragmentFile) ? vertexCode : fragmentFile.readString();
if (parameter != null) {
if (parameter.prependVertexCode != null)
vertexCode = parameter.prependVertexCode + vertexCode;
if (parameter.prependFragmentCode != null)
fragmentCode = parameter.prependFragmentCode + fragmentCode;
}
ShaderProgram shaderProgram = new ShaderProgram(vertexCode, fragmentCode);
if ((parameter == null || parameter.logOnCompileFailure) && !shaderProgram.isCompiled()) {
manager.getLogger().error("ShaderProgram " + fileName + " failed to compile:\n" + shaderProgram.getLog());
}
return shaderProgram;
}
use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.
the class CustomShaderSpriteBatchTest method create.
@Override
public void create() {
batch = new SpriteBatch(10);
ShaderProgram.pedantic = false;
shader = new ShaderProgram(Gdx.files.internal("data/shaders/batch.vert").readString(), Gdx.files.internal("data/shaders/batch.frag").readString());
batch.setShader(shader);
texture = new Texture("data/badlogic.jpg");
}
use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.
the class EdgeDetectionTest method create.
public void create() {
ShaderProgram.pedantic = false;
/*
* shader = new ShaderProgram(Gdx.files.internal("data/shaders/default.vert").readString(), Gdx.files.internal(
* "data/shaders/depthtocolor.frag").readString()); if (!shader.isCompiled()) { Gdx.app.log("EdgeDetectionTest",
* "couldn't compile scene shader: " + shader.getLog()); }
*/
batchShader = new ShaderProgram(Gdx.files.internal("data/shaders/batch.vert").readString(), Gdx.files.internal("data/shaders/convolution.frag").readString());
if (!batchShader.isCompiled()) {
Gdx.app.log("EdgeDetectionTest", "couldn't compile post-processing shader: " + batchShader.getLog());
}
ObjLoader objLoader = new ObjLoader();
scene = objLoader.loadModel(Gdx.files.internal("data/scene.obj"));
sceneInstance = new ModelInstance(scene);
modelBatch = new ModelBatch();
fbo = new FrameBuffer(Format.RGB565, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(0, 0, 10);
cam.lookAt(0, 0, 0);
cam.far = 30;
batch = new SpriteBatch();
batch.setShader(batchShader);
fboRegion = new TextureRegion(fbo.getColorBufferTexture());
fboRegion.flip(false, true);
logger = new FPSLogger();
calculateOffsets();
}
use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.
the class AssetManagerTest method load.
private void load() {
// Gdx.app.setLogLevel(Logger.DEBUG);
start = TimeUtils.nanoTime();
tex1 = new Texture("data/animation.png");
tex2 = new TextureAtlas(Gdx.files.internal("data/pack"));
font2 = new BitmapFont(Gdx.files.internal("data/verdana39.fnt"), false);
// tex3 = new Texture("data/test.etc1");
// map = TiledLoader.createMap(Gdx.files.internal("data/tiledmap/tilemap csv.tmx"));
// atlas = new TileAtlas(map, Gdx.files.internal("data/tiledmap/"));
// renderer = new TileMapRenderer(map, atlas, 8, 8);
shader = new ShaderProgram(Gdx.files.internal("data/g2d/batchCommon.vert").readString(), Gdx.files.internal("data/g2d/monochrome.frag").readString());
System.out.println("plain took: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
start = TimeUtils.nanoTime();
manager.load("data/animation.png", Texture.class);
// manager.load("data/pack1.png", Texture.class);
manager.load("data/pack", TextureAtlas.class);
// manager.load("data/verdana39.png", Texture.class);
manager.load("data/verdana39.fnt", BitmapFont.class);
// manager.load("data/multipagefont.fnt", BitmapFont.class);
// manager.load("data/test.etc1", Texture.class);
// manager.load("data/tiledmap/tilemap csv.tmx", TileMapRenderer.class, new
// TileMapRendererLoader.TileMapParameter("data/tiledmap/", 8, 8));
manager.load("data/i18n/message2", I18NBundle.class, new I18NBundleLoader.I18NBundleParameter(reloads % 2 == 0 ? Locale.ITALIAN : Locale.ENGLISH));
manager.load("data/g2d/monochrome.frag", ShaderProgram.class, new ShaderProgramLoader.ShaderProgramParameter() {
{
vertexFile = "data/g2d/batchCommon.vert";
}
});
}
Aggregations