Skip to content

Commit 71f10f5

Browse files
committed
obj materials: Added ior
1 parent 8aeb8fd commit 71f10f5

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

tests/data/assets/box_with_texture.mtl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
newmtl texture_mat
22
Ns 250.000000
33
Ka 1.000000 1.000000 1.000000
4-
Ks 0.500000 0.500000 0.500000
4+
Ks 0.000000 0.000000 0.000000
55
Ke 0.000000 0.000000 0.000000
66
Ni 1.450000
77
d 1.000000

tests/data/assets/box_with_texture_opacity.mtl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Ns 250.000000
33
Ka 1.000000 1.000000 1.000000
44
Ks 0.000000 0.000000 0.000000
55
Ke 0.000000 0.000000 0.000000
6-
Ni 1.450000
6+
Ni 1.000000
77
d 1.000000
88
illum 2
99
map_Kd ./grid.png

tests/testMaterial.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ def test_material_mesh_texture(self):
456456
self.assertIsNone(diffuse_color)
457457
opacity = self.get_material_opacity(texture_material)
458458
self.assertAlmostEqual(opacity, 1.0, places=6)
459+
ior = self.get_material_ior(texture_material)
460+
self.assertAlmostEqual(ior, 1.45, places=6)
459461
diffuse_color_texture_path = self.get_material_texture_path(texture_material, "diffuseColor")
460462
self.assertEqual(diffuse_color_texture_path, pathlib.Path("./Textures/grid.png"))
461463
normal_texture_path = self.get_material_texture_path(texture_material, "normal")
@@ -473,6 +475,8 @@ def test_material_mesh_texture(self):
473475

474476
diffuse_color = self.get_material_diffuse_color(texture_opacity_material)
475477
self.assertIsNone(diffuse_color)
478+
ior = self.get_material_ior(texture_opacity_material)
479+
self.assertAlmostEqual(ior, 1.0, places=6)
476480
opacity_texture_path = self.get_material_texture_path(texture_opacity_material, "opacity")
477481
self.assertEqual(opacity_texture_path, pathlib.Path("./Textures/opacity.png"))
478482

@@ -482,6 +486,8 @@ def test_material_mesh_texture(self):
482486
texture_specular_workflow_material = UsdShade.Material(texture_specular_workflow_material_prim)
483487
self.assertTrue(texture_specular_workflow_material)
484488

489+
ior = self.get_material_ior(texture_specular_workflow_material)
490+
self.assertAlmostEqual(ior, 1.45, places=6)
485491
diffuse_color = self.get_material_diffuse_color(texture_specular_workflow_material)
486492
diffuse_color = usdex.core.linearToSrgb(diffuse_color)
487493
self.assertTrue(Gf.IsClose(diffuse_color, Gf.Vec3f(0.4, 0.4, 0.4), 1e-6))

tests/util/ConverterTestCase.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def get_material_metallic(self, material: UsdShade.Material) -> float:
4949
shader: UsdShade.Shader = usdex.core.computeEffectivePreviewSurfaceShader(material)
5050
return shader.GetInput("metallic").Get()
5151

52+
def get_material_ior(self, material: UsdShade.Material) -> float:
53+
shader: UsdShade.Shader = usdex.core.computeEffectivePreviewSurfaceShader(material)
54+
return shader.GetInput("ior").Get()
55+
5256
def get_material_texture_path(self, material: UsdShade.Material, texture_type: str = "diffuseColor") -> pathlib.Path:
5357
"""
5458
Get the texture path for the given texture type.

urdf_usd_converter/_impl/material.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def convert_materials(data: ConversionData):
4848
material_data.opacity,
4949
material_data.roughness,
5050
material_data.metallic,
51+
material_data.ior,
5152
material_data.diffuse_texture_path,
5253
material_data.specular_texture_path,
5354
material_data.normal_texture_path,
@@ -102,6 +103,7 @@ def _convert_material(
102103
opacity: float,
103104
roughness: float,
104105
metallic: float,
106+
ior: float,
105107
diffuse_texture_path: pathlib.Path | None,
106108
specular_texture_path: pathlib.Path | None,
107109
normal_texture_path: pathlib.Path | None,
@@ -123,6 +125,7 @@ def _convert_material(
123125
opacity: The opacity of the material.
124126
roughness: The roughness of the material.
125127
metallic: The metallic of the material.
128+
ior: The ior of the material.
126129
diffuse_texture_path: The path to the diffuse texture.
127130
specular_texture_path: The path to the specular texture.
128131
normal_texture_path: The path to the normal texture.
@@ -151,6 +154,10 @@ def _convert_material(
151154
if not material_prim:
152155
Tf.RaiseRuntimeError(f'Failed to convert material "{safe_name}"')
153156

157+
surface_shader: UsdShade.Shader = usdex.core.computeEffectivePreviewSurfaceShader(material_prim)
158+
if ior != 0.0:
159+
surface_shader.CreateInput("ior", Sdf.ValueTypeNames.Float).Set(ior)
160+
154161
if diffuse_texture_path:
155162
usdex.core.addDiffuseTextureToPreviewMaterial(material_prim, _get_texture_asset_path(diffuse_texture_path, texture_paths, data))
156163

@@ -168,9 +175,8 @@ def _convert_material(
168175

169176
# If the specular color is not black or the specular texture exists, use the specular workflow.
170177
if specular_color != [0, 0, 0] or specular_texture_path:
171-
shader: UsdShade.Shader = usdex.core.computeEffectivePreviewSurfaceShader(material_prim)
172-
shader.CreateInput("useSpecularWorkflow", Sdf.ValueTypeNames.Int).Set(1)
173-
shader.CreateInput("specularColor", Sdf.ValueTypeNames.Color3f).Set(specular_color)
178+
surface_shader.CreateInput("useSpecularWorkflow", Sdf.ValueTypeNames.Int).Set(1)
179+
surface_shader.CreateInput("specularColor", Sdf.ValueTypeNames.Color3f).Set(specular_color)
174180
if specular_texture_path:
175181
_add_specular_texture_to_preview_material(material_prim, _get_texture_asset_path(specular_texture_path, texture_paths, data))
176182

@@ -284,6 +290,7 @@ def store_obj_material_data(mesh_file_path: pathlib.Path, reader: tinyobjloader.
284290
material_data.diffuse_color = Gf.Vec3f(material.diffuse[0], material.diffuse[1], material.diffuse[2])
285291
material_data.specular_color = Gf.Vec3f(material.specular[0], material.specular[1], material.specular[2])
286292
material_data.opacity = material.dissolve
293+
material_data.ior = material.ior if material.ior else 0.0
287294

288295
# The following is the extended specification of obj.
289296
material_data.roughness = material.roughness if material.roughness else 0.5

urdf_usd_converter/_impl/material_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(self):
2929
self.opacity: float = 1.0
3030
self.roughness: float = 0.5
3131
self.metallic: float = 0.0
32+
self.ior: float = 0.0
3233

3334
self.diffuse_texture_path: pathlib.Path | None = None
3435
self.specular_texture_path: pathlib.Path | None = None

0 commit comments

Comments
 (0)