Showing posts with label JMonkeyEngine. Show all posts
Showing posts with label JMonkeyEngine. Show all posts

Tuesday, November 24, 2020

JMonkey Engine - How to apply texture

අද අපි බලමු කොහොමද  texture  එකක්  apply  කර ගන්නේ කියල. මුලින්ම වෙනදා වගේ අළුත්  class එකක් හදා ගන්න. ඊට පස්සේ මේ පහළ තියෙන  code  එක copy paste කරන්න. මේ  code  එකින් කරන්නේ  box  එකක් හදලා එකට material  එකක් දාගෙන ඊට පස්සේ  texture එකක්  apply කරන එක. 

package hello3D.test;

import com.jme3.app.SimpleApplication;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.util.TangentBinormalGenerator;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;

public class HelloMaterial extends SimpleApplication {

    public static void main(String[] args) {
        HelloMaterial app = new HelloMaterial();
        app.start();
    }

    @Override
    public void simpleInitApp() {

        Box cubeMesh = new Box(1f, 1f, 1f);
        Geometry cube1Geo = new Geometry("Shiny rock", cubeMesh);
        TangentBinormalGenerator.generate(cubeMesh);           // for lighting effect
        Material cubeMat = new Material(assetManager,
                "Common/MatDefs/Light/Lighting.j3md");
        cubeMat.setTexture("DiffuseMap",
                assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));
        cubeMat.setTexture("NormalMap",
                assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));
        cubeMat.setBoolean("UseMaterialColors", true);
        cubeMat.setColor("Diffuse", ColorRGBA.White);
        cubeMat.setColor("Specular", ColorRGBA.White);
        cubeMat.setFloat("Shininess", 64f);  // [0,128]
        cube1Geo.setMaterial(cubeMat);
        cube1Geo.setLocalTranslation(0, 0, 2); // Move it a bit
        cube1Geo.rotate(1.6f, 5f, 2f);          // Rotate it a bit
        rootNode.attachChild(cube1Geo);
        /**
         * Must add a light to make the lit object visible!
         */
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal());
        sun.setColor(ColorRGBA.White);
        rootNode.addLight(sun);

    }
}

මෙන්න මේ ආකාරයට තමයි  cube  එක දිස් වෙන්නේ.






මම හිතනවා ඔයාලා දෙයක් ඉගෙන ගන්න ඇති කියල. තව පොස්ට් එකකින් හම්බ වෙමු.

-Hasini Senanayaka-

Wednesday, November 4, 2020

Game Development - JmonkeyEngine - Adding GUI

3D games සාමාන්යයෙන් full-screen window එකක play කරන්නේ. කෙසේ වෙතත් standard Swing application එකක jME 3 canvas කැන්වස් එකක් embed කරන්න පුළුවන්. ඒක තමයි අද අපි මේ කරන්න යන්නේ. මේක ප්‍රයෝජනවත් වන්නේ ඔයා user interface එකක interactive 3D viewer එකක් වගේ දානවානම්. උදාහරණයක් ලෙස interactive scientific demo එකක්, level editor එකක් හෝ  game character designer එකක් ගන්න පුළුවන්.
මේ පහළ තියෙන්නේ interactive scientific demo එකක්. එකේ 3D viewer එකක් UI ඇතුලේ embed කරලා තියෙනවා.

Code city - Used to visualize source code 3 dimensionally.


SimpleApplication හි කැමරාව mouse එක මගින් වෙනස් කරන්න පුළුවන් උනත් swing application එකක එහෙම කරන්න බැහැ. ඒ නිසා වෙනම camera action එක define කරන්න ඕනේ මේ වගේ.

