Skip to content
29 changes: 22 additions & 7 deletions src/core/p5.Renderer3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,24 @@ export class Renderer3D extends Renderer {

model(model, count) {
count = count ?? this._instanceCount ?? 1;
if (model.vertices.length > 0) {
if (model.vertices.length > 0 || model.parts.some(part => part.vertices.length > 0)) {
if (this.geometryBuilder) {
this.geometryBuilder.addRetained(model);
} else {
if (!this.geometryInHash(model.gid)) {
model._edgesToVertices();
this._getOrMakeCachedBuffers(model);
if (model.parts.length === 1 && model.parts[0] === model) {
if (!this.geometryInHash(model.gid)) {
model._edgesToVertices(this);
this._getOrMakeCachedBuffers(model);
}
} else {
for (const part of model.parts) {
if (!this.geometryInHash(part.gid)) {
if (part.edges.length > 0) {
part._edgesToVertices(this);
}
this._getOrMakeCachedBuffers(part);
}
}
}

this._drawGeometry(model, { count });
Expand Down Expand Up @@ -605,14 +616,14 @@ export class Renderer3D extends Renderer {

if (
this.states.fillColor &&
geometry.vertices.length >= 3 &&
![constants.LINES, constants.POINTS].includes(mode)
) {
// draw every part. a geometry always has at least one part (the
// constructor makes it its own single part when nothing else does), so no
// fallback is needed. a part with no material state draws straight;
// multi-material parts apply their own material around the draw.
for (const part of geometry.parts) {
if (part.vertices.length < 3) continue;
const state = part.partState;
// fill/ambientColor/specularColor are arrays and texture is a p5.Image,
// so they're truthy when set; shininess is a number where 0 is valid.
Expand All @@ -638,8 +649,12 @@ export class Renderer3D extends Renderer {
}
}

if (this.states.strokeColor && geometry.lineVertices.length >= 1) {
this._drawStrokes(geometry, { count });
if (this.states.strokeColor) {
for (const part of geometry.parts) {
if (part.lineVertices.length > 0) {
this._drawStrokes(part, { count });
}
}
}

this.buffers.user = [];
Expand Down
33 changes: 22 additions & 11 deletions src/webgl/3d_primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,18 @@ function primitives3D(p5, fn) {
* }
*/
fn.freeGeometry = function (geometry) {
this._renderer.geometryBufferCache.freeBuffers(geometry.gid);
const renderer = this._renderer;
const cache = renderer.geometryBufferCache;

cache.freeBuffers(geometry.gid);

for (const part of geometry.parts) {
if (part !== geometry) {
cache.freeBuffers(part.gid);
}
}

renderer._scheduleRetiredBufferDestruction?.();
};

/**
Expand Down Expand Up @@ -1529,7 +1540,7 @@ function primitives3D(p5, fn) {
this.uvs = [0, 0, 1, 0, 1, 1];
};
const triGeom = new Geometry(1, 1, _triangle, this);
triGeom._edgesToVertices();
triGeom._edgesToVertices(this);
triGeom.computeNormals();
triGeom.gid = gid;
this.geometryBufferCache.ensureCached(triGeom);
Expand Down Expand Up @@ -1681,7 +1692,7 @@ function primitives3D(p5, fn) {
arcGeom.computeNormals();

if (detail <= 50) {
arcGeom._edgesToVertices(arcGeom);
arcGeom._edgesToVertices(this);
} else if (this.states.strokeColor) {
console.log(
`Cannot apply a stroke to an ${shape} with more than 50 detail`
Expand Down Expand Up @@ -1743,7 +1754,7 @@ function primitives3D(p5, fn) {
}
};
const rectGeom = new Geometry(detailX, detailY, _rect, this);
rectGeom.computeFaces().computeNormals()._edgesToVertices();
rectGeom.computeFaces().computeNormals()._edgesToVertices(this);
rectGeom.gid = gid;
this.geometryBufferCache.ensureCached(rectGeom);
}
Expand Down Expand Up @@ -1918,7 +1929,7 @@ function primitives3D(p5, fn) {
const endVertex = vertexOrder[(i + 1) % vertexOrder.length];
quadGeom.edges.push([startVertex, endVertex]);
}
quadGeom._edgesToVertices();
quadGeom._edgesToVertices(this);
quadGeom.gid = gid;
this.geometryBufferCache.ensureCached(quadGeom);
}
Expand Down Expand Up @@ -2241,7 +2252,7 @@ function primitives3D(p5, fn) {
const planeGeom = new Geometry(detailX, detailY, _plane, this);
planeGeom.computeFaces().computeNormals();
if (detailX <= 1 && detailY <= 1) {
planeGeom._makeTriangleEdges()._edgesToVertices();
planeGeom._makeTriangleEdges()._edgesToVertices(this);
} else if (this.states.strokeColor) {
console.log(
'Cannot draw stroke on plane objects with more' +
Expand Down Expand Up @@ -2326,7 +2337,7 @@ function primitives3D(p5, fn) {
const boxGeom = new Geometry(detailX, detailY, _box, this);
boxGeom.computeNormals();
if (detailX <= 4 && detailY <= 4) {
boxGeom._edgesToVertices();
boxGeom._edgesToVertices(this);
} else if (this.states.strokeColor) {
console.log(
'Cannot draw stroke on box objects with more' +
Expand Down Expand Up @@ -2391,7 +2402,7 @@ function primitives3D(p5, fn) {
const ellipsoidGeom = new Geometry(detailX, detailY, _ellipsoid, this);
ellipsoidGeom.computeFaces();
if (detailX <= 24 && detailY <= 24) {
ellipsoidGeom._makeTriangleEdges()._edgesToVertices();
ellipsoidGeom._makeTriangleEdges()._edgesToVertices(this);
} else if (this.states.strokeColor) {
console.log(
'Cannot draw stroke on ellipsoids with more' +
Expand Down Expand Up @@ -2439,7 +2450,7 @@ function primitives3D(p5, fn) {
);
// normals are computed in call to _truncatedCone
if (detailX <= 24 && detailY <= 16) {
cylinderGeom._makeTriangleEdges()._edgesToVertices();
cylinderGeom._makeTriangleEdges()._edgesToVertices(this);
} else if (this.states.strokeColor) {
console.log(
'Cannot draw stroke on cylinder objects with more' +
Expand Down Expand Up @@ -2476,7 +2487,7 @@ function primitives3D(p5, fn) {
this
);
if (detailX <= 24 && detailY <= 16) {
coneGeom._makeTriangleEdges()._edgesToVertices();
coneGeom._makeTriangleEdges()._edgesToVertices(this);
} else if (this.states.strokeColor) {
console.log(
'Cannot draw stroke on cone objects with more' +
Expand Down Expand Up @@ -2544,7 +2555,7 @@ function primitives3D(p5, fn) {
const torusGeom = new Geometry(detailX, detailY, _torus, this);
torusGeom.computeFaces();
if (detailX <= 24 && detailY <= 16) {
torusGeom._makeTriangleEdges()._edgesToVertices();
torusGeom._makeTriangleEdges()._edgesToVertices(this);
} else if (this.states.strokeColor) {
console.log(
'Cannot draw strokes on torus object with more' +
Expand Down
40 changes: 32 additions & 8 deletions src/webgl/GeometryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,34 @@ class GeometryBuilder {
* part stays aligned whatever shape the uvs are.
*/
_addToCurrentPart(input, vertices, normals, vertexColors) {
// parts are made of fills; a stroke-only draw contributes no faces
if (!this.renderer.states.fillColor) return;
const hasFill = Boolean(
this.renderer.states.fillColor && input.faces.length
);
const hasStroke = Boolean(
this.renderer.states.strokeColor && input.edges.length
);

if (!hasFill && !hasStroke) return;

const state = this._snapshotPartState();
if (
!this.currentPart ||
!this._sameMaterial(state, this.currentPart.partState)
) {

if (!this.currentPart) {
this.currentPart = new GeometryPart(
`${this.geometry.gid}|part${this.parts.length}`,
state
);
this.parts.push(this.currentPart);
} else if (hasFill) {
if (this.currentPart.faces.length === 0) {
// A stroke-only part can adopt the material of its first fill.
this.currentPart.partState = state;
} else if (!this._sameMaterial(state, this.currentPart.partState)) {
this.currentPart = new GeometryPart(
`${this.geometry.gid}|part${this.parts.length}`,
state
);
this.parts.push(this.currentPart);
}
}

const part = this.currentPart;
Expand All @@ -205,8 +220,17 @@ class GeometryBuilder {
for (const c of vertexColors) {
part.vertexColors.push(c);
}
for (const f of input.faces) {
part.faces.push(f.map(idx => idx + startIdx));

if (hasFill) {
for (const f of input.faces) {
part.faces.push(f.map(idx => idx + startIdx));
}
}

if (hasStroke) {
for (const edge of input.edges) {
part.edges.push(edge.map(idx => idx + startIdx));
}
}

// carry custom per-vertex attributes into the part too, the same way the
Expand Down
2 changes: 1 addition & 1 deletion src/webgl/ShapeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class ShapeBuilder {
);
}
if (shouldProcessEdges && !this.renderer.geometryBuilder) {
this.geometry._edgesToVertices();
this.geometry._edgesToVertices(this.renderer);
}

if (this.shapeMode === constants.PATH) {
Expand Down
1 change: 1 addition & 0 deletions src/webgl/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ function buildMaterialParts(model, faceMaterials, materials) {
});
part.faces.push(localFace);
}
part._makeTriangleEdges();
parts.push(part);
}

Expand Down
Loading