Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
computeFaces() example added.
  • Loading branch information
deveshidwivedi authored Jan 16, 2024
commit 446f039f0f5203f126bf4f9af0c0feb9f87570e3
59 changes: 59 additions & 0 deletions src/webgl/p5.Geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,65 @@ p5.Geometry = class Geometry {
* computes faces for geometry objects based on the vertices.
* @method computeFaces
* @chainable
* @example
* <div>
* <code>
*
* let customGeometry;
*
* function setup() {
* createCanvas(150, 150, WEBGL);
* customGeometry = new CustomGeometry(50);
* customGeometry.computeFaces();
* }
*
* function draw() {
* background(150);
* rotateX(frameCount * 0.01);
* rotateY(frameCount * 0.01);
*
* fill(255);
* stroke(0);
* beginShape(TRIANGLES);
* for (let face of customGeometry.faces) {
* for (let vertexIndex of face) {
* let vertexPosition = customGeometry.vertices[vertexIndex];
* vertex(vertexPosition.x, vertexPosition.y, vertexPosition.z);
* }
* }
* endShape();
* }
*
* class CustomGeometry {
* constructor(size) {
* this.vertices = [
* createVector(0, -size / 2, 0),
* createVector(size / 2, size / 2, size / 2),
* createVector(-size / 2, size / 2, size / 2),
* createVector(-size / 2, size / 2, -size / 2),
* createVector(size / 2, size / 2, -size / 2)
* ];
* this.faces = [];
* this.computeFaces();
* }
*
* computeFaces() {
* this.faces = [
* [0, 1, 2], // Bottom face
* [0, 2, 3], // Left face
* [0, 3, 4], // Front face
* [0, 4, 1], // Right face
* [1, 4, 3], // Back face
* [1, 3, 2] // Base face
* ];
* }
* }
*
* </code>
* </div>
* @alt
* A dynamically rotating 3D pyramid, crafted with triangular faces through custom geometry computations.
*
*/
computeFaces() {
this.faces.length = 0;
Expand Down