MCPcopy
hub / github.com/processing/p5.js / constructFromContours

Method constructFromContours

src/webgl/ShapeBuilder.js:52–206  ·  view source on GitHub ↗
(shape, contours)

Source from the content-addressed store, hash-verified

50 }
51
52 constructFromContours(shape, contours) {
53 if (this._useUserVertexProperties){
54 this._resetUserVertexProperties();
55 }
56 this.geometry.reset();
57 this.contourIndices = [];
58 // TODO: handle just some contours having non-PATH mode
59 this.shapeMode = shape.contours[0].kind;
60 const shouldProcessEdges = !!this.renderer.states.strokeColor;
61
62 const userVertexPropertyHelpers = {};
63 if (shape.userVertexProperties) {
64 this._useUserVertexProperties = true;
65 for (const key in shape.userVertexProperties) {
66 const name = shape.vertexPropertyName(key);
67 const prop = this.geometry._userVertexPropertyHelper(
68 name,
69 [],
70 shape.userVertexProperties[key]
71 );
72 userVertexPropertyHelpers[key] = prop;
73 this.tessyVertexSize += prop.getDataSize();
74 this.bufferStrides[prop.getSrcName()] = prop.getDataSize();
75 this.renderer.buffers.user.push(
76 new RenderBuffer(
77 prop.getDataSize(),
78 prop.getSrcName(),
79 prop.getDstName(),
80 name,
81 this.renderer
82 )
83 );
84 }
85 } else {
86 this._useUserVertexProperties = false;
87 }
88
89 for (const contour of contours) {
90 this.contourIndices.push(this.geometry.vertices.length);
91 for (const vertex of contour) {
92 // WebGL doesn't support QUADS or QUAD_STRIP, so we duplicate data to turn
93 // QUADS into TRIANGLES and QUAD_STRIP into TRIANGLE_STRIP. (There is no extra
94 // work to convert QUAD_STRIP here, since the only difference is in how edges
95 // are rendered.)
96 if (this.shapeMode === constants.QUADS) {
97 // A finished quad turned into triangles should leave 6 vertices in the
98 // buffer:
99 // 0--3 0 3--5
100 // | | --> | \ \ |
101 // 1--2 1--2 4
102 // When vertex index 3 is being added, add the necessary duplicates.
103 if (this.geometry.vertices.length % 6 === 3) {
104 for (const key in this.bufferStrides) {
105 const stride = this.bufferStrides[key];
106 const buffer = this.geometry[key];
107 buffer.push(
108 ...buffer.slice(
109 buffer.length - 3 * stride,

Callers 1

drawShapeMethod · 0.80

Calls 15

_calculateEdgesMethod · 0.95
_tesselateShapeMethod · 0.95
vertexPropertyNameMethod · 0.80
getDataSizeMethod · 0.80
getSrcNameMethod · 0.80
getDstNameMethod · 0.80
sliceMethod · 0.80
setCurrentDataMethod · 0.80
pushCurrentDataMethod · 0.80

Tested by

no test coverage detected