Skip to content

noSmooth does not work in P2D / P3D #5363

Closed as not planned
Closed as not planned

Description

@clankill3r

Processing 3.3.6
Mac OSX 10.12.6

noSmooth does not work with P2D or P3D.

screen shot 2018-01-10 at 13 50 13

PGraphics pg;

void setup() {
  size(640, 640, P2D);
  noSmooth();
  pg = createGraphics(32, 32);
  pg.noSmooth();
}

void draw() {
  pg.beginDraw();
  pg.background(128);
  pg.rect(4, 4, 16, 16);
  pg.endDraw();
  image(pg, 0, 0, width, height);
}

Activity

  1. assigned and unassigned on Jan 10, 2018
  2. gohai commented on Jan 10, 2018

    @gohai
    Contributor

    This thread seems to indicate that ((PGraphicsOpenGL)g).textureSampling(3); does the trick - perhaps noSmooth could be made to do this under the hood on the P2D renderer?

  3. clankill3r commented on Jan 10, 2018

    @clankill3r
    Author

    Nice. It can also be used after setup.

  4. benfry commented on Jan 12, 2018

    @benfry
    Contributor

    Hm, it should be working, so long as it's in settings() (or setup() in the PDE)… so it's a bug.

  5. JakubValtar commented on Jan 12, 2018

    @JakubValtar
    Contributor

    noSmooth() in OpenGL sets only multisampling, not texture filtering. We currently don't have public API to set texture filtering.

  6. benfry commented on Jan 12, 2018

    @benfry
    Contributor

    But in 2D, noSmooth() affects images as well, so the expectation would be the same for OpenGL. (Maybe I'm misunderstanding something?)

  7. JakubValtar commented on Jan 12, 2018

    @JakubValtar
    Contributor

    I think in Java2D it goes to nearest neighbor, because noSmooth() was used to improve performance and turning off image interpolation helped a lot.

    When we got OpenGL, texture filtering was left separate, because it has negligible impact on performance, but lowers image quality a lot compared to AA. Users usually don't want to get pixellated textures, unless it is for artistic reasons (and those would probably want to leave AA on).

    Other reason is that smooth()/noSmooth() can be called only once per sketch, but it should be possible to change texture filtering multiple times. Plus FX does not even support nearest neighbor.

    If we change the behavior, I suggest separating these two in Java2D, because they are two different things used for different purposes (although in Java2D the performance effect overlaps).

  8. benfry commented on Jan 13, 2018

    @benfry
    Contributor

    Nah, it's implemented in Java2D for the effect of it, more than anything else. Things look crummy enough (and performance isn't the factor) that it's really the only reason to do noSmooth() nowadays. I know @codeanticode and I discussed a separate imageSmooth() method but at the end of the day decided to keep things as one. I suppose we could revisit the discussion…

    But at the moment, we're still dealing with buggy behavior in terms of expectations. I say noSmooth() at the beginning of my sketch, and then I see (ugly) smoothed gradients on images, I'm gonna be disappointed (as was the OP).

  9. JakubValtar commented on Jan 13, 2018

    @JakubValtar
    Contributor
  10. clankill3r commented on Dec 23, 2018

    @clankill3r
    Author

    Here is another bug when using noSmooth.

    Expected result:
    screen shot 2018-12-23 at 15 26 55

    Actual result:
    screen shot 2018-12-23 at 15 26 39

    public void settings() {
      size(600, 600);
      noSmooth();
      // pixelDensity(2);
    }
    
    
    
    public void setup() {
    }
    
    
    
    public void draw() {
    
      background(30);
    
    
      fill(255, 0, 0);
      noStroke();
      rect(0, 0, 256, 256);
    
      stroke(255, 0, 0);
      point(5, 5);
    
      int alpha_right_side = 0;
      
      for (int y = 0; y < 256; y++) {
    
        int greyness = 255 - y;
    
        for (int x = 0; x < 256; x++) {
          int alpha = (int) map(x, 0, 255, 255, alpha_right_side);
          int c = color(greyness, alpha);
          stroke(c);
          point((int) x, (int) y);
        }
    
        alpha_right_side++;
      }
      
      
    }
  11. eulphean commented on Jan 11, 2019

    @eulphean

    ((PGraphicsOpenGL)g).textureSampling(3) doesn't seem to help when I draw an ellipse. Why is that so? It smooths the edges but the lines are not continuous. I scaled the image down also but its the same. Somehow the ellipse is downsampled for some reason.

    'PGraphics pg;

    void setup() {
    size(640, 640, P2D);
    noSmooth();
    ((PGraphicsOpenGL)g).textureSampling(3);
    pg = createGraphics(32, 32);
    }

    void draw() {
    pg.beginDraw();
    pg.background(128);
    pg.ellipseMode(CENTER);
    pg.ellipse(16, 16, 10, 10);
    pg.endDraw();
    image(pg, 0, 0, width, height);
    }'
    screen shot 2019-01-11 at 10 48 23 am

  12. chrsmlls333 commented on Sep 30, 2019

    @chrsmlls333

    Note: in P2D, this textureSampling(3) has no effect when drawing a PGraphics canvas object with image(). For example zooming in 2x on a surface half the size of our main canvas results in this same smoothing issue. Any tips?

  13. clankill3r commented on Nov 7, 2019

    @clankill3r
    Author

    @chrsmlls333 can you post an example?

  14. OpenSauce04 commented on Oct 12, 2023

    @OpenSauce04

    This is still an issue nearly 6 years later

  15. aphid91 commented on Oct 16, 2023

    @aphid91

    I am also having issues with noSmooth().
    When I use a PShader to create a simple scrolling effect, my test pattern quickly fills with artifacts if noSmooth() or target.noSmooth() has been called. Note that these images were both captured on frame 400. It seems like some parts of the pattern are scrolling faster than others when target.noSmooth() has ben called.

    artifacts
    noArtifacts

    PGraphics target;
    PShader simpleFrag;
    void setup() {
      size(200, 200, P2D);
      target=createGraphics(200, 200, P2D);
    
      //UNCOMMENTING EITHER OF THESE TWO LINES CAUSES ARTIFACTS
      //noSmooth();
      //target.noSmooth();
    
      simpleFrag=loadShader("simpleFrag.glsl");
    
    /*EDIT: 
    I realized that these two lines are required to draw the pattern if running this code
    in Processing 3. Otherwise, you just get an empty black window. I originally composed 
    the test in Processing 4.
    */
    target.beginDraw();//EDIT
    target.endDraw();//EDIT
    
      drawTestPatternToTarget();
    }
    void draw() {
      background(0);
      target.beginDraw();
      simpleFrag.set("previousFrame", target);
      target.shader(simpleFrag);
      target.rect(0, 0, width, height);
      target.endDraw();
      image(target, 0, 0);
    }
    
    void drawTestPatternToTarget() {
      target.beginDraw();
      target.background(0);
      target.stroke(255);
      target.strokeWeight(5);
      target.line(0, 0, target.width, target.height);
      target.line(0, target.height, target.width, 0);
      target.fill(255, 0, 0);
      target.circle(target.width/2, target.height/2, 100);
      target.endDraw();
    }

    simpleFrag.glsl:

    out vec4 col;
    uniform sampler2D previousFrame;
    void main(){
      ivec2 res=textureSize(previousFrame,0);
      ivec2 texel=ivec2(gl_FragCoord.xy);
      texel.x=(texel.x+2)%(res.x);//scroll to the left by 2 pixels
      vec4 newColor=texelFetch(previousFrame,texel,0);
      col=newColor;
    }
  16. clankill3r commented on Oct 17, 2023

    @clankill3r
    Author

    @aphid91 I have limited time, but can you try to call gl_nearest_for_texture for your target PGraphics?

    public void gl_nearest_for_texture(PImage image) {
      PGL pgl = beginPGL();
      Texture image_tex = ((PGraphicsOpenGL)g).getTexture(image);
      pgl.bindTexture(PGL.TEXTURE_2D, image_tex.glName);
      pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MIN_FILTER, PGL.NEAREST);
      pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MAG_FILTER, PGL.NEAREST);
      pgl.bindTexture(PGL.TEXTURE_2D, 0);
      endPGL();
    }
    
    
    public void gl_linear_for_texture(PImage image) {
      PGL pgl = beginPGL();
      Texture image_tex = ((PGraphicsOpenGL)g).getTexture(image);
      pgl.bindTexture(PGL.TEXTURE_2D, image_tex.glName);
      pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MIN_FILTER, PGL.LINEAR);
      pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MAG_FILTER, PGL.LINEAR);
      pgl.bindTexture(PGL.TEXTURE_2D, 0);
      endPGL();
    }
    
    public Texture gl_texture(PImage img) {
      return ((PGraphicsOpenGL)g).getTexture(img);
    }
    
    static public Texture gl_texture(PApplet p, PImage img) {
      return ((PGraphicsOpenGL)p.g).getTexture(img);
    }
    
  17. aphid91 commented on Oct 19, 2023

    @aphid91

    @clankill3r I tried calling gl_nearest_for_texture(target) right after the call to target.noSmooth(), and the artifact problem persists. I also tried it with plain old noSmooth() instead of/ in addition to target.noSmooth() and the results are the same. A couple notes:

    • The code I posted only shows the test pattern if run from processing 4. Expected (though still buggy) behaivior can be restored in processing 3 by adding target.beginDraw(); target.endDraw() right before drawTestPattern(); P3 is a little weird about PGraphics initialization. I have edited my original post to reflect this.
    • Similarly, in Processing 4, calling gl_nearest_for_texture(target) before the first target.beginDraw(); target.endDraw() pair causes the console to complain that "pixels array is null". Adding a beginDraw-endDraw pair right before the function call has expected behavior (still full of artifacts)
    • gl_nearest_for_texture(target) must be called after target.noSmooth() as it contains a "beginDraw()"
  18. IntangibleMatter commented on Feb 26, 2025

    @IntangibleMatter

    Remains an issue in 2025, making it really hard to have pixel graphics and shaders at once.

  19. github-actions commented on Jul 15, 2026

    @github-actions

    This issue has been automatically closed and locked.

    Processing 3.x is no longer actively developed. Since January 2020, development moved to the Processing 4 repository.

    If this issue is still relevant to Processing 4, we'd love to hear from you. Please open a new issue at:
    https://github.com/processing/processing4/issues/new/choose

    Thank you for helping improve Processing.

  20. locked as resolved and limited conversation to collaborators on Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions