Example usage for com.badlogic.gdx.graphics.g3d Material Material

List of usage examples for com.badlogic.gdx.graphics.g3d Material Material

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g3d Material Material.

Prototype

public Material(final String id, final Material copyFrom) 

Source Link

Document

Create a material which is an exact copy of the specified material

Usage

From source file:com.badlogic.invaders.simulation.Simulation.java

License:Apache License

private void populate() {
    ObjLoader objLoader = new ObjLoader();
    shipModel = objLoader.loadModel(Gdx.files.internal("data/ship.obj"));
    invaderModel = objLoader.loadModel(Gdx.files.internal("data/invader.obj"));
    blockModel = objLoader.loadModel(Gdx.files.internal("data/block.obj"));
    shotModel = objLoader.loadModel(Gdx.files.internal("data/shot.obj"));

    final Texture shipTexture = new Texture(Gdx.files.internal("data/ship.png"), Format.RGB565, true);
    shipTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    final Texture invaderTexture = new Texture(Gdx.files.internal("data/invader.png"), Format.RGB565, true);
    invaderTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    shipModel.materials.get(0).set(TextureAttribute.createDiffuse(shipTexture));
    invaderModel.materials.get(0).set(TextureAttribute.createDiffuse(invaderTexture));

    ((ColorAttribute) blockModel.materials.get(0).get(ColorAttribute.Diffuse)).color.set(0, 0, 1, 0.5f);
    blockModel.materials.get(0).set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));

    shotModel.materials.get(0).set(ColorAttribute.createDiffuse(1, 1, 0, 1f));

    final Texture explosionTexture = new Texture(Gdx.files.internal("data/explode.png"), Format.RGBA4444, true);
    explosionTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);

    final Mesh explosionMesh = new Mesh(true, 4 * 16, 6 * 16,
            new VertexAttribute(Usage.Position, 3, "a_position"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0"));

    float[] vertices = new float[4 * 16 * (3 + 2)];
    short[] indices = new short[6 * 16];
    int idx = 0;// w w  w. j  a v a  2  s  . co  m
    int index = 0;
    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 4; column++) {
            vertices[idx++] = 1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0 + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            vertices[idx++] = 1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            final int t = (4 * row + column) * 4;
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 1);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t + 3);
        }
    }

    explosionMesh.setVertices(vertices);
    explosionMesh.setIndices(indices);

    explosionModel = ModelBuilder.createFromMesh(explosionMesh, GL20.GL_TRIANGLES,
            new Material(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA),
                    TextureAttribute.createDiffuse(explosionTexture)));

    ship = new Ship(shipModel);
    ship.transform.rotate(0, 1, 0, 180);

    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 8; column++) {
            Invader invader = new Invader(invaderModel, -PLAYFIELD_MAX_X / 2 + column * 2f, 0,
                    PLAYFIELD_MIN_Z + row * 2f);
            invaders.add(invader);
        }
    }

    for (int shield = 0; shield < 3; shield++) {
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -2));
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 0, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -2));
    }
}

From source file:com.github.fauu.helix.core.MapRegion.java

License:Open Source License

public void create(Vector2 size, Tile[] tiles, Array<Object> objects, TextureAtlas textureAtlas,
        GeometrySet geometrySet) {//from w  ww . j av  a 2  s  .  c  o m
    this.size = size;
    this.textureAtlas = textureAtlas;
    this.geometrySet = geometrySet;
    this.objects = objects;

    if (tiles != null) {
        this.tiles = tiles;
    } else {
        final int tilesLength = (int) (size.x * size.y);

        tiles = new Tile[tilesLength];
        for (int i = 0; i < tilesLength; i++) {
            final Tile.Builder tileBuilder = new Tile.Builder();
            final int tileX = i % (int) size.x;
            final int tileY = (int) Math.floor(i / (tilesLength / size.y));

            tiles[i] = tileBuilder.setNo(i).setPosition(new Vector2(tileX, tileY)).setElevation(0)
                    .setTextureId(0).setGeometryId(0).setFacing(Direction.SOUTH).build();
        }
    }

    this.tiles = tiles;

    mesh = new MapRegionMesh(tiles, geometrySet, textureAtlas);

    renderable = new Renderable();
    renderable.mesh = mesh;
    renderable.material = new Material(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE),
            new TextureAttribute(TextureAttribute.Diffuse, textureAtlas.getTextures().first()));
    renderable.meshPartOffset = 0;
    renderable.meshPartSize = mesh.getNumVertices();
    renderable.primitiveType = GL20.GL_TRIANGLES;
    renderable.worldTransform.idt();
}

