Skip to content

Commit

Permalink
refactor comments
Browse files Browse the repository at this point in the history
  • Loading branch information
capGoblin committed Nov 17, 2023
1 parent e087f94 commit edb3db6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,18 @@ export const CLAMP = 'clamp';
*/
export const MIRROR = 'mirror';

// WEBGL GEOMETRY SHADING
/**
* @property {String} FLAT
* @final
*/
export const FLAT = 'flat';
/**
* @property {String} SMOOTH
* @final
*/
export const SMOOTH = 'smooth';

// DEVICE-ORIENTATION
/**
* @property {String} LANDSCAPE
Expand Down
31 changes: 19 additions & 12 deletions src/webgl/p5.Geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//some of the functions are adjusted from Three.js(http://threejs.org)

import p5 from '../core/main';
import * as constants from 'constants';
/**
* p5 Geometry class
* @class p5.Geometry
Expand Down Expand Up @@ -186,13 +187,17 @@ p5.Geometry = class Geometry {
}
/**
* This function calculates normals for each face, where each vertex's normal is the average of the normals of all faces it's connected to.
* i.e computes smooth normals per vertex as an average of each face.<br>
* When using 'FLAT' shading, vertices are disconnected/duplicated i.e each face has its own copy of vertices.<br>
* When using 'SMOOTH' shading, vertices are connected/deduplicated i.e each face has its vertices shared with other faces.<br>
* i.e computes smooth normals per vertex as an average of each face.
*
* When using `FLAT` shading, vertices are disconnected/duplicated i.e each face has its own copy of vertices.
* When using `SMOOTH` shading, vertices are connected/deduplicated i.e each face has its vertices shared with other faces.
*
* Options can include:
* - `roundToPrecision`: Precision value for rounding computations. Defaults to 3.
*
* @method computeNormals
* @param {String} [shadingType] shading type ('FLAT' for flat shading or 'SMOOTH' for smooth shading) for buildGeometry() outputs.
* @param {Object} [options] object with roundToPrecision property.
* @param {String} [shadingType] shading type (`FLAT` for flat shading or `SMOOTH` for smooth shading) for buildGeometry() outputs. Defaults to `FLAT`.
* @param {Object} [options] An optional object with configuration.
* @chainable
*
* @example
Expand All @@ -206,11 +211,11 @@ p5.Geometry = class Geometry {
* helix = buildGeometry(() => {
* beginShape();
*
* for (let i = 0; i < TWO_PI * 3; i += 0.1) {
* for (let i = 0; i < TWO_PI * 3; i += 0.6) {
* let radius = 20;
* let x = cos(i) * radius;
* let y = sin(i) * radius;
* let z = i * 10;
* let z = map(i, 0, TWO_PI * 3, -30, 30);
* vertex(x, y, z);
* }
* endShape();
Expand All @@ -222,14 +227,15 @@ p5.Geometry = class Geometry {
* stroke(0);
* fill(150, 200, 250);
* lights();
* rotateX(PI*0.2);
* orbitControl();
* model(helix);
* }
* </code>
* </div>
*
* @alt
* A 3D helix using the computeNormals() function by default uses 'FLAT' to create a flat shading effect on the helix.
* A 3D helix using the computeNormals() function by default uses `FLAT` to create a flat shading effect on the helix.
*
* @example
* <div>
Expand Down Expand Up @@ -257,30 +263,31 @@ p5.Geometry = class Geometry {
* }
* endShape(CLOSE);
* });
* star.computeNormals('SMOOTH');
* star.computeNormals(SMOOTH);
* }
* function draw() {
* background(255);
* stroke(0);
* fill(150, 200, 250);
* lights();
* rotateX(PI*0.2);
* orbitControl();
* model(star);
* }
* </code>
* </div>
*
* @alt
* A star-like geometry, here the computeNormals('SMOOTH') is applied for a smooth shading effect.
* A star-like geometry, here the computeNormals(SMOOTH) is applied for a smooth shading effect.
* This helps to avoid the faceted appearance that can occur with flat shading.
*/
computeNormals(shadingType = 'FLAT', { roundToPrecision = 3 } = {}) {
computeNormals(shadingType = constants.FLAT, { roundToPrecision = 3 } = {}) {
const vertexNormals = this.vertexNormals;
let vertices = this.vertices;
const faces = this.faces;
let iv;

if (shadingType === 'SMOOTH') {
if (shadingType === constants.SMOOTH) {
const vertexIndices = {};
const uniqueVertices = [];

Expand Down

0 comments on commit edb3db6

Please sign in to comment.