@adatechnology/game-interactables-three 0.2.16 → 0.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1049,6 +1049,249 @@ function indexOf(map, id) {
1049
1049
  return map.get(id) ?? 0;
1050
1050
  }
1051
1051
 
1052
+ // src/stand-out.ts
1053
+ var GAP = 2e-3;
1054
+ var SAME_PLANE = 105e-5;
1055
+ var SAME_DIRECTION = 15e-4;
1056
+ var MIN_OVERLAP_AREA = 5e-5;
1057
+ var MIN_THICKNESS_TO_SINK = 0.012;
1058
+ var MAX_ROUNDS = 6;
1059
+ var IDENTITY = [1, 0, 0, 0, 1, 0, 0, 0, 1];
1060
+ var dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
1061
+ var cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
1062
+ function multiply(a, b) {
1063
+ const out = [];
1064
+ for (let row = 0; row < 3; row++) for (let column = 0; column < 3; column++) out.push(a[row * 3] * b[column] + a[row * 3 + 1] * b[3 + column] + a[row * 3 + 2] * b[6 + column]);
1065
+ return out;
1066
+ }
1067
+ function rotationAbout(axis, angle) {
1068
+ const c = Math.cos(angle);
1069
+ const s = Math.sin(angle);
1070
+ if (axis === 0) return [1, 0, 0, 0, c, -s, 0, s, c];
1071
+ if (axis === 1) return [c, 0, s, 0, 1, 0, -s, 0, c];
1072
+ return [c, -s, 0, s, c, 0, 0, 0, 1];
1073
+ }
1074
+ function rotationOf(node) {
1075
+ let matrix = node.heading ? rotationAbout(1, node.heading) : IDENTITY;
1076
+ if (node.rotation) {
1077
+ for (const letter of node.rotationOrder ?? "XYZ") matrix = multiply(matrix, rotationAbout("XYZ".indexOf(letter), node.rotation["XYZ".indexOf(letter)]));
1078
+ }
1079
+ return matrix;
1080
+ }
1081
+ function apply(matrix, vector) {
1082
+ return [0, 1, 2].map((row) => matrix[row * 3] * vector[0] + matrix[row * 3 + 1] * vector[1] + matrix[row * 3 + 2] * vector[2]);
1083
+ }
1084
+ function transpose(matrix) {
1085
+ return [matrix[0], matrix[3], matrix[6], matrix[1], matrix[4], matrix[7], matrix[2], matrix[5], matrix[8]];
1086
+ }
1087
+ function pieceOf(node, parentOrigin, parentRotation) {
1088
+ if (node.matrix || node.size || !Array.isArray(node.scale) || !node.position) return void 0;
1089
+ const isCylinder = node.kind === "cylinder";
1090
+ const isPlane = node.kind === "plane";
1091
+ if (isCylinder && (node.radiusTop ?? 1) !== (node.radiusBottom ?? 1)) return void 0;
1092
+ const rotation = multiply(parentRotation, rotationOf(node));
1093
+ const axes = [0, 1, 2].map((column) => [rotation[column], rotation[3 + column], rotation[6 + column]]);
1094
+ const half = isCylinder ? [node.scale[0], node.scale[1] / 2, node.scale[2]] : isPlane ? [node.scale[0] / 2, node.scale[1] / 2, 0] : [node.scale[0] / 2, node.scale[1] / 2, node.scale[2] / 2];
1095
+ const offset = apply(parentRotation, node.position);
1096
+ return { node, parentRotation, center: [parentOrigin[0] + offset[0], parentOrigin[1] + offset[1], parentOrigin[2] + offset[2]], axes, half, isCylinder, isPlane };
1097
+ }
1098
+ function collect(nodes, origin, rotation, out) {
1099
+ for (const source of nodes) {
1100
+ const node = source;
1101
+ if (node.kind === "group") {
1102
+ if (node.matrix || node.scale !== void 0) continue;
1103
+ const offset = apply(rotation, node.position ?? [0, 0, 0]);
1104
+ collect(node.children ?? [], [origin[0] + offset[0], origin[1] + offset[1], origin[2] + offset[2]], multiply(rotation, rotationOf(node)), out);
1105
+ } else if (node.kind === "box" || node.kind === "cylinder" || node.kind === "plane") {
1106
+ const piece = pieceOf(node, origin, rotation);
1107
+ if (piece) out.push(piece);
1108
+ }
1109
+ }
1110
+ }
1111
+ function faceOf(piece, axis, sign) {
1112
+ const normal = [piece.axes[axis][0] * sign, piece.axes[axis][1] * sign, piece.axes[axis][2] * sign];
1113
+ return { piece, axis, sign, normal, offset: dot(normal, piece.center) + piece.half[axis] };
1114
+ }
1115
+ function facesOf(piece) {
1116
+ if (piece.isPlane) return [faceOf(piece, 2, 1)];
1117
+ return (piece.isCylinder ? [1] : [0, 1, 2]).flatMap((axis) => [faceOf(piece, axis, 1), faceOf(piece, axis, -1)]);
1118
+ }
1119
+ function cornersOf(face, u, v) {
1120
+ const { piece, axis } = face;
1121
+ const [first, second] = [0, 1, 2].filter((other) => other !== axis);
1122
+ return [[1, 1], [1, -1], [-1, -1], [-1, 1]].map(([a, b]) => {
1123
+ const point = [0, 1, 2].map((k) => piece.center[k] + piece.axes[first][k] * piece.half[first] * a + piece.axes[second][k] * piece.half[second] * b);
1124
+ return [dot(point, u), dot(point, v)];
1125
+ });
1126
+ }
1127
+ var side = (p, a, b) => (b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]);
1128
+ function overlapArea(subject, clip) {
1129
+ let output = subject;
1130
+ const orientation = Math.sign(side(clip[2], clip[0], clip[1])) || 1;
1131
+ for (let i = 0; i < clip.length && output.length; i++) {
1132
+ const a = clip[i];
1133
+ const b = clip[(i + 1) % clip.length];
1134
+ const input = output;
1135
+ output = [];
1136
+ for (let j = 0; j < input.length; j++) {
1137
+ const current = input[j];
1138
+ const previous = input[(j + input.length - 1) % input.length];
1139
+ const currentInside = side(current, a, b) * orientation >= 0;
1140
+ if (currentInside !== side(previous, a, b) * orientation >= 0) {
1141
+ const t = side(previous, a, b) / (side(previous, a, b) - side(current, a, b));
1142
+ output.push([previous[0] + (current[0] - previous[0]) * t, previous[1] + (current[1] - previous[1]) * t]);
1143
+ }
1144
+ if (currentInside) output.push(current);
1145
+ }
1146
+ }
1147
+ let area = 0;
1148
+ for (let i = 0; i < output.length; i++) {
1149
+ const p = output[i];
1150
+ const q = output[(i + 1) % output.length];
1151
+ area += p[0] * q[1] - q[0] * p[1];
1152
+ }
1153
+ return Math.abs(area) / 2;
1154
+ }
1155
+ function planeBasis(normal) {
1156
+ const side2 = cross(normal, Math.abs(normal[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0]);
1157
+ const length = Math.hypot(...side2);
1158
+ const u = [side2[0] / length, side2[1] / length, side2[2] / length];
1159
+ return [u, cross(normal, u)];
1160
+ }
1161
+ function faceReach(face, k) {
1162
+ const { piece, axis } = face;
1163
+ let reach = 0;
1164
+ for (let other = 0; other < 3; other++) if (other !== axis) reach += Math.abs(piece.axes[other][k]) * piece.half[other];
1165
+ return reach;
1166
+ }
1167
+ function facesNear(a, b, slack = SAME_PLANE) {
1168
+ for (let k = 0; k < 3; k++) {
1169
+ const centreA = a.piece.center[k] + a.normal[k] * a.piece.half[a.axis];
1170
+ const centreB = b.piece.center[k] + b.normal[k] * b.piece.half[b.axis];
1171
+ if (Math.abs(centreA - centreB) > faceReach(a, k) + faceReach(b, k) + slack) return false;
1172
+ }
1173
+ return true;
1174
+ }
1175
+ function fight(a, b) {
1176
+ if (Math.abs(a.offset - b.offset) > SAME_PLANE) return false;
1177
+ if ([0, 1, 2].some((k) => Math.abs(a.normal[k] - b.normal[k]) > SAME_DIRECTION)) return false;
1178
+ if (!facesNear(a, b)) return false;
1179
+ const [u, v] = planeBasis(a.normal);
1180
+ return overlapArea(cornersOf(a, u, v), cornersOf(b, u, v)) > MIN_OVERLAP_AREA;
1181
+ }
1182
+ function moveFace(face, amount) {
1183
+ const { piece, axis } = face;
1184
+ const scale = piece.node.scale;
1185
+ if (!piece.isPlane) {
1186
+ scale[axis] = scale[axis] + amount;
1187
+ piece.half[axis] = piece.half[axis] + amount / 2;
1188
+ }
1189
+ const shift = face.normal.map((component) => component * amount / (piece.isPlane ? 1 : 2));
1190
+ for (let k = 0; k < 3; k++) piece.center[k] = piece.center[k] + shift[k];
1191
+ const inParent = apply(transpose(piece.parentRotation), shift);
1192
+ const position = piece.node.position;
1193
+ for (let k = 0; k < 3; k++) position[k] = position[k] + inParent[k];
1194
+ }
1195
+ var DIRECTION_SPAN = 2048;
1196
+ var directionKey = (normal) => ((Math.round(normal[0] / SAME_DIRECTION) + DIRECTION_SPAN / 2) * DIRECTION_SPAN + Math.round(normal[1] / SAME_DIRECTION) + DIRECTION_SPAN / 2) * DIRECTION_SPAN + Math.round(normal[2] / SAME_DIRECTION) + DIRECTION_SPAN / 2;
1197
+ function groupFaces(pieces) {
1198
+ const groups = /* @__PURE__ */ new Map();
1199
+ for (const piece of pieces) {
1200
+ for (const face of facesOf(piece)) {
1201
+ const key = directionKey(face.normal);
1202
+ const group = groups.get(key);
1203
+ if (group) group.push(face);
1204
+ else groups.set(key, [face]);
1205
+ }
1206
+ }
1207
+ return groups;
1208
+ }
1209
+ var liveFace = (face) => faceOf(face.piece, face.axis, face.sign);
1210
+ function clearFace(face, groups) {
1211
+ const { piece, axis, sign } = face;
1212
+ const thick = piece.half[axis] * 2 >= MIN_THICKNESS_TO_SINK;
1213
+ const steps = piece.isPlane ? [GAP, 2 * GAP, 3 * GAP] : [GAP, -GAP, 2 * GAP, -2 * GAP].map((step) => thick ? -step : step);
1214
+ const fits = (step) => piece.isPlane || piece.half[axis] * 2 + step >= 2 * GAP;
1215
+ const reach = 2 * GAP * 3 + 2 * SAME_PLANE;
1216
+ const neighbours = (groups.get(directionKey(face.normal)) ?? []).filter((candidate) => candidate.piece !== piece && Math.abs(candidate.offset - face.offset) <= reach && facesNear(face, candidate, reach));
1217
+ const clear = () => {
1218
+ const moved = faceOf(piece, axis, sign);
1219
+ return neighbours.every((candidate) => !fight(moved, liveFace(candidate)));
1220
+ };
1221
+ let applied = 0;
1222
+ for (const step of steps.filter(fits)) {
1223
+ moveFace(face, step - applied);
1224
+ applied = step;
1225
+ if (clear()) return;
1226
+ }
1227
+ moveFace(face, steps[0] - applied);
1228
+ }
1229
+ function separateRadii(a, b) {
1230
+ if (!a.isCylinder || !b.isCylinder || Math.abs(dot(a.axes[1], b.axes[1])) < 0.9999) return false;
1231
+ const between = [b.center[0] - a.center[0], b.center[1] - a.center[1], b.center[2] - a.center[2]];
1232
+ const along = dot(between, a.axes[1]);
1233
+ if (Math.hypot(between[0] - a.axes[1][0] * along, between[1] - a.axes[1][1] * along, between[2] - a.axes[1][2] * along) > SAME_PLANE) return false;
1234
+ if (Math.abs(along) > a.half[1] + b.half[1] - 5e-3 || Math.abs(a.half[0] - b.half[0]) > SAME_PLANE * 1.1) return false;
1235
+ const [mover, other] = volumeOf(a) < volumeOf(b) ? [a, b] : [b, a];
1236
+ const amount = mover.half[0] >= other.half[0] ? GAP : -GAP;
1237
+ const scale = mover.node.scale;
1238
+ scale[0] = scale[0] + amount;
1239
+ scale[2] = scale[2] + amount;
1240
+ mover.half[0] += amount;
1241
+ mover.half[2] += amount;
1242
+ return true;
1243
+ }
1244
+ var volumeOf = (piece) => piece.half[0] * piece.half[1] * piece.half[2];
1245
+ function forEachPlaneMate(groups, visit) {
1246
+ for (const group of groups.values()) {
1247
+ if (group.length < 2) continue;
1248
+ group.sort((a, b) => a.offset - b.offset);
1249
+ for (let i = 0; i < group.length; i++) {
1250
+ const a = group[i];
1251
+ for (let j = i + 1; j < group.length && group[j].offset - a.offset <= 2 * SAME_PLANE; j++) {
1252
+ if (group[j].piece !== a.piece) visit(a, group[j]);
1253
+ }
1254
+ }
1255
+ }
1256
+ }
1257
+ function separateAllRadii(cylinders, moved) {
1258
+ for (let i = 0; i < cylinders.length; i++) {
1259
+ for (let j = i + 1; j < cylinders.length; j++) {
1260
+ const a = cylinders[i];
1261
+ const b = cylinders[j];
1262
+ const reach = Math.max(a.half[0], a.half[1]) + Math.max(b.half[0], b.half[1]) + 0.01;
1263
+ if (Math.abs(a.center[0] - b.center[0]) > reach || Math.abs(a.center[1] - b.center[1]) > reach || Math.abs(a.center[2] - b.center[2]) > reach) continue;
1264
+ if (separateRadii(a, b)) {
1265
+ moved.add(a);
1266
+ moved.add(b);
1267
+ }
1268
+ }
1269
+ }
1270
+ }
1271
+ function standOutCoincidentBoxes(schema) {
1272
+ const pieces = [];
1273
+ collect(schema.root.children ?? [], [0, 0, 0], IDENTITY, pieces);
1274
+ const cylinders = pieces.filter((piece) => piece.isCylinder);
1275
+ let movedLast;
1276
+ for (let round = 0; round < MAX_ROUNDS; round++) {
1277
+ const movedNow = /* @__PURE__ */ new Set();
1278
+ separateAllRadii(cylinders, movedNow);
1279
+ const groups = groupFaces(pieces);
1280
+ forEachPlaneMate(groups, (first, second) => {
1281
+ if (movedLast && !movedLast.has(first.piece) && !movedLast.has(second.piece)) return;
1282
+ const a = liveFace(first);
1283
+ const b = liveFace(second);
1284
+ if (!fight(a, b)) return;
1285
+ const mover = volumeOf(a.piece) < volumeOf(b.piece) ? a : b;
1286
+ clearFace(mover, groups);
1287
+ movedNow.add(mover.piece);
1288
+ });
1289
+ if (!movedNow.size) break;
1290
+ movedLast = movedNow;
1291
+ }
1292
+ return schema;
1293
+ }
1294
+
1052
1295
  // src/mesh.ts
1053
1296
  function createDefaultMaterials(THREE, quality = "high") {
1054
1297
  const defined = (o) => {
@@ -1146,7 +1389,7 @@ function createMeshApi(THREE, options = {}) {
1146
1389
  const materialFor = finishOptions?.materialFor ?? options.materialFor;
1147
1390
  if (finished) throw new Error("createMeshApi: finish() was already called");
1148
1391
  finished = true;
1149
- const buffers = generate(recorder.schema(), {
1392
+ const buffers = generate(options.standOut === true ? standOutCoincidentBoxes(recorder.schema()) : recorder.schema(), {
1150
1393
  colors: colorSpaceOf(THREE),
1151
1394
  materials: MATERIAL_KEYS,
1152
1395
  fallbackMaterial: "plain",
@@ -1414,12 +1657,12 @@ function createScreen(THREE, options) {
1414
1657
  }
1415
1658
  texture.needsUpdate = true;
1416
1659
  };
1417
- const apply = () => {
1660
+ const apply2 = () => {
1418
1661
  material.emissiveIntensity = on ? intensity : 0;
1419
1662
  material.color.setHex(on ? 1118481 : 0);
1420
1663
  draw();
1421
1664
  };
1422
- apply();
1665
+ apply2();
1423
1666
  return {
1424
1667
  material,
1425
1668
  texture,
@@ -1433,7 +1676,7 @@ function createScreen(THREE, options) {
1433
1676
  setOn(value) {
1434
1677
  on = !!value;
1435
1678
  acc = 0;
1436
- apply();
1679
+ apply2();
1437
1680
  },
1438
1681
  setChannel(n) {
1439
1682
  channel2 = Math.max(0, Math.floor(n));
@@ -1570,6 +1813,7 @@ export {
1570
1813
  patchSwayShader,
1571
1814
  poseOf,
1572
1815
  rememberRest,
1816
+ standOutCoincidentBoxes,
1573
1817
  swayMaterial,
1574
1818
  syncObject
1575
1819
  };