Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 | 172x 172x 172x 172x 3x 3x 3x 6x 3x 3x 3x 5x 3x 3x 11x 11x 11x 9x 2x 2x 13x 13x 13x 11x 2x 2x 3x 3x 3x 6x 3x 3x 3x 3x 6x 3x 17x 17x 15x 1x 14x 14x 15x 3x 3x 12x 15x 15x 15x 2x 2x 6x 6x 6x 1x 1x 5x 6x 6x 6x 11x 11x 2x 9x 2x 7x 7x 11x 11x 6x 6x 6x 60x 60x 60x 60x 60x 377x 377x 377x 3x 374x 377x 109x 268x 377x 43x 43x 1x 43x 52x 52x 24x 24x 24x 52x 7x 7x 25x 25x 25x 25x 4x 4x 4x 4x 21x 25x 110x 110x 110x 111x 110x 110x 110x 18x 10x 11x 3x 8x 8x 11x 11x 11x 20x 20x 11x 54x 54x 54x 54x 54x 54x 54x 89x 89x 89x 89x 43x 501x 89x 89x 54x 54x 54x 54x 54x 54x 8x 54x 54x 54x 4x 4x 4x 4x 4x 4x 50x 50x 35x 50x 50x 50x 50x 50x 54x 3x 54x 35x 17x 17x 17x 17x 17x 17x 17x 4x 17x 17x 17x 17x 35x 35x 35x 35x 35x 35x 5x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 71x 35x 35x 16x 16x 16x 16x 16x 16x 16x 2x 16x 16x 35x 35x 15x 15x 35x 71x 213x 213x 213x 213x 213x 213x 71x 26x 19x 19x 19x 19x 19x 19x 19x 2x 19x 19x 26x 26x 4x 4x 26x 1x 26x 26x 26x 26x 26x 18x 18x 8x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 17x 17x 17x 15x 3x 12x 12x 12x 12x 12x 8x 8x 8x 8x 8x 8x 8x 8x 8x 15x 50x 15x 15x 15x 9x 9x 17x 8x 8x 174x 85x 8x 8x 17x 2x 2x 17x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 48x 48x 12x 12x 1x 1x 1x 17x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 24x 24x 6x 6x 1x 1x 1x 17x 122x 77x 45x 45x 45x 45x 15x 15x 15x 15x 15x 15x 15x 122x 122x 122x 283x 283x 385x 385x 385x 385x 385x 283x 122x 107x 107x 107x 107x 107x 265x 265x 265x 722x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 122x 122x 122x 122x 7x 10x 9x 9x 9x 36x 36x 122x 122x 122x 122x 122x 15x 15x 107x 15x 14x 14x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 8x 8x 8x 8x 8x 8x 8x 8x 12x 12x 12x 12x 12x 7x 7x 12x 12x 2x 1x 1x 1x 1x 4x 1x 1x 1x 1x 34x 5x 5x 775x 775x 773x 2x 30x 30x 1335x 1335x 420x 420x 420x 131x 289x 289x 289x 289x 119x 275x 275x 275x 119x 119x 119x 60x 15x 45x 45x 45x 45x 45x 293x 2474x 293x 293x 300x 221x 221x 221x 221x 11x 221x 221x 221x 221x 221x 2496x 300x 293x 293x 293x 300x 300x 299x 299x 2474x 299x 299x 293x 299x 299x 299x 299x 299x 299x 299x 299x 299x 2474x 2474x 299x 299x 299x 299x 299x 299x 299x 299x 299x 299x 299x 299x 299x 2474x 299x |
import { Context } from "./context";
import * as Inputs from "./inputs";
import { DrawHelperCore } from "@bitbybit-dev/core";
import { JSCADText } from "@bitbybit-dev/jscad-worker";
import { Vector } from "@bitbybit-dev/base";
import { JSCADWorkerManager } from "@bitbybit-dev/jscad-worker";
import { ManifoldWorkerManager } from "@bitbybit-dev/manifold-worker";
import { OCCTWorkerManager } from "@bitbybit-dev/occt-worker";
import * as pc from "playcanvas";
import { DEFAULT_COLORS, CACHE_CONFIG } from "./constants";
// Type alias for polyline entities with user data
type PolylineEntity = Inputs.Draw.PolylineEntity;
export class DrawHelper extends DrawHelperCore {
// Map-based material cache for better performance
private readonly materialCache = new Map<string, pc.StandardMaterial>();
// Entity ID generation
private entityIdCounter = 0;
private readonly instanceId = `pc-${Date.now()}`;
constructor(
private readonly context: Context,
private readonly solidText: JSCADText,
public readonly vector: Vector,
private readonly jscadWorkerManager: JSCADWorkerManager,
private readonly manifoldWorkerManager: ManifoldWorkerManager,
private readonly occWorkerManager: OCCTWorkerManager
) {
super(vector);
}
/**
* Check if DrawHelper has been disposed
* @returns True if disposed, false otherwise
*/
public isDisposed(): boolean {
return this.materialCache.size === 0;
}
async drawManifoldsOrCrossSections(inputs: Inputs.Manifold.DrawManifoldsOrCrossSectionsDto<Inputs.Manifold.ManifoldPointer | Inputs.Manifold.CrossSectionPointer, pc.StandardMaterial>): Promise<pc.Entity> {
try {
const safeWorkerOptions = this.getSafeWorkerOptions(inputs);
const decomposedMesh: Inputs.Manifold.DecomposedManifoldMeshDto[] = await this.manifoldWorkerManager.genericCallToWorkerPromise("decomposeManifoldsOrCrossSections", safeWorkerOptions);
const meshes = decomposedMesh.map(dec => this.handleDecomposedManifold(dec, inputs)).filter(s => s !== undefined);
const containerId = this.generateEntityId("manifoldMeshContainer");
const manifoldMeshContainer = new pc.Entity(containerId);
meshes.forEach(mesh => {
manifoldMeshContainer.addChild(mesh);
});
this.context.scene.addChild(manifoldMeshContainer);
return manifoldMeshContainer;
} catch (error) {
console.error("Error drawing manifolds or cross sections:", error);
throw new Error(`Failed to draw manifolds or cross sections: ${error instanceof Error ? error.message : String(error)}`);
}
}
async drawManifoldOrCrossSection(inputs: Inputs.Manifold.DrawManifoldOrCrossSectionDto<Inputs.Manifold.ManifoldPointer | Inputs.Manifold.CrossSectionPointer, pc.StandardMaterial>): Promise<pc.Entity> {
try {
const safeWorkerOptions = this.getSafeWorkerOptions(inputs);
const decomposedMesh: Inputs.Manifold.DecomposedManifoldMeshDto = await this.manifoldWorkerManager.genericCallToWorkerPromise("decomposeManifoldOrCrossSection", safeWorkerOptions);
return this.handleDecomposedManifold(decomposedMesh, inputs);
} catch (error) {
console.error("Error drawing manifold or cross section:", error);
throw new Error(`Failed to draw manifold or cross section: ${error instanceof Error ? error.message : String(error)}`);
}
}
async drawShape(inputs: Inputs.OCCT.DrawShapeDto<Inputs.OCCT.TopoDSShapePointer>): Promise<pc.Entity> {
try {
const safeWorkerOptions = this.getSafeWorkerOptions(inputs);
const decomposedMesh: Inputs.OCCT.DecomposedMeshDto = await this.occWorkerManager.genericCallToWorkerPromise("shapeToMesh", safeWorkerOptions);
return this.handleDecomposedMesh(inputs, decomposedMesh, inputs);
} catch (error) {
console.error("Error drawing OCCT shape:", error);
throw new Error(`Failed to draw OCCT shape: ${error instanceof Error ? error.message : String(error)}`);
}
}
async drawShapes(inputs: Inputs.OCCT.DrawShapesDto<Inputs.OCCT.TopoDSShapePointer>): Promise<pc.Entity> {
try {
const safeWorkerOptions = this.getSafeWorkerOptions(inputs);
const meshes: Inputs.OCCT.DecomposedMeshDto[] = await this.occWorkerManager.genericCallToWorkerPromise("shapesToMeshes", safeWorkerOptions);
const meshesSolved = await Promise.all(meshes.map(async decomposedMesh => this.handleDecomposedMesh(inputs, decomposedMesh, inputs)));
const containerId = this.generateEntityId("shapesMeshContainer");
const shapesMeshContainer = new pc.Entity(containerId);
this.context.scene.addChild(shapesMeshContainer);
meshesSolved.forEach(mesh => {
shapesMeshContainer.addChild(mesh);
});
return shapesMeshContainer;
} catch (error) {
console.error("Error drawing OCCT shapes:", error);
throw new Error(`Failed to draw OCCT shapes: ${error instanceof Error ? error.message : String(error)}`);
}
}
async drawSolidOrPolygonMesh(inputs: Inputs.JSCAD.DrawSolidMeshDto<pc.Entity>): Promise<pc.Entity> {
try {
const res: {
positions: number[],
normals: number[],
indices: number[],
transforms: [],
} = await this.jscadWorkerManager.genericCallToWorkerPromise("shapeToMesh", inputs);
let meshToUpdate: pc.Entity;
if (inputs.jscadMesh && inputs.updatable) {
meshToUpdate = inputs.jscadMesh;
} else {
meshToUpdate = new pc.Entity(this.generateEntityId("jscadMesh"));
this.context.scene.addChild(meshToUpdate);
}
let colour;
if (inputs.mesh.color && inputs.mesh.color.length > 0) {
// if jscad geometry is colorized and color is baked on geometry it will be used over anything that set in the draw options
const c = inputs.mesh.color;
colour = this.normalizeColor(c, DEFAULT_COLORS.DEFAULT);
} else {
colour = Array.isArray(inputs.colours) ? inputs.colours[0] : inputs.colours;
}
const s = this.makeMesh({
...inputs,
colour,
drawTwoSided: inputs.drawTwoSided,
backFaceColour: inputs.backFaceColour,
backFaceOpacity: inputs.backFaceOpacity
}, meshToUpdate, res);
inputs.jscadMesh = s;
return s;
} catch (error) {
console.error("Error drawing JSCAD solid or polygon mesh:", error);
throw new Error(`Failed to draw JSCAD mesh: ${error instanceof Error ? error.message : String(error)}`);
}
}
async drawSolidOrPolygonMeshes(inputs: Inputs.JSCAD.DrawSolidMeshesDto<pc.Entity>): Promise<pc.Entity> {
try {
const res: {
positions: number[],
normals: number[],
indices: number[],
transforms: [],
color?: number[]
}[] = await this.jscadWorkerManager.genericCallToWorkerPromise("shapesToMeshes", inputs);
let localOrigin: pc.Entity;
if (inputs.jscadMesh && inputs.updatable) {
localOrigin = inputs.jscadMesh;
this.clearEntity(localOrigin);
} else {
localOrigin = new pc.Entity(this.generateEntityId("jscadMeshes"));
}
const colourIsArrayAndMatches = Array.isArray(inputs.colours) && inputs.colours.length === res.length;
const colorsAreArrays = Array.isArray(inputs.colours);
res.forEach((r, index) => {
const meshToUpdate = new pc.Entity(this.generateEntityId("jscadMesh", localOrigin.name));
let colour;
if (r.color) {
colour = this.normalizeColor(r.color, DEFAULT_COLORS.DEFAULT);
} else if (colourIsArrayAndMatches) {
colour = inputs.colours[index];
} else Iif (colorsAreArrays) {
colour = inputs.colours[0];
} else {
colour = inputs.colours;
}
const m = this.makeMesh({
...inputs,
colour,
drawTwoSided: inputs.drawTwoSided,
backFaceColour: inputs.backFaceColour,
backFaceOpacity: inputs.backFaceOpacity
}, meshToUpdate, r);
localOrigin.addChild(m);
});
this.context.scene.addChild(localOrigin);
inputs.jscadMesh = localOrigin;
return localOrigin;
} catch (error) {
console.error("Error drawing JSCAD solid or polygon meshes:", error);
throw new Error(`Failed to draw JSCAD meshes: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Draw multiple polylines with individual colors
* @param inputs - Polyline drawing inputs
* @returns Entity containing all polylines
*/
drawPolylinesWithColours(inputs: Inputs.Polyline.DrawPolylinesDto<pc.Entity> & { colorMapStrategy?: Inputs.Base.colorMapStrategyEnum, arrowSize?: number, arrowAngle?: number }): pc.Entity {
const strategy = inputs.colorMapStrategy || Inputs.Base.colorMapStrategyEnum.lastColorRemainder;
const processedPoints = this.processPolylinePoints(inputs.polylines as Inputs.Base.Polyline3[]);
// Determine if we should update existing mesh
const existingMesh = (inputs.updatable && inputs.polylinesMesh)
? inputs.polylinesMesh.children[0] as pc.Entity
: undefined;
// Draw the polylines with per-polyline colors
const polylineEntity = this.drawPolylines(
existingMesh,
processedPoints,
inputs.updatable,
inputs.size,
inputs.opacity,
inputs.colours,
strategy,
inputs.arrowSize,
inputs.arrowAngle
);
// Wrap in container group
return this.wrapPolylineInGroup(polylineEntity, inputs.polylinesMesh, inputs.updatable);
}
drawPoint(inputs: Inputs.Point.DrawPointDto<pc.Entity>): pc.Entity {
const vectorPoints = [inputs.point];
let colorsHex: string[] = [];
if (Array.isArray(inputs.colours)) {
colorsHex = inputs.colours;
} else {
colorsHex = [inputs.colours];
}
if (inputs.pointMesh && inputs.updatable) {
this.updatePointsInstances(inputs.pointMesh, vectorPoints);
} else {
inputs.pointMesh = this.createPointSpheresMesh(
this.generateEntityId("pointMesh"), vectorPoints, colorsHex, inputs.opacity, inputs.size
);
}
return inputs.pointMesh;
}
drawPolylineClose(inputs: Inputs.Polyline.DrawPolylineDto<pc.Entity> & { arrowSize?: number, arrowAngle?: number }): pc.Entity {
const points = inputs.polyline.points;
if (inputs.polyline.isClosed) {
points.push(points[0]);
}
return this.drawPolyline(
inputs.polylineMesh,
points,
inputs.updatable,
inputs.size,
inputs.opacity,
inputs.colours,
inputs.arrowSize,
inputs.arrowAngle
);
}
drawPolyline(mesh: pc.Entity,
pointsToDraw: Inputs.Base.Point3[],
updatable: boolean, size: number, opacity: number, colours: string | string[],
arrowSize = 0, arrowAngle = 30): pc.Entity {
const polylines = this.drawPolylines(mesh, [pointsToDraw], updatable, size, opacity, colours,
Inputs.Base.colorMapStrategyEnum.lastColorRemainder, arrowSize, arrowAngle);
if (!mesh) {
mesh = new pc.Entity(this.generateEntityId("polyline"));
mesh.addChild(polylines);
this.context.scene.addChild(mesh);
}
return mesh;
}
drawCurve(inputs: Inputs.Verb.DrawCurveDto<pc.Entity>): pc.Entity {
const points = inputs.curve.tessellate();
return this.drawPolyline(
inputs.curveMesh,
points,
inputs.updatable,
inputs.size,
inputs.opacity,
inputs.colours
);
}
drawPoints(inputs: Inputs.Point.DrawPointsDto<pc.Entity> & { colorMapStrategy?: Inputs.Base.colorMapStrategyEnum }): pc.Entity {
const vectorPoints = inputs.points;
const strategy = inputs.colorMapStrategy || Inputs.Base.colorMapStrategyEnum.lastColorRemainder;
// Resolve colors for all points using the color mapping strategy
const coloursHex = this.resolveAllColors(inputs.colours, vectorPoints.length, strategy);
if (inputs.pointsMesh && inputs.updatable) {
const children = inputs.pointsMesh.children;
Iif (children.length === vectorPoints.length) {
this.updatePointsInstances(inputs.pointsMesh, vectorPoints);
} else {
inputs.pointsMesh.destroy();
inputs.pointsMesh = this.createPointSpheresMesh(
this.generateEntityId("pointsMesh"), vectorPoints, coloursHex, inputs.opacity, inputs.size
);
}
} else {
inputs.pointsMesh = this.createPointSpheresMesh(
this.generateEntityId("pointsMesh"), vectorPoints, coloursHex, inputs.opacity, inputs.size
);
}
return inputs.pointsMesh;
}
updatePointsInstances(group: pc.Entity, positions: Inputs.Base.Point3[]): void {
// The group contains entities with instanced meshes - each entity has an instanceBuffer
const children = group.children;
// Build a map of original index to new position
const positionMap = new Map<number, Inputs.Base.Point3>();
positions.forEach((pos, index) => {
positionMap.set(index, pos);
});
children.forEach((child: pc.Entity) => {
// Handle GPU-instanced points
Iif (child.tags?.has("instancedPoints")) {
const extendedChild = child as pc.Entity & { instanceBuffer?: pc.VertexBuffer; pointIndices?: number[] };
const instanceBuffer = extendedChild.instanceBuffer;
const pointIndices = extendedChild.pointIndices;
if (instanceBuffer && pointIndices) {
const instanceData = instanceBuffer.lock();
if (instanceData) {
const floatView = new Float32Array(instanceData);
const tempMat = new pc.Mat4();
pointIndices.forEach((originalIndex, instanceIndex) => {
const newPos = positionMap.get(originalIndex);
if (newPos) {
tempMat.setTranslate(newPos[0], newPos[1], newPos[2]);
floatView.set(tempMat.data, instanceIndex * 16);
}
});
instanceBuffer.unlock();
}
}
}
// Handle fallback single-point entities
else Iif (child.tags?.has("singlePoint")) {
const idx = parseInt(child.name.split("-").pop() || "0");
if (positions[idx]) {
child.setLocalPosition(positions[idx][0], positions[idx][1], positions[idx][2]);
}
}
});
}
drawCurves(inputs: Inputs.Verb.DrawCurvesDto<pc.Entity>): pc.Entity {
const points = inputs.curves.map(s => ({ points: s.tessellate() }));
return this.drawPolylinesWithColours({ polylines: points, polylinesMesh: inputs.curvesMesh, ...inputs });
}
drawSurfacesMultiColour(inputs: Inputs.Verb.DrawSurfacesColoursDto<pc.Entity> & { colorMapStrategy?: Inputs.Base.colorMapStrategyEnum }): pc.Entity {
if (inputs.surfacesMesh && inputs.updatable) {
this.clearEntity(inputs.surfacesMesh);
} else {
inputs.surfacesMesh = new pc.Entity(this.generateEntityId("colouredSurfaces"));
this.context.scene.addChild(inputs.surfacesMesh);
}
const strategy = inputs.colorMapStrategy || Inputs.Base.colorMapStrategyEnum.lastColorRemainder;
const resolvedColours = this.resolveAllColors(inputs.colours, inputs.surfaces.length, strategy);
inputs.surfaces.forEach((surface, index) => {
const srf = this.drawSurface({
surface,
colours: resolvedColours[index],
updatable: inputs.updatable,
opacity: inputs.opacity,
hidden: inputs.hidden,
drawTwoSided: inputs.drawTwoSided,
backFaceColour: inputs.backFaceColour,
backFaceOpacity: inputs.backFaceOpacity,
});
inputs.surfacesMesh.addChild(srf);
});
return inputs.surfacesMesh;
}
createOrUpdateSurfacesMesh(
meshDataConverted: { positions: number[]; indices: number[]; normals: number[]; uvs?: number[] }[],
group: pc.Entity, updatable: boolean, material: pc.StandardMaterial, addToScene: boolean, hidden: boolean
): pc.Entity {
const createMesh = () => {
// Merge all geometries into one
const totalPositions: number[] = [];
let totalNormals: number[] = [];
const totalIndices: number[] = [];
const totalUvs: number[] = [];
let indexOffset = 0;
meshDataConverted.forEach(meshItem => {
totalPositions.push(...meshItem.positions);
Eif (meshItem.normals && meshItem.normals.length > 0) {
totalNormals.push(...meshItem.normals);
}
if (meshItem.uvs) {
totalUvs.push(...meshItem.uvs);
}
// Offset indices
const offsetIndices = meshItem.indices.map(i => i + indexOffset);
totalIndices.push(...offsetIndices);
indexOffset += meshItem.positions.length / 3;
});
// Compute normals if they're missing
Iif (totalNormals.length === 0 && totalPositions.length > 0) {
totalNormals = this.computeNormals(totalPositions, totalIndices);
}
const mesh = new pc.Mesh(this.context.app.graphicsDevice);
mesh.setPositions(totalPositions);
mesh.setNormals(totalNormals);
mesh.setIndices(totalIndices);
if (totalUvs.length > 0) {
mesh.setUvs(0, totalUvs);
}
mesh.update(pc.PRIMITIVE_TRIANGLES);
return mesh;
};
if (group && updatable) {
this.clearEntity(group);
const mesh = createMesh();
const meshInstance = new pc.MeshInstance(mesh, material);
const entity = new pc.Entity(this.generateEntityId("surfaceChild"));
entity.addComponent("render", {
meshInstances: [meshInstance]
});
group.addChild(entity);
} else {
group = new pc.Entity(this.generateEntityId("surface"));
if (addToScene) {
this.context.scene.addChild(group);
}
const mesh = createMesh();
const meshInstance = new pc.MeshInstance(mesh, material);
const entity = new pc.Entity(this.generateEntityId("surfaceChild"));
entity.addComponent("render", {
meshInstances: [meshInstance]
});
group.addChild(entity);
}
if (hidden) {
group.enabled = false;
}
return group;
}
/**
* Create a back face mesh with flipped normals and reversed winding order
* This is used for two-sided rendering where back faces have a different color
* @param meshDataConverted - Original mesh data
* @param backFaceColour - Color for the back face
* @param backFaceOpacity - Opacity for the back face
* @param zOffset - Depth bias to prevent z-fighting
* @returns Entity containing the back face mesh
*/
private createBackFaceMesh(
meshDataConverted: { positions: number[]; indices: number[]; normals: number[]; uvs?: number[] }[],
backFaceColour: string,
backFaceOpacity: number,
zOffset: number
): pc.Entity {
// Create material for back face
const backMaterial = this.getOrCreateMaterial(backFaceColour + "-back", backFaceOpacity, zOffset + 0.1, () => {
const mat = new pc.StandardMaterial();
mat.name = this.generateEntityId("backFaceMaterial");
mat.diffuse = this.hexToColor(backFaceColour);
mat.metalness = 0.4;
mat.gloss = 0.2;
mat.opacity = backFaceOpacity;
// Enable alpha blending for transparency when opacity < 1
if (backFaceOpacity < 1) {
mat.blendType = pc.BLEND_NORMAL;
}
mat.depthBias = zOffset + 0.1;
mat.slopeDepthBias = zOffset + 0.1;
mat.update();
return mat;
});
// Use inherited method to prepare back face mesh data
const backFaceData = this.prepareBackFaceMeshData(meshDataConverted);
const mesh = new pc.Mesh(this.context.app.graphicsDevice);
mesh.setPositions(backFaceData.positions);
mesh.setNormals(backFaceData.normals);
mesh.setIndices(backFaceData.indices);
if (backFaceData.uvs && backFaceData.uvs.length > 0) {
mesh.setUvs(0, backFaceData.uvs);
}
mesh.update(pc.PRIMITIVE_TRIANGLES);
const group = new pc.Entity(this.generateEntityId("backFaceSurface"));
const meshInstance = new pc.MeshInstance(mesh, backMaterial);
const entity = new pc.Entity(this.generateEntityId("backFaceSurfaceChild"));
entity.addComponent("render", {
meshInstances: [meshInstance]
});
group.addChild(entity);
return group;
}
drawSurface(inputs: Inputs.Verb.DrawSurfaceDto<pc.Entity>): pc.Entity {
const meshData = inputs.surface.tessellate();
const meshDataConverted = {
positions: [],
indices: [],
normals: [],
};
let countIndices = 0;
meshData.faces.forEach((faceIndices) => {
countIndices = this.parseFaces(faceIndices, meshData, meshDataConverted, countIndices);
});
const color = Array.isArray(inputs.colours) ? inputs.colours[0] : inputs.colours;
const pbr = this.getOrCreateMaterial(color, inputs.opacity, 0, () => {
const material = new pc.StandardMaterial();
material.name = this.generateEntityId("pbrSurface");
material.diffuse = this.hexToColor(color);
material.metalness = 0.5;
material.gloss = 0.3;
material.opacity = inputs.opacity;
// Enable alpha blending for transparency when opacity < 1
if (inputs.opacity < 1) {
material.blendType = pc.BLEND_NORMAL;
}
material.update();
return material;
});
const surfaceEntity = this.createOrUpdateSurfacesMesh(
[meshDataConverted],
inputs.surfaceMesh,
inputs.updatable,
pbr,
true,
inputs.hidden,
);
// Draw back faces with different color when two-sided rendering is enabled
if (inputs.drawTwoSided !== false) {
const backFaceMesh = this.createBackFaceMesh(
[meshDataConverted],
inputs.backFaceColour || DEFAULT_COLORS.BACK_FACE,
inputs.backFaceOpacity ?? inputs.opacity,
0
);
surfaceEntity.addChild(backFaceMesh);
}
return surfaceEntity;
}
private parseFaces(
faceIndices: number[],
meshData: { points: number[][]; normals: number[][]; },
meshDataConverted: { positions: number[]; indices: number[]; normals: number[]; },
countIndices: number): number {
faceIndices.forEach((x) => {
const vn = meshData.normals[x];
meshDataConverted.normals.push(vn[0], vn[1], vn[2]);
const pt = meshData.points[x];
meshDataConverted.positions.push(pt[0], pt[1], pt[2]);
meshDataConverted.indices.push(countIndices);
countIndices++;
});
return countIndices;
}
private makeMesh(inputs: { updatable: boolean, opacity: number, colour: string, hidden: boolean, drawTwoSided?: boolean, backFaceColour?: string, backFaceOpacity?: number }, meshToUpdate: pc.Entity, res: { positions: number[]; normals: number[]; indices: number[]; transforms: []; }): pc.Entity {
const pbr = this.getOrCreateMaterial(inputs.colour, inputs.opacity, 0, () => {
const material = new pc.StandardMaterial();
material.name = this.generateEntityId("jscadMaterial");
material.diffuse = this.hexToColor(inputs.colour);
material.metalness = 0.4;
material.gloss = 0.4;
material.opacity = inputs.opacity;
// Enable alpha blending for transparency when opacity < 1
if (inputs.opacity < 1) {
material.blendType = pc.BLEND_NORMAL;
}
material.update();
return material;
});
this.createMesh(res.positions, res.indices, res.normals, meshToUpdate, res.transforms, inputs.updatable, pbr);
// Draw back faces with different color when two-sided rendering is enabled
if (inputs.drawTwoSided !== false) {
const backFaceMesh = this.createBackFaceMesh(
[{ positions: res.positions, indices: res.indices, normals: res.normals }],
inputs.backFaceColour || DEFAULT_COLORS.BACK_FACE,
inputs.backFaceOpacity ?? inputs.opacity,
0
);
meshToUpdate.addChild(backFaceMesh);
}
if (inputs.hidden) {
meshToUpdate.enabled = false;
}
return meshToUpdate;
}
private createMesh(
positions: number[], indices: number[], normals: number[], jscadMesh: pc.Entity, transforms: number[], updatable: boolean, material: pc.StandardMaterial
): void {
const mesh = new pc.Mesh(this.context.app.graphicsDevice);
mesh.setPositions(positions);
mesh.setIndices(indices);
// Compute normals if they're missing or empty
if (!normals || normals.length === 0) {
const computedNormals = this.computeNormals(positions, indices);
mesh.setNormals(computedNormals);
} else {
mesh.setNormals(normals);
}
mesh.update(pc.PRIMITIVE_TRIANGLES);
const meshInstance = new pc.MeshInstance(mesh, material);
this.clearEntity(jscadMesh);
const entity = new pc.Entity(this.generateEntityId("jscadMeshChild"));
entity.addComponent("render", {
meshInstances: [meshInstance]
});
jscadMesh.addChild(entity);
// Apply transforms
Eif (transforms && transforms.length === 16) {
const mat4 = new pc.Mat4();
mat4.data = new Float32Array(transforms);
const pos = new pc.Vec3();
const rot = new pc.Quat();
const scale = new pc.Vec3();
mat4.getTranslation(pos);
mat4.getScale(scale);
rot.setFromMat4(mat4);
jscadMesh.setLocalPosition(pos);
jscadMesh.setLocalRotation(rot);
jscadMesh.setLocalScale(scale);
}
}
private async handleDecomposedMesh(inputs: Inputs.OCCT.DrawShapeDto<Inputs.OCCT.TopoDSShapePointer>, decomposedMesh: Inputs.OCCT.DecomposedMeshDto, options: Partial<Inputs.Draw.DrawOcctShapeOptions>): Promise<pc.Entity> {
const shapeGroup = new pc.Entity(this.generateEntityId("brepMesh"));
this.context.scene.addChild(shapeGroup);
if (inputs.drawFaces && decomposedMesh && decomposedMesh.faceList && decomposedMesh.faceList.length) {
let pbr: pc.StandardMaterial;
if (options.faceMaterial) {
pbr = options.faceMaterial;
} else {
const hex = Array.isArray(inputs.faceColour) ? inputs.faceColour[0] : inputs.faceColour;
const alpha = inputs.faceOpacity;
const zOffset = inputs.drawEdges ? 2 : 0;
const slopeOffset = inputs.drawEdges ? 2 : 0;
pbr = this.getOrCreateMaterial(hex, alpha, zOffset, () => {
const pbmat = new pc.StandardMaterial();
pbmat.diffuse = this.hexToColor(hex);
pbmat.metalness = 0.4;
pbmat.gloss = 0.2;
pbmat.opacity = alpha;
// Use both depthBias and slopeDepthBias to push faces behind edges
pbmat.depthBias = zOffset;
pbmat.slopeDepthBias = slopeOffset;
pbmat.update();
return pbmat;
});
}
const meshData = decomposedMesh.faceList.map(face => {
return {
positions: face.vertex_coord,
normals: face.normal_coord,
indices: face.tri_indexes,
uvs: face.uvs,
};
});
const mesh = this.createOrUpdateSurfacesMesh(meshData, undefined, false, pbr, false, false);
shapeGroup.addChild(mesh);
// Draw back faces with different color when two-sided rendering is enabled
if (options.drawTwoSided !== false) {
const backFaceMesh = this.createBackFaceMesh(
meshData,
options.backFaceColour || DEFAULT_COLORS.BACK_FACE,
options.backFaceOpacity ?? options.faceOpacity,
inputs.drawEdges ? 2 : 0
);
shapeGroup.addChild(backFaceMesh);
}
}
if (inputs.drawEdges && decomposedMesh && decomposedMesh.edgeList && decomposedMesh.edgeList.length) {
const polylineEdgePoints = [];
decomposedMesh.edgeList.forEach(edge => {
const ev = edge.vertex_coord.filter(s => s !== undefined);
polylineEdgePoints.push(ev);
});
const line = this.drawPolylines(
undefined,
polylineEdgePoints,
false,
inputs.edgeWidth,
inputs.edgeOpacity,
inputs.edgeColour,
Inputs.Base.colorMapStrategyEnum.lastColorRemainder,
options.edgeArrowSize,
options.edgeArrowAngle
);
shapeGroup.addChild(line);
}
if (inputs.drawVertices && decomposedMesh && decomposedMesh.pointsList && decomposedMesh.pointsList.length) {
const mesh = this.drawPoints({
points: decomposedMesh.pointsList,
opacity: 1,
size: inputs.vertexSize,
colours: inputs.vertexColour,
updatable: false,
});
shapeGroup.addChild(mesh);
}
if (inputs.drawEdgeIndexes) {
const promises = decomposedMesh.edgeList.map(async (edge) => {
let edgeMiddle = edge.middle_point;
Iif (edgeMiddle === undefined) {
edgeMiddle = this.computeEdgeMiddlePos(edge);
}
const tdto = new Inputs.JSCAD.TextDto();
tdto.text = `${edge.edge_index + 1}`;
tdto.height = inputs.edgeIndexHeight;
tdto.lineSpacing = 1.5;
const t = await this.solidText.createVectorText(tdto);
const texts = t.map(s => {
const res = s.map(c => {
return [
c[0],
c[1] + 0.05,
0
] as Inputs.Base.Vector3;
});
const movedOnPosition = res.map(r => this.vector.add({ first: r, second: edgeMiddle }));
return movedOnPosition as Inputs.Base.Vector3[];
});
return texts;
});
const textPolylines = await Promise.all(promises);
const edgeMesh = this.drawPolylines(undefined, textPolylines.flat(), false, 0.2, 1, inputs.edgeIndexColour);
shapeGroup.addChild(edgeMesh);
}
if (inputs.drawFaceIndexes) {
const promises = decomposedMesh.faceList.map(async (face) => {
let faceMiddle = face.center_point;
Iif (faceMiddle === undefined) {
faceMiddle = this.computeFaceMiddlePos(face.vertex_coord_vec) as Inputs.Base.Point3;
}
const tdto = new Inputs.JSCAD.TextDto();
tdto.text = `${face.face_index}`;
tdto.height = inputs.faceIndexHeight;
tdto.lineSpacing = 1.5;
const t = await this.solidText.createVectorText(tdto);
const texts = t.map(s => {
const res = s.map(c => {
return [
c[0],
c[1] + 0.05,
0
] as Inputs.Base.Point3;
});
const movedOnPosition = res.map(r => this.vector.add({ first: r, second: faceMiddle }));
return movedOnPosition as Inputs.Base.Point3[];
});
return texts;
});
const textPolylines = await Promise.all(promises);
const faceMesh = this.drawPolylines(undefined, textPolylines.flat(), false, 0.2, 1, inputs.faceIndexColour);
shapeGroup.addChild(faceMesh);
}
return shapeGroup;
}
/**
* Check if a polyline entity can be updated with new point data
* @param entity - Entity to check
* @param polylinePoints - New polyline points
* @param updatable - Whether updates are allowed
* @returns True if entity can be updated
*/
private canUpdatePolylineEntity(
entity: pc.Entity | undefined,
polylinePoints: Inputs.Base.Vector3[][],
updatable: boolean
): entity is PolylineEntity {
if (!entity || !updatable) {
return false;
}
const polylineEntity = entity as PolylineEntity;
const newSignature = super.computePolylineSignature(polylinePoints);
const oldSignature = polylineEntity.bitbybitMeta?.linesForRenderLengths;
return oldSignature === newSignature;
}
/**
* Update an existing polyline entity with new position data
* @param entity - Entity to update
* @param linePositions - New line positions
* @returns True if update succeeded, false otherwise
*/
private updatePolylineEntityPositions(
entity: pc.Entity,
linePositions: number[]
): boolean {
const renderComponent = entity.render;
Iif (!renderComponent?.meshInstances?.[0]?.mesh) {
console.warn("Cannot update polyline: missing render component or mesh");
return false;
}
try {
const mesh = renderComponent.meshInstances[0].mesh;
mesh.setPositions(linePositions);
mesh.update(pc.PRIMITIVE_LINES);
return true;
} catch (error) {
console.error("Error updating polyline positions:", error);
return false;
}
}
/**
* Compute line positions array from polyline points
* @param polylinesPoints - Array of polylines
* @returns Object containing flat array of line positions and segment counts per polyline
*/
private computeLinePositionsWithSegmentCounts(polylinesPoints: Inputs.Base.Vector3[][]): {
positions: number[];
segmentCounts: number[];
} {
const linePositions: number[] = [];
const segmentCounts: number[] = [];
for (const points of polylinesPoints) {
let segmentCount = 0;
for (let i = 0; i < points.length - 1; i++) {
const current = points[i];
const next = points[i + 1];
linePositions.push(current[0], current[1], current[2]);
linePositions.push(next[0], next[1], next[2]);
segmentCount++;
}
segmentCounts.push(segmentCount);
}
return { positions: linePositions, segmentCounts };
}
/**
* Compute per-vertex colors for polylines based on color mapping strategy
* @param colours - Single color or array of colors
* @param segmentCounts - Number of line segments per polyline
* @param colorMapStrategy - Strategy for mapping colors to polylines
* @returns Flat array of RGBA values (0-255) for each vertex
*/
private computePolylineColors(
colours: string | string[],
segmentCounts: number[],
colorMapStrategy: Inputs.Base.colorMapStrategyEnum
): number[] {
const lineColors: number[] = [];
const totalPolylines = segmentCounts.length;
segmentCounts.forEach((segmentCount, polylineIndex) => {
// Get the color for this polyline using the strategy
const colorHex = this.resolveColorForEntity(colours, polylineIndex, totalPolylines, colorMapStrategy);
const color = this.hexToColor(colorHex);
// Each segment has 2 vertices, apply the same color to both (RGBA as 0-255)
for (let i = 0; i < segmentCount * 2; i++) {
lineColors.push(
Math.round(color.r * 255),
Math.round(color.g * 255),
Math.round(color.b * 255),
255 // Full opacity
);
}
});
return lineColors;
}
/**
* Create a new polyline entity with metadata
* @param linePositions - Line positions array
* @param colours - Colors for the lines
* @param size - Line width
* @param polylinePoints - Original polyline points for signature
* @param segmentCounts - Number of segments per polyline
* @param colorMapStrategy - Strategy for mapping colors to polylines
* @returns New polyline entity with metadata
*/
private createPolylineEntityWithMetadata(
linePositions: number[],
colours: string | string[],
size: number,
polylinePoints: Inputs.Base.Vector3[][],
segmentCounts: number[] = [],
colorMapStrategy: Inputs.Base.colorMapStrategyEnum = Inputs.Base.colorMapStrategyEnum.lastColorRemainder
): PolylineEntity {
const entity = this.createLineEntity(linePositions, colours, segmentCounts, colorMapStrategy) as PolylineEntity;
entity.bitbybitMeta = {
linesForRenderLengths: this.computePolylineSignature(polylinePoints)
};
return entity;
}
/**
* Create a new polyline entity with explicit colors (for arrow support)
* @param linePositions - Line positions array
* @param size - Line width
* @param polylinePoints - Original polyline points for signature
* @param segmentCounts - Number of segments per polyline/arrow
* @param explicitColors - Explicit color for each polyline/arrow segment
* @returns New polyline entity with metadata
*/
private createPolylineEntityWithExplicitColors(
linePositions: number[],
size: number,
polylinePoints: Inputs.Base.Vector3[][],
segmentCounts: number[],
explicitColors: string[]
): PolylineEntity {
const entity = this.createLineEntityWithExplicitColors(linePositions, segmentCounts, explicitColors) as PolylineEntity;
entity.bitbybitMeta = {
linesForRenderLengths: this.computePolylineSignature(polylinePoints)
};
return entity;
}
/**
* Compute per-vertex colors using explicit color array
* @param segmentCounts - Number of line segments per polyline/arrow
* @param explicitColors - Explicit color for each polyline/arrow
* @returns Flat array of RGBA values (0-255) for each vertex
*/
private computePolylineColorsWithExplicit(
segmentCounts: number[],
explicitColors: string[]
): number[] {
const lineColors: number[] = [];
segmentCounts.forEach((segmentCount, index) => {
const colorHex = explicitColors[index] || explicitColors[0] || "#ff0000";
const color = this.hexToColor(colorHex);
// Each segment has 2 vertices, apply the same color to both (RGBA as 0-255)
for (let i = 0; i < segmentCount * 2; i++) {
lineColors.push(
Math.round(color.r * 255),
Math.round(color.g * 255),
Math.round(color.b * 255),
255 // Full opacity
);
}
});
return lineColors;
}
/**
* Create line entity with explicit colors
* @param linePositions - Line positions array
* @param segmentCounts - Number of segments per polyline
* @param explicitColors - Explicit color for each segment group
* @returns Entity containing lines
*/
private createLineEntityWithExplicitColors(
linePositions: number[],
segmentCounts: number[],
explicitColors: string[]
): pc.Entity {
const mesh = new pc.Mesh(this.context.app.graphicsDevice);
mesh.setPositions(linePositions);
const vertexColors = this.computePolylineColorsWithExplicit(segmentCounts, explicitColors);
mesh.setColors32(vertexColors);
mesh.update(pc.PRIMITIVE_LINES);
// Create material that uses vertex colors
const mat = new pc.StandardMaterial();
mat.diffuseVertexColor = true;
mat.emissiveVertexColor = true;
mat.diffuse = new pc.Color(1, 1, 1);
mat.emissive = new pc.Color(1, 1, 1);
mat.useLighting = false;
mat.update();
const meshInstance = new pc.MeshInstance(mesh, mat);
const lineEntity = new pc.Entity(this.generateEntityId("lines"));
lineEntity.addComponent("render", {
meshInstances: [meshInstance]
});
return lineEntity;
}
/**
* Draw multiple polylines using PlayCanvas line primitives
* @param existingEntity - Optional existing entity to update
* @param polylinesPoints - Array of polylines
* @param updatable - Whether to attempt updates
* @param size - Line width
* @param opacity - Line opacity
* @param colours - Line colors
* @param colorMapStrategy - Strategy for mapping colors to polylines
* @returns Entity containing rendered polylines, or undefined
*/
private drawPolylines(
existingEntity: pc.Entity | undefined,
polylinesPoints: Inputs.Base.Vector3[][],
updatable: boolean,
size: number,
opacity: number,
colours: string | string[],
colorMapStrategy: Inputs.Base.colorMapStrategyEnum = Inputs.Base.colorMapStrategyEnum.lastColorRemainder,
arrowSize = 0,
arrowAngle = 30
): pc.Entity {
// Validate input
Iif (!polylinesPoints || polylinesPoints.length === 0) {
return undefined;
}
// Compute arrow lines if arrowSize > 0
const arrowLinePoints: Inputs.Base.Vector3[][] = [];
const arrowLineColors: string[] = [];
if (arrowSize > 0) {
polylinesPoints.forEach((pts, polylineIndex) => {
if (pts.length >= 2) {
const arrowLines = this.computeArrowHeadLines(pts as Inputs.Base.Point3[], arrowSize, arrowAngle);
const polylineColor = this.resolveColorForEntity(colours, polylineIndex, polylinesPoints.length, colorMapStrategy);
arrowLines.forEach(arrowLine => {
arrowLinePoints.push(arrowLine);
arrowLineColors.push(polylineColor);
});
}
});
}
// Combine original polylines with arrow lines
const allPolylinePoints = [...polylinesPoints, ...arrowLinePoints];
const { positions: linePositions, segmentCounts } = this.computeLinePositionsWithSegmentCounts(allPolylinePoints);
// Compute explicit colors for all polylines + arrows
const resolvedPolylineColors = this.resolveAllColors(colours, polylinesPoints.length, colorMapStrategy);
const allExplicitColors = [...resolvedPolylineColors, ...arrowLineColors];
// Try to update existing entity
if (this.canUpdatePolylineEntity(existingEntity, polylinesPoints, updatable)) {
Eif (this.updatePolylineEntityPositions(existingEntity, linePositions)) {
return existingEntity;
}
// Update failed, fall through to create new
console.warn("Polyline update failed, creating new entity");
}
// Create new entity with per-polyline colors (including arrows)
return this.createPolylineEntityWithExplicitColors(
linePositions,
size,
allPolylinePoints,
segmentCounts,
allExplicitColors
);
}
private createLineEntity(
linePositions: number[],
colours: string | string[],
segmentCounts: number[] = [],
colorMapStrategy: Inputs.Base.colorMapStrategyEnum = Inputs.Base.colorMapStrategyEnum.lastColorRemainder
): pc.Entity {
const mesh = new pc.Mesh(this.context.app.graphicsDevice);
mesh.setPositions(linePositions);
// Use vertex colors if we have segment counts (multiple polylines)
const useVertexColors = segmentCounts.length > 0;
if (useVertexColors) {
const vertexColors = this.computePolylineColors(colours, segmentCounts, colorMapStrategy);
mesh.setColors32(vertexColors);
}
mesh.update(pc.PRIMITIVE_LINES);
// Create material that uses vertex colors
const mat = new pc.StandardMaterial();
if (useVertexColors) {
// Enable vertex colors in the material
mat.diffuseVertexColor = true;
mat.emissiveVertexColor = true;
mat.diffuse = new pc.Color(1, 1, 1); // White base, vertex colors will tint
mat.emissive = new pc.Color(1, 1, 1); // White base for emissive
} else {
// Single color mode
const color = Array.isArray(colours) ? this.hexToColor(colours[0]) : this.hexToColor(colours);
mat.emissive = color;
mat.diffuse = color;
}
mat.useLighting = false;
mat.update();
const meshInstance = new pc.MeshInstance(mesh, mat);
const lineEntity = new pc.Entity(this.generateEntityId("lines"));
lineEntity.addComponent("render", {
meshInstances: [meshInstance]
});
return lineEntity;
}
private handleDecomposedManifold(
decomposedManifold: Inputs.Manifold.DecomposedManifoldMeshDto | Inputs.Base.Vector2[][],
options: Inputs.Draw.DrawManifoldOrCrossSectionOptions): pc.Entity {
if ((decomposedManifold as Inputs.Manifold.DecomposedManifoldMeshDto).vertProperties) {
const decomposedMesh = decomposedManifold as Inputs.Manifold.DecomposedManifoldMeshDto;
if (decomposedMesh.triVerts.length !== 0) {
const numProp = decomposedMesh.numProp || 3;
const vertProperties = decomposedMesh.vertProperties;
const triVerts = decomposedMesh.triVerts;
// Extract indexed positions based on numProp stride
let indexedPositions: number[];
if (numProp === 3) {
indexedPositions = Array.from(vertProperties);
} else E{
const numVerts = vertProperties.length / numProp;
indexedPositions = [];
for (let i = 0; i < numVerts; i++) {
const baseIdx = i * numProp;
indexedPositions.push(vertProperties[baseIdx], vertProperties[baseIdx + 1], vertProperties[baseIdx + 2]);
}
}
// Unindex the mesh for flat shading - each triangle gets unique vertices
const positions: number[] = [];
const normals: number[] = [];
const indices: number[] = [];
for (let i = 0; i < triVerts.length; i += 3) {
const i0 = triVerts[i];
const i1 = triVerts[i + 1];
const i2 = triVerts[i + 2];
// Get vertex positions
const v0x = indexedPositions[i0 * 3];
const v0y = indexedPositions[i0 * 3 + 1];
const v0z = indexedPositions[i0 * 3 + 2];
const v1x = indexedPositions[i1 * 3];
const v1y = indexedPositions[i1 * 3 + 1];
const v1z = indexedPositions[i1 * 3 + 2];
const v2x = indexedPositions[i2 * 3];
const v2y = indexedPositions[i2 * 3 + 1];
const v2z = indexedPositions[i2 * 3 + 2];
// Compute face normal
const e1x = v1x - v0x;
const e1y = v1y - v0y;
const e1z = v1z - v0z;
const e2x = v2x - v0x;
const e2y = v2y - v0y;
const e2z = v2z - v0z;
let nx = e1y * e2z - e1z * e2y;
let ny = e1z * e2x - e1x * e2z;
let nz = e1x * e2y - e1y * e2x;
// Normalize
const len = Math.sqrt(nx * nx + ny * ny + nz * nz);
Eif (len > 0) {
nx /= len;
ny /= len;
nz /= len;
}
// Add 3 unique vertices for this triangle
const baseIndex = positions.length / 3;
positions.push(v0x, v0y, v0z);
positions.push(v1x, v1y, v1z);
positions.push(v2x, v2y, v2z);
// Same normal for all 3 vertices (flat shading)
normals.push(nx, ny, nz);
normals.push(nx, ny, nz);
normals.push(nx, ny, nz);
indices.push(baseIndex, baseIndex + 1, baseIndex + 2);
}
const mesh = new pc.Mesh(this.context.app.graphicsDevice);
mesh.setPositions(positions);
mesh.setIndices(indices);
mesh.setNormals(normals);
mesh.update(pc.PRIMITIVE_TRIANGLES);
const group = new pc.Entity(this.generateEntityId("manifoldMesh"));
let material: pc.StandardMaterial;
if (options.faceMaterial === undefined) {
material = this.getOrCreateMaterial(options.faceColour, options.faceOpacity, 0, () => {
const mat = new pc.StandardMaterial();
mat.name = this.generateEntityId("pbrManifold");
mat.diffuse = this.hexToColor(options.faceColour);
mat.metalness = 0.5;
mat.gloss = 0.3;
mat.opacity = options.faceOpacity;
mat.update();
return mat;
});
} else E{
material = options.faceMaterial;
}
const meshInstance = new pc.MeshInstance(mesh, material);
const childEntity = new pc.Entity(this.generateEntityId("manifoldMeshChild"));
childEntity.addComponent("render", {
meshInstances: [meshInstance]
});
group.addChild(childEntity);
// Draw back faces with different color when two-sided rendering is enabled
if (options.drawTwoSided !== false) {
const backFaceMesh = this.createBackFaceMesh(
[{ positions, indices, normals }],
options.backFaceColour || DEFAULT_COLORS.BACK_FACE,
options.backFaceOpacity ?? options.faceOpacity,
0
);
group.addChild(backFaceMesh);
}
this.context.scene.addChild(group);
return group;
} else {
return undefined;
}
} else {
const decompsoedPolygons = decomposedManifold as Inputs.Base.Vector2[][];
if (decompsoedPolygons.length > 0) {
const group = new pc.Entity(this.generateEntityId("manifoldCrossSection"));
const polylines = decompsoedPolygons.map(polygon => ({
points: polygon.map(p => [p[0], p[1], 0] as Inputs.Base.Point3),
isClosed: true
}));
const polylineMesh = this.drawPolylinesWithColours({
polylinesMesh: undefined,
polylines,
updatable: false,
size: options.crossSectionWidth,
opacity: options.crossSectionOpacity,
colours: options.crossSectionColour
});
group.addChild(polylineMesh);
this.context.scene.addChild(group);
return group;
}
else E{
return undefined;
}
}
}
private clearEntity(entity: pc.Entity): void {
while (entity.children.length > 0) {
const child = entity.children[0];
child.destroy();
}
}
private hexToColor(hex: string): pc.Color {
const rgb = this.hexToRgb(hex);
if (rgb) {
return new pc.Color(rgb.r, rgb.g, rgb.b);
}
return new pc.Color(1, 0, 0);
}
// Creates a shallow copy of inputs without the faceMaterial property for safe worker communication
// Workers cannot handle complex circular objects like PlayCanvas materials
private getSafeWorkerOptions<T extends { faceMaterial?: pc.StandardMaterial }>(inputs: T): Omit<T, "faceMaterial"> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { faceMaterial, ...safeOptions } = inputs;
return safeOptions as Omit<T, "faceMaterial">;
}
/**
* Generate a unique entity ID with semantic naming
* @param type - The type of entity (e.g., 'manifoldMeshContainer', 'jscadMesh')
* @param parentId - Optional parent ID for hierarchical naming
* @returns Unique entity ID string
*/
private generateEntityId(type: string, parentId?: string): string {
const id = `${this.instanceId}-${type}-${++this.entityIdCounter}`;
return parentId ? `${parentId}/${id}` : id;
}
/**
* Get or create a cached material with the specified properties
* Implements LRU-like eviction when cache is full
* @param hex - Hex color string
* @param alpha - Alpha value (0-1)
* @param zOffset - Z-offset value
* @param createFn - Function to create new material if not cached
* @param unlit - Whether the material is unlit (no lighting, for points/lines)
* @returns Cached or newly created material
*/
private getOrCreateMaterial(
hex: string,
alpha: number,
zOffset: number,
createFn: () => pc.StandardMaterial,
unlit = false
): pc.StandardMaterial {
const key = super.getMaterialKey(hex, alpha, zOffset, unlit);
// Check cache first
const cached = this.materialCache.get(key);
if (cached) {
return cached;
}
// Evict oldest if at capacity (simple FIFO)
Iif (this.materialCache.size >= CACHE_CONFIG.MAX_MATERIALS) {
const firstKey = this.materialCache.keys().next().value;
const material = this.materialCache.get(firstKey);
if (material && material.destroy) {
material.destroy();
}
this.materialCache.delete(firstKey);
console.warn(`Material cache full, evicted: ${firstKey}`);
}
// Create new material
const material = createFn();
this.materialCache.set(key, material);
return material;
}
/**
* Cleanup method to dispose of cached materials and prevent memory leaks
* Should be called when the DrawHelper instance is no longer needed
*/
public dispose(): void {
// Dispose cached materials
this.materialCache.forEach((material, key) => {
try {
Eif (material.destroy) {
material.destroy();
}
} catch (error) {
console.warn(`Error disposing material ${key}:`, error);
}
});
this.materialCache.clear();
// Reset counters
this.entityIdCounter = 0;
console.log("DrawHelper disposed successfully");
}
/**
* Normalize polyline colors from multiple sources into a consistent array
* @param polylines - Array of polylines with potential embedded colors
* @param inputColors - Input colors (single or array)
* @param colorMapStrategy - Strategy for mapping colors to polylines when there are more polylines than colors
* @returns Array of normalized hex color strings
*/
private normalizePolylineColors(
polylines: Inputs.Polyline.PolylinePropertiesDto[],
inputColors: string | string[],
colorMapStrategy: Inputs.Base.colorMapStrategyEnum = Inputs.Base.colorMapStrategyEnum.lastColorRemainder
): string[] {
const defaultColor = Array.isArray(inputColors) ? inputColors[0] : inputColors;
return polylines.map((polyline, index) => {
// Priority 1: Polyline-specific color
if (polyline.color) {
const color = typeof polyline.color === "string" ? polyline.color : polyline.color.join(",");
return super.normalizeColor(color, defaultColor);
}
// Priority 2: Use color map strategy to resolve color
return this.resolveColorForEntity(inputColors, index, polylines.length, colorMapStrategy);
});
}
/**
* Wrap a polyline entity in a group container
* @param polylineEntity - The polyline entity to wrap
* @param existingGroup - Optional existing group for updates
* @param updatable - Whether this is an update operation
* @returns Group entity containing the polyline
*/
private wrapPolylineInGroup(
polylineEntity: pc.Entity,
existingGroup?: pc.Entity,
updatable?: boolean
): pc.Entity {
// If updating and names match, return existing group
if (existingGroup && updatable && existingGroup.children[0]?.name === polylineEntity.name) {
return existingGroup;
}
// Create new group
const groupId = this.generateEntityId("polylinesGroup");
const group = new pc.Entity(groupId);
group.addChild(polylineEntity);
this.context.scene.addChild(group);
return group;
}
private createPointSpheresMesh(
meshName: string, positions: Inputs.Base.Point3[], colors: string[], opacity: number, size: number): pc.Entity {
const positionsModel = positions.map((pos, index) => {
return {
position: pos,
color: colors[index],
index
};
});
const colorSet = Array.from(new Set(colors));
const materialSet = colorSet.map((colour) => {
// Use unlit=true to distinguish from lit materials used for surfaces
const mat = this.getOrCreateMaterial(colour, opacity, 0, () => {
const material = new pc.StandardMaterial();
material.name = this.generateEntityId("mat");
material.opacity = opacity;
if (opacity < 1) {
material.blendType = pc.BLEND_NORMAL;
}
material.emissive = this.hexToColor(colour);
material.diffuse = this.hexToColor(colour);
material.useLighting = false;
material.update();
return material;
}, true); // unlit = true
const positionsFiltered = positionsModel.filter(s => s.color === colour);
return { hex: colour, material: mat, positions: positionsFiltered };
});
const pointsGroup = new pc.Entity(meshName);
this.context.scene.addChild(pointsGroup);
// Create merged geometry for each unique color - one draw call per color
materialSet.forEach(ms => {
const pointCount = ms.positions.length;
if (pointCount === 0) return;
// Use fewer segments for large point counts
const segments = pointCount > 1000 ? 4 : 8;
// Create instanced mesh for all points of this color
const instancedEntity = this.createInstancedSphereMesh(
this.generateEntityId(`points-${ms.hex}`, meshName),
ms.positions.map(p => ({ position: p.position, index: p.index })),
size,
segments,
ms.material
);
// Store point indices for potential updates
instancedEntity.tags?.add("instancedPoints");
pointsGroup.addChild(instancedEntity);
});
return pointsGroup;
}
/**
* Creates an instanced mesh for rendering multiple spheres with a single draw call.
* Uses PlayCanvas GPU hardware instancing via setInstancing().
*/
private createInstancedSphereMesh(
name: string,
positions: { position: Inputs.Base.Point3; index: number }[],
radius: number,
segments: number,
material: pc.StandardMaterial
): pc.Entity {
const graphicsDevice = this.context.app?.graphicsDevice;
Iif (!graphicsDevice) {
// Fallback to individual spheres if no graphics device available
return this.createFallbackPointsMesh(name, positions.map(p => p.position), radius, material);
}
const instanceCount = positions.length;
// Create a single sphere mesh to be instanced
const sphereMesh = pc.Mesh.fromGeometry(graphicsDevice, new pc.SphereGeometry({
radius: radius,
latitudeBands: segments,
longitudeBands: segments
}));
// Create instance vertex buffer with world matrices (Mat4 = 16 floats per instance)
const instanceFormat = pc.VertexFormat.getDefaultInstancingFormat(graphicsDevice);
const instanceBuffer = new pc.VertexBuffer(
graphicsDevice,
instanceFormat,
instanceCount,
{
usage: pc.BUFFER_STATIC
}
);
// Fill the instance buffer with transformation matrices
const instanceData = new Float32Array(instanceCount * 16);
const tempMat = new pc.Mat4();
positions.forEach((pos, i) => {
// Create a translation matrix for each instance
tempMat.setTranslate(pos.position[0], pos.position[1], pos.position[2]);
// Copy matrix data (16 floats) to the buffer
instanceData.set(tempMat.data, i * 16);
});
// Upload instance data to GPU
const lockedData = instanceBuffer.lock();
Eif (lockedData) {
new Float32Array(lockedData).set(instanceData);
instanceBuffer.unlock();
}
// Create mesh instance and enable instancing
const meshInstance = new pc.MeshInstance(sphereMesh, material);
meshInstance.setInstancing(instanceBuffer);
meshInstance.instancingCount = instanceCount;
// Disable shadows for point spheres (they use emissive/unlit materials)
meshInstance.castShadow = false;
meshInstance.receiveShadow = false;
// Create entity with render component
const entity = new pc.Entity(name);
entity.addComponent("render", {
meshInstances: [meshInstance],
castShadows: false,
receiveShadows: false
});
// Store instance data for potential updates
entity.tags?.add("instancedPoints");
(entity as pc.Entity & { instanceBuffer?: pc.VertexBuffer; pointIndices?: number[] }).instanceBuffer = instanceBuffer;
(entity as pc.Entity & { instanceBuffer?: pc.VertexBuffer; pointIndices?: number[] }).pointIndices = positions.map(p => p.index);
return entity;
}
/**
* Fallback method when graphics device is not available
*/
private createFallbackPointsMesh(
name: string,
positions: Inputs.Base.Point3[],
size: number,
material: pc.StandardMaterial
): pc.Entity {
const group = new pc.Entity(name);
positions.forEach((pos, index) => {
const sphereEntity = new pc.Entity(this.generateEntityId(`point-${index}`, name));
sphereEntity.addComponent("render", {
type: "sphere",
material: material,
castShadows: false,
receiveShadows: false
});
sphereEntity.setLocalScale(size * 2, size * 2, size * 2);
sphereEntity.setLocalPosition(pos[0], pos[1], pos[2]);
sphereEntity.tags?.add("singlePoint");
group.addChild(sphereEntity);
});
return group;
}
}
|