Search in sources :

Example 21 with Environment

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

the class LightComponentJson method parse.

@Override
public void parse(JsonValue jsonValue) {
    GraphicsSystem graphicsSystem = entities.getEntitySystem(GraphicsSystem.class);
    LightComponent lightComponent = entities.createComponent(entity, LightComponent.class);
    LightType lightType = LightType.fromString(jsonValue.getString("lightType"));
    float range = jsonValue.getFloat("range", 1f);
    float intensity = jsonValue.getFloat("intensity", 1f);
    float innerAngle = jsonValue.getFloat("innerAngle", 0f);
    float outerAngle = jsonValue.getFloat("outerAngle", 0f);
    if (innerAngle > outerAngle) {
        innerAngle = outerAngle;
    }
    if (range < 1.0f) {
        range = 1.0f;
    }
    JsonValue colorJson = jsonValue.get("color");
    Color color = new Color(colorJson.getFloat("r"), colorJson.getFloat("g"), colorJson.getFloat("b"), colorJson.getFloat("a"));
    /*JsonValue directionJson = jsonValue.get("direction");
        Vector3 direction = VectorPool.getVector3();

        if (directionJson != null) {
            direction.set(
                    directionJson.getFloat("x"),
                    directionJson.getFloat("y"),
                    directionJson.getFloat("z"));
        }*/
    NhgLight light = null;
    switch(lightType) {
        case DIRECTIONAL_LIGHT:
            light = NhgLight.directional(intensity, color);
            break;
        case POINT_LIGHT:
            light = NhgLight.point(intensity, range, color);
            break;
        case SPOT_LIGHT:
            light = NhgLight.spot(intensity, range, innerAngle, outerAngle, color);
            break;
    }
    if (light == null)
        return;
    Environment environment = graphicsSystem.getEnvironment();
    NhgLightsAttribute attribute = (NhgLightsAttribute) environment.get(NhgLightsAttribute.Type);
    if (attribute == null) {
        attribute = new NhgLightsAttribute();
        environment.set(attribute);
    }
    attribute.lights.add(light);
    lightComponent.light = light;
    lightComponent.type = lightType;
    output = lightComponent;
}
Also used : LightType(io.github.voidzombie.nhglib.enums.LightType) LightComponent(io.github.voidzombie.nhglib.runtime.ecs.components.graphics.LightComponent) Color(com.badlogic.gdx.graphics.Color) JsonValue(com.badlogic.gdx.utils.JsonValue) GraphicsSystem(io.github.voidzombie.nhglib.runtime.ecs.systems.impl.GraphicsSystem) Environment(com.badlogic.gdx.graphics.g3d.Environment) NhgLightsAttribute(io.github.voidzombie.nhglib.graphics.lights.NhgLightsAttribute) NhgLight(io.github.voidzombie.nhglib.graphics.lights.NhgLight)

Example 22 with Environment

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

the class ShaderUtils method createPrefix.

public static String createPrefix(Renderable renderable, boolean skinned) {
    String prefix = "";
    final int n = renderable.meshPart.mesh.getVertexAttributes().size();
    for (int i = 0; i < n; i++) {
        final VertexAttribute attr = renderable.meshPart.mesh.getVertexAttributes().get(i);
        if (attr.usage == VertexAttributes.Usage.BoneWeight) {
            prefix += "#define boneWeight" + attr.unit + "Flag\n";
        }
    }
    if (skinned) {
        prefix += "#define skinningFlag\n";
    }
    Environment environment = renderable.environment;
    // Ambient lighting
    ColorAttribute ambientLightAttribute = (ColorAttribute) environment.get(ColorAttribute.AmbientLight);
    if (ambientLightAttribute != null) {
        prefix += "#define ambientLighting\n";
    }
    // Directional lighting
    DirectionalLightsAttribute directionalLightsAttribute = (DirectionalLightsAttribute) environment.get(DirectionalLightsAttribute.Type);
    if (directionalLightsAttribute != null) {
        Array<DirectionalLight> directionalLights = directionalLightsAttribute.lights;
        if (directionalLights.size > 0) {
            prefix += "#define numDirectionalLights " + directionalLights.size + "\n";
        }
    }
    // Point lighting
    PointLightsAttribute pointLightsAttribute = (PointLightsAttribute) environment.get(PointLightsAttribute.Type);
    if (pointLightsAttribute != null) {
        Array<PointLight> pointLights = pointLightsAttribute.lights;
        if (pointLights.size > 0) {
            prefix += "#define numPointLights " + pointLights.size + "\n";
        }
    }
    // Spot lighting
    SpotLightsAttribute spotLightsAttribute = (SpotLightsAttribute) environment.get(SpotLightsAttribute.Type);
    if (spotLightsAttribute != null) {
        Array<SpotLight> spotLights = spotLightsAttribute.lights;
        if (spotLights.size > 0) {
            prefix += "#define numSpotLights " + spotLights.size + "\n";
        }
    }
    return prefix;
}
Also used : SpotLight(com.badlogic.gdx.graphics.g3d.environment.SpotLight) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight) Environment(com.badlogic.gdx.graphics.g3d.Environment) PointLight(com.badlogic.gdx.graphics.g3d.environment.PointLight)

Example 23 with Environment

use of com.badlogic.gdx.graphics.g3d.Environment 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

Environment (com.badlogic.gdx.graphics.g3d.Environment)23 ColorAttribute (com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)17 DirectionalLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalLight)17 ModelBatch (com.badlogic.gdx.graphics.g3d.ModelBatch)13 Material (com.badlogic.gdx.graphics.g3d.Material)12 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)10 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)10 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)8 CameraInputController (com.badlogic.gdx.graphics.g3d.utils.CameraInputController)6 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)4 Texture (com.badlogic.gdx.graphics.Texture)4 Model (com.badlogic.gdx.graphics.g3d.Model)4 DefaultShaderProvider (com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider)4 DirectionalShadowLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight)3 PointLight (com.badlogic.gdx.graphics.g3d.environment.PointLight)3 DepthShaderProvider (com.badlogic.gdx.graphics.g3d.utils.DepthShaderProvider)3 MeshPartBuilder (com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder)3 JsonValue (com.badlogic.gdx.utils.JsonValue)3 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 SpotLight (com.badlogic.gdx.graphics.g3d.environment.SpotLight)2