Search in sources :

Example 21 with Line

use of com.jme3.scene.shape.Line in project jmonkeyengine by jMonkeyEngine.

the class MotionPath method CreateCatmullRomPath.

private Geometry CreateCatmullRomPath() {
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", ColorRGBA.Blue);
    Geometry lineGeometry = new Geometry("line", new Curve(spline, 10));
    lineGeometry.setMaterial(mat);
    return lineGeometry;
}
Also used : Geometry(com.jme3.scene.Geometry) Curve(com.jme3.scene.shape.Curve) Material(com.jme3.material.Material)

Example 22 with Line

use of com.jme3.scene.shape.Line in project jmonkeyengine by jMonkeyEngine.

the class Context method createProgramFromSourceFilesWithInclude.

/**
     * Creates a program object from the provided source code and files.
     * The source code is made up from the specified include string first, 
     * then all files specified by the resource array (array of asset paths)
     * are loaded by the provided asset manager and appended to the source code.
     * <p>
     * The typical use case is:
     * <ul>
     *  <li>The include string contains some compiler constants like the grid size </li>
     *  <li>Some common OpenCL files used as libraries (Convention: file names end with {@code .clh}</li>
     *  <li>One main OpenCL file containing the actual kernels (Convention: file name ends with {@code .cl})</li>
     * </ul>
     * 
     * After the files were combined, additional include statements are resolved
     * by {@link #createProgramFromSourceCodeWithDependencies(java.lang.String, com.jme3.asset.AssetManager) }.
     * 
     * @param assetManager the asset manager used to load the files
     * @param include an additional include string
     * @param resources an array of asset paths pointing to OpenCL source files
     * @return the new program objects
     * @throws AssetNotFoundException if a file could not be loaded
     */
public Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, List<String> resources) {
    StringBuilder str = new StringBuilder();
    str.append(include);
    for (String res : resources) {
        AssetInfo info = assetManager.locateAsset(new AssetKey<String>(res));
        if (info == null) {
            throw new AssetNotFoundException("Unable to load source file \"" + res + "\"");
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(info.openStream()))) {
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                str.append(line).append('\n');
            }
        } catch (IOException ex) {
            LOG.log(Level.WARNING, "unable to load source file '" + res + "'", ex);
        }
    }
    return createProgramFromSourceCodeWithDependencies(str.toString(), assetManager);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) IOException(java.io.IOException) AssetInfo(com.jme3.asset.AssetInfo)

Example 23 with Line

use of com.jme3.scene.shape.Line in project jmonkeyengine by jMonkeyEngine.

the class BitmapFontLoader method load.