From source file:com.github.fauu.helix.editor.displayable.TileHighlightDisplayable.java

License:Open Source License

public TileHighlightDisplayable() {
    ModelBuilder modelBuilder = new ModelBuilder();

    Model model = modelBuilder.createRect(0, 0, Z_OFFSET, 1, 0, Z_OFFSET, 1, 1, Z_OFFSET, 0, 1, Z_OFFSET, 0, 0,
            1, GL20.GL_TRIANGLES,/*from   w ww  . j av a 2s . com*/
            new Material(new ColorAttribute(ColorAttribute.createDiffuse(color)),
                    new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA)),
            VertexAttributes.Usage.Position | VertexAttributes.Usage.TextureCoordinates);

    instance = new ModelInstance(model);
}

From source file:com.mygdx.game.simulation.Simulation.java

License:Apache License

private void populate() {
    ObjLoader objLoader = new ObjLoader();

    for (int i = 0; i < MAX_SHIPS; i++) {
        shipModels[i] = objLoader.loadModel(Gdx.files.internal("data/ship.obj"));
    }/*from  ww  w.  j a v a 2 s.  c o m*/
    invaderModel = objLoader.loadModel(Gdx.files.internal("data/invader.obj"));
    blockModel = objLoader.loadModel(Gdx.files.internal("data/block.obj"));
    shotModel = objLoader.loadModel(Gdx.files.internal("data/shot.obj"));

    final Texture shipTexture = new Texture(Gdx.files.internal("data/ship.png"), Format.RGB565, true);
    shipTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    final Texture shipTexture2 = new Texture(Gdx.files.internal("data/ship.png"), Format.RGB565, true);
    shipTexture2.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    final Texture invaderTexture = new Texture(Gdx.files.internal("data/invader.png"), Format.RGB565, true);
    invaderTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);

    for (int i = 0; i < MAX_SHIPS; i++) {
        shipModels[i].materials.get(0).set(TextureAttribute.createDiffuse(shipTexture));
    }
    invaderModel.materials.get(0).set(TextureAttribute.createDiffuse(invaderTexture));

    ((ColorAttribute) blockModel.materials.get(0).get(ColorAttribute.Diffuse)).color.set(0, 0, 1, 0.5f);
    blockModel.materials.get(0).set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));

    shotModel.materials.get(0).set(ColorAttribute.createDiffuse(1, 1, 0, 1f));

    final Texture explosionTexture = new Texture(Gdx.files.internal("data/explode.png"), Format.RGBA4444, true);
    explosionTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);

    final Mesh explosionMesh = new Mesh(true, 4 * 16, 6 * 16,
            new VertexAttribute(Usage.Position, 3, "a_position"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0"));

    float[] vertices = new float[4 * 16 * (3 + 2)];
    short[] indices = new short[6 * 16];
    int idx = 0;
    int index = 0;
    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 4; column++) {
            vertices[idx++] = 1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0 + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            vertices[idx++] = 1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            final int t = (4 * row + column) * 4;
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 1);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t + 3);
        }
    }

    explosionMesh.setVertices(vertices);
    explosionMesh.setIndices(indices);

    Material explosionMeshMaterial = new Material(
            new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA),
            TextureAttribute.createDiffuse(explosionTexture));
    explosionModel = new Model();
    MeshPart meshPart = new MeshPart();
    meshPart.id = "part1";
    meshPart.offset = 0;
    meshPart.size = explosionMesh.getNumIndices();
    meshPart.primitiveType = GL20.GL_TRIANGLES;
    meshPart.mesh = explosionMesh;

    NodePart partMaterial = new NodePart();
    partMaterial.material = explosionMeshMaterial;
    partMaterial.meshPart = meshPart;
    Node node = new Node();
    node.id = "node1";
    node.parts.add(partMaterial);

    explosionModel.meshes.add(explosionMesh);
    explosionModel.materials.add(explosionMeshMaterial);
    explosionModel.nodes.add(node);
    explosionModel.meshParts.add(meshPart);
    explosionModel.manageDisposable(explosionMesh);

    for (int i = 0; i < MAX_SHIPS; i++) {
        ships[i] = new Ship(shipModels[i]);
        ships[i].transform.rotate(1, 0, 0, 180);
    }

    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 8; column++) {
            Invader invader = new Invader(invaderModel, -PLAYFIELD_MAX_X / 2 + column * 2f, 0,
                    PLAYFIELD_MIN_Z + row * 2f);
            invaders.add(invader);
        }
    }

    for (int shield = 0; shield < 3; shield++) {
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -2));
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 0, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -2));
    }
}

