-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
implementing beginContour and endContour in WEBGL mode #6042
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -45,6 +45,17 @@ const immediateBufferStrides = { | |||
uvs: 2 | ||||
}; | ||||
|
||||
p5.RendererGL.prototype.beginContour = function() { | ||||
this.immediateMode.isContour = true; | ||||
this.immediateMode.isFirstContour = true; | ||||
this.immediateMode.geometry.contourVertices = []; | ||||
return this; | ||||
}; | ||||
|
||||
p5.RendererGL.prototype.endContour = function() { | ||||
return this.immediateMode.geometry.contourVertices; | ||||
}; | ||||
|
||||
/** | ||||
* adds a vertex to be drawn in a custom Shape. | ||||
* @private | ||||
|
@@ -101,7 +112,11 @@ p5.RendererGL.prototype.vertex = function(x, y) { | |||
v = arguments[4]; | ||||
} | ||||
const vert = new p5.Vector(x, y, z); | ||||
this.immediateMode.geometry.vertices.push(vert); | ||||
if (this.immediateMode.isContour) { | ||||
this.immediateMode.geometry.contourVertices.push(vert); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work, I think this is generally the right approach! The one difference is that we might have multiple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually, we'll need to pass all the contours into this array in p5.js/src/webgl/p5.RendererGL.Immediate.js Line 333 in 393ddbc
This probably means we'll need to store |
||||
} else { | ||||
this.immediateMode.geometry.vertices.push(vert); | ||||
} | ||||
this.immediateMode.geometry.vertexNormals.push(this._currentNormal); | ||||
const vertexColor = this.curFillColor || [0.5, 0.5, 0.5, 1.0]; | ||||
this.immediateMode.geometry.vertexColors.push( | ||||
|
@@ -187,6 +202,7 @@ p5.RendererGL.prototype.endShape = function( | |||
if (this.immediateMode.shapeMode === constants.POINTS) { | ||||
this._drawPoints( | ||||
this.immediateMode.geometry.vertices, | ||||
this.immediateMode.geometry.contourVertices, | ||||
this.immediateMode.buffers.point | ||||
); | ||||
return this; | ||||
|
@@ -224,14 +240,16 @@ p5.RendererGL.prototype.endShape = function( | |||
* TRIANGLE_STRIP, TRIANGLE_FAN and TESS(WEBGL only) | ||||
*/ | ||||
p5.RendererGL.prototype._processVertices = function(mode) { | ||||
if (this.immediateMode.geometry.vertices.length === 0) return; | ||||
if (this.immediateMode.geometry.vertices.length === 0 || | ||||
this.immediateMode.geometry.contourVertices === 0) return; | ||||
|
||||
const calculateStroke = this._doStroke; | ||||
const shouldClose = mode === constants.CLOSE; | ||||
if (calculateStroke) { | ||||
this.immediateMode.geometry.edges = this._calculateEdges( | ||||
this.immediateMode.shapeMode, | ||||
this.immediateMode.geometry.vertices, | ||||
[this.immediateMode.geometry.vertices, | ||||
this.immediateMode.geometry.contourVertices], | ||||
shouldClose | ||||
); | ||||
this.immediateMode.geometry._edgesToVertices(); | ||||
|
@@ -262,64 +280,70 @@ p5.RendererGL.prototype._calculateEdges = function( | |||
) { | ||||
const res = []; | ||||
let i = 0; | ||||
switch (shapeMode) { | ||||
case constants.TRIANGLE_STRIP: | ||||
for (i = 0; i < verts.length - 2; i++) { | ||||
res.push([i, i + 1]); | ||||
res.push([i, i + 2]); | ||||
} | ||||
res.push([i, i + 1]); | ||||
break; | ||||
case constants.TRIANGLE_FAN: | ||||
for (i = 1; i < verts.length - 1; i++) { | ||||
res.push([0, i]); | ||||
res.push([i, i + 1]); | ||||
} | ||||
res.push([0, verts.length - 1]); | ||||
break; | ||||
case constants.TRIANGLES: | ||||
for (i = 0; i < verts.length - 2; i = i + 3) { | ||||
res.push([i, i + 1]); | ||||
res.push([i + 1, i + 2]); | ||||
res.push([i + 2, i]); | ||||
} | ||||
break; | ||||
case constants.LINES: | ||||
for (i = 0; i < verts.length - 1; i = i + 2) { | ||||
res.push([i, i + 1]); | ||||
} | ||||
break; | ||||
case constants.QUADS: | ||||
// Quads have been broken up into two triangles by `vertex()`: | ||||
// 0 3--5 | ||||
// | \ \ | | ||||
// 1--2 4 | ||||
for (i = 0; i < verts.length - 5; i += 6) { | ||||
res.push([i, i + 1]); | ||||
res.push([i + 1, i + 2]); | ||||
res.push([i + 3, i + 5]); | ||||
res.push([i + 4, i + 5]); | ||||
} | ||||
break; | ||||
case constants.QUAD_STRIP: | ||||
// 0---2---4 | ||||
// | | | | ||||
// 1---3---5 | ||||
for (i = 0; i < verts.length - 2; i += 2) { | ||||
res.push([i, i + 1]); | ||||
res.push([i, i + 2]); | ||||
res.push([i + 1, i + 3]); | ||||
} | ||||
res.push([i, i + 1]); | ||||
break; | ||||
default: | ||||
for (i = 0; i < verts.length - 1; i++) { | ||||
res.push([i, i + 1]); | ||||
} | ||||
break; | ||||
} | ||||
if (shouldClose) { | ||||
res.push([verts.length - 1, 0]); | ||||
let j = 0; | ||||
while (j < 2) { | ||||
let part = []; | ||||
switch (shapeMode) { | ||||
case constants.TRIANGLE_STRIP: | ||||
for (i = 0; i < verts[j].length - 2; i++) { | ||||
part.push([i, i + 1]); | ||||
part.push([i, i + 2]); | ||||
} | ||||
part.push([i, i + 1]); | ||||
break; | ||||
case constants.TRIANGLE_FAN: | ||||
for (i = 1; i < verts[j].length - 1; i++) { | ||||
part.push([0, i]); | ||||
part.push([i, i + 1]); | ||||
} | ||||
part.push([0, verts[j].length - 1]); | ||||
break; | ||||
case constants.TRIANGLES: | ||||
for (i = 0; i < verts[j].length - 2; i = i + 3) { | ||||
part.push([i, i + 1]); | ||||
part.push([i + 1, i + 2]); | ||||
part.push([i + 2, i]); | ||||
} | ||||
break; | ||||
case constants.LINES: | ||||
for (i = 0; i < verts[j].length - 1; i = i + 2) { | ||||
part.push([i, i + 1]); | ||||
} | ||||
break; | ||||
case constants.QUADS: | ||||
// Quads have been broken up into two triangles by `vertex()`: | ||||
// 0 3--5 | ||||
// | \ \ | | ||||
// 1--2 4 | ||||
for (i = 0; i < verts[j].length - 5; i += 6) { | ||||
part.push([i, i + 1]); | ||||
part.push([i + 1, i + 2]); | ||||
part.push([i + 3, i + 5]); | ||||
part.push([i + 4, i + 5]); | ||||
} | ||||
break; | ||||
case constants.QUAD_STRIP: | ||||
// 0---2---4 | ||||
// | | | | ||||
// 1---3---5 | ||||
for (i = 0; i < verts[j].length - 2; i += 2) { | ||||
part.push([i, i + 1]); | ||||
part.push([i, i + 2]); | ||||
part.push([i + 1, i + 3]); | ||||
} | ||||
part.push([i, i + 1]); | ||||
break; | ||||
default: | ||||
for (i = 0; i < verts[j].length - 1; i++) { | ||||
part.push([i, i + 1]); | ||||
} | ||||
break; | ||||
} | ||||
if (shouldClose) { | ||||
part.push([verts[j].length - 1, 0]); | ||||
} | ||||
res.push(part); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the main branch, the expected output of this function is an array of With your change, this now returns an array of arrays of those tuples. That could be helpful, since now we know that the first array element is the pairs for the main shape vertices, the second array element is the pairs for the first contour, the third array element is the pairs for the second contour, etc. It just means we'll have to update the e.g. over here, it directly accesses p5.js/src/webgl/p5.Geometry.js Line 281 in 393ddbc
|
||||
j++; | ||||
} | ||||
return res; | ||||
}; | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we can store this on
this.immediateMode
instead ofthis.immediateMode.geometry
? We use the same geometry class for 3D models loaded withloadModel()
, which won't have this notion of contours, so this should probably just be internal top5.RendererGL
.