public void simpleInitApp() {

// activate windowed input behaviour flyCam.setDragToRotate(true); // Set up inputs and load your scene as usual ... }

මෙහිදී වෙනස් වන ප්‍රධාන දෙය වන්නේ main() method එකය. අපි සුපුරුදු විදියට start() method එක call කරන්නේ නැහැ. ඒ වෙනුවට අපි Runnable()  එකක් හදල ඒකින් Swing jFrame නිර්මාණය කරනවා.ඊට පස්සේ jFrame එකට Canvas එකක් එකතු කරලා startCanvas() මගින් game එක run කරනවා.

Swing එක thread-safe නැහැ. ඒ නිසා canvas එකත් up-to-date නැහැ. ඒ නිසා තමයි runnable එකක් යොදා ගන්නේ.

public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { // ... see below ... } }); }

Here in the run() method, we start the jME application, create its canvas, create a Swing frame, and add everything together.
 com.jme3.system.AppSettings වලින් Swing panel එකේ size එක specify  කරන්න ඕනේ. 

AppSettings settings = new AppSettings(true); settings.setWidth(640); settings.setHeight(480);


මෙහෙම තමයි canvas එකක් හදන්නේ. com.jme3.system.JmeCanvasContext මගින් canvas එක configure කරගන්න පුළුවන්. setSystemListener() මගින් canvas එක අදාළ events(creation , destroy, update) Listener එකට යැවීම සිදු කරනවා..

SwingCanvasTest canvasApplication = new SwingCanvasTest();
canvasApplication.setSettings(settings);
canvasApplication.createCanvas(); // create canvas!
JmeCanvasContext ctx = (JmeCanvasContext) canvasApplication.getContext();
ctx.setSystemListener(canvasApplication);
Dimension dim = new Dimension(640, 480);
ctx.getCanvas().setPreferredSize(dim);

මෙහම තමයි jFrame එකක් හදන්නේ. 

JFrame window = new JFrame("Swing Application");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ඊට පස්සේ JPanel එකක් create කර ගන්න ඕනේ JFrame එක ඇතුලේ.

JPanel panel = new JPanel(new FlowLayout()); // a panel
// add all your Swing components ...
panel.add(new JButton("Some Swing Component"));
...
// add the JME canvas
panel.add(ctx.getCanvas());JPanel එක JFrame එක ඇතුලට add කරන්නේ මේ විදියට.
window.add(panel);
window.pack();
window.setVisible(true);

application එක start කරන්න startCanvas() method එක call කරන්න ඕනේ. 

canvasApplication.startCanvas();

මේ පහල තියෙන්නේ full code එක. එහෙනම් compile කරලා බලන්නකෝ. මොනවා හරි ප්‍රශ්නයක් ආවොත් පහලින් comment එකක් දාන්න. එහෙනම් මම නවතිනවා. අලුත් පෝස්ට් එකකින් හම්බ වෙමු.

package hello3D.test;

import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import com.jme3.math.ColorRGBA;

import com.jme3.app.LegacyApplication;

import com.jme3.system.AppSettings;

import com.jme3.system.JmeCanvasContext;

import com.jme3.util.JmeFormatter;

import java.awt.BorderLayout;

import java.awt.Canvas;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.FlowLayout;


import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.concurrent.Callable;

import java.util.logging.ConsoleHandler;

import java.util.logging.Handler;

import java.util.logging.Logger;

import javax.swing.*;


public class HelloWorld extends SimpleApplication {


    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                // create new JME appsettings

                AppSettings settings = new AppSettings(true);

                settings.setWidth(640);

                settings.setHeight(480);


                // create new canvas application

                HelloWorld canvasApplication = new HelloWorld();

                canvasApplication.setSettings(settings);

                canvasApplication.createCanvas(); // create canvas!


                JmeCanvasContext ctx = (JmeCanvasContext) canvasApplication.getContext();


                ctx.setSystemListener(canvasApplication);


                Dimension dim = new Dimension(640, 480);


                ctx.getCanvas().setPreferredSize(dim);

                JFrame window = new JFrame("Swing Application");

                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel panel = new JPanel(new FlowLayout()); // a panel

                // add all your Swing components ...

                panel.add(new JButton("Some Swing Component"));


                // add the JME canvas

                panel.add(ctx.getCanvas());


                window.add(panel);

                window.pack();

                window.setVisible(true);

                canvasApplication.startCanvas();

            }

        });

    }


    @Override


    public void simpleInitApp() {

        // activate windowed input behaviour

        flyCam.setDragToRotate(true);


        // Set up inputs and load your scene as usual

        Box b = new Box(1, 1, 1); // create cube shape

        Geometry geom = new Geometry("Box", b);  // create cube geometry from the shape

        Material mat = new Material(assetManager,

                "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material

        mat.setColor("Color", ColorRGBA.Blue);   // set color of material to blue

        geom.setMaterial(mat);                   // set the cube's material

        rootNode.attachChild(geom);              // make the cube appear in the scene

    }

}


