Having a cube with each side having a different color seems like something everyone needs. There is a great page that says that they can be used for retro/1990s games.
Can I make a cube/box with a list of vertices and a list of colors?
Or…
I have two shaders.
2.A) With the first one, input six colors to use for the sides. If I could get the index of the vertex, I could use it to set the color. Two per side. How can I get a vertex index?
2.B) I input a 3 by 4 pixel BufferedImage ColorMap that I generated. I need 1) to add U,V coordinates and 2) to stop the blending between colors. I found a JME UVCoordinatesGenerator but, I need to download the plugin but can’t find it online. http://javadoc.jmonkeyengine.org/com/jme3/scene/plugins/blender/textures/UVCoordinatesGenerator.html
//import com.jme3.scene.plugins.blender.textures.UVCoordinatesGenerator
Or…
Is there another way to make a multicolored cube?
I believe option (1) is the way to go. Create a custom mesh based on com.jme3.scene.shape.Box and add a vertex buffer of type Color. Then apply a material with setBoolean(“UseVertexColor”, true) .
Hummmm…
Would you use “Common/MatDefs/Misc/Unshaded.j3md” or a custom shader? Unshaded.j3md has everything in it; VertexColor for HAS_VERTEXCOLOR.
I set things up like this, but get all black unless I setColor(ColorRGBA.Blue);
Edit: Final code using “Common/MatDefs/Misc/Unshaded.j3md”
private Box box = null;
private Geometry getSixColorCube(){
Geometry geometry = null;
Material material = null;
float[] colorArray;//fixed this
box = new Box(1f, 1f, 1f);
geometry = new Geometry("SixColorBox", box);
material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
colorArray = getColorArray(box);//fixed this
box.setBuffer(VertexBuffer.Type.Color, 4, colorArray);
material.setBoolean("VertexColor", true);
geometry.setMaterial(material);
upright.fromAxes(Vector3f.UNIT_X, Vector3f.UNIT_Z.negate(), Vector3f.UNIT_Y);//1, -1, 1
geometry.setLocalRotation(upright);
//geometry.setLocalTranslation(10f, 7f, -15f);
return(geometry);
}
private float[] getColorArray(Mesh mesh){
float[] tempColorArray = new float[mesh.getVertexCount() * 4];
float r = 1.0f, g = 0.0f, b = 0.0f, a = 1.0f;
for (int y = 0;y < mesh.getVertexCount()/2;y++){
if (y == 0 || y == 1){//bottom
r = 0.0f;g = 1.0f;b = 0.0f;a = 1.0f;//green
}
else if (y == 2 || y == 3){
r = 1.0f;g = 1.0f;b = 1.0f;a = 1.0f;//white
}
else if (y == 4 || y == 5){
r = 0.0f;g = 0.0f;b = 1.0f;a = 1.0f;//blue
}
else if (y == 6 || y == 7){
r = 1.0f;g = 1.0f;b = 0.0f;a = 1.0f;//yellow
}
else if (y == 8 || y == 9){
r = 1.0f;g = 0.0f;b = 0.0f;a = 1.0f;//red
}
else if (y == 10 || y == 11){
r = 1.0f;g = 0.5f;b = 0.0f;a = 1.0f;//orange
}
else{
r = (float)Math.random();g = (float)Math.random();b = (float)Math.random();a = 1.0f;
}
println("tempColorArray[" + y + "]=" + r + "," + g + "," + b + "," + a);
tempColorArray[y * 4 * 2 + 0] = r;//four floats * 2 verts + index
tempColorArray[y * 4 * 2 + 1] = g;
tempColorArray[y * 4 * 2 + 2] = b;
tempColorArray[y * 4 * 2 + 3] = a;
tempColorArray[y * 4 * 2 + 4] = r;
tempColorArray[y * 4 * 2 + 5] = g;
tempColorArray[y * 4 * 2 + 6] = b;
tempColorArray[y * 4 * 2 + 7] = a;
}
return(tempColorArray);
}
i’m currently busy with understanding shaders and one of my challenges was to create that six-color-cube i liked so much in Java3D. I was really suprised how easy it could be to solve tasks not on mesh but on shader base, so here is what i did to easily create such a six-color-cube (btw thanks for the Shader-Wiki at https://jmonkeyengine.github.io/wiki/jme3/advanced/jme3_shaders.html which was my starting point):
Create a material definition file “ColorBoxShader.j3md”:
Create a fragment shader file “ColorBoxShader.frag” which simply sets the color passed from “ColorBoxShader.vert” to “ColorBoxShader.frag” by the variable “varColor”:
I tried that and wanted to pass in the 6 colors. I could pass them in, but I didn’t know how to tell the vertex which color to use. Using Unshaded.j3md works really well.
in my shader example the six colors magenta, green, blue, red, yellow and cyan are calculated in the vertex shader “ColorBoxShader.vert” just to simulate that six-color-cube known from Java3D.
To vary the colors and to be able to pass six colors of your choice to the shader you have to define six color attributes (similar to the “Color” attribute in Unshaded.j3md). This is done by
Replacing the MaterialParameters section of the “ColorBoxShader.j3md” with:
Making these six color attributes known to the vertex shader “ColorBoxShader.vert” so they can be used depending on the cube’s face normals (replace the whole file “ColorBoxShader.vert” with the following code):
Thanks all. All three methods work well. It is nice being able to change the colors on the fly. Oh and thanks for the inNormal line. I still can’t read that doc.
Note: the shader approach is the more expensive approach both in your coding time and in performance… but it is a good learning exercise if you ultimately want to write shaders.
The color buffer approach is the fastest overall and you can still set the colors on the fly if you want.
Edit: (You can also set different colors per corner, etc. for gradients, change UVs in the same way, and so on… pretty flexible for a lot of things short of writing a custom shader and a good lead on to a custom shader.)