From source file:org.ah.gcode.preview.GCodePreview.java

License:Open Source License

protected void parseGCode() {
    long now = System.currentTimeMillis();
    while (System.currentTimeMillis() - now < SIXTY_FPS_FRAME) {
        if (parser.isFinished()) {

            Material whiteMaterial = new Material(ColorAttribute.createAmbient(1f, 1f, 1f, 1f),
                    new BlendingAttribute(true, 1f));
            Context context = new Context();
            context.material = whiteMaterial;

            gCodeModel.prepareForProcessing(context);
            parsingGCode = false;// w  ww  . j  ava  2s  .  c  o  m
            preparingMeshes = true;

            if (!bedCreated) {
                float bedWidth = 210f;
                float bedHeight = 150f;
                if (gCodeModel.getBedWidth() > 0) {
                    bedWidth = gCodeModel.getBedWidth();
                }
                if (gCodeModel.getBedHeight() > 0) {
                    bedHeight = gCodeModel.getBedHeight();
                }
                createBed(bedWidth, bedHeight);
                bedCreated = true;
            }
        } else {
            parser.parseNextLine();
            if (!bedCreated && parser.getModel().getBedWidth() > 0) {
                createBed(parser.getModel().getBedWidth(), parser.getModel().getBedHeight());
                bedCreated = true;
            }
        }
    }
    Panel playPanel = window.getPlayPanel();
    playPanel.setVisible(true);
    playPanel.refresh();
    playPanel.text("Parsed lines: ", 0);
    playPanel.text(parser.getCurrentLine() + "/" + parser.getLines().size(), 1);
}

From source file:scene3d.demo.Scene3dDemo.java

License:Apache License

