Hi folks
I’m trying to create a PShape - a rounded rectangle, with a texture consisting of a background colour and some text overlaid. There may be simpler ways, but this is for a project where I’ll be displaying hundreds of these on screen together so I want to avoid redrawing many times by creating a reusable shape.
I can get the desired result with a simple rect, but the rounded rect version applies the background colour but NOT the text??
I had thought this was down to the UV texture mapping as it’s a ‘custom’ shape, but even forcing the UV values (texture and shape are the same size for simplicity) doesn’t help.
Much googling for setTextureUV has failed to drop the penny. Is this is one of those binary “can’t be done OR trivial user error” questions?
The forum suggested this link as similar (Placing a texture on a shape) but that’s concerned with generating a masked image from a shape, whereas I want a shape using the image as a texture, which feels like the opposite proposition?
PFont font;
MyShape myShape;
static final int windowWidth = 1280;
static final int windowHeight = 968;
void setup() {
size(1280, 968, P3D);
font = createFont("Helvetica", 24.0);
myShape = new MyShape();
}
void draw() {
if ( mousePressed ) {
myShape.regenerate();
}
shape(myShape.shape, ( windowWidth - myShape.shapeWidth ) / 2.0, ( windowHeight - myShape.shapeHeight ) / 2.0);
}
class MyShape {
PShape shape;
int shapeWidth = 400;
int shapeHeight = 200;
int radius = 50;
MyShape() {
regenerate();
}
void regenerate() {
PShape tempShape, regularRect, roundRect;
PGraphics textBoxPGraphics = createGraphics(shapeWidth, shapeHeight);
// Create texture (background colour and some text) for use later
noStroke();
textBoxPGraphics.beginDraw();
textBoxPGraphics.fill(255); // Text colour white
textBoxPGraphics.textSize(24.0);
textBoxPGraphics.textAlign(CENTER, CENTER);
textBoxPGraphics.background(color(0, 0, 255));
textBoxPGraphics.text("Blue!", shapeWidth / 2, shapeHeight / 2 );
textBoxPGraphics.endDraw();
// updatePixels() has no beneficial effect/ .
// textBoxPGraphics.updatePixels();
// loadPixels() has no beneficial effect/ .
// textBoxPGraphics.loadPixels();
// Create regularRect using the textBoxPGraphics as a texture - works fine.
regularRect = createShape();
regularRect.setTexture(textBoxPGraphics);
regularRect.beginShape();
// vertex method includes the UV mappings, the y parameter is offset to enable both shapes to be seen later without overlap
// vertex ( x, y, texturex, texturey)
regularRect.vertex( 0, -1.5 * shapeHeight, 0, 0 );
regularRect.vertex( shapeWidth, -1.5 * shapeHeight, shapeWidth, 0 );
regularRect.vertex( shapeWidth, -0.5 * shapeHeight, shapeWidth, shapeHeight );
regularRect.vertex( 0, -0.5 * shapeHeight, 0, shapeHeight );
regularRect.endShape();
// Create rounded rect using the textBoxPGraphics as a texture - background colour works,
// *** BUT *** text does not display??
roundRect = createShape(RECT, 0, 0, shapeWidth, shapeHeight, radius);
// Attempting to force the UV mappings into the new roundRect PShape doesn't make any beneficial difference
//for (int i = 0; i < roundRect.getVertexCount(); i++) {
// // println(i, roundRect.getVertex(i).x, roundRect.getVertex(i).y);
// roundRect.setTextureUV(i, roundRect.getVertex(i).x, roundRect.getVertex(i).y);
//}
// roundRect picks up the background colour, but NOT the text.
roundRect.setTexture(textBoxPGraphics);
// Add both regular and rounded rects to the shape for display in the draw() method.
tempShape = createShape(GROUP);
tempShape.addChild(roundRect);
tempShape.addChild(regularRect);
shape = tempShape;
}
}