Skip to content

Instantly share code, notes, and snippets.

@xoppa
Last active January 9, 2020 06:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xoppa/8647365 to your computer and use it in GitHub Desktop.
Save xoppa/8647365 to your computer and use it in GitHub Desktop.
CustomTextureTest.java
public class CustomTextureTest extends GdxTest {
public static class BgTextureUnitAttribute extends IntAttribute{
public static final String Alias = "bgTtextureUnit";
public final static long Type = register(Alias);
public BgTextureUnitAttribute(long type, int textureNum) {
super(type, textureNum);
}
}
public static class BgTextureShader extends DefaultShader {
public final static Setter customDiffuseTexture = new Setter() {
@Override public boolean isGlobal (BaseShader shader, int inputID) { return false; }
@Override public void set (BaseShader shader, int inputID, Renderable renderable, Attributes combinedAttributes) {
final int unit = ((IntAttribute)(combinedAttributes.get(BgTextureUnitAttribute.Type))).value;
shader.set(inputID, unit);
}
};
public BgTextureShader (Renderable renderable) {
super(renderable);
register(Inputs.diffuseTexture, customDiffuseTexture);
}
}
public PerspectiveCamera cam;
public CameraInputController inputController;
public ModelBatch modelBatch;
public Model model;
public Renderable renderable;
public Environment environment;
public RenderContext context;
public Texture customTexture;
public Shader shader;
@Override
public void create () {
context = new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.ROUNDROBIN, 2));
modelBatch = new ModelBatch(context);
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(0f, 0f, 2f);
cam.lookAt(0,0,0);
cam.near = 1f;
cam.far = 300f;
cam.update();
Texture texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(1, 1, 1,
new Material(TextureAttribute.createDiffuse(texture), new IntAttribute(BgTextureUnitAttribute.Type, 1)),
Usage.Position | Usage.TextureCoordinates | Usage.Normal);
model.manageDisposable(texture);
renderable = new Renderable();
model.nodes.get(0).parts.get(0).setRenderable(renderable);
renderable.environment = environment;
shader = new BgTextureShader(renderable);
shader.init();
renderable.shader = shader; // comment this line to see the difference
customTexture = new Texture(Gdx.files.internal("data/egg.png"));
customTexture.bind(1);
Gdx.gl.glActiveTexture(0);
Gdx.input.setInputProcessor(new InputMultiplexer(this, inputController = new CameraInputController(cam)));
}
@Override
public void render () {
inputController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
context.begin();
modelBatch.begin(cam);
modelBatch.render(renderable);
modelBatch.end();
context.end();
}
@Override
public void dispose () {
modelBatch.dispose();
model.dispose();
customTexture.dispose();
shader.dispose();
}
public boolean needsGL20 () {
return true;
}
public void resume () {
}
public void resize (int width, int height) {
}
public void pause () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment