Skip to content

Commit

Permalink
Fix bugs for load texture from vrep
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceGeLi committed Jan 16, 2025
1 parent e7cb49f commit 45f2603
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 16 deletions.
73 changes: 57 additions & 16 deletions simpub/parser/coppelia_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from ..simdata import VisualType
from ..core.log import logger


def get_scene_obj_type_str(sim, obj_type_id: int):
# TODO, MAKE THIS A DICT
# Get obj type name from obj type id
Expand Down Expand Up @@ -156,6 +155,26 @@ def get_objects_info_dict(sim, visual_layer_list=None,
auxiliary = sim.getShapeColor(
idx, None, sim.colorcomponent_auxiliary)[1]

texture_id = sim.getShapeTextureId(idx)
if texture_id != -1:
mesh_id = sim.getProperty(idx, "meshes")[0]
texture_width, texture_height = (
sim.getProperty(mesh_id, "textureResolution"))
texture_data = sim.readTexture(texture_id, 0)
texture_repeat_u = sim.getProperty(mesh_id, "textureRepeatU")
texture_repeat_v = sim.getProperty(mesh_id, "textureRepeatV")
num_indices = len(indices)
texture_coord = sim.getProperty(mesh_id, "textureCoordinates")
texture_coord = np.asarray(texture_coord)
texture_coord = texture_coord.reshape(num_indices, -1)
else:
texture_data = None
texture_width = None
texture_height = None
texture_repeat_u = None
texture_repeat_v = None
texture_coord = None

obj_info_dict[idx_str].update({
"shape_result": result,
"primitive_type": primitive_type_str,
Expand All @@ -169,12 +188,19 @@ def get_objects_info_dict(sim, visual_layer_list=None,
"color_emission": emission,
"color_transparency": transparency,
"color_auxiliary": auxiliary,
"texture_data": texture_data,
"texture_width": texture_width,
"texture_height": texture_height,
"texture_repeat_u": texture_repeat_u,
"texture_repeat_v": texture_repeat_v,
"texture_coord": texture_coord,
})

# Get Transform info, absolute to world
if parent_id == "world":
pos = sim.getObjectPosition(idx, sim.handle_world)
quat = sim.getObjectQuaternion(idx, sim.handle_world) # fixme, use quaternion?
quat = sim.getObjectQuaternion(idx,
sim.handle_world) # fixme, use quaternion?
else:
pos = sim.getObjectPosition(idx, int(parent_id))
quat = sim.getObjectQuaternion(idx, int(parent_id))
Expand Down Expand Up @@ -278,31 +304,48 @@ def process_body(self, obj_id, obj_info):
# Geometry info
obj_type = obj_info["type"]
obj_visualize = obj_info["visualize"]
if obj_type != "shape":
# If not a shape, return the object
return sim_object
if not obj_visualize: # FIXME, DOUBLE CHECK THIS IF STATEMENT

if obj_type != "shape" or not obj_visualize:
return sim_object
else:
else: # visual shape

# Process Material / Texture, fixme 3dim or 4dim?
ambient_diffuse = obj_info["color_ambient_diffuse"]
diffuse = obj_info["color_diffuse"]
specular = obj_info["color_specular"]
emission = obj_info["color_emission"] # todo, not used
emission = obj_info["color_emission"] # todo, not used
transparency = obj_info["color_transparency"]
auxiliary = obj_info["color_auxiliary"]

# Texture
texture_data = obj_info["texture_data"]
if texture_data is not None:
texture_data = np.frombuffer(texture_data, dtype=np.uint8)
texture_width = obj_info["texture_width"]
texture_height = obj_info["texture_height"]
texture_repeat_u = obj_info["texture_repeat_u"]
texture_repeat_v = obj_info["texture_repeat_v"]
texture_coord = obj_info["texture_coord"]
mat_texture = SimTexture.create_texture(
texture_data, texture_height, # fixme, why height first?
texture_width, self.sim_scene)
mat_texture.textureScale = [1, 1]
# plt.imshow(texture_data.reshape(texture_height,
# texture_width, 3))
else:
mat_texture = None
texture_coord = None
# mat_texture = None

# FIXME, THESE COLOR PROPERTIES MISMATCH BETWEEN VREP AND UNITY
# FIXME, THE COLOR RENDERING HAS ISSUE, ESPECIALLY FOR THE FLOOR
# Fixme, Setting transparency and emission are buggy
material = SimMaterial(color=ambient_diffuse,
emissionColor=[0., 0., 0., 0.],
# speculPar=0,
# specular=specular,
# shininess=mat_shininess,
# reflectance=mat_reflectance,
# texture=mat_texture
)

texture=mat_texture)

# Process Mesh
vertices = obj_info["shape_vertices"]
Expand All @@ -321,10 +364,8 @@ def process_body(self, obj_id, obj_info):
vertices=vertices,
faces=indices,
# vertex_normals=normals,
# mesh_texcoord=mesh_texcoord, # Fixme, texture details?
# faces_uv=faces_uv,
# mesh_texcoord=texture_coord, # Fixme, texture details?
faces_uv=texture_coord,
)

sim_object.visuals.append(sim_visual)
return sim_object

21 changes: 21 additions & 0 deletions simpub/simdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def write_to_buffer(
bin_buffer: io.BytesIO,
data: np.ndarray,
) -> Tuple[int, int]:
# change all float nparray to float32 and all int nparray to int32
if data.dtype == np.float64:
data = data.astype(np.float32)
elif data.dtype == np.int64:
data = data.astype(np.int32)
byte_data = data.tobytes()
layout = bin_buffer.tell(), len(byte_data)
bin_buffer.write(byte_data)
Expand Down Expand Up @@ -109,6 +114,9 @@ def create_mesh(
f"to number of vertices ({vertices.shape[0]})"
)
uvs = vertex_uvs.flatten()
elif faces_uv is not None and mesh_texcoord is None:
vertices, faces = SimMesh.generate_vertex_by_faces(vertices, faces)
uvs = faces_uv.flatten()
elif mesh_texcoord is not None and faces_uv is not None:
if mesh_texcoord.shape[0] == vertices.shape[0]:
uvs = mesh_texcoord
Expand Down Expand Up @@ -177,6 +185,7 @@ def create_mesh(
f"number of vertices ({num_vertices})"
)
uv_layout = SimMesh.write_to_buffer(bin_buffer, uvs)
print(f"UV layout: {uv_layout}")
bin_data = bin_buffer.getvalue()
hash = SimMesh.generate_hash(bin_data)
scene.raw_data[hash] = bin_data
Expand All @@ -188,6 +197,18 @@ def create_mesh(
uvLayout=uv_layout,
)

@staticmethod
def generate_vertex_by_faces(
vertices: np.ndarray,
faces: np.ndarray,
):
new_vertices = []
for face in faces:
new_vertices.append(vertices[face])
new_vertices = np.concatenate(new_vertices)
new_faces = np.arange(new_vertices.shape[0]).reshape(-1, 3)
return new_vertices, new_faces

@staticmethod
def generate_vertex_uv_from_face_uv(
vertices: np.ndarray,
Expand Down

0 comments on commit 45f2603

Please sign in to comment.