private BitmapFont load(AssetManager assetManager, String folder, InputStream in) throws IOException {
    MaterialDef spriteMat = (MaterialDef) assetManager.loadAsset(new AssetKey("Common/MatDefs/Misc/Unshaded.j3md"));
    BitmapCharacterSet charSet = new BitmapCharacterSet();
    Material[] matPages = null;
    BitmapFont font = new BitmapFont();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String regex = "[\\s=]+";
    font.setCharSet(charSet);
    String line;
    while ((line = reader.readLine()) != null) {
        String[] tokens = line.split(regex);
        if (tokens[0].equals("info")) {
            // Get rendered size
            for (int i = 1; i < tokens.length; i++) {
                if (tokens[i].equals("size")) {
                    charSet.setRenderedSize(Integer.parseInt(tokens[i + 1]));
                }
            }
        } else if (tokens[0].equals("common")) {
            // Fill out BitmapCharacterSet fields
            for (int i = 1; i < tokens.length; i++) {
                String token = tokens[i];
                if (token.equals("lineHeight")) {
                    charSet.setLineHeight(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("base")) {
                    charSet.setBase(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("scaleW")) {
                    charSet.setWidth(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("scaleH")) {
                    charSet.setHeight(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("pages")) {
                    // number of texture pages
                    matPages = new Material[Integer.parseInt(tokens[i + 1])];
                    font.setPages(matPages);
                }
            }
        } else if (tokens[0].equals("page")) {
            int index = -1;
            Texture tex = null;
            for (int i = 1; i < tokens.length; i++) {
                String token = tokens[i];
                if (token.equals("id")) {
                    index = Integer.parseInt(tokens[i + 1]);
                } else if (token.equals("file")) {
                    String file = tokens[i + 1];
                    if (file.startsWith("\"")) {
                        file = file.substring(1, file.length() - 1);
                    }
                    TextureKey key = new TextureKey(folder + file, true);
                    key.setGenerateMips(false);
                    tex = assetManager.loadTexture(key);
                    tex.setMagFilter(Texture.MagFilter.Bilinear);
                    tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
                }
            }
            // set page
            if (index >= 0 && tex != null) {
                Material mat = new Material(spriteMat);
                mat.setTexture("ColorMap", tex);
                mat.setBoolean("VertexColor", true);
                mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
                matPages[index] = mat;
            }
        } else if (tokens[0].equals("char")) {
            // New BitmapCharacter
            BitmapCharacter ch = null;
            for (int i = 1; i < tokens.length; i++) {
                String token = tokens[i];
                if (token.equals("id")) {
                    int index = Integer.parseInt(tokens[i + 1]);
                    ch = new BitmapCharacter();
                    charSet.addCharacter(index, ch);
                } else if (token.equals("x")) {
                    ch.setX(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("y")) {
                    ch.setY(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("width")) {
                    ch.setWidth(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("height")) {
                    ch.setHeight(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("xoffset")) {
                    ch.setXOffset(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("yoffset")) {
                    ch.setYOffset(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("xadvance")) {
                    ch.setXAdvance(Integer.parseInt(tokens[i + 1]));
                } else if (token.equals("page")) {
                    ch.setPage(Integer.parseInt(tokens[i + 1]));
                }
            }
        } else if (tokens[0].equals("kerning")) {
            // Build kerning list
            int index = 0;
            int second = 0;
            int amount = 0;
            for (int i = 1; i < tokens.length; i++) {
                if (tokens[i].equals("first")) {
                    index = Integer.parseInt(tokens[i + 1]);
                } else if (tokens[i].equals("second")) {
                    second = Integer.parseInt(tokens[i + 1]);
                } else if (tokens[i].equals("amount")) {
                    amount = Integer.parseInt(tokens[i + 1]);
                }
            }
            BitmapCharacter ch = charSet.getCharacter(index);
            ch.addKerning(second, amount);
        }
    }
    return font;
}
Also used : InputStreamReader(java.io.InputStreamReader) BitmapCharacterSet(com.jme3.font.BitmapCharacterSet) Material(com.jme3.material.Material) MaterialDef(com.jme3.material.MaterialDef) Texture(com.jme3.texture.Texture) BufferedReader(java.io.BufferedReader) BitmapCharacter(com.jme3.font.BitmapCharacter) BitmapFont(com.jme3.font.BitmapFont)

Example 24 with Line

use of com.jme3.scene.shape.Line in project jmonkeyengine by jMonkeyEngine.

the class J3MLoader method loadFromRoot.

private void loadFromRoot(List<Statement> roots) throws IOException {
    if (roots.size() == 2) {
        Statement exception = roots.get(0);
        String line = exception.getLine();
        if (line.startsWith("Exception")) {
            throw new AssetLoadException(line.substring("Exception ".length()));
        } else {
            throw new IOException("In multiroot material, expected first statement to be 'Exception'");
        }
    } else if (roots.size() != 1) {
        throw new IOException("Too many roots in J3M/J3MD file");
    }
    boolean extending = false;
    Statement materialStat = roots.get(0);
    String materialName = materialStat.getLine();
    if (materialName.startsWith("MaterialDef")) {
        materialName = materialName.substring("MaterialDef ".length()).trim();
        extending = false;
    } else if (materialName.startsWith("Material")) {
        materialName = materialName.substring("Material ".length()).trim();
        extending = true;
    } else {
        throw new IOException("Specified file is not a Material file");
    }
    String[] split = materialName.split(":", 2);
    if (materialName.equals("")) {
        throw new MatParseException("Material name cannot be empty", materialStat);
    }
    if (split.length == 2) {
        if (!extending) {
            throw new MatParseException("Must use 'Material' when extending.", materialStat);
        }
        String extendedMat = split[1].trim();
        MaterialDef def = (MaterialDef) assetManager.loadAsset(new AssetKey(extendedMat));
        if (def == null) {
            throw new MatParseException("Extended material " + extendedMat + " cannot be found.", materialStat);
        }
        material = new Material(def);
        material.setKey(key);
        material.setName(split[0].trim());
    //            material.setAssetName(fileName);
    } else if (split.length == 1) {
        if (extending) {
            throw new MatParseException("Expected ':', got '{'", materialStat);
        }
        materialDef = new MaterialDef(assetManager, materialName);
        // NOTE: pass file name for defs so they can be loaded later
        materialDef.setAssetName(key.getName());
    } else {
        throw new MatParseException("Cannot use colon in material name/path", materialStat);
    }
    for (Statement statement : materialStat.getContents()) {
        split = statement.getLine().split("[ \\{]");
        String statType = split[0];
        if (extending) {
            if (statType.equals("MaterialParameters")) {
                readExtendingMaterialParams(statement.getContents());
            } else if (statType.equals("AdditionalRenderState")) {
                readAdditionalRenderState(statement.getContents());
            } else if (statType.equals("Transparent")) {
                readTransparentStatement(statement.getLine());
            }
        } else {
            if (statType.equals("Technique")) {
                readTechnique(statement);
            } else if (statType.equals("MaterialParameters")) {
                readMaterialParams(statement.getContents());
            } else {
                throw new MatParseException("Expected material statement, got '" + statType + "'", statement);
            }
        }
    }
}
Also used : Statement(com.jme3.util.blockparser.Statement) IOException(java.io.IOException)

Example 25 with Line

use of com.jme3.scene.shape.Line in project jmonkeyengine by jMonkeyEngine.

the class HelloAssets method simpleInitApp.

@Override
public void simpleInitApp() {
    /** Load a teapot model (OBJ file from test-data) */
    Spatial teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");
    Material mat_default = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
    teapot.setMaterial(mat_default);
    rootNode.attachChild(teapot);
    /** Create a wall (Box with material and texture from test-data) */
    Box box = new Box(2.5f, 2.5f, 1.0f);
    Spatial wall = new Geometry("Box", box);
    Material mat_brick = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat_brick.setTexture("ColorMap", assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg"));
    wall.setMaterial(mat_brick);
    wall.setLocalTranslation(2.0f, -2.5f, 0.0f);
    rootNode.attachChild(wall);
    /** Display a line of text (default font from test-data) */
    setDisplayStatView(false);
    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    BitmapText helloText = new BitmapText(guiFont, false);
    helloText.setSize(guiFont.getCharSet().getRenderedSize());
    helloText.setText("Hello World");
    helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);
    guiNode.attachChild(helloText);
    /** Load a Ninja model (OgreXML + material + texture from test_data) */
    Spatial ninja = assetManager.loadModel("Models/Ninja/Ninja.mesh.xml");
    ninja.scale(0.05f, 0.05f, 0.05f);
    ninja.rotate(0.0f, -3.0f, 0.0f);
    ninja.setLocalTranslation(0.0f, -5.0f, -2.0f);
    rootNode.attachChild(ninja);
    /** You must add a light to make the model visible */
    DirectionalLight sun = new DirectionalLight();
    sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal());
    rootNode.addLight(sun);
}
Also used : Geometry(com.jme3.scene.Geometry) BitmapText(com.jme3.font.BitmapText) Spatial(com.jme3.scene.Spatial) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Material(com.jme3.material.Material) Box(com.jme3.scene.shape.Box)

Aggregations

Vector3f (com.jme3.math.Vector3f)8 Material (com.jme3.material.Material)5 Geometry (com.jme3.scene.Geometry)5 Statement (com.jme3.util.blockparser.Statement)4 IOException (java.io.IOException)4 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)3 FloatBuffer (java.nio.FloatBuffer)3 AssetLoadException (com.jme3.asset.AssetLoadException)2 ShaderNodeDefinitionKey (com.jme3.asset.ShaderNodeDefinitionKey)2 Vector2f (com.jme3.math.Vector2f)2 Mesh (com.jme3.scene.Mesh)2 Box (com.jme3.scene.shape.Box)2 Curve (com.jme3.scene.shape.Curve)2 Line (com.jme3.scene.shape.Line)2 Texture (com.jme3.texture.Texture)2 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 AssetInfo (com.jme3.asset.AssetInfo)1