Search in sources :

Example 1 with ModelComponent

use of io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent in project nhglib by VoidZombie.

the class SceneManager method unloadScene.

public void unloadScene(final Scene scene) {
    Observable.fromIterable(scene.sceneGraph.getEntities()).filter(new Predicate<Integer>() {

        @Override
        public boolean test(Integer entity) throws Exception {
            return modelMapper.has(entity);
        }
    }).doFinally(new Action() {

        @Override
        public void run() throws Exception {
            int rootEntity = scene.sceneGraph.getRootEntity();
            NodeComponent nodeComponent = nodeMapper.get(rootEntity);
            nodeComponent.setTranslation(0, 0, 0);
            nodeComponent.setRotation(0, 0, 0);
            nodeComponent.setScale(1, 1, 1);
            nodeComponent.applyTransforms();
        }
    }).subscribe(new Consumer<Integer>() {

        @Override
        public void accept(Integer integer) throws Exception {
            ModelComponent modelComponent = modelMapper.get(integer);
            assets.unloadAsset(modelComponent.asset);
        }
    });
}
Also used : Action(io.reactivex.functions.Action) ModelComponent(io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent) NodeComponent(io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent)

Example 2 with ModelComponent

use of io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent in project nhglib by VoidZombie.

the class GraphicsSystem method inserted.

@Override
protected void inserted(int entityId) {
    super.inserted(entityId);
    final ModelComponent modelComponent = modelMapper.get(entityId);
    messaging.get(Strings.Events.assetLoaded).subscribe(new Consumer<Message>() {

        @Override
        public void accept(Message message) throws Exception {
            if (modelComponent.type == ModelComponent.Type.STATIC) {
                Asset asset = (Asset) message.data.get(Strings.Defaults.assetKey);
                if (asset.is(modelComponent.asset.alias)) {
                    rebuildCache(modelComponent.model);
                }
            }
        }
    });
}
Also used : Message(io.github.voidzombie.nhglib.runtime.messaging.Message) ModelComponent(io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent) Asset(io.github.voidzombie.nhglib.assets.Asset)

Example 3 with ModelComponent

use of io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent in project nhglib by VoidZombie.

the class GraphicsSystem method process.

@Override
protected void process(int entityId) {
    ModelComponent modelComponent = modelMapper.get(entityId);
    NodeComponent nodeComponent = nodeMapper.get(entityId);
    if (modelComponent.enabled) {
        if (modelComponent.animationController != null) {
            modelComponent.animationController.update(Gdx.graphics.getDeltaTime());
        }
        if (modelComponent.type == ModelComponent.Type.DYNAMIC && modelComponent.model != null) {
            modelComponent.model.transform.set(nodeComponent.getTransform());
            for (ModelCache modelCache : dynamicCaches) {
                modelCache.add(modelComponent.model);
            }
        }
    }
}
Also used : ModelComponent(io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent) NodeComponent(io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent) ModelCache(com.badlogic.gdx.graphics.g3d.ModelCache)

Example 4 with ModelComponent

use of io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent in project nhglib by VoidZombie.

the class SceneManager method loadAssets.

