Skip to content

Commit cfa67aa

Browse files
committed
Use class imports wherever possible
1 parent 0c71618 commit cfa67aa

17 files changed

+2928
-4064
lines changed

src/core/p5.Graphics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Graphics {
4444

4545
// Attach renderer properties
4646
for (const p in this._renderer) {
47-
if(p[0] === '_') continue;
47+
if(p[0] === '_' || typeof this._renderer[p] === 'function') continue;
4848
Object.defineProperty(this, p, {
4949
get(){
5050
return this._renderer?.[p];

src/webgl/3d_primitives.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
import * as constants from '../core/constants';
1010
import { RendererGL } from './p5.RendererGL';
11+
import { Vector } from '../math/p5.Vector';
12+
import { Geometry } from './p5.Geometry';
13+
import { Matrix } from './p5.Matrix';
1114

1215
function primitives3D(p5, fn){
1316
/**
@@ -983,13 +986,13 @@ function primitives3D(p5, fn){
983986
v = i / this.detailY;
984987
for (let j = 0; j <= this.detailX; j++) {
985988
u = j / this.detailX;
986-
p = new p5.Vector(u - 0.5, v - 0.5, 0);
989+
p = new Vector(u - 0.5, v - 0.5, 0);
987990
this.vertices.push(p);
988991
this.uvs.push(u, v);
989992
}
990993
}
991994
};
992-
const planeGeom = new p5.Geometry(detailX, detailY, _plane);
995+
const planeGeom = new Geometry(detailX, detailY, _plane);
993996
planeGeom.computeFaces().computeNormals();
994997
if (detailX <= 1 && detailY <= 1) {
995998
planeGeom._makeTriangleEdges()._edgesToVertices();
@@ -1192,7 +1195,7 @@ function primitives3D(p5, fn){
11921195
//inspired by lightgl:
11931196
//https://github.com/evanw/lightgl.js
11941197
//octants:https://en.wikipedia.org/wiki/Octant_(solid_geometry)
1195-
const octant = new p5.Vector(
1198+
const octant = new Vector(
11961199
((d & 1) * 2 - 1) / 2,
11971200
((d & 2) - 1) / 2,
11981201
((d & 4) / 2 - 1) / 2
@@ -1204,7 +1207,7 @@ function primitives3D(p5, fn){
12041207
this.faces.push([v + 2, v + 1, v + 3]);
12051208
});
12061209
};
1207-
const boxGeom = new p5.Geometry(detailX, detailY, _box);
1210+
const boxGeom = new Geometry(detailX, detailY, _box);
12081211
boxGeom.computeNormals();
12091212
if (detailX <= 4 && detailY <= 4) {
12101213
boxGeom._edgesToVertices();
@@ -1417,16 +1420,16 @@ function primitives3D(p5, fn){
14171420
const cur = Math.cos(ur);
14181421

14191422
//VERTICES
1420-
this.vertices.push(new p5.Vector(sur * ringRadius, y, cur * ringRadius));
1423+
this.vertices.push(new Vector(sur * ringRadius, y, cur * ringRadius));
14211424

14221425
//VERTEX NORMALS
14231426
let vertexNormal;
14241427
if (yy < 0) {
1425-
vertexNormal = new p5.Vector(0, -1, 0);
1428+
vertexNormal = new Vector(0, -1, 0);
14261429
} else if (yy > detailY && topRadius) {
1427-
vertexNormal = new p5.Vector(0, 1, 0);
1430+
vertexNormal = new Vector(0, 1, 0);
14281431
} else {
1429-
vertexNormal = new p5.Vector(sur * cosSlant, sinSlant, cur * cosSlant);
1432+
vertexNormal = new Vector(sur * cosSlant, sinSlant, cur * cosSlant);
14301433
}
14311434
this.vertexNormals.push(vertexNormal);
14321435
//UVs
@@ -1944,7 +1947,7 @@ function primitives3D(p5, fn){
19441947

19451948
const gId = `cone|${detailX}|${detailY}|${cap}`;
19461949
if (!this._renderer.geometryInHash(gId)) {
1947-
const coneGeom = new p5.Geometry(detailX, detailY);
1950+
const coneGeom = new Geometry(detailX, detailY);
19481951
_truncatedCone.call(coneGeom, 1, 0, 1, detailX, detailY, cap, false);
19491952
if (detailX <= 24 && detailY <= 16) {
19501953
coneGeom._makeTriangleEdges()._edgesToVertices();
@@ -2162,7 +2165,7 @@ function primitives3D(p5, fn){
21622165
}
21632166
}
21642167
};
2165-
const ellipsoidGeom = new p5.Geometry(detailX, detailY, _ellipsoid);
2168+
const ellipsoidGeom = new Geometry(detailX, detailY, _ellipsoid);
21662169
ellipsoidGeom.computeFaces();
21672170
if (detailX <= 24 && detailY <= 24) {
21682171
ellipsoidGeom._makeTriangleEdges()._edgesToVertices();
@@ -2370,21 +2373,21 @@ function primitives3D(p5, fn){
23702373
const cosTheta = Math.cos(theta);
23712374
const sinTheta = Math.sin(theta);
23722375

2373-
const p = new p5.Vector(
2376+
const p = new Vector(
23742377
r * cosTheta,
23752378
r * sinTheta,
23762379
tubeRatio * sinPhi
23772380
);
23782381

2379-
const n = new p5.Vector(cosPhi * cosTheta, cosPhi * sinTheta, sinPhi);
2382+
const n = new Vector(cosPhi * cosTheta, cosPhi * sinTheta, sinPhi);
23802383

23812384
this.vertices.push(p);
23822385
this.vertexNormals.push(n);
23832386
this.uvs.push(u, v);
23842387
}
23852388
}
23862389
};
2387-
const torusGeom = new p5.Geometry(detailX, detailY, _torus);
2390+
const torusGeom = new Geometry(detailX, detailY, _torus);
23882391
torusGeom.computeFaces();
23892392
if (detailX <= 24 && detailY <= 16) {
23902393
torusGeom._makeTriangleEdges()._edgesToVertices();
@@ -2443,7 +2446,7 @@ function primitives3D(p5, fn){
24432446
RendererGL.prototype.point = function(x, y, z = 0) {
24442447

24452448
const _vertex = [];
2446-
_vertex.push(new p5.Vector(x, y, z));
2449+
_vertex.push(new Vector(x, y, z));
24472450
this._drawPoints(_vertex, this.immediateMode.buffers.point);
24482451

24492452
return this;
@@ -2461,15 +2464,15 @@ function primitives3D(p5, fn){
24612464
if (!this.geometryInHash(gId)) {
24622465
const _triangle = function() {
24632466
const vertices = [];
2464-
vertices.push(new p5.Vector(0, 0, 0));
2465-
vertices.push(new p5.Vector(1, 0, 0));
2466-
vertices.push(new p5.Vector(0, 1, 0));
2467+
vertices.push(new Vector(0, 0, 0));
2468+
vertices.push(new Vector(1, 0, 0));
2469+
vertices.push(new Vector(0, 1, 0));
24672470
this.edges = [[0, 1], [1, 2], [2, 0]];
24682471
this.vertices = vertices;
24692472
this.faces = [[0, 1, 2]];
24702473
this.uvs = [0, 0, 1, 0, 1, 1];
24712474
};
2472-
const triGeom = new p5.Geometry(1, 1, _triangle);
2475+
const triGeom = new Geometry(1, 1, _triangle);
24732476
triGeom._edgesToVertices();
24742477
triGeom.computeNormals();
24752478
this.createBuffers(gId, triGeom);
@@ -2485,7 +2488,7 @@ function primitives3D(p5, fn){
24852488
try {
24862489
// triangle orientation.
24872490
const orientation = Math.sign(x1*y2-x2*y1 + x2*y3-x3*y2 + x3*y1-x1*y3);
2488-
const mult = new p5.Matrix([
2491+
const mult = new Matrix([
24892492
x2 - x1, y2 - y1, 0, 0, // the resulting unit X-axis
24902493
x3 - x1, y3 - y1, 0, 0, // the resulting unit Y-axis
24912494
0, 0, orientation, 0, // the resulting unit Z-axis (Reflect the specified order of vertices)
@@ -2544,7 +2547,7 @@ function primitives3D(p5, fn){
25442547
if (start.toFixed(10) !== stop.toFixed(10)) {
25452548
// if the mode specified is PIE or null, push the mid point of the arc in vertices
25462549
if (mode === constants.PIE || typeof mode === 'undefined') {
2547-
this.vertices.push(new p5.Vector(0.5, 0.5, 0));
2550+
this.vertices.push(new Vector(0.5, 0.5, 0));
25482551
this.uvs.push([0.5, 0.5]);
25492552
}
25502553

@@ -2556,7 +2559,7 @@ function primitives3D(p5, fn){
25562559
const _x = 0.5 + Math.cos(theta) / 2;
25572560
const _y = 0.5 + Math.sin(theta) / 2;
25582561

2559-
this.vertices.push(new p5.Vector(_x, _y, 0));
2562+
this.vertices.push(new Vector(_x, _y, 0));
25602563
this.uvs.push([_x, _y]);
25612564

25622565
if (i < detail - 1) {
@@ -2604,7 +2607,7 @@ function primitives3D(p5, fn){
26042607
}
26052608
};
26062609

2607-
const arcGeom = new p5.Geometry(detail, 1, _arc);
2610+
const arcGeom = new Geometry(detail, 1, _arc);
26082611
arcGeom.computeNormals();
26092612

26102613
if (detail <= 50) {
@@ -2651,7 +2654,7 @@ function primitives3D(p5, fn){
26512654
const v = i / this.detailY;
26522655
for (let j = 0; j <= this.detailX; j++) {
26532656
const u = j / this.detailX;
2654-
const p = new p5.Vector(u, v, 0);
2657+
const p = new Vector(u, v, 0);
26552658
this.vertices.push(p);
26562659
this.uvs.push(u, v);
26572660
}
@@ -2666,7 +2669,7 @@ function primitives3D(p5, fn){
26662669
];
26672670
}
26682671
};
2669-
const rectGeom = new p5.Geometry(detailX, detailY, _rect);
2672+
const rectGeom = new Geometry(detailX, detailY, _rect);
26702673
rectGeom
26712674
.computeFaces()
26722675
.computeNormals()
@@ -2772,7 +2775,7 @@ function primitives3D(p5, fn){
27722775
`quad|${x1}|${y1}|${z1}|${x2}|${y2}|${z2}|${x3}|${y3}|${z3}|${x4}|${y4}|${z4}|${detailX}|${detailY}`;
27732776

27742777
if (!this.geometryInHash(gId)) {
2775-
const quadGeom = new p5.Geometry(detailX, detailY, function() {
2778+
const quadGeom = new Geometry(detailX, detailY, function() {
27762779
//algorithm adapted from c++ to js
27772780
//https://stackoverflow.com/questions/16989181/whats-the-correct-way-to-draw-a-distorted-plane-in-opengl/16993202#16993202
27782781
let xRes = 1.0 / (this.detailX - 1);
@@ -2793,7 +2796,7 @@ function primitives3D(p5, fn){
27932796
let pty = (1 - pctx) * linePt0y + pctx * linePt1y;
27942797
let ptz = (1 - pctx) * linePt0z + pctx * linePt1z;
27952798

2796-
this.vertices.push(new p5.Vector(ptx, pty, ptz));
2799+
this.vertices.push(new Vector(ptx, pty, ptz));
27972800
this.uvs.push([pctx, pcty]);
27982801
}
27992802
}
@@ -3533,11 +3536,10 @@ function primitives3D(p5, fn){
35333536
}
35343537

35353538
this._pInst.push();
3536-
3537-
this._pInst.noLights();
3539+
this.noLights();
35383540
this._pInst.noStroke();
35393541

3540-
this._pInst.texture(img);
3542+
this.texture(img);
35413543
this._pInst.textureMode(constants.NORMAL);
35423544

35433545
let u0 = 0;

src/webgl/GeometryBuilder.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import p5 from '../core/main';
21
import * as constants from '../core/constants';
2+
import { Matrix } from './p5.Matrix';
3+
import { Geometry } from './p5.Geometry';
34

45
/**
56
* @private
@@ -10,9 +11,9 @@ class GeometryBuilder {
1011
constructor(renderer) {
1112
this.renderer = renderer;
1213
renderer._pInst.push();
13-
this.identityMatrix = new p5.Matrix();
14-
renderer.states.uModelMatrix = new p5.Matrix();
15-
this.geometry = new p5.Geometry();
14+
this.identityMatrix = new Matrix();
15+
renderer.states.uModelMatrix = new Matrix();
16+
this.geometry = new Geometry();
1617
this.geometry.gid = `_p5_GeometryBuilder_${GeometryBuilder.nextGeometryId}`;
1718
GeometryBuilder.nextGeometryId++;
1819
this.hasTransform = false;

src/webgl/interaction.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import * as constants from '../core/constants';
9+
import { Vector } from '../math/p5.Vector';
910

1011
function interaction(p5, fn){
1112
/**
@@ -428,7 +429,7 @@ function interaction(p5, fn){
428429
const viewZ = Math.sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ);
429430

430431
// position vector of the center.
431-
let cv = new p5.Vector(cam.centerX, cam.centerY, cam.centerZ);
432+
let cv = new Vector(cam.centerX, cam.centerY, cam.centerZ);
432433

433434
// Calculate the normalized device coordinates of the center.
434435
cv = cam.cameraMatrix.multiplyPoint(cv);

0 commit comments

Comments
 (0)