@wandelbots/wandelbots-js-react-components 1.4.3 → 1.5.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wandelbots/wandelbots-js-react-components",
3
- "version": "1.4.3",
3
+ "version": "1.5.1",
4
4
  "description": "React UI toolkit for building applications on top of the Wandelbots platform",
5
5
  "files": [
6
6
  "dist",
@@ -6,6 +6,7 @@ import { defaultGetModel, SupportedRobot } from "./SupportedRobot"
6
6
  export type ConnectecMotionGroupRobotProps = {
7
7
  connectedMotionGroup: ConnectedMotionGroup
8
8
  getModel?: (modelFromController: string) => string
9
+ isGhost?: boolean
9
10
  } & GroupProps
10
11
 
11
12
  /**
@@ -22,6 +23,7 @@ export type ConnectecMotionGroupRobotProps = {
22
23
  export function Robot({
23
24
  connectedMotionGroup,
24
25
  getModel = defaultGetModel,
26
+ isGhost = false,
25
27
  ...props
26
28
  }: ConnectecMotionGroupRobotProps) {
27
29
  if (!connectedMotionGroup.dhParameters) {
@@ -36,6 +38,7 @@ export function Robot({
36
38
  modelFromController={connectedMotionGroup.modelFromController || ""}
37
39
  dhParameters={connectedMotionGroup.dhParameters}
38
40
  getModel={getModel}
41
+ isGhost={isGhost}
39
42
  {...props}
40
43
  />
41
44
  )
@@ -1,4 +1,4 @@
1
- import { Suspense } from "react"
1
+ import { Suspense, useCallback, useEffect, useRef } from "react"
2
2
 
3
3
  import { UniversalRobots_UR3 } from "./UniversalRobots_UR3"
4
4
  import { UniversalRobots_UR3e } from "./UniversalRobots_UR3e"
@@ -27,6 +27,8 @@ import type {
27
27
  } from "@wandelbots/wandelbots-api-client"
28
28
  import { DHRobot } from "./DHRobot"
29
29
 
30
+ import * as THREE from "three"
31
+
30
32
  export type DHRobotProps = {
31
33
  rapidlyChangingMotionState: MotionGroupStateResponse
32
34
  dhParameters: Array<DHParameter>
@@ -42,6 +44,7 @@ export type SupportedRobotProps = {
42
44
  modelFromController: string
43
45
  dhParameters: DHParameter[]
44
46
  getModel?: (modelFromController: string) => string
47
+ isGhost?: boolean
45
48
  } & GroupProps
46
49
 
47
50
  export function defaultGetModel(modelFromController: string): string {
@@ -53,10 +56,113 @@ export function SupportedRobot({
53
56
  modelFromController,
54
57
  dhParameters,
55
58
  getModel = defaultGetModel,
59
+ isGhost = false,
56
60
  ...props
57
61
  }: SupportedRobotProps) {
58
62
  let Robot
59
63
 
64
+ const robotRef = useRef<THREE.Group>(new THREE.Group())
65
+
66
+ const setRobotRef = useCallback(
67
+ (instance: THREE.Group | null) => {
68
+ if (instance !== null) {
69
+ robotRef.current = instance
70
+ console.log("robotRef.current", robotRef.current)
71
+ if (
72
+ isGhost &&
73
+ robotRef.current &&
74
+ robotRef.current.children.length > 0
75
+ ) {
76
+ addGhosts()
77
+ }
78
+ }
79
+ },
80
+ [isGhost],
81
+ )
82
+
83
+ const addGhosts = () => {
84
+ if (robotRef.current && !robotRef.current.userData.ghostsCreated) {
85
+ robotRef.current.traverse((obj) => {
86
+ if (obj instanceof THREE.Mesh && !obj.userData.isGhost) {
87
+ if (obj.material instanceof THREE.Material) {
88
+ obj.material.colorWrite = false
89
+ }
90
+
91
+ // Create a clone of the mesh
92
+ const depth = obj.clone()
93
+ const ghost = obj.clone()
94
+
95
+ depth.material = new THREE.MeshStandardMaterial({
96
+ depthTest: true,
97
+ depthWrite: true,
98
+ colorWrite: false,
99
+ polygonOffset: true,
100
+ polygonOffsetFactor: 1,
101
+ })
102
+ depth.userData.isGhost = true
103
+
104
+ // Set the material for the ghost mesh
105
+ ghost.material = new THREE.MeshStandardMaterial({
106
+ color: "#D91433",
107
+ opacity: 0.3,
108
+ depthTest: true,
109
+ depthWrite: false,
110
+ transparent: true,
111
+ polygonOffset: true,
112
+ polygonOffsetFactor: -1,
113
+ })
114
+ ghost.userData.isGhost = true
115
+
116
+ if (obj.parent) {
117
+ obj.parent.add(depth)
118
+ obj.parent.add(ghost)
119
+ }
120
+ }
121
+ })
122
+ robotRef.current.userData.ghostsCreated = true
123
+ }
124
+ }
125
+
126
+ const removeGhosts = () => {
127
+ if (robotRef.current) {
128
+ const objectsToRemove: THREE.Object3D[] = []
129
+
130
+ robotRef.current.traverse((obj) => {
131
+ if (obj instanceof THREE.Mesh) {
132
+ if (obj.material instanceof THREE.Material) {
133
+ obj.material.colorWrite = true
134
+ }
135
+ }
136
+
137
+ if (
138
+ obj instanceof THREE.Mesh &&
139
+ obj.userData !== undefined &&
140
+ obj.userData &&
141
+ obj.userData.isGhost !== undefined &&
142
+ obj.userData.isGhost
143
+ ) {
144
+ objectsToRemove.push(obj)
145
+ }
146
+ })
147
+
148
+ objectsToRemove.forEach((obj) => {
149
+ if (obj.parent) {
150
+ obj.parent.remove(obj)
151
+ }
152
+ })
153
+
154
+ robotRef.current.userData.ghostsCreated = false
155
+ }
156
+ }
157
+
158
+ useEffect(() => {
159
+ if (isGhost) {
160
+ addGhosts()
161
+ } else {
162
+ removeGhosts()
163
+ }
164
+ }, [isGhost])
165
+
60
166
  switch (modelFromController) {
61
167
  case "UniversalRobots_UR3":
62
168
  Robot = UniversalRobots_UR3
@@ -136,12 +242,14 @@ export function SupportedRobot({
136
242
  />
137
243
  }
138
244
  >
139
- <Robot
140
- rapidlyChangingMotionState={rapidlyChangingMotionState}
141
- modelURL={getModel(modelFromController)}
142
- dhParameters={dhParameters}
143
- {...props}
144
- />
245
+ <group ref={setRobotRef}>
246
+ <Robot
247
+ rapidlyChangingMotionState={rapidlyChangingMotionState}
248
+ modelURL={getModel(modelFromController)}
249
+ dhParameters={dhParameters}
250
+ {...props}
251
+ />
252
+ </group>
145
253
  </Suspense>
146
254
  )
147
255
  }
@@ -1,8 +1,12 @@
1
- import React from "react";
2
-
3
1
  export function RobotIcon() {
4
2
  return (
5
- <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
3
+ <svg
4
+ width="24"
5
+ height="24"
6
+ viewBox="0 0 24 24"
7
+ fill="none"
8
+ xmlns="http://www.w3.org/2000/svg"
9
+ >
6
10
  <path
7
11
  fill-rule="evenodd"
8
12
  clip-rule="evenodd"
@@ -10,5 +14,5 @@ export function RobotIcon() {
10
14
  fill="white"
11
15
  />
12
16
  </svg>
13
- );
17
+ )
14
18
  }