List of usage examples for com.badlogic.gdx.graphics.g3d.model.data ModelMaterial ModelMaterial
ModelMaterial
From source file:com.kevlanche.threedeetest.ScalableObjLoader.java
License:Apache License
/** loads .mtl file */ public void load(FileHandle file) { String line;/*from www .ja va 2 s .c o m*/ String[] tokens; String curMatName = "default"; Color difcolor = Color.WHITE; Color speccolor = Color.WHITE; float opacity = 1.f; float shininess = 0.f; String texFilename = null; if (file == null || file.exists() == false) return; BufferedReader reader = new BufferedReader(new InputStreamReader(file.read()), 4096); try { while ((line = reader.readLine()) != null) { if (line.length() > 0 && line.charAt(0) == '\t') line = line.substring(1).trim(); tokens = line.split("\\s+"); if (tokens[0].length() == 0) { continue; } else if (tokens[0].charAt(0) == '#') continue; else { final String key = tokens[0].toLowerCase(); if (key.equals("newmtl")) { ModelMaterial mat = new ModelMaterial(); mat.id = curMatName; mat.diffuse = new Color(difcolor); mat.specular = new Color(speccolor); mat.opacity = opacity; mat.shininess = shininess; if (texFilename != null) { ModelTexture tex = new ModelTexture(); tex.usage = ModelTexture.USAGE_DIFFUSE; tex.fileName = new String(texFilename); if (mat.textures == null) mat.textures = new Array<ModelTexture>(1); mat.textures.add(tex); } materials.add(mat); if (tokens.length > 1) { curMatName = tokens[1]; curMatName = curMatName.replace('.', '_'); } else curMatName = "default"; difcolor = Color.WHITE; speccolor = Color.WHITE; opacity = 1.f; shininess = 0.f; } else if (key.equals("kd") || key.equals("ks")) // diffuse or specular { float r = Float.parseFloat(tokens[1]); float g = Float.parseFloat(tokens[2]); float b = Float.parseFloat(tokens[3]); float a = 1; if (tokens.length > 4) a = Float.parseFloat(tokens[4]); if (tokens[0].toLowerCase().equals("kd")) { difcolor = new Color(); difcolor.set(r, g, b, a); } else { speccolor = new Color(); speccolor.set(r, g, b, a); } } else if (key.equals("tr") || key.equals("d")) { opacity = Float.parseFloat(tokens[1]); } else if (key.equals("ns")) { shininess = Float.parseFloat(tokens[1]); } else if (key.equals("map_kd")) { texFilename = file.parent().child(tokens[1]).path(); } } } reader.close(); } catch (IOException e) { return; } // last material ModelMaterial mat = new ModelMaterial(); mat.id = curMatName; mat.diffuse = new Color(difcolor); mat.specular = new Color(speccolor); mat.opacity = opacity; mat.shininess = shininess; if (texFilename != null) { ModelTexture tex = new ModelTexture(); tex.usage = ModelTexture.USAGE_DIFFUSE; tex.fileName = new String(texFilename); if (mat.textures == null) mat.textures = new Array<ModelTexture>(1); mat.textures.add(tex); } materials.add(mat); return; }
From source file:com.kevlanche.threedeetest.ScalableObjLoader.java
License:Apache License
public ModelMaterial getMaterial(final String name) { for (final ModelMaterial m : materials) if (m.id.equals(name)) return m; ModelMaterial mat = new ModelMaterial(); mat.id = name;/* w w w.java2 s .c o m*/ mat.diffuse = new Color(Color.WHITE); materials.add(mat); return mat; }
From source file:com.mbrlabs.mundus.commons.g3d.MG3dModelLoader.java
License:Apache License
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 {//from w w w .j av a 2s .co m 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); } } }
From source file:graphics.solids.MS3DModel.java
License:Open Source License
private void processMaterials() { if (!FORCE_DEFAULT_MATERIAL) for (MS3DMaterial mat : ms3d.materials) { ModelMaterial m = new ModelMaterial(); m.id = mat.name;/*from w ww. j a v a 2 s . c o m*/ m.ambient = color(mat.ambient); m.diffuse = color(mat.diffuse); m.emissive = color(mat.emissive); m.specular = color(mat.specular); m.shininess = mat.shininess; m.opacity = mat.transparency; m.type = MaterialType.Phong; if (m.opacity == 0) { m.opacity = 1; } if (!mat.texture.isEmpty()) { ModelTexture tex = new ModelTexture(); if (mat.texture.startsWith(".\\") || mat.texture.startsWith("//")) { mat.texture = mat.texture.substring(2); } if (verbose) I.say("" + mat.texture); tex.fileName = Assets.safePath(basePath + mat.texture); this.setKeyFile(tex.fileName); tex.id = mat.texture; tex.usage = ModelTexture.USAGE_DIFFUSE; m.textures = new Array<ModelTexture>(); m.textures.add(tex); } data.materials.add(m); } ModelMaterial mat = new ModelMaterial(); mat.id = "default"; mat.ambient = new Color(0.8f, 0.8f, 0.8f, 1f); mat.diffuse = new Color(0.8f, 0.8f, 0.8f, 1f); mat.emissive = new Color(0, 0, 0, 0); mat.specular = new Color(0, 0, 0, 0); mat.shininess = 0; mat.opacity = 1; mat.type = MaterialType.Phong; data.materials.add(mat); }