private void loadAssets(Integer entity) {
    final ModelComponent modelComponent = modelMapper.get(entity);
    // Group all assets
    final Array<Asset> allAssets = new Array<>();
    final Asset modelAsset = modelComponent.asset;
    for (PbrMaterial mat : modelComponent.pbrMaterials) {
        if (mat.albedoAsset != null) {
            allAssets.add(mat.albedoAsset);
        }
        if (mat.metalnessAsset != null) {
            allAssets.add(mat.metalnessAsset);
        }
        if (mat.roughnessAsset != null) {
            allAssets.add(mat.roughnessAsset);
        }
        if (mat.normalAsset != null) {
            allAssets.add(mat.normalAsset);
        }
        if (mat.ambientOcclusionAsset != null) {
            allAssets.add(mat.ambientOcclusionAsset);
        }
    }
    // Start loading them
    assets.queueAsset(modelAsset);
    // Wait for them
    messaging.get(Strings.Events.assetLoaded).subscribe(new Consumer<Message>() {

        @Override
        public void accept(Message message) throws Exception {
            Asset asset = (Asset) message.data.get(Strings.Defaults.assetKey);
            if (asset != null) {
                if (asset.is(modelAsset.alias)) {
                    Model model = assets.get(asset);
                    modelComponent.model = new ModelInstance(model);
                    if (modelComponent.model.animations.size > 0) {
                        modelComponent.animationController = new AnimationController(modelComponent.model);
                    }
                    assets.queueAssets(allAssets);
                } else {
                    if (allAssets.contains(asset, false)) {
                        temporaryLoadedAssets.add(asset);
                        allAssets.removeValue(asset, false);
                        Bundle bundle = new Bundle();
                        bundle.put("size", allAssets.size);
                        bundle.put("modelComponent", modelComponent);
                        sizeSubject.onNext(bundle);
                    }
                }
            }
        }
    });
}
Also used : Message(io.github.voidzombie.nhglib.runtime.messaging.Message) Bundle(io.github.voidzombie.nhglib.utils.data.Bundle) Array(com.badlogic.gdx.utils.Array) ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelComponent(io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent) AnimationController(com.badlogic.gdx.graphics.g3d.utils.AnimationController) Model(com.badlogic.gdx.graphics.g3d.Model) Asset(io.github.voidzombie.nhglib.assets.Asset) PbrMaterial(io.github.voidzombie.nhglib.graphics.utils.PbrMaterial)

Example 5 with ModelComponent

use of io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent in project nhglib by VoidZombie.

the class ModelComponentJson method parse.

@Override
public void parse(JsonValue jsonValue) {
    ModelComponent modelComponent = entities.createComponent(entity, ModelComponent.class);
    String type = jsonValue.getString("graphicsType");
    JsonValue asset = jsonValue.get("asset");
    boolean enabled = jsonValue.getBoolean("enabled", true);
    AssetJson assetJson = new AssetJson();
    assetJson.parse(asset);
    JsonValue materialsJson = jsonValue.get("materials");
    if (materialsJson != null) {
        for (JsonValue mat : materialsJson) {
            PbrMaterialJson pbrMaterialJson = new PbrMaterialJson();
            pbrMaterialJson.parse(mat);
            modelComponent.pbrMaterials.add(pbrMaterialJson.get());
        }
    }
    modelComponent.type = ModelComponent.Type.fromString(type);
    modelComponent.asset = assetJson.get();
    modelComponent.enabled = enabled;
    output = modelComponent;
}
Also used : PbrMaterialJson(io.github.voidzombie.nhglib.data.models.serialization.PbrMaterialJson) ModelComponent(io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent) JsonValue(com.badlogic.gdx.utils.JsonValue) AssetJson(io.github.voidzombie.nhglib.data.models.serialization.AssetJson)

Aggregations

ModelComponent (io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent)6 Asset (io.github.voidzombie.nhglib.assets.Asset)3 NodeComponent (io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent)3 Message (io.github.voidzombie.nhglib.runtime.messaging.Message)3 Model (com.badlogic.gdx.graphics.g3d.Model)2 JsonValue (com.badlogic.gdx.utils.JsonValue)2 FPSLogger (com.badlogic.gdx.graphics.FPSLogger)1 Environment (com.badlogic.gdx.graphics.g3d.Environment)1 Material (com.badlogic.gdx.graphics.g3d.Material)1 ModelCache (com.badlogic.gdx.graphics.g3d.ModelCache)1 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)1 AnimationController (com.badlogic.gdx.graphics.g3d.utils.AnimationController)1 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)1 ImmediateModeRenderer20 (com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20)1 Array (com.badlogic.gdx.utils.Array)1 AssetJson (io.github.voidzombie.nhglib.data.models.serialization.AssetJson)1 PbrMaterialJson (io.github.voidzombie.nhglib.data.models.serialization.PbrMaterialJson)1 Scene (io.github.voidzombie.nhglib.graphics.scenes.Scene)1 GammaCorrectionAttribute (io.github.voidzombie.nhglib.graphics.shaders.attributes.GammaCorrectionAttribute)1 PbrMaterial (io.github.voidzombie.nhglib.graphics.utils.PbrMaterial)1