Search in sources :

Example 31 with Material

use of com.badlogic.gdx.graphics.g3d.Material in project nhglib by VoidZombie.

the class NhgModelLoader method loadSync.

@Override
public Model loadSync(AssetManager manager, String fileName, FileHandle file, P parameters) {
    ModelData data = null;
    synchronized (items) {
        for (int i = 0; i < items.size; i++) {
            if (items.get(i).key.equals(fileName)) {
                data = items.get(i).value;
                items.removeIndex(i);
            }
        }
    }
    if (data == null)
        return null;
    final Model result = new Model(data, new TextureProvider.AssetTextureProvider(manager));
    // need to remove the textures from the managed disposables, or else ref counting
    // doesn't work!
    Iterator<Disposable> disposables = result.getManagedDisposables().iterator();
    while (disposables.hasNext()) {
        Disposable disposable = disposables.next();
        if (disposable instanceof Texture) {
            disposables.remove();
        }
    }
    // Automatically convert all materials to PBR
    for (Material material : result.materials) {
        TextureAttribute textureAttribute = (TextureAttribute) material.get(TextureAttribute.Diffuse);
        if (textureAttribute != null) {
            material.set(PbrTextureAttribute.createAlbedo(textureAttribute.textureDescription.texture));
        }
    }
    data = null;
    return result;
}
Also used : Disposable(com.badlogic.gdx.utils.Disposable) ModelData(com.badlogic.gdx.graphics.g3d.model.data.ModelData) Model(com.badlogic.gdx.graphics.g3d.Model) Material(com.badlogic.gdx.graphics.g3d.Material) ModelMaterial(com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial) TextureProvider(com.badlogic.gdx.graphics.g3d.utils.TextureProvider) ModelTexture(com.badlogic.gdx.graphics.g3d.model.data.ModelTexture) Texture(com.badlogic.gdx.graphics.Texture) TextureAttribute(com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute) PbrTextureAttribute(io.github.voidzombie.nhglib.graphics.shaders.attributes.PbrTextureAttribute)

Example 32 with Material

use of com.badlogic.gdx.graphics.g3d.Material in project nhglib by VoidZombie.

the class Main method engineStarted.

@Override
public void engineStarted() {
    super.engineStarted();
    Nhg.debugLogs = true;
    Gdx.input.setCursorCatched(true);
    world = new NhgWorld(nhg.messaging, nhg.entities, nhg.assets, new DefaultWorldStrategy(), new Bounds(2f, 2f, 2f));
    fpsLogger = new FPSLogger();
    renderer20 = new ImmediateModeRenderer20(false, true, 0);
    nhg.input.addListener(this);
    nhg.assets.queueAsset(new Asset("scene", "myscene.nhs", Scene.class));
    nhg.assets.queueAsset(new Asset("inputMap", "input.nhc", JsonValue.class));
    GraphicsSystem graphicsSystem = nhg.entities.getEntitySystem(GraphicsSystem.class);
    graphicsSystem.setClearColor(Color.GRAY);
    Environment environment = graphicsSystem.getEnvironment();
    GammaCorrectionAttribute gammaCorrectionAttribute = new GammaCorrectionAttribute();
    gammaCorrectionAttribute.gammaCorrection = true;
    environment.set(gammaCorrectionAttribute);
    // Subscribe to asset events
    nhg.messaging.get(Strings.Events.assetLoaded, Strings.Events.assetLoadingFinished).subscribe(new Consumer<Message>() {

        @Override
        public void accept(Message message) throws Exception {
            if (message.is(Strings.Events.assetLoaded)) {
                Asset asset = (Asset) message.data.get(Strings.Defaults.assetKey);
                if (asset.is("scene")) {
                    scene = nhg.assets.get(asset);
                    world.loadScene(scene);
                    world.setReferenceEntity("camera");
                    ModelBuilder mb = new ModelBuilder();
                    Model planeModel = mb.createBox(2f, 0.01f, 20f, new Material(), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates);
                    int plane = scene.sceneGraph.createSceneEntity("plane");
                    scene.sceneGraph.addSceneEntity(plane);
                    ModelComponent modelComponent = nhg.entities.createComponent(plane, ModelComponent.class);
                    modelComponent.initWithModel(planeModel);
                    NodeComponent nodeComponent = nhg.entities.getComponent(plane, NodeComponent.class);
                    nodeComponent.setTranslation(0, 0, 0, true);
                    Integer cameraEntity = scene.sceneGraph.getSceneEntity("camera");
                    cameraNode = nhg.entities.getComponent(cameraEntity, NodeComponent.class);
                } else if (asset.is("inputMap")) {
                    nhg.input.fromJson((JsonValue) nhg.assets.get(asset));
                    nhg.input.setActiveContext("game", true);
                    nhg.input.setActiveContext("global", true);
                }
            }
        }
    });
}
Also used : NhgWorld(io.github.voidzombie.nhglib.graphics.worlds.NhgWorld) Message(io.github.voidzombie.nhglib.runtime.messaging.Message) GammaCorrectionAttribute(io.github.voidzombie.nhglib.graphics.shaders.attributes.GammaCorrectionAttribute) Bounds(io.github.voidzombie.nhglib.utils.data.Bounds) JsonValue(com.badlogic.gdx.utils.JsonValue) Material(com.badlogic.gdx.graphics.g3d.Material) Scene(io.github.voidzombie.nhglib.graphics.scenes.Scene) FPSLogger(com.badlogic.gdx.graphics.FPSLogger) ImmediateModeRenderer20(com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) ModelComponent(io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent) DefaultWorldStrategy(io.github.voidzombie.nhglib.graphics.worlds.strategies.impl.DefaultWorldStrategy) Model(com.badlogic.gdx.graphics.g3d.Model) NodeComponent(io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent) Asset(io.github.voidzombie.nhglib.assets.Asset) GraphicsSystem(io.github.voidzombie.nhglib.runtime.ecs.systems.impl.GraphicsSystem) Environment(com.badlogic.gdx.graphics.g3d.Environment)

Aggregations

Material (com.badlogic.gdx.graphics.g3d.Material)32 ColorAttribute (com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)17 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)17 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)16 Model (com.badlogic.gdx.graphics.g3d.Model)15 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)12 Environment (com.badlogic.gdx.graphics.g3d.Environment)12 ModelBatch (com.badlogic.gdx.graphics.g3d.ModelBatch)12 Texture (com.badlogic.gdx.graphics.Texture)10 DirectionalLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalLight)10 MeshPartBuilder (com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder)7 Renderable (com.badlogic.gdx.graphics.g3d.Renderable)6 CameraInputController (com.badlogic.gdx.graphics.g3d.utils.CameraInputController)6 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)5 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)4 TextureAttribute (com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute)4 DirectionalShadowLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight)4 DefaultShaderProvider (com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider)4 com.badlogic.gdx.physics.bullet.collision.btBoxShape (com.badlogic.gdx.physics.bullet.collision.btBoxShape)4 com.badlogic.gdx.physics.bullet.collision.btSphereShape (com.badlogic.gdx.physics.bullet.collision.btSphereShape)4