From 091b9d8340f85c860c39b457e129c98f2e06e720 Mon Sep 17 00:00:00 2001 From: Yosuke Matsusaka Date: Fri, 4 Sep 2015 16:43:11 +0900 Subject: [PATCH] convert material for primitive shapes (ref #36) --- simtrans/sdf.py | 29 +++++++++++++++++++++++++++++ simtrans/template/vrml.wrl | 30 +++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/simtrans/sdf.py b/simtrans/sdf.py index 6f72ce0..be59fd9 100644 --- a/simtrans/sdf.py +++ b/simtrans/sdf.py @@ -311,6 +311,35 @@ def readShape(self, d): m.data.radius = float(g.find('radius').text) else: raise Exception('unsupported shape type: %s' % g.tag) + materiald = d.find('material') + if materiald is not None: + material = model.MaterialModel() + diffuseset = False + ambient = materiald.find('ambient') + if ambient is not None: + material.ambient = numpy.array([float(v) for v in re.split(' +', ambient.text.strip(' '))]) + diffuse = materiald.find('diffuse') + if diffuse is not None: + material.diffuse = numpy.array([float(v) for v in re.split(' +', diffuse.text.strip(' '))]) + diffuseset = True + specular = materiald.find('specular') + if specular is not None: + material.specular = numpy.array([float(v) for v in re.split(' +', specular.text.strip(' '))]) + emission = materiald.find('emission') + if emission is not None: + material.emission = numpy.array([float(v) for v in re.split(' +', emission.text.strip(' '))]) + if diffuseset == False: + if emission is not None: + material.diffuse = material.emission + logging.warn("diffuse color not set. use emission color instead") + elif specular is not None: + material.diffuse = material.specular + logging.warn("diffuse color not set. use specular color instead") + elif ambient is not None: + material.diffuse = material.ambient + logging.warn("diffuse color not set. use ambient color instead") + if type(m.data) not in [model.MeshData]: + m.data.material = material return m diff --git a/simtrans/template/vrml.wrl b/simtrans/template/vrml.wrl index 781d316..76f4700 100644 --- a/simtrans/template/vrml.wrl +++ b/simtrans/template/vrml.wrl @@ -145,24 +145,40 @@ Transform { Inline { url "{{shapefilemap[v.name]}}" } - {%- elif v.shapeType == ShapeModel.SP_SPHERE %} + {%- else %} Shape { + {%- if v.data.material is not none %} + appearance Appearance { + material Material { + diffuseColor {{v.data.material.diffuse[0]}} {{v.data.material.diffuse[1]}} {{v.data.material.diffuse[2]}} + {%- if v.data.material.specular is not none %} + specularColor {{v.data.material.specular[0]}} {{v.data.material.specular[1]}} {{v.data.material.specular[2]}} + {%- endif %} + {%- if v.data.material.emission is not none %} + emissiveColor {{v.data.material.emission[0]}} {{v.data.material.emission[1]}} {{v.data.material.emission[2]}} + {%- endif %} + } + {%- if v.data.material.texture is not none %} + texture ImageTexture { + url "{{v.data.material.texture}}" + } + {%- endif %} + } + {%- endif %} + {%- if v.shapeType == ShapeModel.SP_SPHERE %} geometry Sphere { radius {{v.data.radius}} } - } - {%- elif v.shapeType == ShapeModel.SP_BOX %} - Shape { + {%- elif v.shapeType == ShapeModel.SP_BOX %} geometry Box { size {{v.data.x}} {{v.data.y}} {{v.data.z}} } - } - {%- elif v.shapeType == ShapeModel.SP_CYLINDER %} - Shape { + {%- elif v.shapeType == ShapeModel.SP_CYLINDER %} geometry Cylinder { radius {{v.data.radius}} height {{v.data.height}} } + {%- endif %} } {%- endif %} ]