@tscircuit/3d-viewer 0.0.634 → 0.0.635

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.
Files changed (2) hide show
  1. package/dist/index.js +32 -22
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -20161,7 +20161,7 @@ var JscadModel = ({
20161
20161
  );
20162
20162
  };
20163
20163
 
20164
- // src/hooks/use-global-obj-loader.ts
20164
+ // src/hooks/use-global-model-loader.ts
20165
20165
  import { useEffect as useEffect10, useState as useState8 } from "react";
20166
20166
 
20167
20167
  // src/utils/vrml.ts
@@ -20183,21 +20183,24 @@ async function loadVrml(url) {
20183
20183
  return loader.parse(sanitizeVrmlIdentifiers(text), url);
20184
20184
  }
20185
20185
 
20186
- // src/hooks/use-global-obj-loader.ts
20187
- import { MTLLoader, OBJLoader } from "three-stdlib";
20188
- if (typeof window !== "undefined" && !window.TSCIRCUIT_OBJ_LOADER_CACHE) {
20189
- window.TSCIRCUIT_OBJ_LOADER_CACHE = /* @__PURE__ */ new Map();
20186
+ // src/hooks/use-global-model-loader.ts
20187
+ import { Mesh as Mesh8, MeshStandardMaterial as MeshStandardMaterial4 } from "three";
20188
+ import { MTLLoader, OBJLoader, STLLoader } from "three-stdlib";
20189
+ if (typeof window !== "undefined" && !window.TSCIRCUIT_MODEL_LOADER_CACHE) {
20190
+ window.TSCIRCUIT_MODEL_LOADER_CACHE = /* @__PURE__ */ new Map();
20190
20191
  }
20191
- function useGlobalObjLoader(url) {
20192
+ function useGlobalModelLoader(url, modelType) {
20192
20193
  const [obj, setObj] = useState8(null);
20193
20194
  useEffect10(() => {
20194
20195
  if (!url) return;
20195
20196
  const cleanUrl = url.replace(/&cachebust_origin=$/, "");
20196
- const cache = window.TSCIRCUIT_OBJ_LOADER_CACHE;
20197
+ const cachesByModelType = window.TSCIRCUIT_MODEL_LOADER_CACHE;
20198
+ const cache = cachesByModelType.get(modelType) ?? /* @__PURE__ */ new Map();
20199
+ cachesByModelType.set(modelType, cache);
20197
20200
  let hasUrlChanged = false;
20198
- async function loadAndParseObj() {
20201
+ async function loadAndParseModel() {
20199
20202
  try {
20200
- if (cleanUrl.endsWith(".wrl")) {
20203
+ if (modelType === "wrl") {
20201
20204
  return await loadVrml(cleanUrl);
20202
20205
  }
20203
20206
  const response = await fetch(cleanUrl);
@@ -20206,6 +20209,11 @@ function useGlobalObjLoader(url) {
20206
20209
  `Failed to fetch "${cleanUrl}": ${response.status} ${response.statusText}`
20207
20210
  );
20208
20211
  }
20212
+ if (modelType === "stl") {
20213
+ const geometry = new STLLoader().parse(await response.arrayBuffer());
20214
+ const material = new MeshStandardMaterial4({ color: 8947848 });
20215
+ return new Mesh8(geometry, material);
20216
+ }
20209
20217
  const text = await response.text();
20210
20218
  const mtlContentArr = text.match(/newmtl[\s\S]*?endmtl/g);
20211
20219
  const objLoader = new OBJLoader();
@@ -20236,7 +20244,7 @@ function useGlobalObjLoader(url) {
20236
20244
  return result.clone();
20237
20245
  });
20238
20246
  }
20239
- const promise = loadAndParseObj().then((result) => {
20247
+ const promise = loadAndParseModel().then((result) => {
20240
20248
  if (result instanceof Error) {
20241
20249
  return result;
20242
20250
  }
@@ -20255,7 +20263,7 @@ function useGlobalObjLoader(url) {
20255
20263
  return () => {
20256
20264
  hasUrlChanged = true;
20257
20265
  };
20258
- }, [url]);
20266
+ }, [url, modelType]);
20259
20267
  return obj;
20260
20268
  }
20261
20269
 
@@ -20265,6 +20273,7 @@ import * as THREE13 from "three";
20265
20273
  import { jsx as jsx10 } from "react/jsx-runtime";
20266
20274
  function MixedStlModel({
20267
20275
  url,
20276
+ modelType,
20268
20277
  position,
20269
20278
  rotation,
20270
20279
  modelOffset = [0, 0, 0],
@@ -20278,7 +20287,7 @@ function MixedStlModel({
20278
20287
  scale: scale11,
20279
20288
  isTranslucent = false
20280
20289
  }) {
20281
- const obj = useGlobalObjLoader(url);
20290
+ const obj = useGlobalModelLoader(url, modelType);
20282
20291
  const model = useMemo8(() => {
20283
20292
  if (obj && !(obj instanceof Error)) {
20284
20293
  obj.traverse((child) => {
@@ -20352,11 +20361,11 @@ import {
20352
20361
  Color as Color3,
20353
20362
  Float32BufferAttribute,
20354
20363
  Group as Group5,
20355
- Mesh as Mesh9,
20356
- MeshStandardMaterial as MeshStandardMaterial5
20364
+ Mesh as Mesh10,
20365
+ MeshStandardMaterial as MeshStandardMaterial6
20357
20366
  } from "three";
20358
20367
  var DEFAULT_COLOR = new Color3(0.82, 0.82, 0.82);
20359
- var createMaterial = (color) => new MeshStandardMaterial5({
20368
+ var createMaterial = (color) => new MeshStandardMaterial6({
20360
20369
  color: color ? new Color3(color[0], color[1], color[2]) : DEFAULT_COLOR
20361
20370
  });
20362
20371
  function applyFaceColorGroups(geometry, mesh) {
@@ -20426,7 +20435,7 @@ function occtMeshesToGroup(meshes) {
20426
20435
  geometry.setIndex(indices);
20427
20436
  geometry.clearGroups();
20428
20437
  const materials2 = applyFaceColorGroups(geometry, mesh);
20429
- const threeMesh = new Mesh9(
20438
+ const threeMesh = new Mesh10(
20430
20439
  geometry,
20431
20440
  materials2.length > 1 ? materials2 : materials2[0]
20432
20441
  );
@@ -21096,6 +21105,7 @@ var AnyCadComponent = ({
21096
21105
  MixedStlModel,
21097
21106
  {
21098
21107
  url,
21108
+ modelType: meshModelType,
21099
21109
  position: adjustedPosition,
21100
21110
  rotation: rotationOffset,
21101
21111
  modelOffset: modelTransform.modelPosition,
@@ -21111,7 +21121,7 @@ var AnyCadComponent = ({
21111
21121
  isHovered,
21112
21122
  isTranslucent
21113
21123
  },
21114
- `${cad_component.cad_component_id}-mixed-${url}`
21124
+ `${cad_component.cad_component_id}-mixed-${meshModelType}-${url}`
21115
21125
  )
21116
21126
  );
21117
21127
  }
@@ -22051,7 +22061,7 @@ import * as THREE30 from "three";
22051
22061
  // package.json
22052
22062
  var package_default = {
22053
22063
  name: "@tscircuit/3d-viewer",
22054
- version: "0.0.633",
22064
+ version: "0.0.634",
22055
22065
  repository: {
22056
22066
  type: "git",
22057
22067
  url: "https://github.com/tscircuit/3d-viewer"
@@ -28011,7 +28021,7 @@ function JscadBoardTextures({
28011
28021
  // src/three-components/STLModel.tsx
28012
28022
  import { useState as useState18, useEffect as useEffect29, useMemo as useMemo22 } from "react";
28013
28023
  import * as THREE50 from "three";
28014
- import { STLLoader } from "three-stdlib";
28024
+ import { STLLoader as STLLoader2 } from "three-stdlib";
28015
28025
  function STLModel({
28016
28026
  stlUrl,
28017
28027
  stlData,
@@ -28023,7 +28033,7 @@ function STLModel({
28023
28033
  const { rootObject } = useThree();
28024
28034
  const [geom, setGeom] = useState18(null);
28025
28035
  useEffect29(() => {
28026
- const loader = new STLLoader();
28036
+ const loader = new STLLoader2();
28027
28037
  if (stlData) {
28028
28038
  try {
28029
28039
  const geometry = loader.parse(stlData);
@@ -40456,10 +40466,10 @@ import * as THREE65 from "three";
40456
40466
  import * as THREE64 from "three";
40457
40467
  import { GLTFLoader as GLTFLoader3 } from "three/examples/jsm/loaders/GLTFLoader.js";
40458
40468
  import { OBJLoader as OBJLoader2 } from "three/examples/jsm/loaders/OBJLoader.js";
40459
- import { STLLoader as STLLoader2 } from "three/examples/jsm/loaders/STLLoader.js";
40469
+ import { STLLoader as STLLoader3 } from "three/examples/jsm/loaders/STLLoader.js";
40460
40470
  async function load3DModel(url) {
40461
40471
  if (url.endsWith(".stl")) {
40462
- const loader = new STLLoader2();
40472
+ const loader = new STLLoader3();
40463
40473
  const geometry = await loader.loadAsync(url);
40464
40474
  const material = new THREE64.MeshStandardMaterial({
40465
40475
  color: 8947848,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscircuit/3d-viewer",
3
- "version": "0.0.634",
3
+ "version": "0.0.635",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/tscircuit/3d-viewer"