-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add methods to construct p5.Geometry from other p5 drawing functions #6287
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8e204a7
Add geometry building
davepagurek 9ae7c5c
Fix bugs
davepagurek 4c6893b
Merge branch 'main' into build-geometry
davepagurek dfaa4d6
Fix row()/column() bug
davepagurek 4c8a45b
Remove old function call
davepagurek 8f44275
Fix broken constants reference
davepagurek b4c388c
Fix camera/mat3 tests
davepagurek 1ac3d29
Add docs and tests
davepagurek 66980a6
Fix error message
davepagurek fde8273
Update documentation and examples
davepagurek 3ea6e7b
Update docs
davepagurek a9f4eb3
Fix vertex colors + add materials test
davepagurek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import p5 from '../core/main'; | ||
import * as constants from '../core/constants'; | ||
|
||
/** | ||
* @private | ||
* A class responsible for converting successive WebGL draw calls into a single | ||
* `p5.Geometry` that can be reused and drawn with `model()`. | ||
*/ | ||
class GeometryBuilder { | ||
constructor(renderer) { | ||
this.renderer = renderer; | ||
renderer._pInst.push(); | ||
this.identityMatrix = new p5.Matrix(); | ||
renderer.uMVMatrix = new p5.Matrix(); | ||
this.geometry = new p5.Geometry(); | ||
this.geometry.gid = `_p5_GeometryBuilder_${GeometryBuilder.nextGeometryId}`; | ||
GeometryBuilder.nextGeometryId++; | ||
davepagurek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.hasTransform = false; | ||
} | ||
|
||
/** | ||
* @private | ||
* Applies the current transformation matrix to each vertex. | ||
*/ | ||
transformVertices(vertices) { | ||
if (!this.hasTransform) return vertices; | ||
|
||
return vertices.map(v => this.renderer.uMVMatrix.multiplyPoint(v)); | ||
} | ||
|
||
/** | ||
* @private | ||
* Applies the current normal matrix to each normal. | ||
*/ | ||
transformNormals(normals) { | ||
if (!this.hasTransform) return normals; | ||
|
||
return normals.map( | ||
v => this.renderer.uNMatrix.multiplyVec3(v) | ||
); | ||
} | ||
|
||
/** | ||
* @private | ||
* Adds a p5.Geometry to the builder's combined geometry, flattening | ||
* transformations. | ||
*/ | ||
addGeometry(input) { | ||
this.hasTransform = !this.renderer.uMVMatrix.mat4 | ||
.every((v, i) => v === this.identityMatrix.mat4[i]); | ||
|
||
if (this.hasTransform) { | ||
this.renderer.uNMatrix.inverseTranspose(this.renderer.uMVMatrix); | ||
} | ||
|
||
let startIdx = this.geometry.vertices.length; | ||
this.geometry.vertices.push(...this.transformVertices(input.vertices)); | ||
this.geometry.vertexNormals.push( | ||
...this.transformNormals(input.vertexNormals) | ||
); | ||
this.geometry.uvs.push(...input.uvs); | ||
|
||
if (this.renderer._doFill) { | ||
this.geometry.faces.push( | ||
...input.faces.map(f => f.map(idx => idx + startIdx)) | ||
); | ||
} | ||
if (this.renderer._doStroke) { | ||
this.geometry.edges.push( | ||
...input.edges.map(edge => edge.map(idx => idx + startIdx)) | ||
); | ||
} | ||
const vertexColors = [...input.vertexColors]; | ||
while (vertexColors.length < input.vertices.length * 4) { | ||
vertexColors.push(...this.renderer.curFillColor); | ||
} | ||
this.geometry.vertexColors.push(...vertexColors); | ||
} | ||
|
||
/** | ||
* Adds geometry from the renderer's immediate mode into the builder's | ||
* combined geometry. | ||
*/ | ||
addImmediate() { | ||
const geometry = this.renderer.immediateMode.geometry; | ||
const shapeMode = this.renderer.immediateMode.shapeMode; | ||
const faces = []; | ||
|
||
if (this.renderer._doFill) { | ||
if ( | ||
shapeMode === constants.TRIANGLE_STRIP || | ||
shapeMode === constants.QUAD_STRIP | ||
) { | ||
for (let i = 2; i < geometry.vertices.length; i++) { | ||
if (i % 2 === 0) { | ||
faces.push([i, i - 1, i - 2]); | ||
} else { | ||
faces.push([i, i - 2, i - 1]); | ||
} | ||
} | ||
} else if (shapeMode === constants.TRIANGLE_FAN) { | ||
for (let i = 2; i < geometry.vertices.length; i++) { | ||
faces.push([0, i - 1, i]); | ||
} | ||
} else { | ||
for (let i = 0; i < geometry.vertices.length; i += 3) { | ||
faces.push([i, i + 1, i + 2]); | ||
} | ||
} | ||
} | ||
this.addGeometry(Object.assign({}, geometry, { faces })); | ||
} | ||
|
||
/** | ||
* Adds geometry from the renderer's retained mode into the builder's | ||
* combined geometry. | ||
*/ | ||
addRetained(geometry) { | ||
this.addGeometry(geometry.model); | ||
} | ||
|
||
/** | ||
* Cleans up the state of the renderer and returns the combined geometry that | ||
* was built. | ||
* @returns p5.Geometry The flattened, combined geometry | ||
*/ | ||
finish() { | ||
this.renderer._pInst.pop(); | ||
|
||
// If all vertices are the same color (no per-vertex colors were | ||
// supplied), remove the vertex color data so that one may override the | ||
// fill when drawing the geometry with `model()` | ||
let allVertexColorsSame = true; | ||
for (let i = 4; i < this.geometry.vertexColors.length; i++) { | ||
if (this.geometry.vertexColors[i] !== this.geometry.vertexColors[i % 4]) { | ||
allVertexColorsSame = false; | ||
break; | ||
} | ||
} | ||
if (allVertexColorsSame) { | ||
this.geometry.vertexColors = []; | ||
} | ||
|
||
return this.geometry; | ||
} | ||
} | ||
|
||
/** | ||
* Keeps track of how many custom geometry objects have been made so that each | ||
* can be assigned a unique ID. | ||
*/ | ||
GeometryBuilder.nextGeometryId = 0; | ||
|
||
export default GeometryBuilder; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.