Example usage for com.badlogic.gdx.graphics.g2d SpriteBatch setShader

List of usage examples for com.badlogic.gdx.graphics.g2d SpriteBatch setShader

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d SpriteBatch setShader.

Prototype

@Override
    public void setShader(ShaderProgram shader) 

Source Link

Usage

From source file:com.badlogic.gdx.tests.PremultiplyAlpha.java

private void gpuPremultiplyAlpha(String in, String out) {
    Texture texture = new Texture(Gdx.files.absolute(in));
    texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
    FrameBuffer buffer = new FrameBuffer(Format.RGBA8888, texture.getWidth(), texture.getHeight(), false);
    buffer.getColorBufferTexture().setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
    ShaderProgram shader = new ShaderProgram(VERTEX_SHADER, FRAG_SHADER);
    Gdx.app.log("Log", shader.getLog());
    SpriteBatch batch = new SpriteBatch(10);
    batch.getProjectionMatrix().setToOrtho2D(0, 0, texture.getWidth(), texture.getHeight());
    batch.disableBlending();// ww w  .  j  a  va 2  s  .com
    batch.setShader(shader);

    //Premultiply
    buffer.begin();
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(texture, 0, 0);
    batch.end();
    Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, texture.getWidth(), texture.getHeight());
    buffer.end();

    //Save
    PixmapIO.writePNG(Gdx.files.absolute(out), pixmap);
    buffer.dispose();
    texture.dispose();
    pixmap.dispose();
    Gdx.app.exit();
}

From source file:com.gamejolt.mikykr5.ceidecpong.effects.ScrollingBackground.java

License:Open Source License

