Search in sources :

Example 76 with JsonValue

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

the class G3dModelLoader method parseMeshes.

private void parseMeshes(ModelData model, JsonValue json) {
    JsonValue meshes = json.get("meshes");
    if (meshes != null) {
        model.meshes.ensureCapacity(meshes.size);
        for (JsonValue mesh = meshes.child; mesh != null; mesh = mesh.next) {
            ModelMesh jsonMesh = new ModelMesh();
            String id = mesh.getString("id", "");
            jsonMesh.id = id;
            JsonValue attributes = mesh.require("attributes");
            jsonMesh.attributes = parseAttributes(attributes);
            jsonMesh.vertices = mesh.require("vertices").asFloatArray();
            JsonValue meshParts = mesh.require("parts");
            Array<ModelMeshPart> parts = new Array<ModelMeshPart>();
            for (JsonValue meshPart = meshParts.child; meshPart != null; meshPart = meshPart.next) {
                ModelMeshPart jsonPart = new ModelMeshPart();
                String partId = meshPart.getString("id", null);
                if (partId == null) {
                    throw new GdxRuntimeException("Not id given for mesh part");
                }
                for (ModelMeshPart other : parts) {
                    if (other.id.equals(partId)) {
                        throw new GdxRuntimeException("Mesh part with id '" + partId + "' already in defined");
                    }
                }
                jsonPart.id = partId;
                String type = meshPart.getString("type", null);
                if (type == null) {
                    throw new GdxRuntimeException("No primitive type given for mesh part '" + partId + "'");
                }
                jsonPart.primitiveType = parseType(type);
                jsonPart.indices = meshPart.require("indices").asShortArray();
                parts.add(jsonPart);
            }
            jsonMesh.parts = parts.toArray(ModelMeshPart.class);
            model.meshes.add(jsonMesh);
        }
    }
}
Also used : ModelMesh(com.badlogic.gdx.graphics.g3d.model.data.ModelMesh) Array(com.badlogic.gdx.utils.Array) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ModelMeshPart(com.badlogic.gdx.graphics.g3d.model.data.ModelMeshPart) JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 77 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 78 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 79 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 80 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)

Aggregations

JsonValue (com.badlogic.gdx.utils.JsonValue)148 JsonReader (com.badlogic.gdx.utils.JsonReader)27 IOException (java.io.IOException)21 Array (com.badlogic.gdx.utils.Array)20 Json (com.badlogic.gdx.utils.Json)15 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)14 FileHandle (com.badlogic.gdx.files.FileHandle)11 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)10 BladeJson (com.bladecoder.engine.serialization.BladeJson)9 HashMap (java.util.HashMap)8 Color (com.badlogic.gdx.graphics.Color)7 GraphBoxImpl (com.gempukku.libgdx.graph.ui.graph.GraphBoxImpl)7 ArrayList (java.util.ArrayList)7 Vector2 (com.badlogic.gdx.math.Vector2)6 Actor (com.badlogic.gdx.scenes.scene2d.Actor)6 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)6 Action (com.bladecoder.engine.actions.Action)6 File (java.io.File)6 ObjectMap (com.badlogic.gdx.utils.ObjectMap)5 SerializationException (com.badlogic.gdx.utils.SerializationException)5