# PShape::attrib() and PShape::setAttrib() dont work proprely

**URL:** <https://discourse.processing.org/t/pshape-attrib-and-pshape-setattrib-dont-work-proprely/845>\
**Category:** Processing\
**Created:** [June 10, 2018, 4:27pm UTC](https://discourse.processing.org/t/pshape-attrib-and-pshape-setattrib-dont-work-proprely/845 "2018-06-10T16:27:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephcraft](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stephcraft/32/22024_2.png) [@Stephcraft](https://discourse.processing.org/u/Stephcraft)\
**Post date:** [June 10, 2018, 4:27pm UTC](https://discourse.processing.org/t/pshape-attrib-and-pshape-setattrib-dont-work-proprely/845/1 "2018-06-10T16:27:04Z")

</div>

the attrib() and setAttrib() do not work proprely in all scenarios

The only way it works is when using attrib() in a not GROUP PShape.

Here is a working Exemple :

```auto
//
// Working Exemple
//

PShape shape;
PShader shader;

public void setup() {
  size(500, 500, P2D);
  
  shape = createShape();
  shape.beginShape();
  
  // Visual
  shape.noStroke();
  shape.fill(255, 0, 0); // Not supposed to be Red
  
  // Custom Attribute
  shape.attrib("vert_color", 1.0, 0.5, 0.0, 1.0); // Supposed to be Orange
  
  shape.vertex(-100, -100);
  shape.vertex(100, -100);
  shape.vertex(100, 100);
  shape.vertex(-100, 100);
  
  shape.endShape();
  
  // Exemple shader
  shader = createShader();
}

public void draw() {
  background(255);
  
  shader(shader);
  translate(width/2, height/2);
  shape(shape);
}

public PShader createShader() {
  String[] vert = new String[] {
    "#version 110",
    "uniform mat4 transform;",
    "",
    "attribute vec4 position;",
    "attribute vec4 vert_color;",
    "",
    "varying vec4 vertColor;",
    "",
    "void main() {",
    " gl_Position = transform * position;",
    " vertColor = vert_color;",
    "}"
  };
  
  String[] frag = new String[] {
    "varying vec4 vertColor;",
    "",
    "void main() {",
    " gl_FragColor = vertColor;",
    "}"
  };
  
  return new PShader(this, vert, frag);
}

```

Here is the first Exemple where it does not work.  
When using PShapes that are GROUP attributes dont work and might give a **java.lang.NullPointerException** :

```auto
//
// Does not work when shape is GROUP
// ERROR : java.lang.NullPointerException
//

PShape shape;
PShape child;
PShader shader;

public void setup() {
  size(500, 500, P2D);
  
  child = createShape();
  child.beginShape();
  
  // Visual
  child.noStroke();
  child.fill(255, 0, 0); // Not supposed to be Red
  
  // Custom Attribute
  child.attrib("vert_color", 1.0, 0.5, 0.0, 1.0); // Supposed to be Orange
  
  child.vertex(-100, -100);
  child.vertex(100, -100);
  child.vertex(100, 100);
  child.vertex(-100, 100);
  
  child.endShape();
  
  shape = createShape(GROUP);
  shape.addChild(child);
  
  // Exemple shader
  shader = createShader();
}

public void draw() {
  background(255);
  
  shader(shader);
  translate(width/2, height/2);
  shape(shape);
}

public PShader createShader() {
  String[] vert = new String[] {
    "#version 110",
    "uniform mat4 transform;",
    "",
    "attribute vec4 position;",
    "attribute vec4 vert_color;",
    "",
    "varying vec4 vertColor;",
    "",
    "void main() {",
    " gl_Position = transform * position;",
    " vertColor = vert_color;",
    "}"
  };
  
  String[] frag = new String[] {
    "varying vec4 vertColor;",
    "",
    "void main() {",
    " gl_FragColor = vertColor;",
    "}"
  };
  
  return new PShader(this, vert, frag);
}

```

Last not Working Exemple, when using the PShape::setAttrib() function, gives again a **java.lang.NullPointerException** :

```auto
//
// Does not work when using PShape::setAttrib() later
// ERROR : java.lang.NullPointerException
//

PShape shape;
PShader shader;

public void setup() {
  size(500, 500, P2D);
  
  shape = createShape();
  shape.beginShape();
  
  // Visual
  shape.noStroke();
  shape.fill(255, 0, 0); // Not supposed to be Red
  
  // Custom Attribute
  //shape.attrib("vert_color", 1.0, 0.5, 0.0, 1.0); // Not yet
  
  shape.vertex(-100, -100);
  shape.vertex(100, -100);
  shape.vertex(100, 100);
  shape.vertex(-100, 100);
  
  shape.endShape();
  
  // Set Custom Attributes
  for(int i=0; i<shape.getVertexCount(); i++) {
    shape.setAttrib("vert_color", i, 1.0, 0.5, 0.0, 1.0); // java.lang.NullPointerException, huh
  }
  
  // Exemple shader
  shader = createShader();
}

public void draw() {
  background(255);
  
  shader(shader);
  translate(width/2, height/2);
  shape(shape);
}

public PShader createShader() {
  String[] vert = new String[] {
    "#version 110",
    "uniform mat4 transform;",
    "",
    "attribute vec4 position;",
    "attribute vec4 vert_color;",
    "",
    "varying vec4 vertColor;",
    "",
    "void main() {",
    " gl_Position = transform * position;",
    " vertColor = vert_color;",
    "}"
  };
  
  String[] frag = new String[] {
    "varying vec4 vertColor;",
    "",
    "void main() {",
    " gl_FragColor = vertColor;",
    "}"
  };
  
  return new PShader(this, vert, frag);
}

```

Am I doing somethong wrong or is this a bug ?

Thank you !

---

<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:** [June 18, 2018, 11:51pm UTC](https://discourse.processing.org/t/pshape-attrib-and-pshape-setattrib-dont-work-proprely/845/2 "2018-06-18T23:51:31Z")

</div>

In your second example, I see that you are calling `shape.setAttrib()` after `shape.endShape()`. That doesn’t look right.

When I change that and run either on Processing 3.3.6 I get a long list of error messages on `shader(shader);`:

```
Cannot compile vertex shader:ERROR: 0:1 " : version '110' is not supported ...

```

That looks like it might be a problem with your shader, not PShape … ?

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [June 19, 2018, 1:10pm UTC](https://discourse.processing.org/t/pshape-attrib-and-pshape-setattrib-dont-work-proprely/845/3 "2018-06-19T13:10:38Z")

</div>

Does it work without the version pragma? If that’s there the automatic rewriting of the shader for GL3+ doesn’t kick in.

---

<div class="post-metadata">

**Author:** ![Stephcraft](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stephcraft/32/22024_2.png) [@Stephcraft](https://discourse.processing.org/u/Stephcraft)\
**Post date:** [June 19, 2018, 2:45pm UTC](https://discourse.processing.org/t/pshape-attrib-and-pshape-setattrib-dont-work-proprely/845/4 "2018-06-19T14:45:11Z")

</div>

The problem should not be the glsl version because the default processing attributes like : **color** and **position** are recognized and passed to the shader correctly.

Also, for the last exemple, all functions like `setNormal()`, `setColor()` and `setAttrib()` should be used outside beginShape and endShape while `normal()`, `color()` and `attrib()` should only be used inside `beginShape` and `endShape` so there is no problem there too.

I think it is a problem in the `shape.setAttrib()` function.

---

<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:** [June 19, 2018, 3:38pm UTC](https://discourse.processing.org/t/pshape-attrib-and-pshape-setattrib-dont-work-proprely/845/5 "2018-06-19T15:38:47Z")

</div>

> [@Stephcraft](#):
>
> all functions like `setNormal()` , `setColor()` and `setAttrib()` should be used outside beginShape and endShape

Good point. Looks like my initial guess was wrong.

---

<div class="post-metadata">

**Author:** ![Stephcraft](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stephcraft/32/22024_2.png) [@Stephcraft](https://discourse.processing.org/u/Stephcraft)\
**Post date:** [June 27, 2018, 9:37pm UTC](https://discourse.processing.org/t/pshape-attrib-and-pshape-setattrib-dont-work-proprely/845/6 "2018-06-27T21:37:12Z")

</div>

Opened a issue on GitHub : [https://github.com/processing/processing/issues/5560](https://github.com/processing/processing/issues/5560)
