Terribly sorry, should have actually shared it from my test sketch – perhaps I forgot the return type? Anyhow,
Replace this line:
// roundRect.setTexture(textBoxPGraphics);
roundRect = addTextureUV(roundRect, textBoxPGraphics);
… and add THIS function:
/**
* https://github.com/processing/processing/issues/2508#issuecomment-50774212
*/
PShape addTextureUV(PShape s, PImage img) {
PShape s2 = s.getTessellation(); // github.com/processing/processing/issues/2508#issuecomment-52296723
s2.setStroke(false);
s2.setTexture(img);
s2.setTextureMode(NORMAL);
for (int i = 0; i < s2.getVertexCount (); i++) {
PVector v = s2.getVertex(i);
s2.setTextureUV(i, map(v.x, 0, img.width, 0, 1), map(v.y, 0, img.height, 0, 1));
}
return s2;
}
Here is the complete working demo sketch.
Summary
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);
roundRect = addTextureUV(roundRect, 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;
}
}
/**
* https://github.com/processing/processing/issues/2508#issuecomment-50774212
*/
PShape addTextureUV(PShape s, PImage img) {
PShape s2 = s.getTessellation(); // github.com/processing/processing/issues/2508#issuecomment-52296723
s2.setStroke(false);
s2.setTexture(img);
s2.setTextureMode(NORMAL);
for (int i = 0; i < s2.getVertexCount (); i++) {
PVector v = s2.getVertex(i);
s2.setTextureUV(i, map(v.x, 0, img.width, 0, 1), map(v.y, 0, img.height, 0, 1));
}
return s2;
}
Which does this: