2121from tesseract import tesseract_geometry
2222from tesseract .tesseract_common import Quaterniond
2323import pkgutil
24+ import re
25+ import base64
2426
2527def tesseract_env_to_babylon_json (t_env , origin_offset = [0 ,0 ,0 ]):
2628 return json .dumps (tesseract_env_to_babylon_json_dict (t_env ,origin_offset ))
@@ -74,8 +76,12 @@ def _process_link_recursive(link_map, joint_map, link_name, parent_joint_name):
7476 tf_link ["parentId" ] = "root"
7577 transform_nodes .append (tf_link )
7678
79+ visual_i = 0
80+
7781 for visual in link .visual :
78- visual_name = "link_" + link_name + "_visual_" + visual .name
82+ visual_i += 1
83+ visual_u_name = visual .name + str (visual_i )
84+ visual_name = "link_" + link_name + "_visual_" + visual_u_name
7985 np_tf_visual = visual .origin
8086 visual_p , visual_q = _np_transform_to_babylon (np_tf_visual )
8187 tf_visual = {"name" : visual_name , "isVisible" : "true" , "isEnabled" : "true" ,
@@ -84,6 +90,8 @@ def _process_link_recursive(link_map, joint_map, link_name, parent_joint_name):
8490 tf_visual ["rotationQuaternion" ] = visual_q
8591 tf_visual ["billboardMode" ] = 0
8692
93+ tf_material = None
94+
8795 visual_geom = visual .geometry
8896 if (isinstance (visual_geom ,tesseract_geometry .Mesh )):
8997
@@ -115,6 +123,57 @@ def _process_link_recursive(link_map, joint_map, link_name, parent_joint_name):
115123 submesh ["indexStart" ] = 0
116124 submesh ["indexCount" ] = len (indices )
117125 tf_visual ["subMeshes" ] = [submesh ]
126+
127+ if not mesh .getResource ().getUrl ().lower ().endswith ('.stl' ):
128+ mesh_material = mesh .getMaterial ()
129+ if mesh_material is not None :
130+
131+ tf_material = {"name" : "material_" + visual_name }
132+
133+ color = mesh_material .getBaseColorFactor ().flatten ()
134+ tf_color = [color [0 ], color [1 ], color [2 ]]
135+ tf_color2 = [color [0 ]* 0.5 , color [1 ]* 0.5 , color [2 ]* 0.5 ]
136+
137+ """tf_material["ambient"] = tf_color
138+ tf_material["diffuse"] = tf_color
139+ tf_material["specular"] = tf_color2
140+ tf_material["alpha"] = 1
141+ tf_material["backFaceCulling"] = True"""
142+
143+ tf_material ["customType" ] = "BABYLON.PBRMaterial"
144+ tf_material ["albedo" ] = tf_color
145+ tf_material ["alpha" ] = color [3 ]
146+ tf_material ["alphaCutOff" ] = 0.4
147+ tf_material ["backFaceCulling" ] = True
148+ tf_material ["roughness" ] = mesh_material .getRoughnessFactor ()
149+ tf_material ["metallic" ] = mesh_material .getMetallicFactor ()
150+ tf_material ["maxSimultaneousLights" ] = 4
151+ tf_material ["environmentIntensity" ] = 0.1
152+
153+ mesh_textures = mesh .getTextures ()
154+ if mesh_textures is not None and len (mesh_textures ) > 0 :
155+ mesh_tex = mesh_textures [0 ]
156+ mesh_tex_image = mesh_tex .getTextureImage ()
157+ tex_name = mesh_tex_image .getUrl ()
158+ tex_mimetype = "image/jpg"
159+ if tex_name .endswith ('png' ):
160+ tex_mimetype = "image/png"
161+ tex_data = base64 .b64encode (bytearray (mesh_tex_image .getResourceContents ())).decode ('ascii' )
162+ tex_data_url = "data:" + tex_mimetype + ";base64," + tex_data
163+
164+ tf_texture = {"name" : "material_texture_" + visual_name , "url" : "material_texture_" + visual_name , "level" : 1 ,"hasAlpha" : True ,"coordinatesMode" :0 ,"uOffset" :0 ,"vOffset" :0 ,"uScale" :1 ,\
165+ "vScale" :1 ,"uAng" :0 ,"vAng" :0 ,"wAng" :0 ,"wrapU" :0 ,"wrapV" :0 ,"coordinatesIndex" :0 , "isRenderTarget" :False , \
166+ "base64String" : tex_data_url }
167+
168+ #tf_material["useAlphaFromAlbedoTexture"] = True
169+ tf_material ["albedoTexture" ] = tf_texture
170+ mesh_uvs = mesh_tex .getUVs ()
171+ tf_uvs = [0 ]* (len (mesh_uvs )* 2 )
172+ for i in range (len (mesh_uvs )):
173+ mesh_uv = mesh_uvs [i ].flatten ()
174+ tf_uvs [i * 2 ] = mesh_uv [0 ]
175+ tf_uvs [i * 2 + 1 ] = mesh_uv [1 ]
176+ tf_visual ["uvs" ] = tf_uvs
118177
119178
120179 elif (isinstance (visual_geom ,tesseract_geometry .Box )):
@@ -160,21 +219,22 @@ def _process_link_recursive(link_map, joint_map, link_name, parent_joint_name):
160219
161220 meshes .append (tf_visual )
162221
163- tf_material = {"name" : "material_" + visual_name }
222+ if tf_material is None :
223+ tf_material = {"name" : "material_" + visual_name }
164224
165- material = visual .material
225+ material = visual .material
166226
167- if material is None :
168- tf_color = [0.5 ,0.5 ,0.5 ]
169- else :
170- color = material .color .flatten ()
171- tf_color = [color [0 ], color [1 ], color [2 ]]
172-
173- tf_material ["ambient" ] = tf_color
174- tf_material ["diffuse" ] = tf_color
175- tf_material ["specular" ] = tf_color
176- tf_material ["alpha" ] = 1
177- tf_material ["backFaceCulling" ] = True
227+ if material is None :
228+ tf_color = [0.5 ,0.5 ,0.5 ]
229+ else :
230+ color = material .color .flatten ()
231+ tf_color = [color [0 ], color [1 ], color [2 ]]
232+
233+ tf_material ["ambient" ] = tf_color
234+ tf_material ["diffuse" ] = tf_color
235+ tf_material ["specular" ] = tf_color
236+ tf_material ["alpha" ] = 1
237+ tf_material ["backFaceCulling" ] = True
178238
179239 materials .append (tf_material )
180240
0 commit comments