diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 5db9c2dc..823ffbe5 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -216,6 +216,8 @@ export const DEFAULT_CYLINDER_HEIGHT = 1; export const DEFAULT_CYLINDER_RADIUS = 1; export const DEFAULT_RADIAL_SEGMENTS = 32; +export const DEFAULT_SCALE = 0.00001; // To prevent: can't invert matrix, determinant is 0 + export const DEFAULT_CONE_HEIGHT = 1; export const DEFAULT_CONE_RADIUS = 1; diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index b8d31248..d9dfa9cf 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -9,6 +9,7 @@ import { Quaternion, TorusGeometry, Vector3, + MeshStandardMaterial, } from 'three'; import { @@ -176,3 +177,16 @@ export function assertIsBufferAttribute( throw new Error('the provided attribute must be an BufferAttribute'); } } + +export function assertMaterialWithColorSettable( + val: any, +): asserts val is MeshBasicMaterial | MeshStandardMaterial { + if ( + !(val instanceof MeshBasicMaterial) && + !(val instanceof MeshStandardMaterial) + ) { + throw new TypeError( + `Expected 'val' to be MeshBasicMaterial | MeshStandardMaterial. Color not settable on material.`, + ); + } +} diff --git a/src/utils/interactiveMarkerManager.ts b/src/utils/interactiveMarkerManager.ts index 51e3807e..fada2a5e 100644 --- a/src/utils/interactiveMarkerManager.ts +++ b/src/utils/interactiveMarkerManager.ts @@ -11,6 +11,7 @@ import { INTERACTIVE_MARKER_INTERACTION_MODES, INTERACTIVE_MARKER_ORIENTATION_MODES, UNSUPPORTED_INTERACTIVE_MARKER_ORIENTATION_MODES, + DEFAULT_SCALE, } from './constants'; import Controls from 'three-freeform-controls/dist/types/controls'; import TfViewer from '../viewers/Tf'; @@ -122,7 +123,8 @@ export default class InteractiveMarkerManager { .hideOtherControlsInstancesOnSelect, showHelperPlane: true, }); - controls.scale.set(scale, scale, scale); + const newScale = scale || DEFAULT_SCALE; + controls.scale.set(newScale, newScale, newScale); controls.visible = visible; InteractiveMarkerManager.enableControls( diff --git a/src/utils/markerManager.ts b/src/utils/markerManager.ts index f7af1a46..cb5290bb 100644 --- a/src/utils/markerManager.ts +++ b/src/utils/markerManager.ts @@ -1,4 +1,5 @@ import getNewPrimitive, { MarkerObjectType } from './markerTypes'; +import { DEFAULT_SCALE } from '../utils/constants'; import MarkerLifetime from './markerLifetime'; import { Object3D } from 'three'; @@ -85,7 +86,11 @@ export default class MarkerManager { // To avoid settings these properties for list types: LINE, TRIANGLE, CUBELIST etc if (markerObject.setScale && !markerObject.updatePoints) { - markerObject.setScale({ x: scale.x, y: scale.y, z: scale.z }); + markerObject.setScale({ + x: scale.x || DEFAULT_SCALE, + y: scale.y || DEFAULT_SCALE, + z: scale.z || DEFAULT_SCALE, + }); } if (markerObject.setColor && colors.length <= 0) { markerObject.setColor(color); diff --git a/src/utils/transform.ts b/src/utils/transform.ts index 565f89c1..6abf6c77 100644 --- a/src/utils/transform.ts +++ b/src/utils/transform.ts @@ -1,5 +1,5 @@ -import { Color, Mesh, Object3D, Quaternion } from 'three'; -import { assertIsMeshBasicMaterial } from './helpers'; +import { Color, Mesh, Object3D, Quaternion, MeshStandardMaterial } from 'three'; +import { assertMaterialWithColorSettable } from './helpers'; import Line from '../primitives/Line'; import LineSegments from '../primitives/LineSegment'; @@ -28,7 +28,7 @@ export const setColor = ( object: Mesh | Line | LineSegments, color: string | number | RosMessage.Color, ) => { - assertIsMeshBasicMaterial(object.material); + assertMaterialWithColorSettable(object.material); if (typeof color === 'string' || typeof color === 'number') { object.material.color = new Color(color); } else {