Saturday, October 24, 2020

Game Development-JMonkeyEngine - Part2

මං අද කරන්න යන්නේ  මගේ netbeans IDE එක jMonkeyEngine එකත් එක්ක වැඩ කරන්න setup කරගන්න

එක. අපි බලමු කොහොමද කරන්නේ කියල. ඉස්සෙල්ලම අපි download කර ගත්තු zip file එක unzip කරගන්න ඕනි.

එතකොට මෙන්න මේ ආකාරයෙන්  තමයි දිස් වෙන්නේ.

 දැන් ඒක පැත්තකින් තියලා  අපි  යමු netbeans IDE  දිහාවට.

මේ පහලින් තියෙන්නේ netbeans IDE  එක. අපි ඉස්සෙල්ලම  අලුත් project එකක් හදන්න ඕන.

File => new project  මගීන්  අලුත් project එකක් හදන්න පුළුවන්


Java Application තෝරන්න. 


මේ ආකාරයෙන් project එකට නමක් සහ save වන ලොකේෂන් එක ලබාදෙන්න


Project  එක create  උනාට පස්සේ  මේ  ආකාරයෙන්  බලාගන්න  පුළුවන්.  දැන් අපි jmonkey engine 

run කරන්න ඕන JAR File  ටික add  කරගන්න ඕන.  

ඒකට  ්‍රොජෙක්ට් එකේ LIbrary folder  එකමත right click  කරලා Add JAR/Folder select කරන්න.

 

ඊට පස්සේ  unzip කරගත්ත  folder එකේ lib එකට ගිහින්  ඒක ඇතුළෙ තියෙන  ඔක්කොම JAR ෆයිල් 

ටික select කරලා ඕපන් දෙන්න 


ඊට පස්සේ  project එක උඩ right click කරලා  project properties යන්න. 

ඒකෙ තියෙන run tab එකට යන්න. දැන් අපි run වෙන්න ඕන configuration ලබා දෙන්න ඕන. 


ඊට පස්සේ අපි අලුතෙන් පැකේජ් එකක්  හදා ගන්න  ඕනේ.


 

ඊට පස්සේ ඒ පැකේජ්  එකට   අලුත්  java class එකක්  එකතු  කර  ගන්න ඕන.


 එතකොට මෙන්න මේ ආකාරයෙන් තමයි  java class එක පේන්නේ.


දැන් මේ පහල code එක copy කරලා paste කරන්න.  මතක ඇතුව  මේ  code එකේ තියෙන  class name එක 

ඔයාගේ class name එක විදිහට  වෙනස්  කරන්න. 

package hello3D.test;


import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import com.jme3.math.ColorRGBA;


/** Sample 1 - how to get started with the most simple JME 3 application.

 * Display a blue 3D cube and view from all sides by

 * moving the mouse and pressing the WASD keys. */

public class HelloWorld extends SimpleApplication {


    public static void main(String[] args){

        HelloWorld app = new HelloWorld();

        app.start(); // start the game

    }


    @Override

    public void simpleInitApp() {

        Box b = new Box(1, 1, 1); // create cube shape

        Geometry geom = new Geometry("Box", b);  // create cube geometry from the shape

        Material mat = new Material(assetManager,

          "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material

        mat.setColor("Color", ColorRGBA.Blue);   // set color of material to blue

        geom.setMaterial(mat);                   // set the cube's material

        rootNode.attachChild(geom);              // make the cube appear in the scene

    }

}


දැන් ඔක්කොම හරි.  අපි දැන් අපේ project එක run කරල බලමු. 

Run කරද්දි මුලින්ම වෙන්න මේ වගේ screen එකක් එයි.  එක continue දෙන්න.


අපි ඇත්තටම මේ code  එකෙන් කරේ  නිල් පාට box එකක්  හදන එක.  අපි අනිත් 3d modeling software  ගත්තොත්

අපිට ඕනේ object drag and drop කරන්න පුළුවන්.  ඒත් මේකෙ ඒ හැම object එකක්ම හදන්න වෙන්නේ

කෝඩ් කරලා. 


සුදු පාටින් සදහන් කරලා තියෙන keys පාවිච්චි කරල  object එක rotate කරන්න පුළුවන්. 

මම ඊළඟ පෝස්ට් එකෙන්  code  එක explain කරන්නම්.


