Search in sources :

Example 1 with LightComponent

use of io.github.voidzombie.nhglib.runtime.ecs.components.graphics.LightComponent 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 2 with LightComponent

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

the class LightingSystem method process.

@Override
protected void process(int entityId) {
    NodeComponent node = nodeMapper.get(entityId);
    LightComponent light = lightMapper.get(entityId);
    light.light.position.set(node.getTranslation());
    light.light.setTransform(node.getTransform());
    switch(light.type) {
        case SPOT_LIGHT:
        case DIRECTIONAL_LIGHT:
            Matrix4 tempMatrix = MatrixPool.getMatrix4();
            tempMatrix.set(node.getTransform());
            tempMatrix.translate(0f, 1f, 0f);
            Vector3 direction = VectorPool.getVector3();
            Vector3 tempVec = VectorPool.getVector3();
            direction.set(light.light.position).sub(tempMatrix.getTranslation(tempVec));
            light.light.direction.set(direction);
            MatrixPool.freeMatrix4(tempMatrix);
            VectorPool.freeVector3(direction, tempVec);
            break;
    }
}
Also used : LightComponent(io.github.voidzombie.nhglib.runtime.ecs.components.graphics.LightComponent) NodeComponent(io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent) Vector3(com.badlogic.gdx.math.Vector3) Matrix4(com.badlogic.gdx.math.Matrix4)

Aggregations

LightComponent (io.github.voidzombie.nhglib.runtime.ecs.components.graphics.LightComponent)2 Color (com.badlogic.gdx.graphics.Color)1 Environment (com.badlogic.gdx.graphics.g3d.Environment)1 Matrix4 (com.badlogic.gdx.math.Matrix4)1 Vector3 (com.badlogic.gdx.math.Vector3)1 JsonValue (com.badlogic.gdx.utils.JsonValue)1 LightType (io.github.voidzombie.nhglib.enums.LightType)1 NhgLight (io.github.voidzombie.nhglib.graphics.lights.NhgLight)1 NhgLightsAttribute (io.github.voidzombie.nhglib.graphics.lights.NhgLightsAttribute)1 NodeComponent (io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent)1 GraphicsSystem (io.github.voidzombie.nhglib.runtime.ecs.systems.impl.GraphicsSystem)1