Skip to content

Commit 6488203

Browse files
authored
Merge pull request #2511 from Spongman/fix-fill-stroke
Fix fill/stroke order bug remove shader.active flag begin using _doFill/_doStroke in GL in line with 2D remove unnecessary userProgram calls set blend settings at end of draw call
2 parents 1321e3d + 62856da commit 6488203

File tree

6 files changed

+74
-127
lines changed

6 files changed

+74
-127
lines changed

src/color/setting.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,29 @@ p5.prototype.fill = function() {
506506
* rect(20, 20, 60, 60);
507507
* </code>
508508
* </div>
509+
*
510+
* <div modernizr='webgl'>
511+
* <code>
512+
* function setup() {
513+
* createCanvas(100, 100, WEBGL);
514+
* }
515+
*
516+
* function draw() {
517+
* background(0);
518+
* noFill();
519+
* stroke(100, 100, 240);
520+
* rotateX(frameCount * 0.01);
521+
* rotateY(frameCount * 0.01);
522+
* box(45, 45, 45);
523+
* }
524+
* </code>
525+
* </div>
526+
*
509527
* @alt
510528
* white rect top middle and noFill rect center. Both 60x60 with black outlines.
529+
* black canvas with purple cube wireframe spinning
511530
*/
512531
p5.prototype.noFill = function() {
513-
if (this._renderer.isP3D) {
514-
this._renderer.noFill();
515-
}
516532
this._renderer._setProperty('_doFill', false);
517533
return this;
518534
};
@@ -531,16 +547,28 @@ p5.prototype.noFill = function() {
531547
* </code>
532548
* </div>
533549
*
550+
* <div modernizr='webgl'>
551+
* <code>
552+
* function setup() {
553+
* createCanvas(100, 100, WEBGL);
554+
* }
534555
*
535-
* @alt
536-
*60x60 white rect at center. no outline.
556+
* function draw() {
557+
* background(0);
558+
* noStroke();
559+
* fill(240, 150, 150);
560+
* rotateX(frameCount * 0.01);
561+
* rotateY(frameCount * 0.01);
562+
* box(45, 45, 45);
563+
* }
564+
* </code>
565+
* </div>
537566
*
567+
* @alt
568+
* 60x60 white rect at center. no outline.
569+
* black canvas with pink cube spinning
538570
*/
539-
540571
p5.prototype.noStroke = function() {
541-
if (this._renderer.isP3D) {
542-
this._renderer.noStroke();
543-
}
544572
this._renderer._setProperty('_doStroke', false);
545573
return this;
546574
};

src/webgl/material.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ p5.prototype.shader = function(s) {
170170
p5.prototype.normalMaterial = function() {
171171
this._renderer.drawMode = constants.FILL;
172172
this._renderer.setFillShader(this._renderer._getNormalShader());
173-
this._renderer.noStroke();
173+
this._renderer.curFillColor = [1, 1, 1, 1];
174+
this.noStroke();
174175
return this;
175176
};
176177

@@ -262,7 +263,7 @@ p5.prototype.texture = function(tex) {
262263
shader.setUniform('uSpecular', false);
263264
shader.setUniform('isTexture', true);
264265
shader.setUniform('uSampler', tex);
265-
this._renderer.noStroke();
266+
this.noStroke();
266267
return this;
267268
};
268269

@@ -303,10 +304,11 @@ p5.prototype.texture = function(tex) {
303304
* @chainable
304305
*/
305306
p5.prototype.ambientMaterial = function(v1, v2, v3, a) {
306-
var colors = this._renderer._applyColorBlend.apply(this._renderer, arguments);
307+
var color = p5.prototype.color.apply(this, arguments);
308+
this._renderer.curFillColor = color._array;
307309

308310
var shader = this._renderer._useLightShader();
309-
shader.setUniform('uMaterialColor', colors);
311+
shader.setUniform('uMaterialColor', this._renderer.curFillColor);
310312
shader.setUniform('uSpecular', false);
311313
shader.setUniform('isTexture', false);
312314
return this;
@@ -349,10 +351,11 @@ p5.prototype.ambientMaterial = function(v1, v2, v3, a) {
349351
* @chainable
350352
*/
351353
p5.prototype.specularMaterial = function(v1, v2, v3, a) {
352-
var colors = this._renderer._applyColorBlend.apply(this._renderer, arguments);
354+
var color = p5.prototype.color.apply(this, arguments);
355+
this._renderer.curFillColor = color._array;
353356

354357
var shader = this._renderer._useLightShader();
355-
shader.setUniform('uMaterialColor', colors);
358+
shader.setUniform('uMaterialColor', this._renderer.curFillColor);
356359
shader.setUniform('uSpecular', true);
357360
shader.setUniform('isTexture', false);
358361
return this;
@@ -362,16 +365,11 @@ p5.prototype.specularMaterial = function(v1, v2, v3, a) {
362365
* @private blends colors according to color components.
363366
* If alpha value is less than 1, we need to enable blending
364367
* on our gl context. Otherwise opaque objects need to a depthMask.
365-
* @param {Number} v1 [description]
366-
* @param {Number} v2 [description]
367-
* @param {Number} v3 [description]
368-
* @param {Number} a [description]
369-
* @return {[Number]} Normalized numbers array
368+
* @param {Number[]} color [description]
369+
* @return {Number[]]} Normalized numbers array
370370
*/
371-
p5.RendererGL.prototype._applyColorBlend = function(v1, v2, v3, a) {
371+
p5.RendererGL.prototype._applyColorBlend = function(colors) {
372372
var gl = this.GL;
373-
var color = this._pInst.color.apply(this._pInst, arguments);
374-
var colors = color._array;
375373
if (colors[colors.length - 1] < 1.0) {
376374
gl.depthMask(false);
377375
gl.enable(gl.BLEND);

src/webgl/p5.RendererGL.Immediate.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ p5.RendererGL.prototype.endShape = function(
115115
) {
116116
this._useImmediateModeShader();
117117

118-
if (this.curStrokeShader.active === true) {
118+
if (this._doStroke && this.drawMode !== constants.TEXTURE) {
119119
for (var i = 0; i < this.immediateMode.vertices.length - 1; i++) {
120120
this.immediateMode.edges.push([i, i + 1]);
121121
}
@@ -129,7 +129,7 @@ p5.RendererGL.prototype.endShape = function(
129129
this._edgesToVertices(this.immediateMode);
130130
this._drawStrokeImmediateMode();
131131
}
132-
if (this.curFillShader.active === true) {
132+
if (this._doFill) {
133133
this._drawFillImmediateMode(
134134
mode,
135135
isCurve,
@@ -258,6 +258,7 @@ p5.RendererGL.prototype._drawFillImmediateMode = function(
258258
' not yet implemented in webgl mode.'
259259
);
260260
} else {
261+
this._applyColorBlend(this.curFillColor);
261262
gl.enable(gl.BLEND);
262263
gl.drawArrays(
263264
this.immediateMode.shapeMode,
@@ -312,6 +313,7 @@ p5.RendererGL.prototype._drawStrokeImmediateMode = function() {
312313
);
313314
}
314315

316+
this._applyColorBlend(this.curStrokeColor);
315317
gl.drawArrays(gl.TRIANGLES, 0, this.immediateMode.lineVertices.length);
316318

317319
// todo / optimizations? leave bound until another shader is set?

src/webgl/p5.RendererGL.Retained.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ p5.RendererGL.prototype.drawBuffers = function(gId) {
200200
this._useColorShader();
201201
var geometry = this.gHash[gId];
202202

203-
if (this.curStrokeShader.active !== false && geometry.lineVertexCount > 0) {
203+
if (this._doStroke && geometry.lineVertexCount > 0) {
204204
this.curStrokeShader.bindShader();
205205

206206
// bind the stroke shader's 'aPosition' buffer
@@ -229,11 +229,12 @@ p5.RendererGL.prototype.drawBuffers = function(gId) {
229229
);
230230
}
231231

232+
this._applyColorBlend(this.curStrokeColor);
232233
this._drawArrays(gl.TRIANGLES, gId);
233234
this.curStrokeShader.unbindShader();
234235
}
235236

236-
if (this.curFillShader.active !== false) {
237+
if (this._doFill !== false) {
237238
this.curFillShader.bindShader();
238239

239240
// bind the fill shader's 'aPosition' buffer
@@ -282,6 +283,7 @@ p5.RendererGL.prototype.drawBuffers = function(gId) {
282283
);
283284
}
284285

286+
this._applyColorBlend(this.curFillColor);
285287
this._drawElements(gl.TRIANGLES, gId);
286288
this.curFillShader.unbindShader();
287289
}

src/webgl/p5.RendererGL.js

Lines changed: 13 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) {
122122
this.fill(255, 255, 255, 255);
123123
//this.stroke(0, 0, 0, 255);
124124
this.pointSize = 5.0; //default point size
125-
this.curStrokeWeight = 2; //default stroke weight
126-
this.curStrokeColor = [0, 0, 0, 1];
127-
this._setStrokeWeight();
128-
this._setStrokeColor();
125+
this.strokeWeight(2);
126+
this.stroke(0, 0, 0);
129127
// array of textures created in this gl context via this.getTexture(src)
130128
this.textures = [];
131129
this.name = 'p5.RendererGL'; // for friendly debugger system
@@ -425,6 +423,7 @@ p5.RendererGL.prototype.background = function() {
425423
var _b = _col.levels[2] / 255;
426424
var _a = _col.levels[3] / 255;
427425
this.GL.clearColor(_r, _g, _b, _a);
426+
this.GL.depthMask(true);
428427
this.GL.clear(this.GL.COLOR_BUFFER_BIT | this.GL.DEPTH_BUFFER_BIT);
429428
};
430429

@@ -471,77 +470,16 @@ p5.RendererGL.prototype.background = function() {
471470
*/
472471
p5.RendererGL.prototype.fill = function(v1, v2, v3, a) {
473472
//see material.js for more info on color blending in webgl
474-
var colors = this._applyColorBlend.apply(this, arguments);
475-
this.curFillColor = colors;
476-
if (this.curFillShader.active === false) {
477-
this.curFillShader.active = true;
478-
}
473+
var color = p5.prototype.color.apply(this._pInst, arguments);
474+
this.curFillColor = color._array;
475+
479476
if (this.isImmediateDrawing) {
480477
this.setFillShader(this._getImmediateModeShader());
481478
} else {
482479
this.setFillShader(this._getColorShader());
483480
}
484481
this.drawMode = constants.FILL;
485-
this.curFillShader.setUniform('uMaterialColor', colors);
486-
};
487-
488-
/**
489-
* Does not render fill material
490-
* @method noFill
491-
* @example
492-
* <div>
493-
* <code>
494-
* function setup() {
495-
* createCanvas(200, 200, WEBGL);
496-
* }
497-
*
498-
* function draw() {
499-
* background(0);
500-
* noFill();
501-
* stroke(100, 100, 240);
502-
* rotateX(frameCount * 0.01);
503-
* rotateY(frameCount * 0.01);
504-
* box(75, 75, 75);
505-
* }
506-
* </code>
507-
* </div>
508-
*
509-
* @alt
510-
* black canvas with purple cube wireframe spinning
511-
*
512-
*/
513-
514-
p5.RendererGL.prototype.noFill = function() {
515-
this.curFillShader.active = false;
516-
};
517-
518-
/**
519-
* Does not render stroke
520-
* @method noStroke
521-
* @example
522-
* <div>
523-
* <code>
524-
* function setup() {
525-
* createCanvas(200, 200, WEBGL);
526-
* }
527-
*
528-
* function draw() {
529-
* background(0);
530-
* noStroke();
531-
* fill(240, 150, 150);
532-
* rotateX(frameCount * 0.01);
533-
* rotateY(frameCount * 0.01);
534-
* box(75, 75, 75);
535-
* }
536-
* </code>
537-
* </div>
538-
*
539-
* @alt
540-
* black canvas with pink cube spinning
541-
*
542-
*/
543-
p5.RendererGL.prototype.noStroke = function() {
544-
this.curStrokeShader.active = false;
482+
this.curFillShader.setUniform('uMaterialColor', this.curFillColor);
545483
};
546484

547485
/**
@@ -576,17 +514,12 @@ p5.RendererGL.prototype.noStroke = function() {
576514
*
577515
*/
578516
p5.RendererGL.prototype.stroke = function(r, g, b, a) {
579-
if (this.curStrokeShader.active === false) {
580-
this.curStrokeShader.active = true;
581-
}
582517
//@todo allow transparency in stroking currently doesn't have
583518
//any impact and causes problems with specularMaterial
584519
arguments[3] = 255;
585-
var colors = this._applyColorBlend.apply(this, arguments);
586-
if (this.curStrokeColor !== colors) {
587-
this.curStrokeColor = colors;
588-
this._setStrokeColor();
589-
}
520+
var color = p5.prototype.color.apply(this._pInst, arguments);
521+
this.curStrokeColor = color._array;
522+
this.curStrokeShader.setUniform('uMaterialColor', this.curStrokeColor);
590523
};
591524

592525
/**
@@ -630,28 +563,13 @@ p5.RendererGL.prototype.stroke = function(r, g, b, a) {
630563
*
631564
*/
632565
p5.RendererGL.prototype.strokeWeight = function(w) {
633-
if (this.curStrokeShader.active === false) {
634-
this.curStrokeShader.active = true;
635-
}
636566
if (this.curStrokeWeight !== w) {
637567
this.pointSize = w;
638568
this.curStrokeWeight = w;
639569
this.curStrokeShader.setUniform('uStrokeWeight', w);
640570
}
641571
};
642572

643-
p5.RendererGL.prototype._setStrokeWeight = function() {
644-
// this should only be called after an appropriate call
645-
// to shader() internally....
646-
this.curStrokeShader.setUniform('uStrokeWeight', this.curStrokeWeight);
647-
};
648-
649-
p5.RendererGL.prototype._setStrokeColor = function() {
650-
// this should only be called after an appropriate call
651-
// to shader() internally....
652-
this.curStrokeShader.setUniform('uMaterialColor', this.curStrokeColor);
653-
};
654-
655573
/**
656574
* Returns an array of [R,G,B,A] values for any pixel or grabs a section of
657575
* an image. If no parameters are specified, the entire image is returned.
@@ -874,8 +792,7 @@ p5.RendererGL.prototype.setFillShader = function(s) {
874792
// safe to do this multiple times;
875793
// init() will bail early if has already been run.
876794
this.curFillShader.init();
877-
this.curFillShader.useProgram();
878-
this.curFillShader.active = true;
795+
//this.curFillShader.useProgram();
879796
}
880797
// always return this.curFillShader, even if no change was made.
881798
return this.curFillShader;
@@ -893,8 +810,7 @@ p5.RendererGL.prototype.setStrokeShader = function(s) {
893810
// safe to do this multiple times;
894811
// init() will bail early if has already been run.
895812
this.curStrokeShader.init();
896-
this.curStrokeShader.useProgram();
897-
this.curStrokeShader.active = true;
813+
//this.curStrokeShader.useProgram();
898814
}
899815
// always return this.curLineShader, even if no change was made.
900816
return this.curStrokeShader;
@@ -943,6 +859,7 @@ p5.RendererGL.prototype._useImmediateModeShader = function() {
943859
// note that if we're using the texture shader...
944860
// this shouldn't change. :)
945861
}
862+
return this.curFillShader;
946863
};
947864

948865
p5.RendererGL.prototype._getLightShader = function() {

0 commit comments

Comments
 (0)