Linking path with black and white image


#1

So custom code based terrains at a beginner stage without the depth more straight forward. I generate quads with a texture from a black and white image. There is not any physics to it so you pass thru it. I am working on it and made sequential progress. Although there is a testJME3 collision that uses a wireframe box as a floor.

I get the pixel and if it is black i create a quad. If it is white i ignore that pixel. This presentation can be used at your disposable.

Here is what it created
gen_path

public Quad[] generateQuad(int array_loc, float x, float y, float z, int colorType){
    Quad[] sq = new Quad[ARRAY_SIZE];
    
    
    if(array_loc != ARRAY_SIZE){
        sq[array_loc] = new Quad(1f, 1f);
        Mesh m = new Geometry("", sq[array_loc]).getMesh();
        geom = new Geometry("quad", m);
    }
    
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    if(colorType  == 0){
        mat.setColor("Color", ColorRGBA.DarkGray);
        mat.setTexture("ColorMap",
            assetManager.loadTexture("Textures/shade_green.jpg"));
    }else if(colorType  == 1){
        mat.setColor("Color", ColorRGBA.DarkGray);
        mat.setTexture("ColorMap",
            assetManager.loadTexture("Textures/shade_light_green.jpg"));
    }
    
    geom.setMaterial(mat);
    
    geom.setLocalScale(new Vector3f(1,1,1));
    geom.setLocalTranslation(new Vector3f(x,y,z));
    
    r = new Quaternion();
    r.fromAngleAxis(-FastMath.PI / 2, new Vector3f(1, 0, 0));
    geom.setLocalRotation(r);

    pivot.attachChild(geom);
    
    return sq;
}

private Image convertImage(Image image, Image.Format newFormat) {
    
   
    int width = image.getWidth();
    int height = image.getHeight();
    ByteBuffer data = BufferUtils.createByteBuffer( (int)Math.ceil(newFormat.getBitsPerPixel() / 8.0) * width * height);
    Image convertedImage = new Image(newFormat, width, height, data,null, image.getColorSpace());
    
    ImageRaster sourceReader = ImageRaster.create(image);
    ImageRaster targetWriter = ImageRaster.create(convertedImage);
    float f = 0.003921569f;
    
    
    ColorRGBA clr = new ColorRGBA().set(f, f, f, 1.0f);
    ColorRGBA clr1 = new ColorRGBA().set(1.0f, 1.0f, 1.0f, 1.0f);
    
    int ar_ = 0;
    
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            ColorRGBA color = sourceReader.getPixel(x, y);
                if(clr.equals(color)){
                    this.generateQuad(ar_, x, 0, y, 0);
                    if(ar_ != ARRAY_SIZE)ar_++;
                }
            targetWriter.setPixel(x, y, color);
            
        }
    }

    return convertedImage;
}

I am still learning JMONKEY yet studing programming Java for several years off and on. Could use some help adding depth.


#2

path_