 ස්තුතියි


-Hasini Senanayaka-



Wednesday, October 21, 2020

Game Development-JMonkeyEngine - Part1

JMonkeyEngine යනු විශේෂයෙන් 3D game development සඳහා සාදන ලද game engine  එකක්. එය shader technology පුළුල් ලෙස භාවිතා  කරනවා. මෙම එන්ජිම භාවිතා කරමින් ඇන්ඩ්‍රොයිඩ් සහ ඩෙස්ක්ටොප් උපාංග සඳහා ත්‍රිමාණ ක්‍රීඩා  ලියන්න පුළුවන්. jMonkeyEngine එක Java වලින් ලියා ඇති අතර එහි default renderer ලෙස LWJGL භාවිතා  වනවා. ඒ වගේම මෙය OpenGL 2 සඳහා පූර්ණ සහය දක්වනු  ලබනවා.

මේ තියන්නේ jMonkey engine එක  පාවිච්චි කරලා හදපු ගේම් ටිකක්. පේනවා ඇතිනේ කොච්චර ලස්සනද කියලා.

 දැන් අපි බලමු කොහොමද jmonkey engine  එකෙන් වැඩ ගන්නේ කියලා.

JMonkeyEngine භාවිතා කිරීමට විවිධ ක්‍රම  තියෙනවා:

  • Download the SDK
  • Download the engine
  • Add the libraries to a build script
මේ පහලින් තියෙන්නේ SDK එක. එක design කරලා තියෙන්නේ Netbeans IDE එක වගේ. මේ ලින්ක් එකින් SDK එක බාගන්න පුළුවන්.


jMonkeyEngine එකට SDK එකම පාවිච්චි කරන්න ඕනේ කියලා නියමයක්  නැහැ. එසේම නිශ්චිත IDE එකක් හෝ නැහැ. ඔබට ජාවා පිළිබද සාමාන්‍ය දැනීමක් ඇත්නම් ඕනෑම IDE එකක් භාවිතා  කරන්න පුළුවන්. (eg:- Netbeans, Intellij).

ඔබ online dependencies භාවිතා කිරීමට කැමති නම්, ඔබට ඒවා jcenter වෙතින් ලබා ගත හැකිය. Intellij වැනි IDE භාවිතා කරන පරිශීලකයින් අතර online dependencies වඩාත් ජනප්‍රියයි.
repositories {
    jcenter()
}

dependencies {
    implementation "org.jmonkeyengine:jme3-core:3.3.2-stable"
    implementation "org.jmonkeyengine:jme3-desktop:3.3.2-stable"
    implementation "org.jmonkeyengine:jme3-lwjgl:3.3.2-stable" 
}

මං කලින් කිව්වා වගේ SDK  එක පාවිච්චි නොකර  තමන්ට කැමති IDE එකක්  පාවිච්චි කරන්න පුළුවන්.  එතකොට IDE  එකට අදාල Engine එක සහ එහි dependencies බාගත කරගන්න ඕන. මේවා ඔබේ ජාවා ව්‍යාපෘතියට වෙනත් library එක මෙන් ඇතුළත්  කරන්න පුළුවන්.  

මේ ලින්ක් එකින් Engine එක බාගන්න පුළුවන්.

https://github.com/jMonkeyEngine/jmonkeyengine/releases


 මේ පොස්ට් එකෙන් අපි JmonkeyEngine  සඳහා අවශ්‍ය වන tools  ටික ඩවුන්ලෝඩ් කරගත්තා නේ.  ඊළඟ  පෝස්ට් එකෙන්  බලමු කොහොමද setup  කරගන්නේ කියලා. 



-Hasini Senanayaka-