Skip to content

Commit 5d99712

Browse files
committed
Merge branch 'main' into imageLight
2 parents 14aed59 + 611941a commit 5d99712

File tree

9 files changed

+243
-98
lines changed

9 files changed

+243
-98
lines changed

package-lock.json

Lines changed: 89 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/webgl/3d_primitives.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ p5.prototype.buildGeometry = function(callback) {
174174
* from <a href="#/p5/loadModel">loadModel()</a>.
175175
*
176176
* @method freeGeometry
177-
* @param {p5.Geometry} The geometry whose resources should be freed
177+
* @param {p5.Geometry} geometry The geometry whose resources should be freed
178178
*/
179179
p5.prototype.freeGeometry = function(geometry) {
180180
this._renderer._freeBuffers(geometry.gid);

src/webgl/GeometryBuilder.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,6 @@ class GeometryBuilder {
126126
*/
127127
finish() {
128128
this.renderer._pInst.pop();
129-
130-
// If all vertices are the same color (no per-vertex colors were
131-
// supplied), remove the vertex color data so that one may override the
132-
// fill when drawing the geometry with `model()`
133-
let allVertexColorsSame = true;
134-
for (let i = 4; i < this.geometry.vertexColors.length; i++) {
135-
if (this.geometry.vertexColors[i] !== this.geometry.vertexColors[i % 4]) {
136-
allVertexColorsSame = false;
137-
break;
138-
}
139-
}
140-
if (allVertexColorsSame) {
141-
this.geometry.vertexColors = [];
142-
}
143-
144129
return this.geometry;
145130
}
146131
}

src/webgl/p5.Camera.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,12 +1946,12 @@ p5.Camera = class Camera {
19461946
// @TODO: combine this function with _setDefaultCamera to compute these values
19471947
// as-needed
19481948
_computeCameraDefaultSettings() {
1949-
this.defaultCameraFOV = 60 / 180 * Math.PI;
19501949
this.defaultAspectRatio = this._renderer.width / this._renderer.height;
19511950
this.defaultEyeX = 0;
19521951
this.defaultEyeY = 0;
1953-
this.defaultEyeZ =
1954-
this._renderer.height / 2.0 / Math.tan(this.defaultCameraFOV / 2.0);
1952+
this.defaultEyeZ = 800;
1953+
this.defaultCameraFOV =
1954+
2 * Math.atan(this._renderer.height / 2 / this.defaultEyeZ);
19551955
this.defaultCenterX = 0;
19561956
this.defaultCenterY = 0;
19571957
this.defaultCenterZ = 0;
@@ -1986,12 +1986,9 @@ p5.Camera = class Camera {
19861986
// If we're using the default camera, update the aspect ratio
19871987
if (this.cameraType === 'default') {
19881988
this._computeCameraDefaultSettings();
1989-
this._setDefaultCamera();
1990-
} else {
1991-
this.perspective(
1992-
this.cameraFOV,
1993-
this._renderer.width / this._renderer.height
1994-
);
1989+
this.cameraFOV = this.defaultCameraFOV;
1990+
this.aspectRatio = this.defaultAspectRatio;
1991+
this.perspective();
19951992
}
19961993
}
19971994

src/webgl/p5.Framebuffer.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,8 @@ class FramebufferCamera extends p5.Camera {
3333
_computeCameraDefaultSettings() {
3434
super._computeCameraDefaultSettings();
3535
this.defaultAspectRatio = this.fbo.width / this.fbo.height;
36-
this.defaultEyeZ =
37-
this.fbo.height / 2.0 / Math.tan(this.defaultCameraFOV / 2.0);
38-
this.defaultCameraNear = this.defaultEyeZ * 0.1;
39-
this.defaultCameraFar = this.defaultEyeZ * 10;
40-
}
41-
42-
/**
43-
* Resets the camera to a default perspective camera sized to match
44-
* the p5.Framebuffer it is attached to.
45-
*
46-
* @method resize
47-
* @private
48-
*/
49-
_resize() {
50-
// If we're using the default camera, update the aspect ratio
51-
if (this.cameraType === 'default') {
52-
this._computeCameraDefaultSettings();
53-
this._setDefaultCamera();
54-
} else {
55-
this.perspective(
56-
this.cameraFOV,
57-
this.fbo.width / this.fbo.height
58-
);
59-
}
36+
this.defaultCameraFOV =
37+
2 * Math.atan(this.fbo.height / 2 / this.defaultEyeZ);
6038
}
6139
}
6240

src/webgl/p5.Geometry.js

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import p5 from '../core/main';
1818
* @param {function} [callback] function to call upon object instantiation.
1919
*/
2020
p5.Geometry = class Geometry {
21-
constructor(detailX, detailY, callback){
22-
//an array containing every vertex
23-
//@type [p5.Vector]
21+
constructor(detailX, detailY, callback) {
22+
//an array containing every vertex
23+
//@type [p5.Vector]
2424
this.vertices = [];
2525

2626
//an array containing every vertex for stroke drawing
@@ -87,7 +87,60 @@ p5.Geometry = class Geometry {
8787

8888
this.dirtyFlags = {};
8989
}
90-
90+
/**
91+
* Removes the internal colors of p5.Geometry.
92+
* Using `clearColors()`, you can use `fill()` to supply new colors before drawing each shape.
93+
* If `clearColors()` is not used, the shapes will use their internal colors by ignoring `fill()`.
94+
*
95+
* @method clearColors
96+
*
97+
* @example
98+
* <div>
99+
* <code>
100+
* let shape01;
101+
* let shape02;
102+
* let points = [];
103+
*
104+
* function setup() {
105+
* createCanvas(600, 600, WEBGL);
106+
* points.push(new p5.Vector(-1, -1, 0), new p5.Vector(-1, 1, 0),
107+
* new p5.Vector(1, -1, 0), new p5.Vector(-1, -1, 0));
108+
* buildShape01();
109+
* buildShape02();
110+
* }
111+
* function draw() {
112+
* background(0);
113+
* fill('pink'); // shape01 retains its internal blue color, so it won't turn pink.
114+
* model(shape01);
115+
* fill('yellow'); // Now, shape02 is yellow.
116+
* model(shape02);
117+
* }
118+
*
119+
* function buildShape01() {
120+
* beginGeometry();
121+
* fill('blue'); // shape01's color is blue because its internal colors remain.
122+
* beginShape();
123+
* for (let vec of points) vertex(vec.x * 100, vec.y * 100, vec.z * 100);
124+
* endShape(CLOSE);
125+
* shape01 = endGeometry();
126+
* }
127+
*
128+
* function buildShape02() {
129+
* beginGeometry();
130+
* fill('red'); // shape02.clearColors() removes its internal colors. Now, shape02 is red.
131+
* beginShape();
132+
* for (let vec of points) vertex(vec.x * 200, vec.y * 200, vec.z * 200);
133+
* endShape(CLOSE);
134+
* shape02 = endGeometry();
135+
* shape02.clearColors(); // Resets shape02's colors.
136+
* }
137+
* </code>
138+
* </div>
139+
*/
140+
clearColors() {
141+
this.vertexColors = [];
142+
return this;
143+
}
91144
/**
92145
* computes faces for geometry objects based on the vertices.
93146
* @method computeFaces
@@ -111,7 +164,7 @@ p5.Geometry = class Geometry {
111164
}
112165

113166
_getFaceNormal(faceId) {
114-
//This assumes that vA->vB->vC is a counter-clockwise ordering
167+
//This assumes that vA->vB->vC is a counter-clockwise ordering
115168
const face = this.faces[faceId];
116169
const vA = this.vertices[face[0]];
117170
const vB = this.vertices[face[1]];
@@ -196,7 +249,7 @@ p5.Geometry = class Geometry {
196249
* @chainable
197250
*/
198251
averagePoleNormals() {
199-
//average the north pole
252+
//average the north pole
200253
let sum = new p5.Vector(0, 0, 0);
201254
for (let i = 0; i < this.detailX; i++) {
202255
sum.add(this.vertexNormals[i]);
@@ -507,7 +560,7 @@ p5.Geometry = class Geometry {
507560
*/
508561
normalize() {
509562
if (this.vertices.length > 0) {
510-
// Find the corners of our bounding box
563+
// Find the corners of our bounding box
511564
const maxPosition = this.vertices[0].copy();
512565
const minPosition = this.vertices[0].copy();
513566

src/webgl/shaders/line.vert

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ void main() {
169169
// find where the lines intersect to find the elbow of the join
170170
vec2 c = (posp.xy/posp.w + vec2(1.,1.)) * 0.5 * uViewport.zw;
171171
vec2 intersection = lineIntersection(
172-
c + (side * normalIn * uStrokeWeight / 2.) * curPerspScale,
172+
c + (side * normalIn * uStrokeWeight / 2.),
173173
tangentIn,
174-
c + (side * normalOut * uStrokeWeight / 2.) * curPerspScale,
174+
c + (side * normalOut * uStrokeWeight / 2.),
175175
tangentOut
176176
);
177177
offset = (intersection - c);
@@ -186,9 +186,9 @@ void main() {
186186
offset *= maxMag / mag;
187187
}
188188
} else if (sideEnum == 1.) {
189-
offset = side * normalIn * curPerspScale * uStrokeWeight / 2.;
189+
offset = side * normalIn * uStrokeWeight / 2.;
190190
} else if (sideEnum == 3.) {
191-
offset = side * normalOut * curPerspScale * uStrokeWeight / 2.;
191+
offset = side * normalOut * uStrokeWeight / 2.;
192192
}
193193
}
194194
if (uStrokeJoin == STROKE_JOIN_BEVEL) {
@@ -207,12 +207,12 @@ void main() {
207207
// extends out from the line
208208
float tangentOffset = abs(aSide) - 1.;
209209
offset = (normal * normalOffset + tangent * tangentOffset) *
210-
uStrokeWeight * 0.5 * curPerspScale;
210+
uStrokeWeight * 0.5;
211211
vMaxDist = uStrokeWeight / 2.;
212212
}
213-
vPosition = vCenter + offset / curPerspScale;
213+
vPosition = vCenter + offset;
214214

215-
gl_Position.xy = p.xy + offset.xy;
215+
gl_Position.xy = p.xy + offset.xy * curPerspScale;
216216
gl_Position.zw = p.zw;
217217

218218
vColor = (uUseLineColor ? aVertexColor : uMaterialColor);

0 commit comments

Comments
 (0)