Search in sources :

Example 16 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project libgdx by libgdx.

the class G3dModelLoader method parseMaterials.

private void parseMaterials(ModelData model, JsonValue json, String materialDir) {
    JsonValue materials = json.get("materials");
    if (materials == null) {
    // we should probably create some default material in this case
    } else {
        model.materials.ensureCapacity(materials.size);
        for (JsonValue material = materials.child; material != null; material = material.next) {
            ModelMaterial jsonMaterial = new ModelMaterial();
            String id = material.getString("id", null);
            if (id == null)
                throw new GdxRuntimeException("Material needs an id.");
            jsonMaterial.id = id;
            // Read material colors
            final JsonValue diffuse = material.get("diffuse");
            if (diffuse != null)
                jsonMaterial.diffuse = parseColor(diffuse);
            final JsonValue ambient = material.get("ambient");
            if (ambient != null)
                jsonMaterial.ambient = parseColor(ambient);
            final JsonValue emissive = material.get("emissive");
            if (emissive != null)
                jsonMaterial.emissive = parseColor(emissive);
            final JsonValue specular = material.get("specular");
            if (specular != null)
                jsonMaterial.specular = parseColor(specular);
            final JsonValue reflection = material.get("reflection");
            if (reflection != null)
                jsonMaterial.reflection = parseColor(reflection);
            // Read shininess
            jsonMaterial.shininess = material.getFloat("shininess", 0.0f);
            // Read opacity
            jsonMaterial.opacity = material.getFloat("opacity", 1.0f);
            // Read textures
            JsonValue textures = material.get("textures");
            if (textures != null) {
                for (JsonValue texture = textures.child; texture != null; texture = texture.next) {
                    ModelTexture jsonTexture = new ModelTexture();
                    String textureId = texture.getString("id", null);
                    if (textureId == null)
                        throw new GdxRuntimeException("Texture has no id.");
                    jsonTexture.id = textureId;
                    String fileName = texture.getString("filename", null);
                    if (fileName == null)
                        throw new GdxRuntimeException("Texture needs filename.");
                    jsonTexture.fileName = materialDir + (materialDir.length() == 0 || materialDir.endsWith("/") ? "" : "/") + fileName;
                    jsonTexture.uvTranslation = readVector2(texture.get("uvTranslation"), 0f, 0f);
                    jsonTexture.uvScaling = readVector2(texture.get("uvScaling"), 1f, 1f);
                    String textureType = texture.getString("type", null);
                    if (textureType == null)
                        throw new GdxRuntimeException("Texture needs type.");
                    jsonTexture.usage = parseTextureUsage(textureType);
                    if (jsonMaterial.textures == null)
                        jsonMaterial.textures = new Array<ModelTexture>();
                    jsonMaterial.textures.add(jsonTexture);
                }
            }
            model.materials.add(jsonMaterial);
        }
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) JsonValue(com.badlogic.gdx.utils.JsonValue) ModelMaterial(com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial) ModelTexture(com.badlogic.gdx.graphics.g3d.model.data.ModelTexture)

Example 17 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project libgdx by libgdx.

the class G3dModelLoader method parseNodesRecursively.

private ModelNode parseNodesRecursively(JsonValue json) {
    ModelNode jsonNode = new ModelNode();
    String id = json.getString("id", null);
    if (id == null)
        throw new GdxRuntimeException("Node id missing.");
    jsonNode.id = id;
    JsonValue translation = json.get("translation");
    if (translation != null && translation.size != 3)
        throw new GdxRuntimeException("Node translation incomplete");
    jsonNode.translation = translation == null ? null : new Vector3(translation.getFloat(0), translation.getFloat(1), translation.getFloat(2));
    JsonValue rotation = json.get("rotation");
    if (rotation != null && rotation.size != 4)
        throw new GdxRuntimeException("Node rotation incomplete");
    jsonNode.rotation = rotation == null ? null : new Quaternion(rotation.getFloat(0), rotation.getFloat(1), rotation.getFloat(2), rotation.getFloat(3));
    JsonValue scale = json.get("scale");
    if (scale != null && scale.size != 3)
        throw new GdxRuntimeException("Node scale incomplete");
    jsonNode.scale = scale == null ? null : new Vector3(scale.getFloat(0), scale.getFloat(1), scale.getFloat(2));
    String meshId = json.getString("mesh", null);
    if (meshId != null)
        jsonNode.meshId = meshId;
    JsonValue materials = json.get("parts");
    if (materials != null) {
        jsonNode.parts = new ModelNodePart[materials.size];
        int i = 0;
        for (JsonValue material = materials.child; material != null; material = material.next, i++) {
            ModelNodePart nodePart = new ModelNodePart();
            String meshPartId = material.getString("meshpartid", null);
            String materialId = material.getString("materialid", null);
            if (meshPartId == null || materialId == null) {
                throw new GdxRuntimeException("Node " + id + " part is missing meshPartId or materialId");
            }
            nodePart.materialId = materialId;
            nodePart.meshPartId = meshPartId;
            JsonValue bones = material.get("bones");
            if (bones != null) {
                nodePart.bones = new ArrayMap<String, Matrix4>(true, bones.size, String.class, Matrix4.class);
                int j = 0;
                for (JsonValue bone = bones.child; bone != null; bone = bone.next, j++) {
                    String nodeId = bone.getString("node", null);
                    if (nodeId == null)
                        throw new GdxRuntimeException("Bone node ID missing");
                    Matrix4 transform = new Matrix4();
                    JsonValue val = bone.get("translation");
                    if (val != null && val.size >= 3)
                        transform.translate(val.getFloat(0), val.getFloat(1), val.getFloat(2));
                    val = bone.get("rotation");
                    if (val != null && val.size >= 4)
                        transform.rotate(tempQ.set(val.getFloat(0), val.getFloat(1), val.getFloat(2), val.getFloat(3)));
                    val = bone.get("scale");
                    if (val != null && val.size >= 3)
                        transform.scale(val.getFloat(0), val.getFloat(1), val.getFloat(2));
                    nodePart.bones.put(nodeId, transform);
                }
            }
            jsonNode.parts[i] = nodePart;
        }
    }
    JsonValue children = json.get("children");
    if (children != null) {
        jsonNode.children = new ModelNode[children.size];
        int i = 0;
        for (JsonValue child = children.child; child != null; child = child.next, i++) {
            jsonNode.children[i] = parseNodesRecursively(child);
        }
    }
    return jsonNode;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Quaternion(com.badlogic.gdx.math.Quaternion) JsonValue(com.badlogic.gdx.utils.JsonValue) ModelNodePart(com.badlogic.gdx.graphics.g3d.model.data.ModelNodePart) Vector3(com.badlogic.gdx.math.Vector3) ModelNode(com.badlogic.gdx.graphics.g3d.model.data.ModelNode) Matrix4(com.badlogic.gdx.math.Matrix4)

Example 18 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project libgdx by libgdx.

the class G3dModelLoader 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");
    model.id = json.getString("id", "");
    parseMeshes(model, json);
    parseMaterials(model, json, handle.parent().path());
    parseNodes(model, json);
    parseAnimations(model, json);
    return model;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ModelData(com.badlogic.gdx.graphics.g3d.model.data.ModelData) JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 19 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project libgdx by libgdx.

the class UBJsonTest method create.

@Override
public void create() {
    try {
        UBJsonWriter uw = new UBJsonWriter(Gdx.files.external(fn).write(false));
        uw.object();
        uw.set(longString, longString);
        uw.set("0floats", new float[] {});
        uw.set("3floats", new float[] { 1, 2, 3.456789f });
        uw.set("xfloats", new float[] { Float.MIN_VALUE, Float.MAX_VALUE, Float.NaN, Float.NEGATIVE_INFINITY });
        uw.set("double", 0.000000000000000000001);
        uw.set("long", Long.MAX_VALUE);
        uw.set("3bytes", new byte[] { (byte) 1, (byte) 2, (byte) 3 });
        uw.set("3shorts", new short[] { (short) 1, (short) 2, (short) 3 });
        uw.set("3ints", new int[] { 1, 2, 3 });
        uw.set("3long", new long[] { 1l, 2l, 3l });
        uw.set("3double", new double[] { 1, 2, 3.456789 });
        uw.set("3char", new char[] { 'a', 'b', 'c' });
        uw.set("3strings", new String[] { "", "a", "abc" });
        uw.array("arr");
        uw.object().pop();
        uw.value(true).value(false).value(true);
        uw.value((byte) 254);
        uw.value((byte) (-2));
        uw.value((short) -32000);
        uw.value((int) -123456);
        uw.value((long) (-((1 << 63) - 1)));
        uw.pop();
        uw.pop();
        uw.close();
        UBJsonReader ur = new UBJsonReader();
        ur.oldFormat = false;
        JsonValue v = ur.parse(Gdx.files.external(fn));
        Gdx.app.log("UBJsonTest", "result = \n" + v.toString());
        performanceTest();
        Gdx.app.log("UBJsonTest", "Test succeeded");
    } catch (Throwable t) {
        Gdx.app.error("UBJsonTest", "Test failed", t);
    }
}
Also used : UBJsonWriter(com.badlogic.gdx.utils.UBJsonWriter) JsonValue(com.badlogic.gdx.utils.JsonValue) UBJsonReader(com.badlogic.gdx.utils.UBJsonReader)

Example 20 with JsonValue

use of com.badlogic.gdx.utils.JsonValue 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)

Aggregations

JsonValue (com.badlogic.gdx.utils.JsonValue)25 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)7 Array (com.badlogic.gdx.utils.Array)5 Color (com.badlogic.gdx.graphics.Color)3 Environment (com.badlogic.gdx.graphics.g3d.Environment)3 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)3 Matrix4 (com.badlogic.gdx.math.Matrix4)3 Vector3 (com.badlogic.gdx.math.Vector3)3 FileHandle (com.badlogic.gdx.files.FileHandle)2 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 Quaternion (com.badlogic.gdx.math.Quaternion)2 Json (com.badlogic.gdx.utils.Json)2 ReadOnlySerializer (com.badlogic.gdx.utils.Json.ReadOnlySerializer)2 SerializationException (com.badlogic.gdx.utils.SerializationException)2 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)2 ArrayListGameObject (com.nilunder.bdx.GameObject.ArrayListGameObject)2 ModelComponent (io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent)2 NodeComponent (io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent)2 GraphicsSystem (io.github.voidzombie.nhglib.runtime.ecs.systems.impl.GraphicsSystem)2