-
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathPShapeWebGPU.java
More file actions
290 lines (251 loc) · 7.46 KB
/
Copy pathPShapeWebGPU.java
File metadata and controls
290 lines (251 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package processing.webgpu;
import processing.core.PGraphics;
import processing.core.PShape;
import processing.core.PVector;
/**
* WebGPU implementation of PShape.
*/
public class PShapeWebGPU extends PShape {
/** Reference to the graphics context */
protected PGraphicsWebGPU pg;
/** Native geometry ID (0 = not yet created) */
protected long geometryId = 0;
/** Native layout ID for custom vertex attributes */
protected long layoutId = 0;
/** Current topology for this shape */
protected byte topology = PWebGPU.TOPOLOGY_TRIANGLE_LIST;
/** Track if we're currently building the shape */
protected boolean building = false;
/** Pending normal for next vertex */
protected float normalX, normalY, normalZ;
protected boolean hasNormal = false;
/** Pending color for next vertex */
protected float colorR, colorG, colorB, colorA;
protected boolean hasColor = false;
/** Pending UV for next vertex */
protected float uvU, uvV;
protected boolean hasUV = false;
/**
* Create a new WebGPU shape.
*/
public PShapeWebGPU(PGraphicsWebGPU pg, int family) {
this.pg = pg;
this.family = family;
}
/**
* Map Processing shape kinds to WebGPU topologies.
*/
protected byte kindToTopology(int kind) {
return switch (kind) {
case POINTS -> PWebGPU.TOPOLOGY_POINT_LIST;
case LINES -> PWebGPU.TOPOLOGY_LINE_LIST;
case LINE_STRIP -> PWebGPU.TOPOLOGY_LINE_STRIP;
case TRIANGLES -> PWebGPU.TOPOLOGY_TRIANGLE_LIST;
case TRIANGLE_STRIP -> PWebGPU.TOPOLOGY_TRIANGLE_STRIP;
case TRIANGLE_FAN -> PWebGPU.TOPOLOGY_TRIANGLE_LIST; // Will need tessellation
case QUADS -> PWebGPU.TOPOLOGY_TRIANGLE_LIST; // Will need tessellation
case QUAD_STRIP -> PWebGPU.TOPOLOGY_TRIANGLE_STRIP;
default -> PWebGPU.TOPOLOGY_TRIANGLE_LIST;
};
}
@Override
public void beginShape(int kind) {
this.kind = kind;
this.topology = kindToTopology(kind);
// Create or reset the geometry
if (geometryId != 0) {
PWebGPU.geometryDestroy(geometryId);
}
geometryId = PWebGPU.geometryCreate(topology);
building = true;
// Reset pending attributes
hasNormal = false;
hasColor = false;
hasUV = false;
}
@Override
public void endShape(int mode) {
building = false;
// If CLOSE mode and we have a polygon, we might need to add indices
// For now, the geometry is ready to be rendered
}
@Override
public void normal(float nx, float ny, float nz) {
if (geometryId != 0) {
PWebGPU.geometryNormal(geometryId, nx, ny, nz);
}
normalX = nx;
normalY = ny;
normalZ = nz;
hasNormal = true;
}
@Override
public void vertex(float x, float y) {
vertex(x, y, 0);
}
@Override
public void vertex(float x, float y, float z) {
if (geometryId == 0) {
return;
}
if (hasColor) {
PWebGPU.geometryColor(geometryId, colorR, colorG, colorB, colorA);
}
if (hasUV) {
PWebGPU.geometryUv(geometryId, uvU, uvV);
}
PWebGPU.geometryVertex(geometryId, x, y, z);
}
@Override
public void vertex(float x, float y, float u, float v) {
texture(u, v);
vertex(x, y, 0);
}
@Override
public void vertex(float x, float y, float z, float u, float v) {
texture(u, v);
vertex(x, y, z);
}
/**
* Set texture coordinates for the next vertex.
*/
public void texture(float u, float v) {
uvU = u;
uvV = v;
hasUV = true;
}
/**
* Set the fill color for subsequent vertices.
*/
@Override
public void fill(int rgb) {
colorR = ((rgb >> 16) & 0xFF) / 255f;
colorG = ((rgb >> 8) & 0xFF) / 255f;
colorB = (rgb & 0xFF) / 255f;
colorA = ((rgb >> 24) & 0xFF) / 255f;
hasColor = true;
}
@Override
public void fill(float gray) {
colorR = colorG = colorB = gray / 255f;
colorA = 1f;
hasColor = true;
}
@Override
public void fill(float r, float g, float b) {
colorR = r / 255f;
colorG = g / 255f;
colorB = b / 255f;
colorA = 1f;
hasColor = true;
}
@Override
public void fill(float r, float g, float b, float a) {
colorR = r / 255f;
colorG = g / 255f;
colorB = b / 255f;
colorA = a / 255f;
hasColor = true;
}
/**
* Add an index for indexed rendering.
*/
public void index(int i) {
if (geometryId != 0) {
PWebGPU.geometryIndex(geometryId, i);
}
}
@Override
public int getVertexCount() {
if (geometryId == 0) {
return 0;
}
return PWebGPU.geometryVertexCount(geometryId);
}
/**
* Get the index count for this shape.
*/
public int getIndexCount() {
if (geometryId == 0) {
return 0;
}
return PWebGPU.geometryIndexCount(geometryId);
}
@Override
public void setVertex(int index, float x, float y, float z) {
if (geometryId != 0) {
PWebGPU.geometrySetVertex(geometryId, index, x, y, z);
}
}
@Override
public void setNormal(int index, float nx, float ny, float nz) {
if (geometryId != 0) {
PWebGPU.geometrySetNormal(geometryId, index, nx, ny, nz);
}
}
/**
* Set the color of a specific vertex.
*/
public void setColor(int index, float r, float g, float b, float a) {
if (geometryId != 0) {
PWebGPU.geometrySetColor(geometryId, index, r, g, b, a);
}
}
/**
* Set the UV coordinates of a specific vertex.
*/
public void setUv(int index, float u, float v) {
if (geometryId != 0) {
PWebGPU.geometrySetUv(geometryId, index, u, v);
}
}
/**
* Get the native geometry ID for direct rendering.
*/
public long getGeometryId() {
return geometryId;
}
/**
* Draw this shape using the associated graphics context.
*/
public void draw() {
if (geometryId != 0 && pg != null) {
pg.model(geometryId);
}
}
@Override
protected void drawImpl(PGraphics g) {
if (geometryId != 0 && g instanceof PGraphicsWebGPU webgpu) {
webgpu.model(geometryId);
}
}
/**
* Release native resources.
*/
public void dispose() {
if (geometryId != 0) {
PWebGPU.geometryDestroy(geometryId);
geometryId = 0;
}
if (layoutId != 0) {
PWebGPU.geometryLayoutDestroy(layoutId);
layoutId = 0;
}
}
/**
* Create a box geometry.
*/
public static PShapeWebGPU createBox(PGraphicsWebGPU pg, float width, float height, float depth) {
PShapeWebGPU shape = new PShapeWebGPU(pg, GEOMETRY);
shape.geometryId = PWebGPU.geometryBox(width, height, depth);
return shape;
}
/**
* Create a sphere geometry.
*/
public static PShapeWebGPU createSphere(PGraphicsWebGPU pg, float radius, int sectors, int stacks) {
PShapeWebGPU shape = new PShapeWebGPU(pg, GEOMETRY);
shape.geometryId = PWebGPU.geometrySphere(radius, sectors, stacks);
return shape;
}
}