Add VertexColor to an heightmap

Hi there!



Recently I’ve been trying to add vertex colors and remove any UVs (simply because I don’t need them). So I thought about creating a custom mesh. I read all the tutorial but I get an error with the VertexColor material.



This is my error:

GRAVE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
com.jme3.asset.AssetLoadException: VertexColor.j3md has been marked as obsolete. Please use Unshaded.j3md instead.
at com.jme3.material.plugins.J3MLoader.loadFromScanner(J3MLoader.java:513)
at com.jme3.material.plugins.J3MLoader.load(J3MLoader.java:594)
at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:240)
at com.jme3.material.Material.(Material.java:124)
at mygame.TEST2.simpleInitApp(TEST2.java:101)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:230)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:124)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:200)
at java.lang.Thread.run(Thread.java:662)


and this is my code:
[java]HillHeightMap heightmap = null;
try {
heightmap = new HillHeightMap(513, 1000, 50, 100);
} catch (Exception ex) {
ex.printStackTrace();
}

mesh = new Mesh();

// Setup map avec vertexcolor
float[] buffer = heightmap.getHeightMap();
int sqrt = (int)Math.sqrt(buffer.length);

// Vertices
Vector3f[] vertices = new Vector3f[buffer.length];
for(int x = 0; x < sqrt; x++)
for(int z = 0; z < sqrt; z++)
{
vertices[(x * sqrt) + z] = new Vector3f(x, buffer[(x * sqrt) + z], z); // !!!!!!!
}
buffer = null;

// Indices
int[] indices = new int[(sqrt - 1) * (sqrt - 1) * 6];
int counter = 0;
for (int y = 0; y < sqrt - 1; y++)
{
for (int x = 0; x < sqrt - 1; x++)
{
int lowerLeft = x + y*sqrt;
int lowerRight = (x + 1) + y*sqrt;
int topLeft = x + (y + 1) * sqrt;
int topRight = (x + 1) + (y + 1) * sqrt;

indices[counter++] = topLeft;
indices[counter++] = lowerRight;
indices[counter++] = lowerLeft;

indices[counter++] = topLeft;
indices[counter++] = topRight;
indices[counter++] = lowerRight;
}
}

// Colors
float[] colors = new float[vertices.length * 4];
for(int i = 4; i < vertices.length * 4; i = i + 4)
{
if(vertices.y > 75)
{
colors = 1; // R
colors = 1; // G
colors = 1; // B
colors = 1; // A 1 = opaque, 0 = fully transparent
}
else
{
colors = 1; // R
colors = 0; // G
colors = 1; // B
colors = 1; // A 1 = opaque, 0 = fully transparent
}
}

mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
mesh.setBuffer(Type.Color, 2, BufferUtils.createFloatBuffer(colors));
mesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indices));
mesh.updateBound();

Geometry coloredMesh = new Geometry ("ColoredMesh", mesh);
Material matVC = new Material(assetManager, "Common/MatDefs/Misc/VertexColor.j3md");
coloredMesh.setMaterial(matVC);
rootNode.attachChild(coloredMesh);[/java]

Thank you for your help! :)

I even tryed with the easy tutorial and I still get the same error D:

[java]Mesh m = new Mesh();

Vector3f [] vertices = new Vector3f[4];

vertices[0] = new Vector3f(0,0,0);

vertices[1] = new Vector3f(3,0,0);

vertices[2] = new Vector3f(0,3,0);

vertices[3] = new Vector3f(3,3,0);

int [] indexes = { 2,0,1, 1,3,2 };

float[] colorArray = new float[44];

int colorIndex = 0;

for(int i = 0; i < 4; i++)

{

// Red value (is increased by .2 on each next vertex here)

colorArray[colorIndex++]= 0.1f+(.2f
i);

// Green value (is reduced by .2 on each next vertex)

colorArray[colorIndex++]= 0.9f-(0.2f*i);

// Blue value (remains the same in our case)

colorArray[colorIndex++]= 0.5f;

// Alpha value (no transparency set here)

colorArray[colorIndex++]= 1.0f;

}

m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));

m.setBuffer(Type.Color, 2, colorArray);

m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));

m.updateBound();

Geometry coloredMesh = new Geometry ("ColoredMesh", m);

Material matVC = new Material(assetManager, "Common/MatDefs/Misc/VertexColor.j3md");

coloredMesh.setMaterial(matVC);

rootNode.attachChild(coloredMesh);[/java]

Is it:

com.jme3.asset.AssetLoadException: VertexColor.j3md has been marked as obsolete. Please use Unshaded.j3md instead. ?



I would try changing

[java]Material matVC = new Material(assetManager, "Common/MatDefs/Misc/VertexColor.j3md");[/java]



to

[java]Material matVC = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");[/java]

Yea, but how do you want me to use verticex colors with the unshaded shader? :open_mouth: And yes, that’s the error.

is there any requirements so that you cannot use textures?

Well, it’s the style of the game, inspired from Tesseract http://tesseract-fps.sourceforge.net/. Also, I’m totally noob to make textures and map them onto my model in Blender lol.

well, it’s really not that hard to create the textures that simulate the vertex coloring. create an image (even you can do it in paint) which has the size of vertices, paint each pixel to the color you would add in code to the vertex, and you’re done. :slight_smile:

1 Like

That’s a good idea! But I think I didn’t eplain myself well sorry. D: I can import ogrexml models with vertex colors, but I can’t create meshes in jme with vertex colors. For exemple, my model is perfect, but if I create a simple box with vertex colors, it bugs. I think I’ll just have to live with it.



Anyway, thank you for your responses!



PS: By the way, how can you make meshes in jme flat shaded (I know for blender but how can I do it with a simple box for exemple)?

This tutorial shows how to create a custom mesh with vertex colors:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_meshes

I know Momoko_Fan, that’s the one I followed. The problem is not to assign vertex colors or to make a custom mesh. The problem is the shader VertexColor because jme3 won’t let me use because it’s obsolete.

Just use Unshaded and set VertexColor to true

Thank you, but I still get a weird image and I don’t know why. D: I don’t think it’s the colors I assigned because even when I follow the tutorial, the square is all green. I used the Unshaded sahder with VertexColor enable.

When I run the TestCustomMesh i get green → purple gradient … Are you using the SVN/nightly version of jME3?

Usually yes, but my jme platform still can’t download all the updates for an unknown reason.