-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Modify the shader so that the line can be colored per vertex #5915
Changes from 9 commits
8c0b790
f9d48bf
6522a55
c200fba
f07bf49
b51b70a
e369748
c54b7a9
e951bcb
86a99f8
2c851c3
4d6178d
a04d12e
a570ddf
42f6608
7d977e3
38db825
aea54f2
a460068
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,7 @@ p5.Geometry = function(detailX, detailY, callback) { | |
//based on faces for most objects; | ||
this.edges = []; | ||
this.vertexColors = []; | ||
this.lineVertexColors = []; | ||
this.detailX = detailX !== undefined ? detailX : 1; | ||
this.detailY = detailY !== undefined ? detailY : 1; | ||
this.dirtyFlags = {}; | ||
|
@@ -62,6 +63,7 @@ p5.Geometry.prototype.reset = function() { | |
this.vertices.length = 0; | ||
this.edges.length = 0; | ||
this.vertexColors.length = 0; | ||
this.lineVertexColors.length = 0; | ||
this.vertexNormals.length = 0; | ||
this.uvs.length = 0; | ||
|
||
|
@@ -238,12 +240,16 @@ p5.Geometry.prototype._makeTriangleEdges = function() { | |
* @chainable | ||
*/ | ||
p5.Geometry.prototype._edgesToVertices = function() { | ||
const data = this.lineVertexColors.slice(); | ||
this.lineVertexColors.length = 0; | ||
this.lineVertices.length = 0; | ||
this.lineNormals.length = 0; | ||
|
||
for (let i = 0; i < this.edges.length; i++) { | ||
const begin = this.vertices[this.edges[i][0]]; | ||
const end = this.vertices[this.edges[i][1]]; | ||
const e0 = this.edges[i][0]; | ||
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. It's a little more verbose but maybe we can call these 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. It's certainly easier to understand... I think it's better to read and understand even if it's verbose, so I'm going to do it. |
||
const e1 = this.edges[i][1]; | ||
var begin = this.vertices[e0]; | ||
var end = this.vertices[e1]; | ||
const dir = end | ||
.copy() | ||
.sub(begin) | ||
|
@@ -260,6 +266,13 @@ p5.Geometry.prototype._edgesToVertices = function() { | |
dirSub.push(-1); | ||
this.lineNormals.push(dirAdd, dirSub, dirAdd, dirAdd, dirSub, dirSub); | ||
this.lineVertices.push(a, b, c, c, b, d); | ||
if(data.length > 0){ | ||
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. Super minor, but do you mind adding spaces around the brackets to match the style of the if statements elsewhere? 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. It sure looks better with some space in it... ok! |
||
var beginColor = [data[4*e0], data[4*e0+1], data[4*e0+2], data[4*e0+3]]; | ||
var endColor = [data[4*e1], data[4*e1+1], data[4*e1+2], data[4*e1+3]]; | ||
this.lineVertexColors.push( | ||
beginColor, beginColor, endColor, endColor, beginColor, endColor | ||
); | ||
} | ||
} | ||
return this; | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,8 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) { | |
this._useNormalMaterial = false; | ||
this._useShininess = 1; | ||
|
||
this._useLineColor = false; | ||
|
||
this._tint = [255, 255, 255, 255]; | ||
|
||
// lightFalloff variables | ||
|
@@ -149,6 +151,7 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) { | |
geometry: {}, | ||
buffers: { | ||
stroke: [ | ||
new p5.RenderBuffer(4, 'lineVertexColors', 'lineColorBuffer', 'aVertexColor', this, this._flatten), | ||
new p5.RenderBuffer(3, 'lineVertices', 'lineVertexBuffer', 'aPosition', this, this._flatten), | ||
new p5.RenderBuffer(4, 'lineNormals', 'lineNormalBuffer', 'aDirection', this, this._flatten) | ||
], | ||
|
@@ -184,6 +187,7 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) { | |
new p5.RenderBuffer(2, 'uvs', 'uvBuffer', 'aTexCoord', this, this._flatten) | ||
], | ||
stroke: [ | ||
new p5.RenderBuffer(4, 'lineVertexColors', 'lineColorBuffer', 'aVertexColor', this, this._flatten), | ||
new p5.RenderBuffer(3, 'lineVertices', 'lineVertexBuffer', 'aPosition', this, this._flatten), | ||
new p5.RenderBuffer(4, 'lineNormals', 'lineNormalBuffer', 'aDirection', this, this._flatten) | ||
], | ||
|
@@ -1257,6 +1261,7 @@ p5.RendererGL.prototype._setStrokeUniforms = function(strokeShader) { | |
strokeShader.bindShader(); | ||
|
||
// set the uniform values | ||
strokeShader.setUniform('uUseLineColor', this._useLineColor); | ||
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! 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. thanks!('ω') |
||
strokeShader.setUniform('uMaterialColor', this.curStrokeColor); | ||
strokeShader.setUniform('uStrokeWeight', this.curStrokeWeight); | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
precision mediump float; | ||
precision mediump int; | ||
|
||
varying vec4 vColor; | ||
|
||
uniform bool uUseLineColor; | ||
uniform vec4 uMaterialColor; | ||
|
||
void main() { | ||
gl_FragColor = uMaterialColor; | ||
} | ||
gl_FragColor = (uUseLineColor ? vColor : uMaterialColor); | ||
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. We can maybe avoid doing this in the fragment shader if, in the vertex shader, we set 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. I see, the vertexShader receives uMaterialColor and writes the branch processing...certainly, considering the number of calls, that might be better...Okay, I'll give it a try! |
||
} |
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 we need to make a copy of
lineVertexColors
or does it not work if we reference it directly? If we do need to copy it, maybe we can call this something more descriptive, likelineColorData
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.
Here, lineVertices are newly created from vertices, but since the color arrangement of lineVertexColors corresponds to vertices, it does not correspond to lineVertices as it is.
So, after making a copy of lineVertexColors corresponding to vertices in advance, we use that data to create an array of colors corresponding to lineVertices. So I thought I should make a copy.
As for the name, lineColorData is certainly easier to understand, so I would like to do so!
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.
It makes sense that
lineVertexColors
is built using info inlineColorData
to correspond to the data inlineVertices
. Does the code work if we doconst lineColorData = this.lineVertexColors;
? So, replacingthis.lineVertexColors.slice()
withlineVertexColors
? Having a separate variable for it is fine if that communicates the intention of the variable more clearly, but I'm just trying to avoid the performance cost of doing an extra copy if we can.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.
After thinking about it overnight, I realized I didn't need to make a copy, so I'd like to rewrite it! I'm sorry...
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.
No need to apologize! that's what code review is for :D