Search code examples
javajava-3d

Is it possible to have different colors on each side of a surface in Java 3D?


This is how I create a sample triangular surface with no culling

Point3f a = new Point3f(0, 0, 0);
Point3f b = new Point3f(0, 0, 1);
Point3f c = new Point3f(0, 1, 0);

TriangleArray pyramidGeometry = new TriangleArray(3, GeometryArray.COORDINATES);

pyramidGeometry.setCoordinate(0, a);
pyramidGeometry.setCoordinate(1, b);
pyramidGeometry.setCoordinate(2, c);

GeometryInfo geometryInfo = new GeometryInfo(pyramidGeometry);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(geometryInfo);

GeometryArray result = geometryInfo.getGeometryArray();

Appearance appearance = new Appearance();
PolygonAttributes polyAttrib = new PolygonAttributes();
polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
appearance.setPolygonAttributes(polyAttrib);

Shape3D shape = new Shape3D(result, appearance);

If I set some color to the Appearance object, this color will show on both sides of the surface.

My best guess is that it is not possible, which would make sense from a logical standpoint since a surface has only two dimensions (and hence one face), but I wonder if one could set a different color for each side of the surface. It would be convenient for my application. Otherwise I'll need to duplicate the surfaces to show a color per side.

I've already searched for an answer with no luck.


Solution

  • You can decide whether you see only forward-facing sides or not but if you really want to use different colors or texture coordinates, you'll need to create another face. Maybe it's possible to use different colors for the forward-facing side and the backward-facing side by using shaders that would use the normal or the orientation of your vertices but it's not trivial and I don't think it's worth it... except if there is a relationship between the colors shown in the front face and the colors shown in the back face. You should ask some help to Phil if you want to use shaders in Java3D, preferably with the next version, i.e Java3D 1.7. Rather contact us on the official JogAmp forum.