Custom Mesh texture coordinate error

Hello,

I’m trying to implement a plugins so i wan have a grid Editor binded to JME3, i’m starting to understand how to install and make plugins but i run to a problem, when i use the plugins i got an error as :

java.lang.IllegalArgumentException at java.nio.Buffer.limit(Buffer.java:267) at com.jme3.scene.VertexBuffer.copyElements(VertexBuffer.java:886) at jme3tools.optimize.GeometryBatchFactory.mergeGeometries(GeometryBatchFactory.java:227) at com.kingofmultiverse.hexsystem.ChunkGenerator.getChunk(ChunkGenerator.java:95) at com.kingofmultiverse.hexsystem.ChunkGenerator.generateChunk(ChunkGenerator.java:57) at com.kingofmultiverse.plugins.hexgrideditor.HexMap.getNewHexMap(HexMap.java:42) at com.kingofmultiverse.plugins.hexgrideditor.HexMap.doCreateSpatial(HexMap.java:29) at com.jme3.gde.core.sceneexplorer.nodes.actions.AbstractNewSpatialAction$1$1.call(AbstractNewSpatialAction.java:70) at com.jme3.gde.core.sceneexplorer.nodes.actions.AbstractNewSpatialAction$1$1.call(AbstractNewSpatialAction.java:67) at com.jme3.app.AppTask.invoke(AppTask.java:142) at com.jme3.app.Application.runQueuedTasks(Application.java:583) at com.jme3.app.Application.update(Application.java:596) at com.jme3.gde.core.scene.SceneApplication.update(SceneApplication.java:302) at com.jme3.system.awt.AwtPanelsContext.updateInThread(AwtPanelsContext.java:188) at com.jme3.system.awt.AwtPanelsContext.access$100(AwtPanelsContext.java:44) at com.jme3.system.awt.AwtPanelsContext$AwtPanelsListener.update(AwtPanelsContext.java:68) at com.jme3.system.lwjgl.LwjglOffscreenBuffer.runLoop(LwjglOffscreenBuffer.java:125) at com.jme3.system.lwjgl.LwjglOffscreenBuffer.run(LwjglOffscreenBuffer.java:151) at java.lang.Thread.run(Thread.java:722)
After a few try i found where my error is comming from, the method used to initiate the texCoord is messing all down @_@, i've commented out the line and all is working fine with a unshaded materials. But why does it send me this error ? I need the text coord, so how can i fix it. The code used as main plugin, no error comming from this so far, i'm putting it in case... i dono it can help, maybe : [java] package com.kingofmultiverse.plugins.hexgrideditor;

import com.kingofmultiverse.hexsystem.ChunkGenerator;
import com.jme3.gde.core.sceneexplorer.nodes.actions.AbstractNewSpatialAction;
import com.jme3.gde.core.sceneexplorer.nodes.actions.NewSpatialAction;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

@org.openide.util.lookup.ServiceProvider(service = NewSpatialAction.class)
public class HexMap extends AbstractNewSpatialAction {

public HexMap() {
    name = "HexMap...";
}

@Override
protected Spatial doCreateSpatial(Node parent) {
    Node chunks = getNewHexMap(5,5, parent.getWorldTranslation());
    return chunks;
}

/**
 * 
 * @param mapSizeX
 * @param mapSizeY
 * @param HexMapPos parent Node world position.
 * @return Node with all chunk attach on it.
 */
public Node getNewHexMap(int mapSizeX, int mapSizeY, Vector3f HexMapPos){
    Node result = new Node("Chunks");
    ChunkGenerator chunkGenerator = new ChunkGenerator(HexMapPos);
    Geometry[]chunk = new Geometry[mapSizeY];

    for(int i = 0; i < mapSizeY; i++){
        if(i % 2 == 1) {              //if is odd row
            chunk[i] = chunkGenerator.generateChunk(mapSizeX, i, true);
        } else {
            chunk[i] = chunkGenerator.generateChunk(mapSizeX, i);
        }
        result.attachChild(chunk[i]);
    }
    
    return result;
}

}[/java]

The other one, the one who got the error, the one whoes doing stuff :
The error come from “private Vector2f getTriTexCoord()” method and i don’t understand the error as i’ve say before.
[java]/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package com.kingofmultiverse.hexsystem;

import com.jme3.asset.AssetManager;
import com.jme3.gde.core.assets.ProjectAssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.VertexBuffer;
import com.jme3.util.BufferUtils;
import java.util.ArrayList;
import jme3tools.optimize.GeometryBatchFactory;