/**
 * Render this effect./*  w w  w.  java  2s. c om*/
 * 
 * @param batch The {@link SpriteBatch} to use for the rendering.
 * @throws IllegalStateException If the {@link SpriteBatch} did not call {@link SpriteBatch#begin()} befor this method was called.
 */
public void render(SpriteBatch batch) throws IllegalStateException {
    if (!batch.isDrawing())
        throw new IllegalStateException("Must be called between SpriteBatch.begin() and SpriteBatch.end()");

    // If the shader was loaded then set it as the current shader.
    if (shader != null) {
        batch.setShader(shader);
        shader.setUniformf(u_scaling, scaling);
        shader.setUniformf(u_displacement, displacement);
    }

    // Render.
    background.draw(batch);

    // If the shader was loaded then disable it.
    if (shader != null)
        batch.setShader(null);

    // Update the displacement a little bit.
    // GOTCHA: This will look slower or faster depending on the speed of the computer.
    displacement = displacement < 0.0f ? 1.0f : displacement - 0.0005f;
}

From source file:com.maplescot.loggerbill.game.LoggerEngine.java

License:Creative Commons License

@Override
public void drawWorld(SpriteBatch batch, float delta) {
    if (isNight) {
        // Night sky.
        Gdx.gl.glClearColor(0.036f, 0.058f, 0.0988f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    } else {//w w  w.  j a v a2s  .co  m
        // Colour my world blue.
        Gdx.gl.glClearColor(0.36f, 0.58f, 0.988f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }
    BackgroundScenery.getInstance().draw(delta);

    // The Grass...
    setShader(batch);
    batch.setColor(0f, 1f, 0f, 1f); // Green Grass
    batch.draw(Assets.getInstance().grassRegion, -(VIEW_WIDTH / 2) - 50, 0f, VIEW_WIDTH + 100,
            BILL_HEIGHT + 20);

    if (isNight) {
        batch.setShader(null);
        fireFlies_back.draw(batch, delta);
    }

    setShader(batch);
    // The stump...
    batch.setColor(1f, 1f, 1f, 1f);
    batch.draw(Assets.getInstance().stumpRegion, -(Assets.getInstance().stumpRegion.getRegionWidth() / 2) - 7,
            40f);

    drawTree(batch); // Draw the actual tree

    ListIterator<EjectedChunk> ejectIterator = ejectedChunks.listIterator();
    while (ejectIterator.hasNext()) {
        EjectedChunk myChunk = ejectIterator.next();
        if (!myChunk.draw(batch, delta))
            ejectIterator.remove(); // Clean up ejected chunks when off screen.
    }

    bill.draw(batch, delta);

    if (billGhost != null) {
        billGhost.draw(batch, ghostShader, delta);
    }

    chips.draw(batch);
    if (!chips.isComplete())
        chips.update(delta);

    if (isNight) {
        batch.setShader(null);
        fireFlies_front.draw(batch, delta);
    }

}

From source file:com.maplescot.loggerbill.game.LoggerEngine.java

License:Creative Commons License

@Override
public void drawHUD(SpriteBatch batch, float delta) {
    batch.setShader(null); // No darkening of the HUD at night
    String str = String.valueOf(chunk_counter);
    BitmapFont.TextBounds tb = Assets.getInstance().font.big.getBounds(str);
    Assets.getInstance().font.big.setColor(0f, 1f, 0f, 1f);
    Assets.getInstance().font.big.draw(batch, str, VIEWPORT_GUI_WIDTH / 2 - (tb.width / 2), 250);

    Assets.getInstance().pauseButtonDrawable.draw(batch, VIEWPORT_GUI_WIDTH - 150, 135, 100, 100);
    drawTimerGuage(batch);//from   w w w .j a  v a 2  s.  c o m
    if (showHelp)
        drawHelp(batch, delta);
}

From source file:com.maplescot.loggerbill.game.LoggerEngine.java

License:Creative Commons License

public void setShader(SpriteBatch batch) {
    if (isNight) {
        batch.setShader(nightShader);
        nightShader.setUniformf("u_amount", 0.85f);
    } else/*from www.  j a va 2 s.  c  o m*/
        batch.setShader(null);
}

From source file:com.maplescot.loggerbill.game.world.BillGhost.java

License:Creative Commons License

public boolean draw(SpriteBatch batch, ShaderProgram ghostShader, float delta) {

    // We will use a wibbly-wobbly shader for bill's ghost
    batch.setShader(ghostShader);
    ghostTime += delta;/*  w  ww .  j  a v  a 2  s  . c o m*/
    ghostShader.setUniformf("time", ghostTime);
    //ghostShader.setUniformf("resolution", 1.0f, 1.0f);

    sprite.setPosition(x, y);
    sprite.setAlpha(alpha);

    sprite.draw(batch);
    y += delta * 250;
    x -= (MathUtils.sin(y / 100 % 360) * 2) * side;
    alpha = (VIEW_HEIGHT - (y / VIEW_HEIGHT / 2));

    batch.setShader(null); // turn off ghost shader.
    return y <= VIEW_HEIGHT + sprite.getHeight();
}

From source file:core.september.pushathon.workers.HelpRenderer.java

License:Apache License

protected void renderGame(SpriteBatch batch) {
    batch.setProjectionMatrix(camera.combined);
    batch.setShader(Shader.grayscaleShader);
    batch.begin();/*from  ww  w .  j a  va2  s. co  m*/
    renderPowerUnit(batch);
    renderButton(batch);
    renderCounter(batch);
    renderScorer(batch);
    //renderText(batch);

    batch.end();
}

From source file:core.september.pushathon.workers.MainRenderer.java

License:Apache License

protected void renderGame(SpriteBatch batch) {
    batch.setProjectionMatrix(camera.combined);
    batch.setShader(Shader.grayscaleShader);
    batch.begin();/*w  ww  . j  a  v  a2 s .com*/
    renderPowerUnit(batch);
    renderButton(batch);
    renderCounter(batch);
    renderScorer(batch);

    batch.end();
    stage.draw();

}

From source file:es.eucm.ead.engine.gameobjects.sceneelements.transitions.MaskTransitionGO.java

License:Open Source License

@Override
public void drawChildren(SpriteBatch batch, float parentAlpha) {
    previousScene.setVisible(true);/*from  www  .  j  a  v a  2s .c o  m*/
    nextScene.setVisible(false);
    super.drawChildren(batch, parentAlpha);
    batch.end();
    nextSceneBuffer.begin();
    batch.begin();

    previousScene.setVisible(false);
    nextScene.setVisible(true);

    super.drawChildren(batch, parentAlpha);
    nextSceneBuffer.end();

    batch.flush();
    batch.setShader(maskShader);
    maskShader.setAttributef("a_offset", offset.x, offset.y, offset.z, 0);
    texture.bind(1);
    Gdx.gl.glActiveTexture(GL10.GL_TEXTURE0);
    Gdx.gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
    batch.draw(nextSceneBuffer.getColorBufferTexture(), 0, 0);
    batch.setShader(null);
}

From source file:hku.fyp14017.blencode.content.Look.java

License:Open Source License

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
    checkImageChanged();//from w w w  . j  av  a2s .  co  m
    batch.setShader(shader);
    if (alpha == 0.0f) {
        setVisible(false);
    } else {
        setVisible(true);
    }
    if (this.visible && this.getDrawable() != null) {
        super.draw(batch, this.alpha);
    }
}