Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for per vertex transparency when blending #6541

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/webgl/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -1279,14 +1279,17 @@ p5.prototype.metalness = function (metallic) {
* @private blends colors according to color components.
* If alpha value is less than 1, or non-standard blendMode
* we need to enable blending on our gl context.
* @param {Number[]} color [description]
* @return {Number[]} Normalized numbers array
* @param {Number[]} color The currently set color, with values in 0-1 range
* @param {Boolean} [hasTransparency] Whether the shape being drawn has other
* transparency internally, e.g. via vertex colors
* @return {Number[]]} Normalized numbers array
*/
p5.RendererGL.prototype._applyColorBlend = function (colors) {
p5.RendererGL.prototype._applyColorBlend = function(colors, hasTransparency) {
const gl = this.GL;

const isTexture = this.drawMode === constants.TEXTURE;
const doBlend =
hasTransparency ||
this.userFillShader ||
this.userStrokeShader ||
this.userPointShader ||
Expand Down
32 changes: 32 additions & 0 deletions src/webgl/p5.Geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ p5.Geometry = class Geometry {
this.detailY = detailY !== undefined ? detailY : 1;
this.dirtyFlags = {};

this._hasFillTransparency = undefined;
this._hasStrokeTransparency = undefined;

if (callback instanceof Function) {
callback.call(this);
}
return this; // TODO: is this a constructor?
}

reset() {
this._hasFillTransparency = undefined;
this._hasStrokeTransparency = undefined;

this.lineVertices.clear();
this.lineTangentsIn.clear();
this.lineTangentsOut.clear();
Expand All @@ -88,6 +94,32 @@ p5.Geometry = class Geometry {

this.dirtyFlags = {};
}

hasFillTransparency() {
if (this._hasFillTransparency === undefined) {
this._hasFillTransparency = false;
for (let i = 0; i < this.vertexColors.length; i += 4) {
if (this.vertexColors[i + 3] < 1) {
this._hasFillTransparency = true;
break;
}
}
}
return this._hasFillTransparency;
}
hasStrokeTransparency() {
if (this._hasStrokeTransparency === undefined) {
this._hasStrokeTransparency = false;
for (let i = 0; i < this.lineVertexColors.length; i += 4) {
if (this.lineVertexColors[i + 3] < 1) {
this._hasStrokeTransparency = true;
break;
}
}
}
return this._hasStrokeTransparency;
}

/**
* Removes the internal colors of p5.Geometry.
* Using `clearColors()`, you can use `fill()` to supply new colors before drawing each shape.
Expand Down
10 changes: 8 additions & 2 deletions src/webgl/p5.RendererGL.Immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,10 @@ p5.RendererGL.prototype._drawImmediateFill = function(count = 1) {
}
shader.disableRemainingAttributes();

this._applyColorBlend(this.curFillColor);
this._applyColorBlend(
this.curFillColor,
this.immediateMode.geometry.hasFillTransparency()
);

if (count === 1) {
gl.drawArrays(
Expand Down Expand Up @@ -561,7 +564,10 @@ p5.RendererGL.prototype._drawImmediateStroke = function() {
buff._prepareBuffer(this.immediateMode.geometry, shader);
}
shader.disableRemainingAttributes();
this._applyColorBlend(this.curStrokeColor);
this._applyColorBlend(
this.curStrokeColor,
this.immediateMode.geometry.hasFillTransparency()
);

gl.drawArrays(
gl.TRIANGLES,
Expand Down
10 changes: 8 additions & 2 deletions src/webgl/p5.RendererGL.Retained.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ p5.RendererGL.prototype.drawBuffers = function(gId) {
//vertex index buffer
this._bindBuffer(geometry.indexBuffer, gl.ELEMENT_ARRAY_BUFFER);
}
this._applyColorBlend(this.curFillColor);
this._applyColorBlend(
this.curFillColor,
geometry.model.hasFillTransparency()
);
this._drawElements(gl.TRIANGLES, gId);
fillShader.unbindShader();
}
Expand All @@ -156,7 +159,10 @@ p5.RendererGL.prototype.drawBuffers = function(gId) {
buff._prepareBuffer(geometry, strokeShader);
}
strokeShader.disableRemainingAttributes();
this._applyColorBlend(this.curStrokeColor);
this._applyColorBlend(
this.curStrokeColor,
geometry.model.hasStrokeTransparency()
);
this._drawArrays(gl.TRIANGLES, gId);
strokeShader.unbindShader();
}
Expand Down
24 changes: 24 additions & 0 deletions test/unit/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,30 @@ suite('p5.RendererGL', function() {
assert.deepEqual(myp5.get(16, 16), [255, 0, 255, 255]);
done();
});

test('transparency works the same with per-vertex colors', function() {
myp5.createCanvas(20, 20, myp5.WEBGL);
myp5.noStroke();

function drawShapes() {
myp5.fill(255, 0, 0, 100);
myp5.rect(-10, -10, 15, 15);
myp5.fill(0, 0, 255, 100);
myp5.rect(-5, -5, 15, 15);
}

drawShapes();
myp5.loadPixels();
const eachShapeResult = [...myp5.pixels];

myp5.clear();
const shapes = myp5.buildGeometry(drawShapes);
myp5.model(shapes);
myp5.loadPixels();
const singleShapeResult = [...myp5.pixels];

assert.deepEqual(eachShapeResult, singleShapeResult);
});
});

suite('BufferDef', function() {
Expand Down
Loading