@Override
public void create() {
    //2d stuff/*from w ww.j av  a2s. co  m*/
    stage2d = new Stage();
    skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
    fpsLabel = new Label("ff", skin);
    fpsLabel.setPosition(Gdx.graphics.getWidth() - 260, Gdx.graphics.getHeight() - 40);
    visibleLabel = new Label("ff", skin);
    visibleLabel.setPosition(Gdx.graphics.getWidth() - 260, Gdx.graphics.getHeight() - 60);
    positionLabel = new Label("Position", skin);
    positionLabel.setPosition(Gdx.graphics.getWidth() - 260, Gdx.graphics.getHeight() - 80);
    rotationLabel = new Label("Rotation", skin);
    rotationLabel.setPosition(Gdx.graphics.getWidth() - 260, Gdx.graphics.getHeight() - 100);
    positionCameraLabel = new Label("Position", skin);
    positionCameraLabel.setPosition(20, Gdx.graphics.getHeight() - 40);
    rotationCameraLabel = new Label("Rotation", skin);
    rotationCameraLabel.setPosition(20, Gdx.graphics.getHeight() - 60);
    stage2d.addActor(fpsLabel);
    stage2d.addActor(visibleLabel);
    stage2d.addActor(positionLabel);
    stage2d.addActor(rotationLabel);
    stage2d.addActor(positionCameraLabel);
    stage2d.addActor(rotationCameraLabel);
    stage2d.addListener(new InputListener() {
        @Override
        public boolean keyUp(InputEvent event, int keycode) {
            if (keycode == Keys.LEFT)
                leftKey = false;
            if (keycode == Keys.RIGHT)
                rightKey = false;
            if (keycode == Keys.UP)
                upKey = false;
            if (keycode == Keys.DOWN)
                downKey = false;
            if (keycode == Keys.SPACE)
                spaceKey = false;
            return super.keyUp(event, keycode);
        }

        @Override
        public boolean keyDown(InputEvent event, int keycode) {
            if (keycode == Keys.LEFT)
                leftKey = true;
            if (keycode == Keys.RIGHT)
                rightKey = true;
            if (keycode == Keys.UP)
                upKey = true;
            if (keycode == Keys.DOWN)
                downKey = true;
            if (keycode == Keys.SPACE)
                spaceKey = true;
            return super.keyDown(event, keycode);
        }
    });

    //3dstuff
    stage3d = new Stage3d();
    modelBuilder = new ModelBuilder();
    model = modelBuilder.createBox(5f, 5f, 5f, new Material("Color", ColorAttribute.createDiffuse(Color.WHITE)),
            Usage.Position | Usage.Normal);

    model2 = modelBuilder.createBox(2f, 2f, 2f,
            new Material("Color", ColorAttribute.createDiffuse(Color.WHITE)), Usage.Position | Usage.Normal);
    actor2 = new Actor3d(model2, 10f, 0f, 0f);
    model3 = modelBuilder.createBox(2f, 2f, 2f,
            new Material("Color", ColorAttribute.createDiffuse(Color.ORANGE)), Usage.Position | Usage.Normal);
    actor3 = new Actor3d(model3, -10f, 0f, 0f);
    actor2.setColor(Color.RED);
    actor2.setName("actor2");
    actor3.setName("actor3");
    camController = new CameraInputController(stage3d.getCamera());
    InputMultiplexer im = new InputMultiplexer();
    im.addProcessor(stage2d);// 2d should get click events first
    //im.addProcessor(stage3d);
    im.addProcessor(camController);
    Gdx.input.setInputProcessor(im);
    stage3d.touchable = Touchable.enabled; // only then will it detect hit actor3d

    ModelBuilder builder = new ModelBuilder();
    builder.begin();
    MeshPartBuilder part = builder.part("floor", GL20.GL_TRIANGLES,
            Usage.Position | Usage.TextureCoordinates | Usage.Normal, new Material());
    for (float x = -200f; x < 200f; x += 10f) {
        for (float z = -200f; z < 200f; z += 10f) {
            part.rect(x, 0, z + 10f, x + 10f, 0, z + 10f, x + 10f, 0, z, x, 0, z, 0, 1, 0);
        }
    }
    floor = new Actor3d(builder.end());
    AssetManager am = new AssetManager();
    am.load("data/g3d/knight.g3db", Model.class);
    am.load("data/g3d/skydome.g3db", Model.class);
    am.load("data/g3d/concrete.png", Texture.class);
    am.finishLoading();
    knight = new Actor3d(am.get("data/g3d/knight.g3db", Model.class), 0f, 18f, 0f);
    knight.getAnimation().inAction = true;
    knight.getAnimation().animate("Walk", -1, 1f, null, 0.2f);
    skydome = new Actor3d(am.get("data/g3d/skydome.g3db", Model.class));
    floor.materials.get(0).set(TextureAttribute.createDiffuse(am.get("data/g3d/concrete.png", Texture.class)));
    stage3d.addActor3d(skydome);
    stage3d.addActor3d(floor);
    knight.setPitch(-90f);
    knight.setYaw(-130f);
    testActor3d();
    //stage3d.addAction3d(Actions3d.rotateBy(0f, 90f, 0f, 2f));
    //testGroup3d();
    //testStage3d();
}