# PShape texture only applies colour not text?

**URL:** <https://discourse.processing.org/t/pshape-texture-only-applies-colour-not-text/13596>\
**Category:** Coding Questions\
**Created:** [August 25, 2019, 11:27pm UTC](https://discourse.processing.org/t/pshape-texture-only-applies-colour-not-text/13596 "2019-08-25T23:27:43Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [August 30, 2019, 5:39pm UTC](https://discourse.processing.org/t/pshape-texture-only-applies-colour-not-text/13596/10 "2019-08-30T17:39:52Z")

</div>

> [@michthom](#):
>
> I’ve been trying to use the function that @jeremydouglass provided, but when I replace the suggested line in my code ( `roundRect.setTexture(textBoxPGraphics);` ) with the new call ( `addTextureUV(roundRect, textBoxPGraphics);` ), the rounded rectangle displays as plain white

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:

```auto
/**
 * 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**
>
> ```auto
> 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:

![17%20AM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a05a0f139c926f0be4f19d725b8c2b2b6763ce6b.png)

---

_[View the full topic](https://discourse.processing.org/t/pshape-texture-only-applies-colour-not-text/13596)._