/**
*

  • @author roah
    */
    public class ChunkGenerator {
    private final float hexSize = 1f; //rayon du hex
    private final AssetManager assetManager;
    private final Vector3f hexMapPos;
    private int gridPosition;
    private int count;
    private boolean odd;
    private float hexWidth = FastMath.sqrt(3)/2 * (hexSize * 2);

    /**

    • Grid Editor Constructor.
    • @param hexMapPos
      */
      public ChunkGenerator(Vector3f hexMapPos){
      this(null, hexMapPos);
      }

    /**

    • Run Time constructor.
    • @param assetManager To load texture and object.
    • @param hexMapPos To get the chunk world position.
      */
      public ChunkGenerator(AssetManager assetManager, Vector3f hexMapPos){
      this.assetManager = assetManager;
      this.hexMapPos = hexMapPos;
      }

    /**

    • Generate an Chunk with odd set as false.
    • @param count Hex number on X axis.
    • @param gridPosition Chunk position on Y axis.
    • @return Newly generated chunk.
      */
      public Geometry generateChunk(int count, int gridPosition) {
      return this.generateChunk(count, gridPosition, false);
      }

    /**

    • Generate a chunk where we can change if it’s odd.

    • @param count Hex number on X axis.

    • @param gridPosition Chunk position on Y axis.

    • @param odd

    • @return Newly generated chunk.
      */
      public Geometry generateChunk(int count, int gridPosition, boolean odd) {
      this.count = count;
      this.gridPosition = gridPosition;
      this.odd = odd;

      return getChunk();
      }

    private Geometry getChunk(){
    Vector3f triVertices = getTriVerticesPosition();
    Vector3f quadVertices = getQuadVerticesPosition();

// Vector2f triTexCoord = getTriTexCoord(); //@todo Not working, sends error
// Vector2f quadTexCoord = getQuadTexCoord(); //@todo Not finished
int triIndex = getTriIndex();
int quadIndex = getQuadIndex();

    Mesh[] chunk = {new Mesh(), new Mesh()};
    
    chunk[0].setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(triVertices));

// chunk[0].setBuffer(VertexBuffer.Type.TexCoord, 3, BufferUtils.createFloatBuffer(triTexCoord)); //@todo Not working, sends error
chunk[0].setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(triIndex));
chunk[0].updateBound();

    chunk[1].setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(quadVertices));

// quadMesh.setBuffer(VertexBuffer.Type.TexCoord, 3, BufferUtils.createFloatBuffer(texCoord));
chunk[1].setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(quadIndex));
chunk[1].updateBound();

    ArrayList<Geometry> geoChunk = new ArrayList<Geometry>();
    geoChunk.add(new Geometry("chunk "+gridPosition, chunk[0]));
    geoChunk.add(new Geometry("chunk "+gridPosition, chunk[1]));
    
    Mesh finalChunk = new Mesh();
    GeometryBatchFactory.mergeGeometries(geoChunk, finalChunk);
    
    return generateJMESpatial(finalChunk);
}

/**
 * @todo updateMesh or rebuildMesh Method.
 */

private int[] getQuadIndex(){
    int[] index = new int[]{2,3,0, 1,2,0};
    return index;
}

private int[] getTriIndex(){
    int[] index = new int[(count*2)*3];
    
    int j = 0;
    for(int i = 0; i < (count*2)*3; i+= 6){
        //Hex in Y+
        index[i] = j+5;
        index[i+1] = j+1;
        index[i+2] = j;
        
        //Hex in Y-
        index[i+3] = j+2;
        if(((i+6) < ((count*2)*3))){
            index[i+4] = j+6;
        } else {
            index[i+4] = j+4;
        }
        index[i+5] = j+3;
        j+= 4;
    }
    
    /**
     * XY == World position
     * ++++++++++++++++++++++++++++++++++++++=> Y-
     * +++++[03]++++[07]++++[11]++++[15]+++++
     * +[02]++++[06]++++[10]++++[14]++++[16]+
     * +++++[XY]++++[XY]++++[XY]++++[XY]+++++
     * +[01]++++[05]++++[09]++++[13]++++[17]+
     * +++++[00]++++[04]++++[08]++++[12]+++++
     * ++++++++++++++++++++++++++++++++++++++=> Y+
     */
    return index;
}

/**
 * @todo Can be improve?
 * @return Array of all tri needed to complete the Hex.
 */
private Vector3f[] getTriVerticesPosition() {
    Vector3f[] vertices = new Vector3f[count*4+2];
    float currentGridPosition =  ((gridPosition * (hexSize)*2));
    if(gridPosition != 0){
        currentGridPosition *= 0.75f;
    }
    int j = 0;
    float currentHex;
    for(int i = 0; i < count*4; i+=4){
        currentHex = j*hexWidth;
        if(odd){
            currentHex -= hexWidth/2;
        }
        vertices[i] = new Vector3f(hexMapPos.x + currentHex, hexMapPos.y, hexMapPos.z + currentGridPosition + hexSize);
        vertices[i+1] = new Vector3f(hexMapPos.x + currentHex -(hexWidth/2), hexMapPos.y, hexMapPos.z + currentGridPosition +(hexSize/2));
        vertices[i+2] = new Vector3f(hexMapPos.x + currentHex -(hexWidth/2), hexMapPos.y, hexMapPos.z + currentGridPosition -(hexSize/2));
        vertices[i+3] = new Vector3f(hexMapPos.x + currentHex, hexMapPos.y, hexMapPos.z + currentGridPosition - hexSize);
        j++;
    }
    currentHex = j*hexWidth;
    if(odd){
        currentHex -= hexWidth/2;
    }
    vertices[count*4] = new Vector3f(hexMapPos.x + currentHex - (hexWidth/2), hexMapPos.y, hexMapPos.z + currentGridPosition -(hexSize/2));
    vertices[count*4+1] = new Vector3f(hexMapPos.x + currentHex - (hexWidth/2), hexMapPos.y, hexMapPos.z + currentGridPosition +(hexSize/2));
    
    return vertices;
}

/**
 * The chunk center are merged on X to form two tri so we gain on performance.
 * @todo Can be improve on merging all chunk together but it can cost more than the gain, (need a try?).
 * @return Center Array of the chunk.
 */
private Vector3f[] getQuadVerticesPosition(){
    Vector3f[] vertices = new Vector3f[4];
    float currentGridPosition = (((gridPosition) * (hexSize)*2));
    float decallage = 0f;
    if(gridPosition != 0){
        currentGridPosition *= 0.75f;
    }
    if(odd){
        decallage = -(hexWidth/2);
    }
    vertices[0] = new Vector3f(hexMapPos.x + decallage -(hexWidth/2), hexMapPos.y, hexMapPos.z + currentGridPosition + (hexSize/2));
    vertices[1] = new Vector3f(hexMapPos.x + (count * hexWidth)-(hexWidth/2) + decallage, hexMapPos.y, hexMapPos.z + currentGridPosition + (hexSize/2));
    vertices[2] = new Vector3f(hexMapPos.x + (count * hexWidth)-(hexWidth/2) + decallage, hexMapPos.y, hexMapPos.z + currentGridPosition - (hexSize/2));
    vertices[3] = new Vector3f(hexMapPos.x + decallage -(hexWidth/2), hexMapPos.y, hexMapPos.z + currentGridPosition - (hexSize/2));
    
    return vertices;
}

/**
 * @todo textCoord not working, it send an error.
 * @return 
 */
private Vector2f[] getTriTexCoord(){
    Vector2f[] texCoord = new Vector2f[count*4+2];
    int j = 0;
    for(int i = 0; i < count*4; i+=4){
        texCoord[j] = new Vector2f(0.5f, 0f);
        texCoord[j+1] = new Vector2f(0.5f - hexWidth/2, 0.25f);
        texCoord[j+2] = new Vector2f(0.5f - hexWidth/2, 0.75f);
        texCoord[j+3] = new Vector2f(0.5f, 1f);
        j++;
    }
    texCoord[count*4] = new Vector2f(0.5f + hexWidth/2, 0.75f);
    texCoord[count*4+1] = new Vector2f(0.5f + hexWidth/2,0.25f);
    
    return texCoord;
}

private Geometry generateJMESpatial(Mesh finalChunk) {
    Geometry finalGeometryChunk = new Geometry("chunk "+gridPosition, finalChunk);
    Material mat;
    if(assetManager != null){
        mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    } else {
        ProjectAssetManager pm = new ProjectAssetManager();
        mat = new Material(pm, "Common/MatDefs/Misc/Unshaded.j3md"); //@todo testing
    }
    mat.setColor("Color", ColorRGBA.Gray);
    finalGeometryChunk.setMaterial(mat);

// mat.getAdditionalRenderState().setWireframe(true); //Debuging purpose.
return finalGeometryChunk;
}
}[/java]

Thanks in advance for the time you will accord to me.
Ps : I’m starting in java, in programmation in fact if you got some tips to improve this i’m open to any suggestion even if it’s not why i’ve made the subject for. Thx again