-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Update computeNormals() to support smooth shading for buildGeometry() outputs #6553
Changes from 3 commits
d299497
465ef05
2db604d
d1529df
e087f94
edb3db6
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 | ||
---|---|---|---|---|
|
@@ -185,17 +185,47 @@ p5.Geometry = class Geometry { | |||
return n.mult(Math.asin(sinAlpha) / ln); | ||||
} | ||||
/** | ||||
* computes smooth normals per vertex as an average of each | ||||
* face. | ||||
* @method computeNormals | ||||
* @chainable | ||||
*/ | ||||
computeNormals() { | ||||
* computes smooth normals per vertex as an average of each | ||||
* face. | ||||
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. Maybe we can add a sentence or two explaining how this works, so that users will know what to expect from it? I think the information would be:
|
||||
* @method computeNormals | ||||
* @param {String} [shadingType] (optional) Shading type ('FLAT' for flat shading or 'SMOOTH' for smooth shading) for buildGeometry() outputs. | ||||
* @param {Object} [settings={ roundToPrecision: 3 }] (optional) Additional settings object with rounding precision for vertex coordinates when shadingType is 'SMOOTH'. | ||||
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. Maybe we can try to match the style used for the options object in p5.js/src/typography/p5.Font.js Line 278 in bd42ed1
Object and the name is just the name, and above the parameters, they list all the options it can include and the default values.
|
||||
* @chainable | ||||
davepagurek marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
*/ | ||||
computeNormals(shadingType = 'FLAT', { roundToPrecision = 3 } = {}) { | ||||
const vertexNormals = this.vertexNormals; | ||||
const vertices = this.vertices; | ||||
let vertices = this.vertices; | ||||
const faces = this.faces; | ||||
let iv; | ||||
|
||||
if (shadingType === 'SMOOTH') { | ||||
const vertexIndices = {}; | ||||
const uniqueVertices = []; | ||||
|
||||
// loop through each vertex and add uniqueVertices | ||||
for (let i = 0; i < vertices.length; i++) { | ||||
const vertex = vertices[i]; | ||||
const key = `${vertex.x.toFixed(roundToPrecision)},${vertex.y.toFixed(roundToPrecision)},${vertex.z.toFixed(roundToPrecision)}`; | ||||
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 have this big string here and then again on line 220. Maybe to make sure they're always in sync, we can put something like this above (maybe around line 200): const getKey = ({ x, y, z }) =>
`${x.toFixed(roundToPrecision)},${y.toFixed(roundToPrecision)},${z.toFixed(roundToPrecision)}`; ...so then here we could do |
||||
if (vertexIndices[key] === undefined) { | ||||
vertexIndices[key] = uniqueVertices.length; | ||||
uniqueVertices.push(vertex); | ||||
} | ||||
} | ||||
|
||||
// update face indices to use the new deduplicated vertex indices | ||||
faces.forEach(face => { | ||||
for (let fv = 0; fv < 3; ++fv) { | ||||
const originalVertexIndex = face[fv]; | ||||
const originalVertex = vertices[originalVertexIndex]; | ||||
const key = `${originalVertex.x.toFixed(roundToPrecision)},${originalVertex.y.toFixed(roundToPrecision)},${originalVertex.z.toFixed(roundToPrecision)}`; | ||||
face[fv] = vertexIndices[key]; | ||||
} | ||||
}); | ||||
|
||||
// update the deduplicated vertices | ||||
vertices = uniqueVertices; | ||||
} | ||||
|
||||
// initialize the vertexNormals array with empty vectors | ||||
vertexNormals.length = 0; | ||||
for (iv = 0; iv < vertices.length; ++iv) { | ||||
|
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.
While we're augmenting this method's docs, a quick capitalization fix: