Search in sources :

Example 1 with Asset

use of io.github.voidzombie.nhglib.assets.Asset in project nhglib by VoidZombie.

the class AssetJson method parse.

@Override
public void parse(JsonValue jsonValue) {
    String alias = jsonValue.getString("alias");
    String source = jsonValue.getString("source");
    String classString = jsonValue.getString("classAlias");
    String dependenciesPath = jsonValue.getString("dependenciesPath", getDefaultDependenciesPath(source));
    if (!dependenciesPath.endsWith("/")) {
        dependenciesPath += "/";
    }
    Class assetClass = SceneUtils.assetClassFromAlias(classString);
    output = new Asset(alias, source, assetClass);
    output.dependenciesPath = dependenciesPath;
}
Also used : Asset(io.github.voidzombie.nhglib.assets.Asset)

Example 2 with Asset

use of io.github.voidzombie.nhglib.assets.Asset 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 Asset

use of io.github.voidzombie.nhglib.assets.Asset 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 4 with Asset

use of io.github.voidzombie.nhglib.assets.Asset in project nhglib by VoidZombie.

the class NhgG3dModelLoader method parseModel.

public ModelData parseModel(FileHandle handle) {
    JsonValue json = reader.parse(handle);
    ModelData model = new ModelData();
    JsonValue version = json.require("version");
    model.version[0] = version.getShort(0);
    model.version[1] = version.getShort(1);
    if (model.version[0] != VERSION_HI || model.version[1] != VERSION_LO)
        throw new GdxRuntimeException("Model version not supported");
    // Get the current asset
    ArrayMap.Values<Asset> cachedAssets = assets.getCachedAssets();
    for (Asset asset : cachedAssets) {
        if (asset.source.contentEquals(handle.path())) {
            currentAsset = asset;
            break;
        }
    }
    model.id = json.getString("id", "");
    parseMeshes(model, json);
    parseMaterials(model, json, currentAsset.dependenciesPath);
    parseNodes(model, json);
    parseAnimations(model, json);
    return model;
}
Also used : Asset(io.github.voidzombie.nhglib.assets.Asset)

Example 5 with Asset

use of io.github.voidzombie.nhglib.assets.Asset 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

Asset (io.github.voidzombie.nhglib.assets.Asset)5 ModelComponent (io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent)3 Message (io.github.voidzombie.nhglib.runtime.messaging.Message)3 Model (com.badlogic.gdx.graphics.g3d.Model)2 FPSLogger (com.badlogic.gdx.graphics.FPSLogger)1 Environment (com.badlogic.gdx.graphics.g3d.Environment)1 Material (com.badlogic.gdx.graphics.g3d.Material)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 JsonValue (com.badlogic.gdx.utils.JsonValue)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 NhgWorld (io.github.voidzombie.nhglib.graphics.worlds.NhgWorld)1 DefaultWorldStrategy (io.github.voidzombie.nhglib.graphics.worlds.strategies.impl.DefaultWorldStrategy)1 NodeComponent (io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent)1 GraphicsSystem (io.github.voidzombie.nhglib.runtime.ecs.systems.impl.GraphicsSystem)1