Membuat Terrain dari Gambar pada JMonkeyEngine


 

hasil terrain

hasil terrain

Pada JMonkeyEngine terdapat fasilitas untuk membuat map melalui gambar grayscale. Fasilitas tersebut menggunakan image processing sebagai pengenalan warna pada gambar. Kita dapat mendefinisikan warna putih untuk ketinggian dan warna hitam untuk warna rendah. Jadi kita tidak perlu mendefinisikan titik-titik atau pun mengekspor file 3d dari 3d editor seperti 3ds max dan Blender.

 

Pertama kita buat gambar peta grayscale kita..šŸ˜€

peta grayscale

peta grayscale

Kemudian kita buat gambar untuk dataran tinggi, dataran rendah dan menengah :

rumput.jpg

rumput.jpg

pasir.jpg

pasir.jpg

dasar.jpg

dasar.jpg

Sekarang kita menuju ke coding, di sini saya menggunakan library JMonkeyEngine versi 2.0.1, di semua gambar tadi masukkan ke package org/febfeb/game/data/. Dan coding ini berada pada package org/febfeb/game/core

/*
 *  Copyright 2009 Febrianto Arif R.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.febfeb.game.core;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.util.ImageBasedHeightMap;
import com.jmex.terrain.util.ProceduralTextureGenerator;
import java.net.URL;
import javax.swing.ImageIcon;
import org.febfeb.game.kerajaan.testing.HelloTerrain;

/**
 *
 * @author Febrianto Arif Rakhman
 */
public class CobaTerrain extends SimpleGame {

    public static void main(String[] args){
        Njajal app = new Njajal();
        app.setConfigShowMode(ConfigShowMode.AlwaysShow);
        app.start();
    }
    protected void simpleInitGame() {
        URL grayScale=HelloTerrain.class.getClassLoader().getResource("org/febfeb/game/data/peta2.jpg");

        URL waterImage=HelloTerrain.class.getClassLoader().getResource("org/febfeb/game/data/dasar.jpg");
        URL dirtImage=HelloTerrain.class.getClassLoader().getResource("org/febfeb/game/data/pasir.jpg");
        URL highest=HelloTerrain.class.getClassLoader().getResource("org/febfeb/game/data/rumput.jpg");

        ImageBasedHeightMap ib=new ImageBasedHeightMap(
                new ImageIcon(grayScale).getImage()
        );
        TerrainBlock tb=new TerrainBlock("map",ib.getSize(),
                new Vector3f(.5f,.05f,.5f),ib.getHeightMap(),
                new Vector3f(0,0,0));

        //  Create an object to generate textured terrain from the image based height map.
        ProceduralTextureGenerator pg=new ProceduralTextureGenerator(ib);
        //  Look like water from height 0-60 with the strongest "water look" at 30
        pg.addTexture(new ImageIcon(waterImage),0,30,60);
        //  Look like dirt from height 40-120 with the strongest "dirt look" at 80
        pg.addTexture(new ImageIcon(dirtImage),40,80,120);
        //  Look like highest (pure white) from height 110-256 with the strongest "white look" at 130
        pg.addTexture(new ImageIcon(highest),110,130,256);

        pg.createTexture(256);
        TextureState ts=display.getRenderer().createTextureState();

        ts.setTexture(
                TextureManager.loadTexture(
                        pg.getImageIcon().getImage(),
                        Texture.MinificationFilter.Trilinear,
                        Texture.MagnificationFilter.Bilinear,
                        true
                )
        );
        tb.setRenderState(ts);
        tb.setModelBound(new BoundingBox());
        tb.updateModelBound();

        rootNode.attachChild(tb);
    }

}

Nah, pada coding terdapat classĀ ImageBasedMap, nah class itu yang membuat kita dapat me-load map berupa gambaršŸ˜‰

One thought on “Membuat Terrain dari Gambar pada JMonkeyEngine

  1. Ping balik: Satya's Blog » Game Engine

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s